UnicomComboTaskController.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : UnicomComboTaskController.py
  4. @Time : 2022/6/30 16:23
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import datetime
  10. import logging
  11. import time
  12. from django.db import transaction
  13. from django.views import View
  14. from Model.models import UnicomComboOrderInfo, UnicomCombo, Order_Model
  15. from Object.ResponseObject import ResponseObject
  16. from Object.UnicomObject import UnicomObjeect
  17. class UnicomComboTaskView(View):
  18. def get(self, request, *args, **kwargs):
  19. request.encoding = 'utf-8'
  20. operation = kwargs.get('operation')
  21. return self.validation(request.GET, request, operation)
  22. def post(self, request, *args, **kwargs):
  23. request.encoding = 'utf-8'
  24. operation = kwargs.get('operation')
  25. return self.validation(request.POST, request, operation)
  26. def validation(self, request_dict, request, operation):
  27. response = ResponseObject()
  28. print(request)
  29. if operation == 'check-activate':
  30. return self.check_activate_combo(request_dict, response)
  31. elif operation == 'check-flow':
  32. return self.check_flow_usage(response)
  33. elif operation == 'check-expire':
  34. today = datetime.datetime.today()
  35. year = today.year
  36. month = today.month
  37. self.query_unused_combo_and_activate(request_dict.get('iccid'), year, month, '666')
  38. return response.json(0)
  39. @classmethod
  40. def check_activate_combo(cls, request_dict, response):
  41. """
  42. 定时检查是否有次月激活套餐
  43. @param request_dict:
  44. @param response:
  45. @return:
  46. """
  47. logger = logging.getLogger('info')
  48. print(request_dict)
  49. logger.info('--->定时检查是否有次月激活联通套餐')
  50. now_time = int(time.time())
  51. combo_order_info_qs = UnicomComboOrderInfo.objects.filter(status=0, next_month_activate=True,
  52. activation_time__lte=now_time,
  53. expire_time__gte=now_time, is_del=0).values()
  54. if not combo_order_info_qs.exists():
  55. return response.json(0)
  56. try:
  57. now_time = int(time.time())
  58. today = datetime.datetime.today()
  59. year = today.year
  60. month = today.month
  61. with transaction.atomic():
  62. unicom_api = UnicomObjeect()
  63. for item in combo_order_info_qs:
  64. if item['order_id']:
  65. order_id = item['order_id']
  66. order_qs = Order_Model.objects.filter(orderID=order_id, status=1)
  67. if not order_qs.exists():
  68. continue
  69. combo_order_qs = UnicomComboOrderInfo.objects.filter(status=1, iccid=item['iccid'])
  70. # 当前已有套餐正在使用则跳出当前循环
  71. if combo_order_qs.exists():
  72. continue
  73. combo_id = item['combo_id']
  74. combo_qs = UnicomCombo.objects.filter(id=combo_id).values()
  75. if not combo_qs.exists():
  76. continue
  77. # 查询当月用量情况
  78. flow_total_usage = unicom_api.get_flow_usage_total(year, month, item['iccid'])
  79. flow_total_usage = str(flow_total_usage)
  80. iccid = item['iccid']
  81. # 检查激活iccid
  82. unicom_api.change_device_to_activate(iccid)
  83. UnicomComboOrderInfo.objects.filter(id=item['id']) \
  84. .update(status=1, updated_time=now_time, year=year, month=month,
  85. flow_total_usage=flow_total_usage)
  86. logger.info('激活成功,订单编号:{}'.format(order_id))
  87. return response.json(0)
  88. except Exception as e:
  89. print(e)
  90. return response.json(177, repr(e))
  91. @classmethod
  92. def check_flow_usage(cls, response):
  93. """
  94. 检查流量使用情况
  95. @return:
  96. """
  97. logger = logging.getLogger('info')
  98. logger.info('--->进入检查流量使用情况')
  99. try:
  100. combo_order_qs = UnicomComboOrderInfo.objects.filter(status=1, is_del=False).values()
  101. if not combo_order_qs.exists():
  102. return response.json(0)
  103. today = datetime.datetime.today()
  104. year = today.year
  105. month = today.month
  106. unicom_api = UnicomObjeect()
  107. now_time = int(time.time())
  108. for item in combo_order_qs:
  109. iccid = item['iccid']
  110. usage_flow = float(item['flow_total_usage']) if item['flow_total_usage'] else 0.0
  111. combo_id = item['combo_id']
  112. combo_qs = UnicomCombo.objects.filter(id=combo_id).values()
  113. if combo_qs.exists():
  114. combo_qs = combo_qs.first()
  115. flow_total = combo_qs['flow_total']
  116. # 查询当前月用量历史
  117. month_usage_flow = unicom_api.get_flow_usage_total(year, month, iccid)
  118. logger.info('--->{}-{},iccid:{};套餐总值:{},激活时当月用量值:{},月已用量:{}'
  119. .format(year, month, iccid, flow_total, usage_flow, month_usage_flow))
  120. is_expire = False
  121. if item['year'] == year and item['month'] == month:
  122. if month_usage_flow > 0:
  123. # 初始套餐已使用流量 + 套餐总流量
  124. flow = usage_flow + flow_total
  125. if month_usage_flow >= flow:
  126. is_expire = True
  127. else:
  128. activate_year = item['year']
  129. activate_month = item['month']
  130. # 上月使用流量
  131. last_usage_flow = unicom_api.get_flow_usage_total(activate_year, activate_month, iccid)
  132. # 上月套餐实际使用量
  133. actual_usage_flow = last_usage_flow - usage_flow
  134. # 剩余
  135. surplus_flow = flow_total - actual_usage_flow
  136. if month_usage_flow > 0:
  137. if month_usage_flow >= surplus_flow:
  138. is_expire = True
  139. # 检查是否有当月未使用套餐 没有则停卡
  140. if is_expire:
  141. UnicomComboOrderInfo.objects.filter(id=item['id']).update(status=2, updated_time=now_time)
  142. activate_status = cls.query_unused_combo_and_activate(iccid, year, month,
  143. month_usage_flow)
  144. if not activate_status:
  145. # 停用
  146. unicom_api.change_device_to_disable(iccid)
  147. return response.json(0)
  148. except Exception as e:
  149. logger.info('检测流量用量详情异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  150. return response.json(177, repr(e))
  151. @staticmethod
  152. def query_unused_combo_and_activate(iccid, year, month, usage_flow):
  153. """
  154. 查询未使用套餐并激活
  155. @param iccid:
  156. @param year:
  157. @param month:
  158. @param usage_flow:
  159. @return:
  160. """
  161. try:
  162. now_time = int(time.time())
  163. combo_order_qs = UnicomComboOrderInfo.objects.filter(expire_time__gt=now_time, status=0,
  164. next_month_activate=False, iccid=iccid).order_by(
  165. 'created_time')
  166. if not combo_order_qs.exists():
  167. return False
  168. combo_order = combo_order_qs.first()
  169. upd_data = {
  170. 'status': 1,
  171. 'year': year,
  172. 'month': month,
  173. 'flow_total_usage': str(usage_flow),
  174. 'updated_time': now_time,
  175. }
  176. UnicomComboOrderInfo.objects.filter(id=combo_order.id).update(**upd_data)
  177. return True
  178. except Exception as e:
  179. print(e)
  180. return False