123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- # Copyright (C) 2022 #
- # @Time : 2022/7/18 16:16
- # @Author : ghl
- # @Email : Guanhailogn@asj6.wecom.work
- # @File : UnicomManageController.py
- # @Software: PyCharm
- import time
- from django.db import transaction
- from django.views.generic.base import View
- from Model.models import UnicomDeviceInfo, UnicomCombo, Pay_Type, UnicomComboOrderInfo, Device_User
- from Object.ResponseObject import ResponseObject
- class UnicomComboView(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 == 'get/deta/info':
- return self.get_unicom_info(request_dict, response)
- # 获取支付类型
- elif operation == 'get/pay':
- return self.get_pay_type(response)
- # 添加和编辑卡套餐
- elif operation == 'edit/combo':
- return self.edit_combo(request_dict, response)
- # 获取卡用户信息
- elif operation == 'get/user':
- return self.get_user_combo(request_dict, response)
- # 获取设备套餐信息
- elif operation == 'order/info':
- return self.get_order_info(request_dict, response)
- # 统计4G套餐
- elif operation == 'getComboDataList':
- return self.combo_order_list(request_dict, response)
- # 删除卡套餐
- elif operation == 'dele/combo/info':
- return self.dele_combo_info(request_dict, response)
- @staticmethod
- def get_user_combo(request_dict, response):
- pageNo = request_dict.get('pageNo', None)
- pageSize = request_dict.get('pageSize', None)
- if not all([pageNo, [pageSize]]):
- return response.json(444)
- page = int(pageNo)
- line = int(pageSize)
- get_info_qs = UnicomComboOrderInfo.objects.filter().values(
- 'iccid', 'activation_time', 'expire_time', 'status',
- 'combo__combo_name',
- 'combo__pay_type__payment',
- 'combo__price'
- )[(page - 1) * line:page * line]
- total = get_info_qs.count()
- try:
- res_list = []
- for item in get_info_qs:
- res = {
- 'userID': '',
- 'phone': '',
- 'userEmail': '',
- 'serialNo': '',
- 'loginTime': '',
- 'iccid': '',
- 'openTime': '',
- 'endTime': '',
- 'status': '',
- 'comboName': '',
- 'payType': '',
- 'price': ''
- }
- iccid = item['iccid']
- res['iccid'] = iccid
- res['openTime'] = item['activation_time']
- res['endTime'] = item['expire_time']
- res['status'] = item['status']
- res['comboName'] = item['combo__combo_name']
- res['payType'] = item['combo__pay_type__payment']
- res['price'] = item['combo__price']
- serial_no_qs = UnicomDeviceInfo.objects.filter(iccid=iccid).values('serial_no')
- # serial_no = serial_no_qs[0]['serial_no']
- res['serialNo'] = serial_no_qs.first()['serial_no']
- user_id_qs = UnicomDeviceInfo.objects.filter(iccid=iccid).values('user_id')
- user_id = user_id_qs[0]['user_id']
- if user_id_qs.exists():
- user_info_qs = Device_User.objects.filter(userID=user_id).values(
- 'userEmail', 'data_joined',
- 'phone')
- res['userID'] = user_id
- res['loginTime'] = user_info_qs.first()['data_joined'].strftime("%Y-%m-%d %H:%M:%S")
- res['userEmail'] = user_info_qs.first()['userEmail']
- res['phone'] = user_info_qs.first()['phone']
- res_list.append(res)
- return response.json(0, {'list': res_list, 'total': total})
- except Exception as e:
- return response.json(500, repr(e))
- @staticmethod
- def get_order_info(request_dict, response):
- """
- 获取卡套餐信息
- @param request_dict:
- @param response:
- @return:
- """
- serial_no = request_dict.get('serialNo', None)
- pageNo = request_dict.get('pageNo', None)
- pageSize = request_dict.get('pageSize', None)
- if not all([pageSize, pageNo]):
- return response.json(444)
- # else:
- page = int(pageNo)
- line = int(pageSize)
- # 参考云端获取设备套餐信息(代码逻辑)
- # 参考云端多情况(条件)下的查询,返回结果
- try:
- get_info_qs = UnicomDeviceInfo.objects.filter().values('iccid')
- iccid = get_info_qs[0]['iccid']
- if serial_no:
- get_info_qs = get_info_qs.filter(serial_no__contains=serial_no).values('iccid')
- iccid = get_info_qs[0]['iccid']
- if not get_info_qs.exists():
- return response.json(0, [])
- combo_info_qs = UnicomComboOrderInfo.objects.filter(iccid=iccid).values()
- combo_info_qs = combo_info_qs.values()
- combo_info_qs = combo_info_qs.order_by('-created_time')
- combo_info_qs = combo_info_qs[(page - 1) * line:page * line]
- total = combo_info_qs.count()
- return response.json(0, {'list': list(combo_info_qs), 'total': total})
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
- @classmethod
- def edit_combo(cls, request_dict, response):
- """
- 添加和编辑卡套餐
- """
- combo_id = request_dict.get('comboID', None)
- package_id = request_dict.get('packageId', None)
- combo_name = request_dict.get('comboName', None)
- status = request_dict.get('status', None)
- combo_type = request_dict.get('comboType', None)
- flow_total = request_dict.get('flowTotal', None)
- expiration_days = request_dict.get('expirationDays', None)
- expiration_type = request_dict.get('expirationType', None)
- pay_type = request_dict.get('payType', None)
- price = request_dict.get('price', None)
- remark = request_dict.get('remark', None)
- updated_time = request_dict.get('updatedTime', None)
- created_time = request_dict.get('createdTime', None)
- is_show = request_dict.get('show', None)
- add = request_dict.get('add', None)
- if not all([add]):
- return response.json(444)
- UnicomCombo.objects.filter(id=combo_id).values()
- try:
- with transaction.atomic():
- re_data = {
- 'package_id': package_id,
- 'combo_name': combo_name,
- 'status': status,
- 'combo_type': combo_type,
- 'flow_total': flow_total,
- 'expiration_days': expiration_days,
- 'expiration_type': expiration_type,
- 'price': price,
- 'remark': remark,
- 'updated_time': updated_time,
- 'created_time': created_time,
- 'is_show': is_show
- }
- if add == 1:
- UnicomCombo.objects.filter(id=combo_id).update(**re_data)
- UnicomCombo.objects.get(id=combo_id).pay_type.set(pay_type)
- else:
- UnicomCombo.objects.filter(id=combo_id).update(**re_data)
- UnicomCombo.objects.get(id=combo_id).pay_type.set(pay_type)
- UnicomCombo.objects.create(**re_data).pay_type.set(pay_type)
- return response.json(0)
- except Exception as e:
- return response.json(500, repr(e))
- @staticmethod
- def get_unicom_info(request_dict, response):
- """
- 获取套餐详细表
- @param request_dict:
- @param response:
- @return:
- """
- pageNo = request_dict.get('pageNo', None)
- pageSize = request_dict.get('pageSize', None)
- if not all([pageNo, pageSize]):
- return response.json(444)
- page = int(pageNo)
- line = int(pageSize)
- try:
- combo_qs = UnicomCombo.objects.filter(is_del=False) \
- .order_by('sort').values('id', 'combo_name',
- 'flow_total',
- 'expiration_days',
- 'expiration_type', 'price',
- 'remark')[(page - 1) * line:page * line]
- if not combo_qs.exists():
- return response.json(0, [])
- total = combo_qs.count()
- combo_list = []
- for item in combo_qs:
- # 获取支付方式列表
- pay_type_qs = Pay_Type.objects.filter(unicomcombo=item['id']).values('id', 'payment')
- combo_list.append({
- 'id': item['id'],
- 'comboName': item['combo_name'],
- 'flowTotal': item['flow_total'],
- 'expirationDays': item['expiration_days'],
- 'expirationType': item['expiration_type'],
- 'price': item['price'],
- 'remark': item['remark'],
- 'payTypes': list(pay_type_qs),
- })
- return response.json(0, {'list': combo_list, 'total': total})
- except Exception as e:
- return response.json(177, repr(e))
- @classmethod
- def get_pay_type(cls, response):
- """
- 获取支付类型
- @param response:
- @return:
- """
- pay_type_qs = Pay_Type.objects.all().values('id', 'payment')
- if not pay_type_qs.exists():
- return response.json(444)
- try:
- pay_type_list = []
- for pay_type in pay_type_qs:
- pay_type_qs.exists()
- pay_type_list.append(pay_type)
- return response.json(0, pay_type_list)
- except Exception as e:
- return response.json(500, e)
- @classmethod
- def dele_combo_info(cls, request_doct, response):
- pass
- def combo_order_list(self, request_dict, response):
- year = request_dict.get('year', None)
- Jan = int(time.mktime(time.strptime(year + '-1-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Feb = int(time.mktime(time.strptime(year + '-2-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Mar = int(time.mktime(time.strptime(year + '-3-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Apr = int(time.mktime(time.strptime(year + '-4-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- May = int(time.mktime(time.strptime(year + '-5-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Jun = int(time.mktime(time.strptime(year + '-6-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Jul = int(time.mktime(time.strptime(year + '-7-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Aug = int(time.mktime(time.strptime(year + '-8-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Sep = int(time.mktime(time.strptime(year + '-9-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Oct = int(time.mktime(time.strptime(year + '-10-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Nov = int(time.mktime(time.strptime(year + '-11-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Dec = int(time.mktime(time.strptime(year + '-12-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- Jan_next = int(time.mktime(time.strptime(str(int(year) + 1) + '-1-1 00:00:00', "%Y-%m-%d %H:%M:%S")))
- list_data = []
- unicom_combo_qs = UnicomCombo.objects.filter().values('id', 'combo_type', 'combo_name')
- if not unicom_combo_qs.exists():
- return response.json(173)
- try:
- for unicom_combo in unicom_combo_qs:
- name = unicom_combo['combo_name']
- combo_order = UnicomComboOrderInfo.objects.filter(combo_id=unicom_combo['id'])
- if not combo_order.exists():
- continue
- Jan_count = combo_order.filter(created_time__range=[Jan, Feb]).count()
- Feb_count = combo_order.filter(created_time__range=[Feb, Mar]).count()
- Mar_count = combo_order.filter(created_time__range=[Mar, Apr]).count()
- Apr_count = combo_order.filter(created_time__range=[Apr, May]).count()
- May_count = combo_order.filter(created_time__range=[May, Jun]).count()
- Jun_count = combo_order.filter(created_time__range=[Jun, Jul]).count()
- Jul_count = combo_order.filter(created_time__range=[Jul, Aug]).count()
- Aug_count = combo_order.filter(created_time__range=[Aug, Sep]).count()
- Sep_count = combo_order.filter(created_time__range=[Sep, Oct]).count()
- Oct_count = combo_order.filter(created_time__range=[Oct, Nov]).count()
- Nov_count = combo_order.filter(created_time__range=[Nov, Dec]).count()
- Dec_count = combo_order.filter(created_time__range=[Dec, Jan_next]).count()
- data = [Jan_count, Feb_count, Mar_count, Apr_count, May_count, Jun_count, Jul_count, Aug_count,
- Sep_count,
- Oct_count, Nov_count, Dec_count]
- cloud_data = {
- 'name': name,
- 'type': 'line',
- 'data': data,
- }
- list_data.append(cloud_data)
- return response.json(0, {'list': list_data})
- except Exception as e:
- print(e)
- return response.json(500, repr(e))
|