|
@@ -4,10 +4,12 @@
|
|
|
# @Email : Guanhailogn@asj6.wecom.work
|
|
|
# @File : UnicomManageController.py
|
|
|
# @Software: PyCharm
|
|
|
+import datetime
|
|
|
import hashlib
|
|
|
import json
|
|
|
import time
|
|
|
import uuid
|
|
|
+from decimal import Decimal
|
|
|
|
|
|
import openpyxl
|
|
|
import requests
|
|
@@ -20,9 +22,11 @@ from Ansjer.config import LOGGER
|
|
|
from Controller.UnicomCombo.UnicomComboController import UnicomComboView
|
|
|
from Model.models import UnicomDeviceInfo, UnicomCombo, Pay_Type, UnicomComboOrderInfo, Device_User, Order_Model, \
|
|
|
ExchangeCode, UnicomFlowPush, SysMsgModel, UnicomComboExperienceHistory, LogModel
|
|
|
+from Object.Enums.WXOperatorEnum import WXOperatorEnum
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from Object.UnicomObject import UnicomObjeect
|
|
|
+from Object.WXTechObject import WXTechObject
|
|
|
from Service.CommonService import CommonService
|
|
|
|
|
|
|
|
@@ -83,6 +87,8 @@ class UnicomManageControllerView(View):
|
|
|
return self.create_package_cdk(request_dict, response)
|
|
|
elif operation == 'resetCardPackage':
|
|
|
return self.reset_card_package(request, request_dict, response)
|
|
|
+ elif operation == 'getPackageDetails':
|
|
|
+ return self.get_package_details(request_dict, response)
|
|
|
else:
|
|
|
return response.json(404)
|
|
|
|
|
@@ -96,8 +102,14 @@ class UnicomManageControllerView(View):
|
|
|
now_time = int(time.time())
|
|
|
if device_info_qs.exists(): # 首先查询SIM卡绑定信息是否存在
|
|
|
iccid = device_info_qs.first().iccid
|
|
|
- if device_info_qs.first().card_type == 1:
|
|
|
- return response.json(0)
|
|
|
+ if device_info_qs.first().card_type == 1: # 五兴电信
|
|
|
+ data = {'iccids': iccid, 'operator': WXOperatorEnum.TELECOM.value}
|
|
|
+ wx_tech = WXTechObject()
|
|
|
+ res = wx_tech.delete_card_package(**data)
|
|
|
+ if res['code'] == '0':
|
|
|
+ UnicomComboExperienceHistory.objects.filter(iccid=iccid).delete()
|
|
|
+ return response.json(0)
|
|
|
+ return response.json(176)
|
|
|
flow_push_qs = UnicomFlowPush.objects.filter(serial_no=serial_number)
|
|
|
if flow_push_qs.exists(): # 删除流量预警推送
|
|
|
flow_push_qs.delete()
|
|
@@ -112,7 +124,7 @@ class UnicomManageControllerView(View):
|
|
|
combo_experience_history_qs = UnicomComboExperienceHistory.objects.filter(iccid=iccid)
|
|
|
if combo_experience_history_qs.exists():
|
|
|
combo_experience_history_qs.delete()
|
|
|
- UnicomObjeect().change_device_to_activate(iccid)
|
|
|
+ UnicomObjeect().change_device_to_disable(iccid) # 重置流量停用设备
|
|
|
ip = CommonService.get_ip_address(request)
|
|
|
describe = '重置4G流量序列号{},iccid:{}'.format(serial_number, iccid)
|
|
|
cls.generate_card_package_order(iccid, serial_number)
|
|
@@ -221,16 +233,11 @@ class UnicomManageControllerView(View):
|
|
|
unicom_device_qs = unicom_device_qs[(page - 1) * line:page * line]
|
|
|
list_data = []
|
|
|
for unicom_device in unicom_device_qs:
|
|
|
- data = {
|
|
|
- 'iccid': unicom_device.iccid,
|
|
|
- 'serialNo': unicom_device.serial_no,
|
|
|
- 'userID': unicom_device.user_id,
|
|
|
- 'cardType': unicom_device.card_type,
|
|
|
- 'status': unicom_device.status,
|
|
|
- 'mainCard': unicom_device.main_card,
|
|
|
- 'createdTime': unicom_device.created_time,
|
|
|
- 'updatedTime': unicom_device.updated_time
|
|
|
- }
|
|
|
+ data = {'iccid': unicom_device.iccid, 'serialNo': unicom_device.serial_no,
|
|
|
+ 'userID': unicom_device.user_id, 'cardType': unicom_device.card_type,
|
|
|
+ 'status': unicom_device.status, 'mainCard': unicom_device.main_card,
|
|
|
+ 'createdTime': unicom_device.created_time, 'updatedTime': unicom_device.updated_time,
|
|
|
+ 'cardStatus': self.get_device_status_by_iccid(unicom_device.iccid, unicom_device.card_type)}
|
|
|
device_user_qs = Device_User.objects.filter(userID=unicom_device.user_id).values('username', 'NickName',
|
|
|
'phone')
|
|
|
data['userName'] = device_user_qs[0]['username'] if device_user_qs.exists() else ''
|
|
@@ -299,16 +306,7 @@ class UnicomManageControllerView(View):
|
|
|
col_names = [desc[0] for desc in cursor.description]
|
|
|
for item in data_obj:
|
|
|
order_dict = dict(zip(col_names, item))
|
|
|
- if order_dict['status'] == 1:
|
|
|
- total_flow = UnicomObjeect.current_sim_traffic_usage_details(order_dict['iccid']) # 4G总值
|
|
|
- total_flow = int(total_flow)
|
|
|
- flow_total_usage = order_dict['flow_total_usage'] # 激活时当月已用流量
|
|
|
- if flow_total_usage == '': # 判断数值是否为空
|
|
|
- flow_total_usage = 0
|
|
|
- flow_total_usage = float(flow_total_usage)
|
|
|
- order_dict['using_total'] = total_flow - flow_total_usage
|
|
|
- else:
|
|
|
- order_dict['using_total'] = 0
|
|
|
+ order_dict['using_total'] = 0
|
|
|
result_list.append(order_dict)
|
|
|
return response.json(0, {'orderList': result_list, 'total': total})
|
|
|
except Exception as e:
|
|
@@ -736,3 +734,112 @@ class UnicomManageControllerView(View):
|
|
|
# 取哈希值的前8位,并将其转换为大写字母
|
|
|
code = sha1.hexdigest()[:8].upper()
|
|
|
return code
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_package_details(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 根据序列号获取套餐包列表
|
|
|
+ """
|
|
|
+ serial_number = request_dict.get('serialNumber', None)
|
|
|
+ if not serial_number:
|
|
|
+ return response.json(444)
|
|
|
+ ud_qs = UnicomDeviceInfo.objects.filter(serial_no=serial_number) \
|
|
|
+ .values('iccid', 'card_type')
|
|
|
+ package_list = []
|
|
|
+ if not ud_qs.exists():
|
|
|
+ return response.json(0, {'packageList': package_list})
|
|
|
+ iccid = ud_qs[0]['iccid']
|
|
|
+ card_type = ud_qs[0]['card_type']
|
|
|
+ if card_type == 0:
|
|
|
+ o_qs = UnicomComboOrderInfo.objects.filter(iccid=iccid) \
|
|
|
+ .values('status', 'flow_total_usage', 'flow_exceed', 'activation_time', 'expire_time',
|
|
|
+ 'combo__combo_name', 'combo__flow_total', 'updated_time') \
|
|
|
+ .order_by('created_time')
|
|
|
+ if not o_qs:
|
|
|
+ return response.json(0, {'packageList': package_list})
|
|
|
+ return response.json(0, {'package_list': cls.get_unicom_package_list(iccid, o_qs)})
|
|
|
+ if card_type == 1:
|
|
|
+ data = {'iccid': iccid, 'operator': 3}
|
|
|
+ return response.json(0, {'package_list': cls.get_wx_package_list(**data)})
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_unicom_package_list(iccid, o_qs):
|
|
|
+ package_list = []
|
|
|
+ unicom_api = UnicomObjeect()
|
|
|
+ for i, item in enumerate(o_qs):
|
|
|
+ package_status = item['status']
|
|
|
+ flow_total = item['combo__flow_total']
|
|
|
+ flow_total_usage = float(unicom_api.get_flow_usage_total(iccid))
|
|
|
+ activate_flow = float(item['flow_total_usage']) if item['flow_total_usage'] else 0
|
|
|
+ used = 0
|
|
|
+ if package_status == 1:
|
|
|
+ used = flow_total_usage - activate_flow # 已用流量
|
|
|
+ elif package_status == 2:
|
|
|
+ index = i + 1
|
|
|
+ if index < len(o_qs) and o_qs[index]['flow_total_usage']:
|
|
|
+ package_used_flow = float(o_qs[i + 1]['flow_total_usage'])
|
|
|
+ used = package_used_flow - activate_flow
|
|
|
+ else:
|
|
|
+ used = flow_total_usage - activate_flow
|
|
|
+ status_dict = {0: "待使用", 1: "使用中", 2: "已失效"}
|
|
|
+ status = status_dict.get(item['status'])
|
|
|
+ package_list.append({
|
|
|
+ 'status': status,
|
|
|
+ 'flowTotal': flow_total,
|
|
|
+ 'used': Decimal(used).quantize(Decimal('0.00')),
|
|
|
+ 'activationTime': datetime.datetime.fromtimestamp(item['activation_time']).strftime(
|
|
|
+ '%Y-%m-%d %H:%M:%S'),
|
|
|
+ 'expireTime': datetime.datetime.fromtimestamp(item['expire_time']).strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
+ 'updatedTime': datetime.datetime.fromtimestamp(item['updated_time']).strftime('%Y-%m-%d %H:%M:%S')
|
|
|
+ })
|
|
|
+ return package_list
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_wx_package_list(**data):
|
|
|
+ """
|
|
|
+ 获取五兴套餐订购记录
|
|
|
+ @param data: iccid,operator
|
|
|
+ @return: 订购记录结果
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ package_list = []
|
|
|
+ wx_tech = WXTechObject()
|
|
|
+ result = wx_tech.get_package_order_record(**data)
|
|
|
+ if not result:
|
|
|
+ return package_list
|
|
|
+ status_dict = {0: "待使用", 1: "使用中", 2: "已完成", 3: "已退订", 4: "已失效", 5: "已删除"}
|
|
|
+ for item in result['data']:
|
|
|
+ used_flow = float(item['flowTotal']) - float(item['flowRemain'])
|
|
|
+ package_list.append({
|
|
|
+ 'status': status_dict.get(int(item['state'])),
|
|
|
+ 'flowTotal': item['flowTotal'],
|
|
|
+ 'used': Decimal(used_flow).quantize(Decimal('0.00')),
|
|
|
+ 'activationTime': item['startDate'],
|
|
|
+ 'expireTime': item['endDate'],
|
|
|
+ 'updatedTime': ''
|
|
|
+ })
|
|
|
+ return package_list
|
|
|
+ except Exception as e:
|
|
|
+ print(repr(e))
|
|
|
+ return []
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_device_status_by_iccid(iccid, card_type):
|
|
|
+ try:
|
|
|
+ re_data = {'iccid': iccid}
|
|
|
+ if card_type == 0:
|
|
|
+ status_dict = {1: '已激活', 2: '可激活', 3: '已停用', 4: '已失效', 5: '可测试', 6: '库存', 7: '已更换', 8: '已清除'}
|
|
|
+ result = UnicomObjeect().query_device_status(**re_data)
|
|
|
+ res_dict = UnicomObjeect().get_text_dict(result)
|
|
|
+ return status_dict.get(int(res_dict['data']['status']), 'N/A')
|
|
|
+ elif card_type == 1:
|
|
|
+ status_dict = {1: '库存', 2: '可激活', 3: '已激活', 4: '已停用', 5: '已失效', 6: '强制停机'}
|
|
|
+ data = {'iccid': iccid, 'operator': 3}
|
|
|
+ wx_tech = WXTechObject()
|
|
|
+ result = wx_tech.get_cards_info(**data)
|
|
|
+ return status_dict.get(int(result['data']['cardStatusCode']), 'N/A')
|
|
|
+ else:
|
|
|
+ return 'N/A'
|
|
|
+ except Exception as e:
|
|
|
+ print(repr(e))
|
|
|
+ return 'N/A'
|