|
@@ -36,6 +36,8 @@ class SerialNumberView(View):
|
|
|
response = ResponseObject()
|
|
|
if operation == 'create':
|
|
|
return self.do_create(request_dict, response)
|
|
|
+ elif operation == 'getSerial':
|
|
|
+ return self.do_get_serial_number(request_dict, response)
|
|
|
|
|
|
token = request_dict.get('token', None)
|
|
|
token = TokenObject(token)
|
|
@@ -174,3 +176,64 @@ class SerialNumberView(View):
|
|
|
return response.json(0)
|
|
|
else:
|
|
|
return response.json(444)
|
|
|
+
|
|
|
+ # 提供给pc端获取序列号
|
|
|
+ def do_get_serial_number(self, request_dict, response):
|
|
|
+ quantity = request_dict.get('quantity', None)
|
|
|
+ company_id = request_dict.get('company_id', None)
|
|
|
+ token = request_dict.get('token', None)
|
|
|
+ time_stamp = request_dict.get('time_stamp', None)
|
|
|
+ p2p_type = request_dict.get('p2p_type', None)
|
|
|
+
|
|
|
+ if token and time_stamp and quantity and company_id:
|
|
|
+
|
|
|
+ token = int(CommonService.decode_data(token))
|
|
|
+ time_stamp = int(time_stamp)
|
|
|
+
|
|
|
+ now_time = int(time.time())
|
|
|
+ distance = now_time - time_stamp
|
|
|
+
|
|
|
+ if token != time_stamp or distance > 60000 or distance < -60000: # 为了全球化时间控制在一天内
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ redisObject = RedisObject()
|
|
|
+ key = 'serial_lock'
|
|
|
+ value = redisObject.lpop(key)
|
|
|
+ count = 0
|
|
|
+ while value is False and count < 5:
|
|
|
+ time.sleep(1)
|
|
|
+ value = redisObject.lpop(key)
|
|
|
+ count += 1
|
|
|
+
|
|
|
+ if count == 5 and value is False: # 暂时注释
|
|
|
+ return response.json(5)
|
|
|
+
|
|
|
+ quantity = 1 #只能取一个
|
|
|
+
|
|
|
+
|
|
|
+ company_serial_qs = CompanySerialModel.objects.filter(company__secret=company_id, status=0,
|
|
|
+ p2p=p2p_type)
|
|
|
+ if not company_serial_qs.exists():
|
|
|
+ redisObject.rpush(key, value)
|
|
|
+ return response.json(373)
|
|
|
+
|
|
|
+ # 存在对应的企业
|
|
|
+ company_serial_qs = company_serial_qs[0:quantity]
|
|
|
+
|
|
|
+ # company_serial_qs = company_serial_qs.values('id', 'serial_number__serial_number', 'company__mark')
|
|
|
+ save_point = transaction.savepoint() # 设置事务保存点
|
|
|
+ try:
|
|
|
+ data = []
|
|
|
+ ids = []
|
|
|
+ for serial in company_serial_qs:
|
|
|
+ ids.append(serial.id)
|
|
|
+ data.append(serial.serial_number + serial.company.mark)
|
|
|
+ SerialNumberModel.objects.filter(serial_number=serial.serial_number).update(use_status=2)
|
|
|
+ CompanySerialModel.objects.filter(id__in=ids).update(status=1)
|
|
|
+ except Exception as e:
|
|
|
+ if save_point:
|
|
|
+ transaction.savepoint_rollback(save_point) # 事务回滚
|
|
|
+ redisObject.rpush(key, value)
|
|
|
+ return response.json(0, data)
|
|
|
+ else:
|
|
|
+ return response.json(444)
|