|
@@ -9,8 +9,9 @@ from django.http import StreamingHttpResponse, HttpResponse, QueryDict
|
|
|
from django.utils.decorators import method_decorator
|
|
|
from django.views import View
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
+from django.db import transaction
|
|
|
|
|
|
-from Model.models import UIDModel, UserUIDModel, UserModel, LogModel
|
|
|
+from Model.models import UIDModel, UserUIDModel, UserModel, LogModel, MacModel
|
|
|
from Object.RedisObject import RedisObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from Object.ResponseObject import ResponseObject
|
|
@@ -47,6 +48,7 @@ class UploadUIDFileView(View):
|
|
|
content = content[3:(len(content) - 3)]
|
|
|
uids = content.split('\n')
|
|
|
print(uids)
|
|
|
+ print(len(uids))
|
|
|
|
|
|
response = ResponseObject()
|
|
|
|
|
@@ -71,13 +73,22 @@ class UploadUIDFileView(View):
|
|
|
else:
|
|
|
return self.do_reset(data, response, area, request, tko)
|
|
|
|
|
|
+ @transaction.atomic
|
|
|
def do_insert(self, data, duplicate, area, response, request, token):
|
|
|
bulk = []
|
|
|
count = 0
|
|
|
add_time = int(time.time())
|
|
|
update_time = int(time.time())
|
|
|
keys = data.keys()
|
|
|
+
|
|
|
+ # 获取最新的mac
|
|
|
+ mac = MacModel.objects.filter().values('id', 'value', 'is_active')[0]
|
|
|
+ if not mac['is_active']:
|
|
|
+ return response.json(175)
|
|
|
# redisObject = RedisObject(db=3)
|
|
|
+ key = ''
|
|
|
+ tmpMac = mac['value']
|
|
|
+ savePoint = None
|
|
|
for item in keys:
|
|
|
key = item.strip()
|
|
|
# value = redisObject.get_data(key)
|
|
@@ -92,28 +103,74 @@ class UploadUIDFileView(View):
|
|
|
status=0,
|
|
|
add_time=add_time,
|
|
|
update_time=update_time,
|
|
|
- area=area
|
|
|
+ area=area,
|
|
|
+ mac=mac['value']
|
|
|
))
|
|
|
try:
|
|
|
- if (count % 5000) == 0:
|
|
|
- UIDModel.objects.bulk_create(bulk)
|
|
|
- bulk.clear()
|
|
|
+ try:
|
|
|
+ if (count % 5000) == 0:
|
|
|
+ savePoint = transaction.savepoint()
|
|
|
+ UIDModel.objects.bulk_create(bulk)
|
|
|
+ bulk.clear()
|
|
|
+ data = {
|
|
|
+ 'value': mac['value'],
|
|
|
+ 'is_active': tmpMac is not None
|
|
|
+ }
|
|
|
+ MacModel.objects.filter().update(**data)
|
|
|
+ except Exception as e:
|
|
|
+ # print('--------------------------error 5000')
|
|
|
+ # print(repr(e))
|
|
|
+ if savePoint:
|
|
|
+ transaction.rollback(savePoint)
|
|
|
+ return response.json(174, str(e))
|
|
|
+ else:
|
|
|
+ savePoint = None
|
|
|
except Exception as e:
|
|
|
- print(repr(e))
|
|
|
+ # print('--------------------------error 5001')
|
|
|
+ # print(repr(e))
|
|
|
return response.json(174, str(e))
|
|
|
count += 1
|
|
|
+ tmpMac = CommonService.updateMac(mac['value'])
|
|
|
+ if tmpMac is None:
|
|
|
+ # 能分配的mac已用完
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ mac['value'] = tmpMac
|
|
|
|
|
|
- if len(bulk) > 0:
|
|
|
- UIDModel.objects.bulk_create(bulk)
|
|
|
- bulk.clear()
|
|
|
- del data
|
|
|
- del bulk
|
|
|
+ # 当bulk不足5000时,还有数据要插入
|
|
|
+ try:
|
|
|
+ try:
|
|
|
+ savePoint = transaction.savepoint() # 事务保存点
|
|
|
+ if len(bulk) > 0:
|
|
|
+ UIDModel.objects.bulk_create(bulk)
|
|
|
+ bulk.clear()
|
|
|
+ except Exception as e:
|
|
|
+ # print('--------------------------error')
|
|
|
+ # print(repr(e))
|
|
|
+ if savePoint:
|
|
|
+ transaction.rollback(savePoint)
|
|
|
+ return response.json(174)
|
|
|
+ else:
|
|
|
+ del data
|
|
|
+ del bulk
|
|
|
+ data = {
|
|
|
+ 'value': mac['value'],
|
|
|
+ 'is_active': tmpMac is not None
|
|
|
+ }
|
|
|
+ MacModel.objects.filter().update(**data)
|
|
|
+ except Exception as e:
|
|
|
+ # print('--------------------------error 1111')
|
|
|
+ # print(repr(e))
|
|
|
+ return response.json(174)
|
|
|
|
|
|
# print('重复:')
|
|
|
# print(duplicate)
|
|
|
|
|
|
operation = self.formatOperation(operation='上传', quantity=int(count), area=int(area))
|
|
|
self.add_log(request, operation, token)
|
|
|
+
|
|
|
+ if tmpMac is None:
|
|
|
+ return response.json(175, {'last_uid': key})
|
|
|
return response.json(0, {'count': count, 'duplicate_count': len(duplicate), 'data': duplicate})
|
|
|
|
|
|
def do_reset(self, data, response, area, request, token):
|
|
@@ -172,6 +229,7 @@ class UploadUIDFileView(View):
|
|
|
else:
|
|
|
return str.format(operation=operation, quantity=quantity, area='国外')
|
|
|
|
|
|
+
|
|
|
class DownloadUIDFileView(View):
|
|
|
|
|
|
@method_decorator(csrf_exempt)
|
|
@@ -209,7 +267,7 @@ class DownloadUIDFileView(View):
|
|
|
if area >= 0 and quantity > 0:
|
|
|
|
|
|
uid_qs = UserUIDModel.objects.filter(user__id=token.userID, uid__status=1, uid__area=area). \
|
|
|
- values('uid__id', 'uid__uid', 'uid__uid_extra', 'uid__add_time', 'uid__update_time', 'uid__area')
|
|
|
+ values('uid__id', 'uid__uid', 'uid__mac', 'uid__uid_extra', 'uid__add_time', 'uid__update_time', 'uid__area')
|
|
|
count = uid_qs.count()
|
|
|
if count < quantity:
|
|
|
return response.json(444, '设备UID不足')
|
|
@@ -231,11 +289,17 @@ class DownloadUIDFileView(View):
|
|
|
updates = []
|
|
|
content = ''
|
|
|
for item in uid_qs:
|
|
|
+ # print(item)
|
|
|
+ mac: str = item['uid__mac']
|
|
|
+ index = mac.rfind(':')
|
|
|
+ tmp = mac[0:index] + '\t' + mac[index:]
|
|
|
+ content += tmp + '\t'
|
|
|
content += item['uid__uid']
|
|
|
- content += '\n'
|
|
|
+ content += '\r\n'
|
|
|
updates.append(UIDModel(
|
|
|
id=item['uid__id'],
|
|
|
uid=item['uid__uid'],
|
|
|
+ mac=item['uid__mac'],
|
|
|
uid_extra=item['uid__uid_extra'],
|
|
|
status=2,
|
|
|
add_time=item['uid__add_time'],
|