|
@@ -0,0 +1,92 @@
|
|
|
+# -*- encoding: utf-8 -*-
|
|
|
+"""
|
|
|
+@File : UnicomComboTaskController.py
|
|
|
+@Time : 2022/6/30 16:23
|
|
|
+@Author : stephen
|
|
|
+@Email : zhangdongming@asj6.wecom.work
|
|
|
+@Software: PyCharm
|
|
|
+"""
|
|
|
+import logging
|
|
|
+import time
|
|
|
+
|
|
|
+from django.db import transaction
|
|
|
+from django.views import View
|
|
|
+
|
|
|
+from Model.models import UnicomComboOrderInfo, UnicomCombo, Order_Model
|
|
|
+from Object.ResponseObject import ResponseObject
|
|
|
+from Object.UnicomObject import UnicomObjeect
|
|
|
+
|
|
|
+
|
|
|
+class UnicomComboTaskView(View):
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ return self.validation(request.GET, request, operation)
|
|
|
+
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ return self.validation(request.POST, request, operation)
|
|
|
+
|
|
|
+ def validation(self, request_dict, request, operation):
|
|
|
+ response = ResponseObject()
|
|
|
+ print(request)
|
|
|
+ if operation == 'check-activate':
|
|
|
+ return self.check_activate_combo(request_dict, response)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def check_activate_combo(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 定时检查是否有次月激活套餐
|
|
|
+ @param request_dict:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ logger = logging.getLogger('info')
|
|
|
+ print(request_dict)
|
|
|
+ logger.info('定时检查是否有次月激活联通套餐')
|
|
|
+ now_time = int(time.time())
|
|
|
+ combo_order_info_qs = UnicomComboOrderInfo.objects.filter(status=0, next_month_activate=True,
|
|
|
+ activation_time__lte=now_time,
|
|
|
+ expire_time__gte=now_time, is_del=0).values()
|
|
|
+ if not combo_order_info_qs.exists():
|
|
|
+ return response.json(0)
|
|
|
+ try:
|
|
|
+ now_time = int(time.time())
|
|
|
+ with transaction.atomic():
|
|
|
+ unicom_api = UnicomObjeect()
|
|
|
+
|
|
|
+ for item in combo_order_info_qs:
|
|
|
+ if item['order_id']:
|
|
|
+ order_id = item['order_id']
|
|
|
+ order_qs = Order_Model.objects.filter(orderID=order_id, status=1)
|
|
|
+ if not order_qs.exists():
|
|
|
+ logger.info('当前已付款联通订单套餐不存在:orderId={}'.format(order_id))
|
|
|
+ continue
|
|
|
+ combo_id = item['combo_id']
|
|
|
+ combo_qs = UnicomCombo.objects.filter(id=combo_id).values()
|
|
|
+ if combo_qs.exists():
|
|
|
+ item['status'] = 1
|
|
|
+ item['updated_time'] = now_time
|
|
|
+ params = {'iccid': item['iccid']}
|
|
|
+ result = unicom_api.verify_device(**params)
|
|
|
+ result = unicom_api.get_text_dict(result)
|
|
|
+ if result and result['success']:
|
|
|
+ # 1:已激活;2:可激活;3:已停用;4:已失效;5:可测试;6:库存;7:已更换;8:已清除;
|
|
|
+ status = result['data']['status']
|
|
|
+ if status != 1:
|
|
|
+ up_data = {'iccid': item['iccid'], 'status': 1}
|
|
|
+ device_result = UnicomObjeect.update_device_state(**up_data)
|
|
|
+ device_result = UnicomObjeect.get_text_dict(device_result)
|
|
|
+ if device_result and device_result['success']:
|
|
|
+ UnicomComboOrderInfo.objects.filter(id=item['id']) \
|
|
|
+ .update(status=1, updated_time=now_time)
|
|
|
+ else:
|
|
|
+ UnicomComboOrderInfo.objects.filter(id=item['id']) \
|
|
|
+ .update(status=1, updated_time=now_time)
|
|
|
+ logger.info('激活成功,订单编号:{}'.format(order_id))
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(177, repr(e))
|