|
@@ -80,7 +80,7 @@ class UIDBurnManageView(View):
|
|
|
elif operation == 'importBatchUids':
|
|
|
return self.import_batch_uids(request, response)
|
|
|
elif operation == 'addBurnRecord':
|
|
|
- return self.add_burn_record(request, response)
|
|
|
+ return self.add_burn_record(request,request_dict, response)
|
|
|
elif operation == 'getBurnUidsPage':
|
|
|
return self.get_burn_uids_page(request_dict, response)
|
|
|
elif operation == 'getImportProgress':
|
|
@@ -99,53 +99,50 @@ class UIDBurnManageView(View):
|
|
|
:param response: 响应对象(用于返回JSON)
|
|
|
:return: 分页查询结果的JSON响应
|
|
|
"""
|
|
|
- # 1. 分页参数处理与验证(严格类型转换+边界控制)
|
|
|
+ # 1. 分页参数处理与验证
|
|
|
try:
|
|
|
page = int(request_dict.get('page', 1))
|
|
|
page_size = int(request_dict.get('pageSize', 10))
|
|
|
- # 限制分页范围(避免页码为0或负数,页大小控制在1-100)
|
|
|
+ # 限制分页范围
|
|
|
page = max(page, 1)
|
|
|
page_size = max(1, min(page_size, 100))
|
|
|
except (ValueError, TypeError):
|
|
|
return response.json(444, "分页参数错误(必须为整数)")
|
|
|
|
|
|
- # 2. 构建查询条件(解决原代码中query可能未定义的问题)
|
|
|
- query = Q() # 初始化空条件(无条件查询)
|
|
|
- order_number = request_dict.get('orderNumber', '').strip() # 去除首尾空格,避免空字符串查询
|
|
|
+ # 2. 构建查询条件
|
|
|
+ query = Q()
|
|
|
+ order_number = request_dict.get('orderNumber', '').strip()
|
|
|
if order_number:
|
|
|
query &= Q(order_number__icontains=order_number)
|
|
|
|
|
|
- # 3. 获取查询集(延迟执行,不立即查询数据库)
|
|
|
- burn_qs = BurnRecord.objects.filter(query).order_by('-created_time')
|
|
|
+ # 3. 获取查询集并指定需要的字段
|
|
|
+ burn_qs = BurnRecord.objects.filter(query).order_by('-created_time').values(
|
|
|
+ 'id', 'order_number', 'burn_count', 'purpose', 'created_time'
|
|
|
+ )
|
|
|
|
|
|
- # 4. 分页处理(完善异常捕获)
|
|
|
+ # 4. 分页处理
|
|
|
paginator = Paginator(burn_qs, page_size)
|
|
|
try:
|
|
|
page_obj = paginator.page(page)
|
|
|
except PageNotAnInteger:
|
|
|
- # 若页码不是整数,返回第一页
|
|
|
page_obj = paginator.page(1)
|
|
|
except EmptyPage:
|
|
|
- # 若页码超出范围,返回最后一页(或空列表,根据业务需求调整)
|
|
|
page_obj = paginator.page(paginator.num_pages)
|
|
|
|
|
|
- burn_list = serializers.serialize(
|
|
|
- 'python', # 输出Python字典格式
|
|
|
- page_obj,
|
|
|
- fields=['id', 'order_number', 'burn_count', 'purpose', 'created_time'] # 指定需要的字段
|
|
|
- )
|
|
|
-
|
|
|
+ # 转换为列表
|
|
|
+ burn_list = list(page_obj)
|
|
|
+ # 返回结果
|
|
|
return response.json(
|
|
|
0,
|
|
|
{
|
|
|
- 'list': burn_list,
|
|
|
'total': paginator.count, # 总记录数
|
|
|
- 'currentPage': page_obj.number, # 当前页码
|
|
|
- 'totalPages': paginator.num_pages, # 总页数
|
|
|
- 'pageSize': page_size # 返回实际使用的页大小(便于前端同步)
|
|
|
+ 'list': burn_list, # 当前页数据列表
|
|
|
+ 'currentPage': page_obj.number,
|
|
|
+ 'totalPages': paginator.num_pages
|
|
|
}
|
|
|
)
|
|
|
|
|
|
+
|
|
|
@classmethod
|
|
|
def import_batch_uids(cls, request, response) -> Any:
|
|
|
"""
|
|
@@ -347,16 +344,15 @@ class UIDBurnManageView(View):
|
|
|
return 0
|
|
|
|
|
|
@classmethod
|
|
|
- def add_burn_record(cls, request, response) -> Any:
|
|
|
+ def add_burn_record(cls, request,request_dict, response) -> Any:
|
|
|
"""
|
|
|
新增烧录记录(带UID文件) - Redis字符串优化版
|
|
|
+ :param request_dict:
|
|
|
:param request: HttpRequest对象(包含上传文件和表单数据)
|
|
|
:param response: 响应对象
|
|
|
:return: JSON响应
|
|
|
"""
|
|
|
|
|
|
- # 1. 参数验证
|
|
|
- request_dict = request.POST
|
|
|
required_fields = ['order_number', 'burn_count', 'purpose']
|
|
|
for field in required_fields:
|
|
|
if not request_dict.get(field):
|
|
@@ -547,58 +543,81 @@ class UIDBurnManageView(View):
|
|
|
@classmethod
|
|
|
def get_import_progress(cls, request_dict: Dict[str, Any], response) -> Any:
|
|
|
"""
|
|
|
- 查询导入任务进度
|
|
|
- :param request_dict: 请求参数字典(必须包含task_id)
|
|
|
+ 查询任务进度(支持导入和烧录任务)
|
|
|
+ :param request_dict: 请求参数字典(必须包含task_id, 可选type)
|
|
|
:param response: 响应对象
|
|
|
:return: JSON响应
|
|
|
+ type参数说明:
|
|
|
+ - import: 导入任务(默认)
|
|
|
+ - burn: 烧录任务
|
|
|
"""
|
|
|
-
|
|
|
# 1. 参数验证
|
|
|
task_id = request_dict.get('task_id')
|
|
|
if not task_id:
|
|
|
return response.json(444, "缺少task_id参数")
|
|
|
-
|
|
|
+
|
|
|
+ task_type = request_dict.get('type', 'import').lower()
|
|
|
+ if task_type not in ['import', 'burn']:
|
|
|
+ return response.json(444, "type参数必须是'import'或'burn'")
|
|
|
+
|
|
|
# 2. 构建Redis key
|
|
|
- redis_key = f"import_task:{task_id}"
|
|
|
-
|
|
|
+ redis_key = f"{task_type}_task:{task_id}"
|
|
|
+
|
|
|
try:
|
|
|
# 3. 从Redis获取任务数据
|
|
|
redis_obj = RedisObject()
|
|
|
task_data_str = redis_obj.get_data(redis_key)
|
|
|
-
|
|
|
+
|
|
|
if not task_data_str:
|
|
|
- return response.json(404, "任务不存在或已过期")
|
|
|
-
|
|
|
+ return response.json(173, "任务不存在或已过期")
|
|
|
+
|
|
|
# 4. 解析任务数据
|
|
|
+ if isinstance(task_data_str, bytes):
|
|
|
+ task_data_str = task_data_str.decode('utf-8')
|
|
|
task_data = json.loads(task_data_str)
|
|
|
-
|
|
|
+
|
|
|
# 5. 计算耗时(秒)
|
|
|
current_time = int(time.time())
|
|
|
start_time = task_data.get('start_time', current_time)
|
|
|
elapsed = current_time - start_time
|
|
|
if task_data.get('end_time'):
|
|
|
elapsed = task_data['end_time'] - start_time
|
|
|
-
|
|
|
- # 6. 返回标准化进度信息
|
|
|
- return response.json(0, {
|
|
|
+
|
|
|
+ # 6. 构建基础响应数据
|
|
|
+ result = {
|
|
|
'status': task_data.get('status', 'unknown'),
|
|
|
'progress': task_data.get('progress', 0),
|
|
|
'processed': task_data.get('processed', 0),
|
|
|
'total': task_data.get('total', 0),
|
|
|
- 'batch_number': task_data.get('batch_number', ''),
|
|
|
- 'success_count': task_data.get('success_count', 0),
|
|
|
'elapsed_seconds': elapsed,
|
|
|
'start_time': start_time,
|
|
|
'end_time': task_data.get('end_time'),
|
|
|
- 'error': task_data.get('error')
|
|
|
- })
|
|
|
-
|
|
|
+ 'error': task_data.get('error'),
|
|
|
+ 'task_type': task_type
|
|
|
+ }
|
|
|
+
|
|
|
+ # 7. 根据任务类型添加特定字段
|
|
|
+ if task_type == 'import':
|
|
|
+ result.update({
|
|
|
+ 'batch_number': task_data.get('batch_number', ''),
|
|
|
+ 'success_count': task_data.get('success_count', 0)
|
|
|
+ })
|
|
|
+ else: # burn task
|
|
|
+ result.update({
|
|
|
+ 'order_number': task_data.get('order_number', ''),
|
|
|
+ 'purpose': task_data.get('purpose', ''),
|
|
|
+ 'burn_count': task_data.get('burn_count', 0),
|
|
|
+ 'burn_record_id': task_data.get('burn_record_id')
|
|
|
+ })
|
|
|
+
|
|
|
+ return response.json(0, result)
|
|
|
+
|
|
|
except json.JSONDecodeError:
|
|
|
LOGGER.error(f"任务数据解析失败, redis_key: {redis_key}")
|
|
|
return response.json(500, "任务数据格式错误")
|
|
|
except Exception as e:
|
|
|
- LOGGER.error(f"查询导入进度失败: {str(e)}")
|
|
|
- return response.json(500)
|
|
|
+ LOGGER.error(f"查询任务进度失败: {str(e)}")
|
|
|
+ return response.json(500, "查询进度失败")
|
|
|
|
|
|
@classmethod
|
|
|
def get_import_task_list(cls, request_dict: Dict[str, Any], response) -> Any:
|