|
@@ -29,10 +29,12 @@ class CompanyView(View):
|
|
|
|
|
|
def validate(self, request_dict, operation):
|
|
|
|
|
|
- token = TokenObject(request_dict.get('token', None))
|
|
|
-
|
|
|
response = uidManageResponseObject()
|
|
|
|
|
|
+ if operation == 'createSerial':
|
|
|
+ return self.do_create_serial(request_dict, response)
|
|
|
+
|
|
|
+ token = TokenObject(request_dict.get('token', None))
|
|
|
if token.code != 0:
|
|
|
return response.json(token.code)
|
|
|
|
|
@@ -44,8 +46,6 @@ class CompanyView(View):
|
|
|
return self.do_delete(token.userID, request_dict, response)
|
|
|
elif operation == 'list':
|
|
|
return self.do_list(token.userID, request_dict, response)
|
|
|
- elif operation == 'createSerial':
|
|
|
- return self.do_create_serial(token.userID, request_dict, response)
|
|
|
else:
|
|
|
return response.json(404)
|
|
|
|
|
@@ -124,80 +124,56 @@ class CompanyView(View):
|
|
|
else:
|
|
|
return response.json(444)
|
|
|
|
|
|
- @transaction.atomic
|
|
|
- def do_create_serial(self, userID, request_dict, response):
|
|
|
+ def do_create_serial(self, request_dict, response):
|
|
|
# perm = ModelService.check_perm_uid_manage(userID, 0)
|
|
|
# if not perm:
|
|
|
# return response.json(309)
|
|
|
|
|
|
id = request_dict.get('id', None)
|
|
|
- quantity = request_dict.get('quantity', None)
|
|
|
p2p = request_dict.get('p2p', None)
|
|
|
- if id and quantity:
|
|
|
- company_qs = CompanyModel.objects.filter(id=id)
|
|
|
+ quantity = request_dict.get('quantity', None)
|
|
|
|
|
|
- if not company_qs.exists():
|
|
|
- return response.json(444)
|
|
|
- p2p_sum_Serial = SerialNumberModel.objects.filter(p2p=p2p).count()
|
|
|
- p2p_sum_Serial_company = CompanySerialModel.objects.filter(p2p=p2p).count()
|
|
|
- p2p_sum_bind = p2p_sum_Serial - p2p_sum_Serial_company
|
|
|
- if int(quantity) > int(p2p_sum_bind):
|
|
|
- return response.json(10041)
|
|
|
-
|
|
|
- savePoint = transaction.savepoint()
|
|
|
- try:
|
|
|
- try:
|
|
|
-
|
|
|
- company = company_qs[0]
|
|
|
- # start = company.quantity
|
|
|
- # start = int(start)
|
|
|
- # end = start + int(quantity)
|
|
|
- # serial_qs = SerialNumberModel.objects.filter(p2p=p2p)[start:end]
|
|
|
- start_1 = p2p_sum_Serial_company
|
|
|
- end_1 = int(start_1) + int(quantity)
|
|
|
- serial_qs = SerialNumberModel.objects.filter(p2p=p2p)[start_1:end_1]
|
|
|
- if serial_qs.exists():
|
|
|
- data = []
|
|
|
- now_time = int(time.time())
|
|
|
- for item in serial_qs:
|
|
|
- data.append(CompanySerialModel(
|
|
|
- company_id=company.id,
|
|
|
- serial_number=item.serial_number,
|
|
|
- add_time=now_time,
|
|
|
- update_time=now_time,
|
|
|
- p2p=p2p
|
|
|
- ))
|
|
|
-
|
|
|
- if len(data) == 5000:
|
|
|
- CompanySerialModel.objects.bulk_create(data)
|
|
|
- data.clear()
|
|
|
- if len(data) > 0:
|
|
|
- CompanySerialModel.objects.bulk_create(data)
|
|
|
- data.clear()
|
|
|
-
|
|
|
- # company.quantity = company.quantity + end - start
|
|
|
- company.quantity =CompanySerialModel.objects.filter(company_id=id).count()
|
|
|
- company.save()
|
|
|
- return response.json(0)
|
|
|
- else:
|
|
|
- return response.json(173)
|
|
|
- except Exception as e:
|
|
|
- # print('--------------------------error 5000')
|
|
|
- # print(repr(e))
|
|
|
- if savePoint:
|
|
|
- transaction.rollback(savePoint)
|
|
|
- djangoLogger = logging.getLogger('django')
|
|
|
- djangoLogger.exception(repr(e))
|
|
|
- return response.json(176, str(e))
|
|
|
- except Exception as e:
|
|
|
- # print('--------------------------error 5001')
|
|
|
- # print(repr(e))
|
|
|
- djangoLogger = logging.getLogger('django')
|
|
|
- djangoLogger.exception(repr(e))
|
|
|
- return response.json(176, str(e))
|
|
|
- else:
|
|
|
+ if not all([id, quantity]):
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ company = CompanyModel.objects.get(id=id)
|
|
|
+ if not company.exists():
|
|
|
return response.json(444)
|
|
|
|
|
|
+ p2p_sum_Serial = SerialNumberModel.objects.filter(p2p=p2p).count()
|
|
|
+ p2p_sum_Serial_company = CompanySerialModel.objects.filter(p2p=p2p).count()
|
|
|
+ p2p_sum_bind = p2p_sum_Serial - p2p_sum_Serial_company # 剩余可绑定的序列号
|
|
|
+ if int(quantity) > int(p2p_sum_bind):
|
|
|
+ return response.json(10041)
|
|
|
+
|
|
|
+ try:
|
|
|
+ start_1 = p2p_sum_Serial_company
|
|
|
+ end_1 = int(start_1) + int(quantity)
|
|
|
+ serial_qs = SerialNumberModel.objects.filter(p2p=p2p)[start_1:end_1]
|
|
|
+ if serial_qs.exists():
|
|
|
+ company_serial_bulk = []
|
|
|
+ now_time = int(time.time())
|
|
|
+ for item in serial_qs:
|
|
|
+ company_serial_bulk.append(CompanySerialModel(
|
|
|
+ p2p=p2p,
|
|
|
+ status=1,
|
|
|
+ add_time=now_time,
|
|
|
+ update_time=now_time,
|
|
|
+ company_id=company.id,
|
|
|
+ serial_number=item.serial_number,
|
|
|
+ ))
|
|
|
+ with transaction.atomic():
|
|
|
+ SerialNumberModel.objects.bulk_create(company_serial_bulk)
|
|
|
+ company.quantity = CompanySerialModel.objects.filter(company_id=id).count()
|
|
|
+ company.save()
|
|
|
+ return response.json(0)
|
|
|
+ else:
|
|
|
+ return response.json(173)
|
|
|
+ except Exception as e:
|
|
|
+ djangoLogger = logging.getLogger('django')
|
|
|
+ djangoLogger.exception(repr(e))
|
|
|
+ return response.json(176, str(e))
|
|
|
+
|
|
|
def do_list(self, userID, request_dict, response):
|
|
|
# perm = ModelService.check_perm_uid_manage(userID, 0)
|
|
|
# if not perm:
|