UnicomManageController.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # Copyright (C) 2022 #
  2. # @Time : 2022/7/18 16:16
  3. # @Author : ghl
  4. # @Email : Guanhailogn@asj6.wecom.work
  5. # @File : UnicomManageController.py
  6. # @Software: PyCharm
  7. import time
  8. from django.db import transaction
  9. from django.views.generic.base import View
  10. from Model.models import UnicomDeviceInfo, UnicomCombo, Pay_Type, UnicomComboOrderInfo, Device_User
  11. from Object.ResponseObject import ResponseObject
  12. class UnicomComboView(View):
  13. def get(self, request, *args, **kwargs):
  14. request.encoding = 'utf-8'
  15. operation = kwargs.get('operation')
  16. return self.validation(request.GET, request, operation)
  17. def post(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. operation = kwargs.get('operation')
  20. return self.validation(request.POST, request, operation)
  21. def validation(self, request_dict, request, operation):
  22. response = ResponseObject()
  23. print(request)
  24. # 获取套餐详细表
  25. if operation == 'get/deta/info':
  26. return self.get_unicom_info(request_dict, response)
  27. # 获取支付类型
  28. elif operation == 'get/pay':
  29. return self.get_pay_type(response)
  30. # 添加和编辑卡套餐
  31. elif operation == 'edit/combo':
  32. return self.edit_combo(request_dict, response)
  33. # 获取卡用户信息
  34. elif operation == 'get/user':
  35. return self.get_user_combo(request_dict, response)
  36. # 获取设备套餐信息
  37. elif operation == 'order/info':
  38. return self.get_order_info(request_dict, response)
  39. # 统计4G套餐
  40. elif operation == 'getComboDataList':
  41. return self.combo_list(request_dict, response)
  42. # 删除卡套餐
  43. elif operation == 'dele/combo/info':
  44. return self.combo_order_info(request_dict, response)
  45. @staticmethod
  46. def get_user_combo(request_dict, response):
  47. """
  48. 定时检查是否有次月激活套餐
  49. @param request_dict:
  50. @param response:
  51. @return:
  52. """
  53. pageNo = request_dict.get('pageNo', None)
  54. pageSize = request_dict.get('pageSize', None)
  55. if not all([pageNo, [pageSize]]):
  56. return response.json(444)
  57. page = int(pageNo)
  58. line = int(pageSize)
  59. # cardType:状态(0:联通,1:电信,2:移动)
  60. # mainCard:状态(0:主卡,1:拔插卡)
  61. get_info_qs = UnicomDeviceInfo.objects.filter().values(
  62. 'iccid', 'serial_no', 'user_id', 'card_type', 'main_card', 'created_time'
  63. )[(page - 1) * line:page * line]
  64. total = get_info_qs.count()
  65. try:
  66. res_list = []
  67. for item in get_info_qs:
  68. res = {
  69. 'userID': item['user_id'],
  70. 'serialNo': item['serial_no'],
  71. 'iccid': item['iccid'],
  72. 'cardType': item['card_type'],
  73. 'mainCard': item['main_card'],
  74. 'createdTime': item['created_time']
  75. }
  76. usreID = get_info_qs[0]['user_id']
  77. user_info_qs = Device_User.objects.filter(userID=usreID).values(
  78. 'NickName', 'phone')
  79. res['userID'] = usreID
  80. res['NickName'] = user_info_qs.first()['NickName']
  81. res['phone'] = user_info_qs.first()['phone']
  82. res_list.append(res)
  83. return response.json(0, {'list': res_list, 'total': total})
  84. except Exception as e:
  85. return response.json(500, repr(e))
  86. @staticmethod
  87. def get_order_info(request_dict, response):
  88. """
  89. 获取卡套餐信息
  90. @param request_dict:
  91. @param response:
  92. @return:
  93. """
  94. serial_no = request_dict.get('serialNo', None)
  95. pageNo = request_dict.get('pageNo', None)
  96. pageSize = request_dict.get('pageSize', None)
  97. if not all([pageSize, pageNo]):
  98. return response.json(444)
  99. # else:
  100. page = int(pageNo)
  101. line = int(pageSize)
  102. # 参考云端获取设备套餐信息(代码逻辑)
  103. # 参考云端多情况(条件)下的查询,返回结果
  104. try:
  105. get_info_qs = UnicomDeviceInfo.objects.filter().values('iccid')
  106. iccid = get_info_qs[0]['iccid']
  107. if serial_no:
  108. get_info_qs = get_info_qs.filter(serial_no__contains=serial_no).values('iccid')
  109. iccid = get_info_qs[0]['iccid']
  110. if not get_info_qs.exists():
  111. return response.json(0, [])
  112. combo_info_qs = UnicomComboOrderInfo.objects.filter(iccid=iccid).values()
  113. combo_info_qs = combo_info_qs.values()
  114. combo_info_qs = combo_info_qs.order_by('-created_time')
  115. combo_info_qs = combo_info_qs[(page - 1) * line:page * line]
  116. total = combo_info_qs.count()
  117. return response.json(0, {'list': list(combo_info_qs), 'total': total})
  118. except Exception as e:
  119. print(e)
  120. return response.json(500, repr(e))
  121. @classmethod
  122. def edit_combo(cls, request_dict, response):
  123. """
  124. 添加和编辑卡套餐
  125. """
  126. combo_id = request_dict.get('comboID', None)
  127. package_id = request_dict.get('packageId', None)
  128. combo_name = request_dict.get('comboName', None)
  129. status = request_dict.get('status', None)
  130. combo_type = request_dict.get('comboType', None)
  131. flow_total = request_dict.get('flowTotal', None)
  132. expiration_days = request_dict.get('expirationDays', None)
  133. expiration_type = request_dict.get('expirationType', None)
  134. pay_type = request_dict.get('payType', None)
  135. price = request_dict.get('price', None)
  136. remark = request_dict.get('remark', None)
  137. updated_time = request_dict.get('updatedTime', None)
  138. created_time = request_dict.get('createdTime', None)
  139. is_show = request_dict.get('show', None)
  140. add = request_dict.get('add', None)
  141. if not all([add]):
  142. return response.json(444)
  143. UnicomCombo.objects.filter(id=combo_id).values()
  144. try:
  145. with transaction.atomic():
  146. re_data = {
  147. 'package_id': package_id,
  148. 'combo_name': combo_name,
  149. 'status': status,
  150. 'combo_type': combo_type,
  151. 'flow_total': flow_total,
  152. 'expiration_days': expiration_days,
  153. 'expiration_type': expiration_type,
  154. 'price': price,
  155. 'remark': remark,
  156. 'updated_time': updated_time,
  157. 'created_time': created_time,
  158. 'is_show': is_show
  159. }
  160. if add == 1:
  161. UnicomCombo.objects.filter(id=combo_id).update(**re_data)
  162. UnicomCombo.objects.get(id=combo_id).pay_type.set(pay_type)
  163. else:
  164. UnicomCombo.objects.filter(id=combo_id).update(**re_data)
  165. UnicomCombo.objects.get(id=combo_id).pay_type.set(pay_type)
  166. UnicomCombo.objects.create(**re_data).pay_type.set(pay_type)
  167. return response.json(0)
  168. except Exception as e:
  169. return response.json(500, repr(e))
  170. @staticmethod
  171. def get_unicom_info(request_dict, response):
  172. """
  173. 获取套餐详细表
  174. @param request_dict:
  175. @param response:
  176. @return:
  177. """
  178. pageNo = request_dict.get('pageNo', None)
  179. pageSize = request_dict.get('pageSize', None)
  180. if not all([pageNo, pageSize]):
  181. return response.json(444)
  182. page = int(pageNo)
  183. line = int(pageSize)
  184. try:
  185. combo_qs = UnicomCombo.objects.filter(is_del=False) \
  186. .order_by('sort').values('id', 'combo_name',
  187. 'flow_total',
  188. 'expiration_days',
  189. 'expiration_type', 'price',
  190. 'remark')[(page - 1) * line:page * line]
  191. if not combo_qs.exists():
  192. return response.json(0, [])
  193. total = combo_qs.count()
  194. combo_list = []
  195. for item in combo_qs:
  196. # 获取支付方式列表
  197. pay_type_list = [pay_type['id'] for pay_type in
  198. UnicomCombo.objects.get(id=item['id']).pay_type.values('id')]
  199. combo_list.append({
  200. 'id': item['id'],
  201. 'comboName': item['combo_name'],
  202. 'flowTotal': item['flow_total'],
  203. 'expirationDays': item['expiration_days'],
  204. 'expirationType': item['expiration_type'],
  205. 'price': item['price'],
  206. 'remark': item['remark'],
  207. 'payTypes': pay_type_list,
  208. })
  209. return response.json(0, {'list': combo_list, 'total': total})
  210. except Exception as e:
  211. return response.json(177, repr(e))
  212. @classmethod
  213. def get_pay_type(cls, response):
  214. """
  215. 获取支付类型
  216. @param response:
  217. @return:
  218. """
  219. pay_type_qs = Pay_Type.objects.all().values('id', 'payment')
  220. if not pay_type_qs.exists():
  221. return response.json(444)
  222. try:
  223. pay_type_list = []
  224. for pay_type in pay_type_qs:
  225. pay_type_qs.exists()
  226. pay_type_list.append(pay_type)
  227. return response.json(0, pay_type_list)
  228. except Exception as e:
  229. return response.json(500, e)
  230. @classmethod
  231. def combo_order_info(cls, request_doct, response):
  232. pass
  233. def combo_list(self, request_dict, response):
  234. year = request_dict.get('year', None)
  235. Jan = int(time.mktime(time.strptime(year + '-1-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  236. Feb = int(time.mktime(time.strptime(year + '-2-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  237. Mar = int(time.mktime(time.strptime(year + '-3-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  238. Apr = int(time.mktime(time.strptime(year + '-4-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  239. May = int(time.mktime(time.strptime(year + '-5-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  240. Jun = int(time.mktime(time.strptime(year + '-6-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  241. Jul = int(time.mktime(time.strptime(year + '-7-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  242. Aug = int(time.mktime(time.strptime(year + '-8-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  243. Sep = int(time.mktime(time.strptime(year + '-9-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  244. Oct = int(time.mktime(time.strptime(year + '-10-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  245. Nov = int(time.mktime(time.strptime(year + '-11-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  246. Dec = int(time.mktime(time.strptime(year + '-12-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  247. Jan_next = int(time.mktime(time.strptime(str(int(year) + 1) + '-1-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  248. list_data = []
  249. unicom_combo_qs = UnicomCombo.objects.filter().values('id', 'combo_type', 'combo_name')
  250. if not unicom_combo_qs.exists():
  251. return response.json(173)
  252. try:
  253. for unicom_combo in unicom_combo_qs:
  254. name = unicom_combo['combo_name']
  255. combo_order = UnicomComboOrderInfo.objects.filter(combo_id=unicom_combo['id'])
  256. if not combo_order.exists():
  257. continue
  258. Jan_count = combo_order.filter(created_time__range=[Jan, Feb]).count()
  259. Feb_count = combo_order.filter(created_time__range=[Feb, Mar]).count()
  260. Mar_count = combo_order.filter(created_time__range=[Mar, Apr]).count()
  261. Apr_count = combo_order.filter(created_time__range=[Apr, May]).count()
  262. May_count = combo_order.filter(created_time__range=[May, Jun]).count()
  263. Jun_count = combo_order.filter(created_time__range=[Jun, Jul]).count()
  264. Jul_count = combo_order.filter(created_time__range=[Jul, Aug]).count()
  265. Aug_count = combo_order.filter(created_time__range=[Aug, Sep]).count()
  266. Sep_count = combo_order.filter(created_time__range=[Sep, Oct]).count()
  267. Oct_count = combo_order.filter(created_time__range=[Oct, Nov]).count()
  268. Nov_count = combo_order.filter(created_time__range=[Nov, Dec]).count()
  269. Dec_count = combo_order.filter(created_time__range=[Dec, Jan_next]).count()
  270. data = [Jan_count, Feb_count, Mar_count, Apr_count, May_count, Jun_count, Jul_count, Aug_count,
  271. Sep_count,
  272. Oct_count, Nov_count, Dec_count]
  273. cloud_data = {
  274. 'name': name,
  275. 'type': 'line',
  276. 'data': data,
  277. }
  278. list_data.append(cloud_data)
  279. return response.json(0, {'list': list_data})
  280. except Exception as e:
  281. print(e)
  282. return response.json(500, repr(e))