|
@@ -8,7 +8,8 @@
|
|
|
"""
|
|
|
import datetime
|
|
|
import json
|
|
|
-import logging
|
|
|
+import secrets
|
|
|
+import string
|
|
|
import time
|
|
|
from io import BytesIO
|
|
|
|
|
@@ -19,11 +20,10 @@ from django.db.models import Q
|
|
|
from django.http import QueryDict, HttpResponse
|
|
|
from django.views import View
|
|
|
|
|
|
+from Ansjer.config import LOGGER
|
|
|
from Model.models import ProductsScheme, DeviceScheme
|
|
|
-from Object.RedisObject import RedisObject
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
-from Ansjer.config import LOGGER
|
|
|
|
|
|
|
|
|
class ProductsSchemeManageView(View):
|
|
@@ -73,10 +73,10 @@ class ProductsSchemeManageView(View):
|
|
|
|
|
|
# 操作路由映射
|
|
|
operation_handlers = {
|
|
|
- 'queryList': self.query_list,# 查询列表
|
|
|
- 'add': self.add_scheme,# 添加方案
|
|
|
- 'edit': self.edit_scheme,# 编辑方案
|
|
|
- 'delete': self.delete_scheme,# 删除方案
|
|
|
+ 'queryList': self.query_list, # 查询列表
|
|
|
+ 'add': self.add_scheme, # 添加方案
|
|
|
+ 'edit': self.edit_scheme, # 编辑方案
|
|
|
+ 'delete': self.delete_scheme, # 删除方案
|
|
|
'generateQR': self.generate_qr_code, # 生成二维码
|
|
|
'deviceSchemeList': self.device_scheme_list
|
|
|
}
|
|
@@ -90,6 +90,7 @@ class ProductsSchemeManageView(View):
|
|
|
except Exception as e:
|
|
|
LOGGER.error(f"操作{operation}执行异常:{repr(e)}")
|
|
|
return response.json(500, "服务器内部错误")
|
|
|
+
|
|
|
def query_list(self, user_id, request_dict, response):
|
|
|
"""查询方案列表(优化点:分页封装/字段映射)"""
|
|
|
# 参数处理与验证
|
|
@@ -144,36 +145,20 @@ class ProductsSchemeManageView(View):
|
|
|
})
|
|
|
|
|
|
@staticmethod
|
|
|
- def _generate_storage_code(device_type=0):
|
|
|
+ def generate_timestamp_code(device_type: int = 0) -> str:
|
|
|
"""
|
|
|
- 生成入库编码规则:SC+年月日+3位序列号
|
|
|
- 示例:SC20231125-001
|
|
|
- 特性:
|
|
|
- 1. 每日按设备类型独立计数(Redis原子计数器)
|
|
|
- 2. 自动处理分布式并发
|
|
|
- 3. 序列号每日自动重置
|
|
|
+ 方案1:基于时间戳的编码
|
|
|
+ 格式:PC+设备类型+年月日+时分秒+6位随机大写字符
|
|
|
+ 示例:PC020250519143022-ABCDEF
|
|
|
"""
|
|
|
try:
|
|
|
- redis_conn = RedisObject(2)
|
|
|
- # 生成 Redis 键名(格式:SC日期:设备类型)
|
|
|
- date_prefix = datetime.datetime.now().strftime("SC%Y%m%d")
|
|
|
- redis_key = f"{date_prefix}:{device_type}"
|
|
|
-
|
|
|
- # 原子操作:递增并获取序列号(自动创建并设置过期时间)
|
|
|
- sequence = redis_conn.incr(redis_key)
|
|
|
-
|
|
|
- # 首次创建时设置过期时间(保留两天防止跨天边界问题)
|
|
|
- if sequence == 1:
|
|
|
- redis_conn.set_expire(redis_key, 172800) # 60*60*24*2 = 2天
|
|
|
-
|
|
|
- # 检查序列号溢出
|
|
|
- if sequence > 999:
|
|
|
- raise ValueError(f"当日设备类型 {device_type} 的序列号已超过最大值999")
|
|
|
-
|
|
|
- return f"{date_prefix}-{sequence:03d}"
|
|
|
-
|
|
|
+ current_time = datetime.datetime.now()
|
|
|
+ date_part = current_time.strftime("%Y%m%d%H%M%S")
|
|
|
+ # 使用string.ascii_uppercase生成大写随机字符
|
|
|
+ random_part = ''.join(secrets.choice(string.ascii_uppercase) for _ in range(6))
|
|
|
+ return f"PC{device_type}{date_part}-{random_part}"
|
|
|
except Exception as e:
|
|
|
- LOGGER.error(f"生成storage_code失败: {str(e)}")
|
|
|
+ LOGGER.error(f"生成编码失败: {repr(e)}")
|
|
|
raise
|
|
|
|
|
|
def add_scheme(self, user_id, request_dict, response):
|
|
@@ -182,7 +167,7 @@ class ProductsSchemeManageView(View):
|
|
|
device_type = int(request_dict.get('deviceType', 0))
|
|
|
|
|
|
# 生成唯一storage_code
|
|
|
- storage_code = self._generate_storage_code(device_type)
|
|
|
+ storage_code = self.generate_timestamp_code(device_type)
|
|
|
|
|
|
# 构造方案数据
|
|
|
scheme_data = {
|
|
@@ -365,7 +350,7 @@ class ProductsSchemeManageView(View):
|
|
|
'sensor': scheme.sensor,
|
|
|
'orderQuantity': scheme.order_quantity,
|
|
|
'customerCode': scheme.customer_code,
|
|
|
- 'phy':scheme.phy,
|
|
|
+ 'phy': scheme.phy,
|
|
|
'remark': scheme.remark,
|
|
|
'createdTime': scheme.created_time,
|
|
|
'createdBy': scheme.created_by
|
|
@@ -500,4 +485,4 @@ class ProductsSchemeManageView(View):
|
|
|
return response.json(0, data)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
- return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|