增加流量卡状态信息,对流量信息上提进行调整,取消超过500MB进行的警告整行标黄和上提功能,仅保留流量数字标黄

This commit is contained in:
DXC
2026-01-22 10:55:41 +08:00
parent cb567b2c7d
commit 195c3f8fa4
3 changed files with 100 additions and 89 deletions

View File

@ -173,29 +173,37 @@ def sync_iot_data_service():
1. 登录
2. 遍历所有分页获取 ICCID
3. 批量查询详情
4. 返回完整数据列表 (List[Dict])
4. 解析 cardStatus 状态码
5. 返回完整数据列表 (List[Dict])
"""
print("[IoT Service] 开始同步任务...")
# 1. 登录
# 1. 定义状态码映射表 (根据提供的需求文档)
STATUS_MAP = {
"1": "测试期",
"2": "沉默期",
"3": "在使用",
"4": "停机",
"5": "停机保号",
"6": "销户"
}
token = get_access_token()
if not token:
return []
# 2. 循环翻页获取所有 ICCID
all_iccids = []
page_no = 1
page_size = 100
# ✅ 2. 循环翻页获取所有 ICCID
while True:
res = get_iot_card_page(token, page_no, page_size)
# 校验响应
if not res or (res.get('code') != 0 and res.get('code') != 200):
print(f"[IoT Service] 列表获取结束或中断: {res.get('msg') if res else 'No Response'}")
break
# 解析数据结构 (兼容 data 为 list 或 data.rows)
data_field = res.get('data', {})
rows = []
if isinstance(data_field, list):
@ -206,43 +214,47 @@ def sync_iot_data_service():
if not rows:
break
# 提取 ICCID
current_batch = [str(x.get('iccid')) for x in rows if x.get('iccid')]
all_iccids.extend(current_batch)
# print(f"DEBUG: page {page_no} done, items: {len(current_batch)}")
# 判断是否最后一页
if len(rows) < page_size:
break
page_no += 1
time.sleep(0.2) # 避免请求过快
time.sleep(0.2)
total_count = len(all_iccids)
if total_count == 0:
print("[IoT Service] 未找到任何卡片")
return []
# 3. 分批查询详情
# 3. 分批查询详情并处理状态
final_data_list = []
batch_size = 50
# print(f"DEBUG: 开始查询 {total_count} 张卡的详情...")
for i in range(0, total_count, batch_size):
batch_iccids = all_iccids[i: i + batch_size]
detail_res = get_iot_card_details_batch(token, batch_iccids)
if detail_res and (detail_res.get('code') == 0 or detail_res.get('code') == 200):
details = detail_res.get('data', [])
if isinstance(details, list):
final_data_list.extend(details)
# === 核心修改:增加状态解析逻辑 ===
for card in details:
# 获取原始状态码 (如 "3")
raw_status = str(card.get('cardStatus', ''))
# 匹配中文描述 (如 "在使用")
status_desc = STATUS_MAP.get(raw_status, "未知状态")
# 将描述写入新字段,前端可直接取用 card.statusDesc
card['statusDesc'] = status_desc
final_data_list.append(card)
# =================================
time.sleep(0.2)
print(f"[IoT Service] 同步完成,共获取 {len(final_data_list)} 条详情数据")
# 4. 返回列表供 api.py 写入数据库
return final_data_list