# %%
from requests import get,post
import time
import json
ip = "http://band.hxzhxy.cn"
param = {
    "username": "tangchao",
    "password": "123456a",
    "client_id": "client",
    "grant_type": "password",
    "client_secret": "123456",
}
data = get(url=f"http://band.hxzhxy.cn/oauth/token?username=zhongwei&password=123456a&client_id=client&grant_type=password&client_secret=123456").json()
token = data["access_token"]
header = {
    "content-type": "application/json",
    "authorization": f"Bearer {token}",
}
# http://139.159.182.121:8092/card/findUser?bandNo=0269414644&controllerSN=233159158
data = get(f"https://band.hxzhxy.cn/card/listStuWithRule?gradeId=&classId=&screen=0&param=&board=&type=1&currentPage=1&pageNumber=0&pageSize=1000&pageTotal=593&teamId=110",headers=header).json()
stus2 = data["data"]["listData"]
# %%
incorrect_students = []

for stu in stus2:
    night_study = stu['晚自习']
    band_no = stu['手环号']
    
    url = f"http://139.159.182.121:8092/card/findUser?bandNo={band_no}&controllerSN=233159164"
    
    try:
        response = get(url)
        result = response.json()
        
        if result.get('success') and result.get('msg'):
            try:
                msg_data = json.loads(result['msg'])
                permissions = msg_data.get('result', {}).get('权限', [])
                
                if permissions:
                    permission = permissions[0]
                    door1_time = permission.get('1号门控制时段', -1)
                    
                    expected_time = 9 if night_study == 2 else 16
                    
                    if door1_time != expected_time:
                        incorrect_students.append({
                            '姓名': stu.get('姓名', '未知'),
                            '手环号': band_no,
                            '晚自习': night_study,
                            '实际1号门控制时段': door1_time,
                            '应该的1号门控制时段': expected_time
                        })
            except json.JSONDecodeError:
                print(f"解析JSON失败: {result.get('msg')}")
        else:
            print(f"请求失败: {result}")
    except Exception as e:
        print(f"请求异常 {band_no}: {e}")

print("\n" + "="*80)
print("检测结果：")
print("="*80)

if incorrect_students:
    print(f"\n发现 {len(incorrect_students)} 个学生权限配置不正确：\n")
    for i, student in enumerate(incorrect_students, 1):
        print(f"{i}. 姓名: {student['姓名']}, 手环号: {student['手环号']}")
        print(f"   晚自习配置: {student['晚自习']} (2=上, 1=不上)")
        print(f"   实际1号门控制时段: {student['实际1号门控制时段']}")
        print(f"   应该的1号门控制时段: {student['应该的1号门控制时段']}")
        print()
else:
    print("\n所有学生权限配置正确！")
