UnicomManageController.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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_order_list(request_dict, response)
  42. # 删除卡套餐
  43. elif operation == 'dele/combo/info':
  44. return self.dele_combo_info(request_dict, response)
  45. @staticmethod
  46. def get_user_combo(request_dict, response):
  47. pageNo = request_dict.get('pageNo', None)
  48. pageSize = request_dict.get('pageSize', None)
  49. if not all([pageNo, [pageSize]]):
  50. return response.json(444)
  51. page = int(pageNo)
  52. line = int(pageSize)
  53. get_info_qs = UnicomComboOrderInfo.objects.filter().values(
  54. 'iccid', 'activation_time', 'expire_time', 'status',
  55. 'combo__combo_name',
  56. 'combo__pay_type__payment',
  57. 'combo__price'
  58. )[(page - 1) * line:page * line]
  59. total = get_info_qs.count()
  60. try:
  61. res_list = []
  62. for item in get_info_qs:
  63. res = {
  64. 'userID': '',
  65. 'phone': '',
  66. 'userEmail': '',
  67. 'serialNo': '',
  68. 'loginTime': '',
  69. 'iccid': '',
  70. 'openTime': '',
  71. 'endTime': '',
  72. 'status': '',
  73. 'comboName': '',
  74. 'payType': '',
  75. 'price': ''
  76. }
  77. iccid = item['iccid']
  78. res['iccid'] = iccid
  79. res['openTime'] = item['activation_time']
  80. res['endTime'] = item['expire_time']
  81. res['status'] = item['status']
  82. res['comboName'] = item['combo__combo_name']
  83. res['payType'] = item['combo__pay_type__payment']
  84. res['price'] = item['combo__price']
  85. serial_no_qs = UnicomDeviceInfo.objects.filter(iccid=iccid).values('serial_no')
  86. # serial_no = serial_no_qs[0]['serial_no']
  87. res['serialNo'] = serial_no_qs.first()['serial_no']
  88. user_id_qs = UnicomDeviceInfo.objects.filter(iccid=iccid).values('user_id')
  89. user_id = user_id_qs[0]['user_id']
  90. if user_id_qs.exists():
  91. user_info_qs = Device_User.objects.filter(userID=user_id).values(
  92. 'userEmail', 'data_joined',
  93. 'phone')
  94. res['userID'] = user_id
  95. res['loginTime'] = user_info_qs.first()['data_joined'].strftime("%Y-%m-%d %H:%M:%S")
  96. res['userEmail'] = user_info_qs.first()['userEmail']
  97. res['phone'] = user_info_qs.first()['phone']
  98. res_list.append(res)
  99. return response.json(0, {'list': res_list, 'total': total})
  100. except Exception as e:
  101. return response.json(500, repr(e))
  102. @staticmethod
  103. def get_order_info(request_dict, response):
  104. """
  105. 获取卡套餐信息
  106. @param request_dict:
  107. @param response:
  108. @return:
  109. """
  110. serial_no = request_dict.get('serialNo', None)
  111. pageNo = request_dict.get('pageNo', None)
  112. pageSize = request_dict.get('pageSize', None)
  113. if not all([pageSize, pageNo]):
  114. return response.json(444)
  115. # else:
  116. page = int(pageNo)
  117. line = int(pageSize)
  118. # 参考云端获取设备套餐信息(代码逻辑)
  119. # 参考云端多情况(条件)下的查询,返回结果
  120. try:
  121. get_info_qs = UnicomDeviceInfo.objects.filter().values('iccid')
  122. iccid = get_info_qs[0]['iccid']
  123. if serial_no:
  124. get_info_qs = get_info_qs.filter(serial_no__contains=serial_no).values('iccid')
  125. iccid = get_info_qs[0]['iccid']
  126. if not get_info_qs.exists():
  127. return response.json(0, [])
  128. combo_info_qs = UnicomComboOrderInfo.objects.filter(iccid=iccid).values()
  129. combo_info_qs = combo_info_qs.values()
  130. combo_info_qs = combo_info_qs.order_by('-created_time')
  131. combo_info_qs = combo_info_qs[(page - 1) * line:page * line]
  132. total = combo_info_qs.count()
  133. return response.json(0, {'list': list(combo_info_qs), 'total': total})
  134. except Exception as e:
  135. print(e)
  136. return response.json(500, repr(e))
  137. @classmethod
  138. def edit_combo(cls, request_dict, response):
  139. """
  140. 添加和编辑卡套餐
  141. """
  142. combo_id = request_dict.get('comboID', None)
  143. package_id = request_dict.get('packageId', None)
  144. combo_name = request_dict.get('comboName', None)
  145. status = request_dict.get('status', None)
  146. combo_type = request_dict.get('comboType', None)
  147. flow_total = request_dict.get('flowTotal', None)
  148. expiration_days = request_dict.get('expirationDays', None)
  149. expiration_type = request_dict.get('expirationType', None)
  150. pay_type = request_dict.get('payType', None)
  151. price = request_dict.get('price', None)
  152. remark = request_dict.get('remark', None)
  153. updated_time = request_dict.get('updatedTime', None)
  154. created_time = request_dict.get('createdTime', None)
  155. is_show = request_dict.get('show', None)
  156. add = request_dict.get('add', None)
  157. if not all([add]):
  158. return response.json(444)
  159. UnicomCombo.objects.filter(id=combo_id).values()
  160. try:
  161. with transaction.atomic():
  162. re_data = {
  163. 'package_id': package_id,
  164. 'combo_name': combo_name,
  165. 'status': status,
  166. 'combo_type': combo_type,
  167. 'flow_total': flow_total,
  168. 'expiration_days': expiration_days,
  169. 'expiration_type': expiration_type,
  170. 'price': price,
  171. 'remark': remark,
  172. 'updated_time': updated_time,
  173. 'created_time': created_time,
  174. 'is_show': is_show
  175. }
  176. if add == 1:
  177. UnicomCombo.objects.filter(id=combo_id).update(**re_data)
  178. UnicomCombo.objects.get(id=combo_id).pay_type.set(pay_type)
  179. else:
  180. UnicomCombo.objects.filter(id=combo_id).update(**re_data)
  181. UnicomCombo.objects.get(id=combo_id).pay_type.set(pay_type)
  182. UnicomCombo.objects.create(**re_data).pay_type.set(pay_type)
  183. return response.json(0)
  184. except Exception as e:
  185. return response.json(500, repr(e))
  186. @staticmethod
  187. def get_unicom_info(request_dict, response):
  188. """
  189. 获取套餐详细表
  190. @param request_dict:
  191. @param response:
  192. @return:
  193. """
  194. pageNo = request_dict.get('pageNo', None)
  195. pageSize = request_dict.get('pageSize', None)
  196. if not all([pageNo, pageSize]):
  197. return response.json(444)
  198. page = int(pageNo)
  199. line = int(pageSize)
  200. try:
  201. combo_qs = UnicomCombo.objects.filter(is_del=False) \
  202. .order_by('sort').values('id', 'combo_name',
  203. 'flow_total',
  204. 'expiration_days',
  205. 'expiration_type', 'price',
  206. 'remark')[(page - 1) * line:page * line]
  207. if not combo_qs.exists():
  208. return response.json(0, [])
  209. total = combo_qs.count()
  210. combo_list = []
  211. for item in combo_qs:
  212. # 获取支付方式列表
  213. pay_type_qs = Pay_Type.objects.filter(unicomcombo=item['id']).values('id', 'payment')
  214. combo_list.append({
  215. 'id': item['id'],
  216. 'comboName': item['combo_name'],
  217. 'flowTotal': item['flow_total'],
  218. 'expirationDays': item['expiration_days'],
  219. 'expirationType': item['expiration_type'],
  220. 'price': item['price'],
  221. 'remark': item['remark'],
  222. 'payTypes': list(pay_type_qs),
  223. })
  224. return response.json(0, {'list': combo_list, 'total': total})
  225. except Exception as e:
  226. return response.json(177, repr(e))
  227. @classmethod
  228. def get_pay_type(cls, response):
  229. """
  230. 获取支付类型
  231. @param response:
  232. @return:
  233. """
  234. pay_type_qs = Pay_Type.objects.all().values('id', 'payment')
  235. if not pay_type_qs.exists():
  236. return response.json(444)
  237. try:
  238. pay_type_list = []
  239. for pay_type in pay_type_qs:
  240. pay_type_qs.exists()
  241. pay_type_list.append(pay_type)
  242. return response.json(0, pay_type_list)
  243. except Exception as e:
  244. return response.json(500, e)
  245. @classmethod
  246. def dele_combo_info(cls, request_doct, response):
  247. pass
  248. def combo_order_list(self, request_dict, response):
  249. year = request_dict.get('year', None)
  250. Jan = int(time.mktime(time.strptime(year + '-1-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  251. Feb = int(time.mktime(time.strptime(year + '-2-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  252. Mar = int(time.mktime(time.strptime(year + '-3-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  253. Apr = int(time.mktime(time.strptime(year + '-4-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  254. May = int(time.mktime(time.strptime(year + '-5-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  255. Jun = int(time.mktime(time.strptime(year + '-6-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  256. Jul = int(time.mktime(time.strptime(year + '-7-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  257. Aug = int(time.mktime(time.strptime(year + '-8-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  258. Sep = int(time.mktime(time.strptime(year + '-9-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  259. Oct = int(time.mktime(time.strptime(year + '-10-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  260. Nov = int(time.mktime(time.strptime(year + '-11-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  261. Dec = int(time.mktime(time.strptime(year + '-12-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  262. Jan_next = int(time.mktime(time.strptime(str(int(year) + 1) + '-1-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
  263. list_data = []
  264. unicom_combo_qs = UnicomCombo.objects.filter().values('id', 'combo_type', 'combo_name')
  265. if not unicom_combo_qs.exists():
  266. return response.json(173)
  267. try:
  268. for unicom_combo in unicom_combo_qs:
  269. name = unicom_combo['combo_name']
  270. combo_order = UnicomComboOrderInfo.objects.filter(combo_id=unicom_combo['id'])
  271. if not combo_order.exists():
  272. continue
  273. Jan_count = combo_order.filter(created_time__range=[Jan, Feb]).count()
  274. Feb_count = combo_order.filter(created_time__range=[Feb, Mar]).count()
  275. Mar_count = combo_order.filter(created_time__range=[Mar, Apr]).count()
  276. Apr_count = combo_order.filter(created_time__range=[Apr, May]).count()
  277. May_count = combo_order.filter(created_time__range=[May, Jun]).count()
  278. Jun_count = combo_order.filter(created_time__range=[Jun, Jul]).count()
  279. Jul_count = combo_order.filter(created_time__range=[Jul, Aug]).count()
  280. Aug_count = combo_order.filter(created_time__range=[Aug, Sep]).count()
  281. Sep_count = combo_order.filter(created_time__range=[Sep, Oct]).count()
  282. Oct_count = combo_order.filter(created_time__range=[Oct, Nov]).count()
  283. Nov_count = combo_order.filter(created_time__range=[Nov, Dec]).count()
  284. Dec_count = combo_order.filter(created_time__range=[Dec, Jan_next]).count()
  285. data = [Jan_count, Feb_count, Mar_count, Apr_count, May_count, Jun_count, Jul_count, Aug_count,
  286. Sep_count,
  287. Oct_count, Nov_count, Dec_count]
  288. cloud_data = {
  289. 'name': name,
  290. 'type': 'line',
  291. 'data': data,
  292. }
  293. list_data.append(cloud_data)
  294. return response.json(0, {'list': list_data})
  295. except Exception as e:
  296. print(e)
  297. return response.json(500, repr(e))