|
@@ -6,18 +6,25 @@
|
|
|
@Email : zhangdongming@asj6.wecom.work
|
|
|
@Software: PyCharm
|
|
|
"""
|
|
|
+import datetime
|
|
|
import json
|
|
|
import logging
|
|
|
import time
|
|
|
+import traceback
|
|
|
+from decimal import Decimal
|
|
|
|
|
|
from django.db import transaction
|
|
|
-from django.http import HttpResponse
|
|
|
+from django.http import HttpResponse, JsonResponse
|
|
|
from django.views.generic.base import View
|
|
|
|
|
|
-from Model.models import UnicomDeviceInfo, UnicomCombo, Pay_Type
|
|
|
+from Model.models import UnicomDeviceInfo, UnicomCombo, Pay_Type, Order_Model, Store_Meal, AiStoreMeal, \
|
|
|
+ UnicomComboOrderInfo, UnicomComboExperienceHistory, UnicomDeviceStatusChangePush, SysMsgModel, UnicomFlowPush
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from Object.UnicomObject import UnicomObjeect
|
|
|
+from Object.utils import LocalDateTimeUtil
|
|
|
+from Object.utils.PayUtil import PayService
|
|
|
+from Service.CommonService import CommonService
|
|
|
|
|
|
|
|
|
class UnicomComboView(View):
|
|
@@ -32,25 +39,217 @@ class UnicomComboView(View):
|
|
|
return self.validation(request.POST, request, operation)
|
|
|
|
|
|
def validation(self, request_dict, request, operation):
|
|
|
- if operation == 'buy-notify':
|
|
|
- return self.package_callback_notify(request_dict, request)
|
|
|
+ response = ResponseObject('cn')
|
|
|
+ if operation == 'query-usage-history':
|
|
|
+ return self.query_device_usage_history(request_dict, response)
|
|
|
+ elif operation == 'test-notify':
|
|
|
+ order_id = request_dict.get('orderId', None)
|
|
|
+ activate_type = request_dict.get('activateType', 0)
|
|
|
+ iccid = request_dict.get('iccid', None)
|
|
|
+ combo_id = request_dict.get('comboId', None)
|
|
|
+ self.create_combo_order_info(order_id, int(activate_type), iccid, int(combo_id))
|
|
|
+ return HttpResponse('SUCCESS')
|
|
|
elif operation == 'device-queue-monitoring':
|
|
|
return self.device_queue_monitoring_push(request_dict, request)
|
|
|
elif operation == 'device-status-change':
|
|
|
return self.device_status_change_push(request_dict, request)
|
|
|
elif operation == 'device-bind':
|
|
|
- response = ResponseObject('cn')
|
|
|
return self.device_add(request_dict, response)
|
|
|
+ elif operation == 'device-status':
|
|
|
+ return self.update_device_status(request_dict, response)
|
|
|
+ elif operation == 'xxx-sign':
|
|
|
+ return self.get_test_sign(request_dict, response)
|
|
|
else:
|
|
|
token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
|
|
|
lang = request_dict.get('lang', token.lang)
|
|
|
response = ResponseObject(lang)
|
|
|
if token.code != 0:
|
|
|
return response.json(token.code)
|
|
|
+ user_id = token.userID
|
|
|
if operation == 'combo-save':
|
|
|
return self.save_unicom_combo(request_dict, response)
|
|
|
+ elif operation == 'combo-pay':
|
|
|
+ return self.buy_unicom_combo(user_id, request_dict, request, response)
|
|
|
elif operation == 'combo-list':
|
|
|
return self.query_package_list(response)
|
|
|
+ elif operation == 'get-device-info':
|
|
|
+ return self.get_device_info(request_dict, response)
|
|
|
+ elif operation == 'user-combo-query':
|
|
|
+ return self.user_combo_query(user_id, request_dict, response)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def user_combo_query(cls, user_id, request_dict, response):
|
|
|
+ """
|
|
|
+ 查询套餐流量列表与正在使用流量详情
|
|
|
+ @param user_id:
|
|
|
+ @param request_dict:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ iccid = request_dict.get('iccid', None)
|
|
|
+ if not iccid:
|
|
|
+ return response.json(444)
|
|
|
+ unicom_device_info_qs = UnicomDeviceInfo.objects.filter(iccid=iccid)
|
|
|
+ if not unicom_device_info_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ if not unicom_device_info_qs[0].user_id:
|
|
|
+ unicom_device_info_qs.update(user_id=user_id)
|
|
|
+ unicom_api = UnicomObjeect()
|
|
|
+ combo_order_qs = UnicomComboOrderInfo.objects.filter(iccid=iccid, status=1, is_del=False) \
|
|
|
+ .values('iccid', 'status', 'combo__status', 'combo__combo_name', 'combo__combo_type',
|
|
|
+ 'combo__flow_total', 'combo__remark', 'combo__expiration_days', 'combo__expiration_type',
|
|
|
+ 'year', 'month', 'flow_total_usage', 'expire_time', 'activation_time', 'combo__is_unlimited')
|
|
|
+ if combo_order_qs.exists():
|
|
|
+ combo_order = combo_order_qs.first()
|
|
|
+ flow_details = {
|
|
|
+ 'flowInvalid': 0,
|
|
|
+ 'iccid': iccid,
|
|
|
+ 'status': combo_order['status'],
|
|
|
+ 'isUnlimited': combo_order['combo__is_unlimited'],
|
|
|
+ 'comboName': combo_order['combo__combo_name'],
|
|
|
+ 'comboType': combo_order['combo__combo_type'],
|
|
|
+ 'flowTotal': combo_order['combo__flow_total'],
|
|
|
+ 'comboRemark': combo_order['combo__remark'],
|
|
|
+ 'expirationDays': combo_order['combo__expiration_days'],
|
|
|
+ 'expirationType': combo_order['combo__expiration_type'],
|
|
|
+ 'flowTotalUsage': combo_order['flow_total_usage'],
|
|
|
+ 'activationTime': combo_order['activation_time'],
|
|
|
+ 'expireTime': combo_order['expire_time'],
|
|
|
+ 'year': combo_order['year'],
|
|
|
+ 'month': combo_order['month'],
|
|
|
+ }
|
|
|
+ activate_flow = float(flow_details['flowTotalUsage'])
|
|
|
+ flow_total_usage = unicom_api.get_flow_usage_total(iccid)
|
|
|
+ flow = flow_total_usage - activate_flow
|
|
|
+ flow_details['usableFlow'] = flow_details['flowTotal'] - flow
|
|
|
+
|
|
|
+ flow_details['usableFlow'] = \
|
|
|
+ 0.00 if flow_details['usableFlow'] <= 0 else flow_details['usableFlow']
|
|
|
+ flow_details['usableFlow'] = Decimal(flow_details['usableFlow']).quantize(Decimal('0.00'))
|
|
|
+ flow_details.pop('flowTotalUsage')
|
|
|
+ cls.update_combo_order_sort(iccid)
|
|
|
+ flow_details['comboList'] = cls.get_combo_order_list(iccid)
|
|
|
+ return response.json(0, flow_details)
|
|
|
+ else:
|
|
|
+ flow_details = {'iccid': iccid, 'flowInvalid': 1, 'comboList': cls.get_combo_order_list(iccid)}
|
|
|
+ cls.update_combo_order_sort(iccid)
|
|
|
+ return response.json(0, flow_details)
|
|
|
+ except Exception as e:
|
|
|
+ print(e.args)
|
|
|
+ ex = traceback.format_exc()
|
|
|
+ print(ex)
|
|
|
+ return response.json(177, ex)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_combo_order_list(cls, iccid):
|
|
|
+ """
|
|
|
+ 查询套餐列表
|
|
|
+ @param iccid:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ combo_list = []
|
|
|
+ now_time = int(time.time())
|
|
|
+ before_days = LocalDateTimeUtil.get_before_days_timestamp(now_time, 90)
|
|
|
+ combo_order_qs = UnicomComboOrderInfo.objects.filter(iccid=iccid, is_del=False, created_time__gt=before_days) \
|
|
|
+ .values('iccid', 'status', 'combo__combo_name', 'combo__flow_total',
|
|
|
+ 'combo__remark', 'combo__expiration_days', 'combo__expiration_type', 'flow_total_usage',
|
|
|
+ 'expire_time', 'combo__is_unlimited').order_by('sort')
|
|
|
+ for item in combo_order_qs:
|
|
|
+ combo_list.append({
|
|
|
+ 'iccid': iccid,
|
|
|
+ 'status': item['status'],
|
|
|
+ 'comboName': item['combo__combo_name'],
|
|
|
+ 'isUnlimited': item['combo__is_unlimited'],
|
|
|
+ 'flowTotal': item['combo__flow_total'],
|
|
|
+ 'comboRemark': item['combo__remark'],
|
|
|
+ 'expirationDays': item['combo__expiration_days'],
|
|
|
+ 'expirationType': item['combo__expiration_type'],
|
|
|
+ 'expireTime': item['expire_time'],
|
|
|
+ })
|
|
|
+ return combo_list
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def update_combo_order_sort(cls, iccd):
|
|
|
+ """
|
|
|
+ 修改套餐排序
|
|
|
+ @param iccd: 联通20位ICCID
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ combo_order_qs = UnicomComboOrderInfo.objects.filter(iccid=iccd, is_del=False)
|
|
|
+ if combo_order_qs.exists():
|
|
|
+ unused_qs = combo_order_qs.filter(status=0)
|
|
|
+ if unused_qs.exists():
|
|
|
+ unused_qs.update(sort=50)
|
|
|
+ used_qs = combo_order_qs.filter(status=1)
|
|
|
+ if used_qs.exists():
|
|
|
+ used_qs.update(sort=1)
|
|
|
+ expire_qs = combo_order_qs.filter(status=2)
|
|
|
+ if expire_qs.exists():
|
|
|
+ expire_qs.update(sort=100)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def update_device_status(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 修改绑定设备状态,如产生免费套餐用量则删除
|
|
|
+ @param request_dict:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ logger = logging.getLogger('info')
|
|
|
+
|
|
|
+ serial_no = request_dict.get('serialNo', None)
|
|
|
+ iccid = request_dict.get('iccid', None)
|
|
|
+ time_stamp = request_dict.get('timeStamp', None)
|
|
|
+ sign = request_dict.get('sign', None)
|
|
|
+ if not all([iccid, serial_no, sign, time_stamp]):
|
|
|
+ return response.json(444)
|
|
|
+ logger.info('PC工具进入重置ICCID{}'.format(iccid))
|
|
|
+ try:
|
|
|
+ if not CommonService.check_time_stamp_token(sign, time_stamp):
|
|
|
+ return response.json(13)
|
|
|
+ now_time = int(time.time())
|
|
|
+ with transaction.atomic():
|
|
|
+ device_info_qs = UnicomDeviceInfo.objects.filter(iccid=iccid, serial_no=serial_no)
|
|
|
+ if device_info_qs.exists():
|
|
|
+ flow_push_qs = UnicomFlowPush.objects.filter(serial_no=serial_no)
|
|
|
+ if flow_push_qs.exists():
|
|
|
+ flow_push_qs.delete()
|
|
|
+ sys_msg_qs = SysMsgModel.objects.filter(uid=serial_no)
|
|
|
+ if sys_msg_qs.exists():
|
|
|
+ sys_msg_qs.delete()
|
|
|
+ device_info_qs.update(status=1, updated_time=now_time, user_id='')
|
|
|
+
|
|
|
+ combo_order_qs = UnicomComboOrderInfo.objects.filter(iccid=iccid)
|
|
|
+ if combo_order_qs.exists():
|
|
|
+ combo_order_qs.delete()
|
|
|
+ combo_experience_history_qs = UnicomComboExperienceHistory.objects.filter(iccid=iccid)
|
|
|
+ if combo_experience_history_qs.exists():
|
|
|
+ combo_experience_history_qs.delete()
|
|
|
+
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ print(e.args)
|
|
|
+ ex = traceback.format_exc()
|
|
|
+ print(ex)
|
|
|
+ logger.info('PC工具重置异常ICCID{},msg={}'.format(iccid, ex))
|
|
|
+ return response.json(177, ex)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_device_info(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 获取设备信息
|
|
|
+ @param request_dict:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ serial_no = request_dict.get('serialNumber', None)
|
|
|
+ if not serial_no:
|
|
|
+ return response.json(444)
|
|
|
+ unicom_device_info_qs = UnicomDeviceInfo.objects.filter(serial_no=serial_no).values()
|
|
|
+ if not unicom_device_info_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ return response.json(0, dict(unicom_device_info_qs.first()))
|
|
|
|
|
|
@classmethod
|
|
|
def device_add(cls, request_dict, response):
|
|
@@ -62,14 +261,24 @@ class UnicomComboView(View):
|
|
|
"""
|
|
|
iccid = request_dict.get('iccid', None)
|
|
|
serial_no = request_dict.get('serialNo', None)
|
|
|
- if not all([iccid, serial_no]):
|
|
|
+ time_stamp = request_dict.get('timeStamp', None)
|
|
|
+ sign = request_dict.get('sign', None)
|
|
|
+ logger = logging.getLogger('info')
|
|
|
+ logger.info('--->设备调用4G注册接口')
|
|
|
+ if not all([iccid, serial_no, sign, time_stamp]):
|
|
|
return response.json(444)
|
|
|
+ # 时间戳token校验
|
|
|
+ if not CommonService.check_time_stamp_token(sign, time_stamp):
|
|
|
+ return response.json(13)
|
|
|
n_time = int(time.time())
|
|
|
try:
|
|
|
+ logger.info('--->参数验证通过,sign验证通过')
|
|
|
with transaction.atomic():
|
|
|
# 待完善代码 根据uid与用户id验证系统设备
|
|
|
unicom_device_qs = UnicomDeviceInfo.objects.filter(iccid=iccid)
|
|
|
if unicom_device_qs.exists():
|
|
|
+ if unicom_device_qs.first().status == 1 and unicom_device_qs.first().serial_no == serial_no:
|
|
|
+ cls.user_activate_flow(iccid)
|
|
|
return response.json(174)
|
|
|
unicom_obj = UnicomObjeect()
|
|
|
result = unicom_obj.verify_device(iccid=iccid)
|
|
@@ -80,14 +289,57 @@ class UnicomComboView(View):
|
|
|
return response.json(173)
|
|
|
params = {'iccid': iccid, 'serial_no': serial_no, 'updated_time': n_time,
|
|
|
'created_time': n_time}
|
|
|
+ unicom_obj.change_device_to_activate(iccid)
|
|
|
UnicomDeviceInfo.objects.create(**params)
|
|
|
return response.json(0)
|
|
|
else:
|
|
|
return response.json(173)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
+ logger.info('--->设备调用4G注册接口异常')
|
|
|
return response.json(177, repr(e))
|
|
|
|
|
|
+ @classmethod
|
|
|
+ def user_activate_flow(cls, iccid):
|
|
|
+ """
|
|
|
+ 用户激活初始化流量套餐
|
|
|
+ @param iccid:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ logger = logging.getLogger('info')
|
|
|
+ try:
|
|
|
+ while transaction.atomic():
|
|
|
+ now_time = int(time.time())
|
|
|
+ unicom_device_info_qs = UnicomDeviceInfo.objects.filter(iccid=iccid)
|
|
|
+ if not unicom_device_info_qs.exists():
|
|
|
+ return False
|
|
|
+ unicom_device_info_qs = unicom_device_info_qs.first()
|
|
|
+ if unicom_device_info_qs.status != 1:
|
|
|
+ logger.info('---->用户激活iccid={},业务系统状态为{}'.format(iccid, unicom_device_info_qs.status))
|
|
|
+ return False
|
|
|
+ # 联通业务逻辑
|
|
|
+ unicom_api = UnicomObjeect()
|
|
|
+ unicom_api.change_device_to_activate(iccid)
|
|
|
+ # 查看是否体验过免费套餐
|
|
|
+ experience_history_qs = UnicomComboExperienceHistory.objects.filter(iccid=iccid)
|
|
|
+ if not experience_history_qs.exists():
|
|
|
+ logger.info('---->用户首次激活iccid={}'.format(iccid))
|
|
|
+ combo_qs = UnicomCombo.objects.filter(combo_type=1, status=0, is_del=False) \
|
|
|
+ .values('id', 'expiration_type', 'expiration_days', 'combo_type')
|
|
|
+ if combo_qs.exists():
|
|
|
+ combo_qs = combo_qs.first()
|
|
|
+ # 保存体验记录
|
|
|
+ experience_history_vo = {'iccid': iccid, 'experience_type': 0, 'do_time': now_time}
|
|
|
+ UnicomComboExperienceHistory.objects.create(**experience_history_vo)
|
|
|
+ # 保存套餐激活信息
|
|
|
+ cls.create_combo_order_info('', 0, iccid, combo_qs['id'])
|
|
|
+ # 修改业务联通卡设备激活信息
|
|
|
+ UnicomDeviceInfo.objects.filter(iccid=iccid).update(status=2, updated_time=now_time)
|
|
|
+ return True
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return False
|
|
|
+
|
|
|
@classmethod
|
|
|
def save_unicom_combo(cls, request_dict, response):
|
|
|
"""
|
|
@@ -165,19 +417,101 @@ class UnicomComboView(View):
|
|
|
return response.json(177, repr(e))
|
|
|
|
|
|
@classmethod
|
|
|
- def buy_package(cls):
|
|
|
+ def buy_unicom_combo(cls, user_id, request_dict, request, response):
|
|
|
"""
|
|
|
- 购买套餐
|
|
|
+ 购买联通套餐
|
|
|
@return:
|
|
|
"""
|
|
|
- pass
|
|
|
+ try:
|
|
|
+ with transaction.atomic():
|
|
|
+ iccid = request_dict.get('iccid', None)
|
|
|
+ combo_id = request_dict.get('id', None)
|
|
|
+ pay_type = request_dict.get('payType', None)
|
|
|
+ activate_type = request_dict.get('activateType', 0)
|
|
|
+ if not all([iccid, combo_id, pay_type]):
|
|
|
+ return response.json(444)
|
|
|
+ combo_id = int(combo_id)
|
|
|
+ pay_type = int(pay_type)
|
|
|
+ now_time = int(time.time())
|
|
|
+ unicom_combo_qs = UnicomCombo.objects.filter(id=combo_id, pay_type=pay_type, is_show=1, is_del=False,
|
|
|
+ status=0) \
|
|
|
+ .values('id', 'combo_name', 'price', 'remark')
|
|
|
+ if not unicom_combo_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ unicom_device_qs = UnicomDeviceInfo.objects.filter(iccid=iccid) \
|
|
|
+ .values('serial_no')
|
|
|
+ if not unicom_device_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ unicom_combo_qs = unicom_combo_qs.first()
|
|
|
+ price = unicom_combo_qs['price']
|
|
|
+ if not price:
|
|
|
+ return response.json(173)
|
|
|
+ unicom_device_qs = unicom_device_qs.first()
|
|
|
+ device_uid = CommonService.query_uid_with_serial(unicom_device_qs['serial_no'])
|
|
|
+ order_id = CommonService.createOrderID()
|
|
|
+ rank_id, ai_rank_id = cls.get_cloud_or_ai_combo()
|
|
|
+
|
|
|
+ order_dict = {'rank_id': rank_id, 'ai_rank_id': ai_rank_id, 'orderID': order_id, 'UID': device_uid,
|
|
|
+ 'userID_id': user_id, 'desc': unicom_combo_qs['combo_name'], 'payType': pay_type,
|
|
|
+ 'payTime': now_time, 'price': price, 'currency': 'CNY', 'addTime': now_time,
|
|
|
+ 'updTime': now_time,
|
|
|
+ 'unify_combo_id': str(unicom_combo_qs['id']), 'order_type': 2,
|
|
|
+ }
|
|
|
+
|
|
|
+ params = 'lang=cn' + '&activateType=' + activate_type
|
|
|
+ result = {'result_code': 0, 'reason': 'success', 'error_code': 0}
|
|
|
+ if pay_type == 2: # 支付宝
|
|
|
+ pay_price = PayService.get_two_float(price, 2)
|
|
|
+ notify_url = 'unicom/wap/pay/ali-notify'
|
|
|
+
|
|
|
+ order_dict['pay_url'] = PayService.create_alipay_payment(params, order_id, pay_price,
|
|
|
+ unicom_combo_qs['combo_name'],
|
|
|
+ notify_url,
|
|
|
+ unicom_combo_qs['remark'], response)
|
|
|
+ res_data = {'redirectUrl': order_dict['pay_url'], 'orderID': order_id}
|
|
|
+ elif pay_type == 3: # 微信支付
|
|
|
+ notify_url = 'unicom/wap/pay/wechat-notify'
|
|
|
+ ip = CommonService.get_ip_address(request)
|
|
|
+ params = 'activateType=' + activate_type
|
|
|
+ sign_params = PayService.create_wechat_payment(params, order_id,
|
|
|
+ price, ip,
|
|
|
+ notify_url,
|
|
|
+ unicom_combo_qs['remark'],
|
|
|
+ response)
|
|
|
+ result['result'] = sign_params
|
|
|
+ else:
|
|
|
+ return response.json(444, {'param': 'pay_type'})
|
|
|
+
|
|
|
+ Order_Model.objects.create(**order_dict)
|
|
|
+ if pay_type == 3:
|
|
|
+ return JsonResponse(status=200, data=result)
|
|
|
+ return response.json(0, res_data)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500, repr(e))
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_cloud_or_ai_combo():
|
|
|
+ """
|
|
|
+ 获取云存或者AI套餐id
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ rank_id = Store_Meal.objects.all().order_by('sort').values().first()['id']
|
|
|
+ ai_rank_id = AiStoreMeal.objects.all().values().first()['id']
|
|
|
+ return rank_id, ai_rank_id
|
|
|
|
|
|
@classmethod
|
|
|
- def query_device_usage_history(cls):
|
|
|
+ def query_device_usage_history(cls, request_dict, response):
|
|
|
"""
|
|
|
- 查询用量历史
|
|
|
+ 查询实时已用总流量API
|
|
|
@return:
|
|
|
"""
|
|
|
+ iccid = request_dict.get('iccid', None)
|
|
|
+ if not iccid:
|
|
|
+ return response.json(444)
|
|
|
+ unicom_api = UnicomObjeect()
|
|
|
+ flow = unicom_api.get_flow_usage_total(iccid)
|
|
|
+ return response.json(0, flow)
|
|
|
|
|
|
@staticmethod
|
|
|
def package_callback_notify(request_dict, request):
|
|
@@ -254,11 +588,151 @@ class UnicomComboView(View):
|
|
|
dict_data.pop('sign')
|
|
|
unicom_obj = UnicomObjeect()
|
|
|
generate_sign = unicom_obj.createSign(**dict_data)
|
|
|
- logger.info('设备状态变更推送请求签名{}'.format(sign))
|
|
|
- logger.info('设备状态变更推送生成签名{}'.format(generate_sign))
|
|
|
+ logger.info('联通设备状态变更推送请求签名{}'.format(sign))
|
|
|
+ logger.info('联通设备状态变更推送生成签名{}'.format(generate_sign))
|
|
|
+ assert generate_sign == sign
|
|
|
+ now_time = int(time.time())
|
|
|
+ re_data = {
|
|
|
+ 'iccid': dict_data['iccid'],
|
|
|
+ 'serial_no': dict_data['serialNo'],
|
|
|
+ 'sign': sign,
|
|
|
+ 'type': dict_data['type'],
|
|
|
+ 'previous_val': dict_data['previousVal'],
|
|
|
+ 'current_val': dict_data['currentVal'],
|
|
|
+ 'reason': dict_data['reason'],
|
|
|
+ 'time': dict_data['time'],
|
|
|
+ 'updated_time': now_time,
|
|
|
+ 'created_time': now_time
|
|
|
+ }
|
|
|
+ UnicomDeviceStatusChangePush.objects.create(**re_data)
|
|
|
r_data = {'success': True, 'msg': '成功'}
|
|
|
return HttpResponse(json.dumps(r_data, ensure_ascii=False), content_type="application/json,charset=utf-8")
|
|
|
except Exception as e:
|
|
|
print(repr(e))
|
|
|
r_data = {'success': False, 'msg': '失败'}
|
|
|
return HttpResponse(json.dumps(r_data, ensure_ascii=False), content_type="application/json,charset=utf-8")
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def create_combo_order_info(cls, order_id, activate_type, iccid, combo_id):
|
|
|
+ """
|
|
|
+ 创建套餐生效记录
|
|
|
+ @param order_id: 订单id
|
|
|
+ @param activate_type: 激活类型
|
|
|
+ @param iccid: 联通20位iccid
|
|
|
+ @param combo_id: 套餐id
|
|
|
+ @return: True Or False
|
|
|
+ """
|
|
|
+ logger = logging.getLogger('info')
|
|
|
+ logger.info('创建联通订单套餐信息,订单id{}'.format(order_id))
|
|
|
+ try:
|
|
|
+ today = datetime.datetime.today()
|
|
|
+ year = today.year
|
|
|
+ month = today.month
|
|
|
+ with transaction.atomic():
|
|
|
+ unicom_combo_qs = UnicomCombo.objects.filter(id=int(combo_id)).values()
|
|
|
+ if unicom_combo_qs.exists():
|
|
|
+ unicom_combo = unicom_combo_qs.first()
|
|
|
+ now_time = int(time.time())
|
|
|
+ combo_order_qs = UnicomComboOrderInfo.objects.filter(status=1, iccid=iccid)
|
|
|
+ status = 0
|
|
|
+ if not combo_order_qs.exists():
|
|
|
+ status = 1
|
|
|
+ combo_order_data = {'iccid': iccid, 'status': status, 'combo_id': int(combo_id),
|
|
|
+ 'updated_time': now_time,
|
|
|
+ 'created_time': now_time,
|
|
|
+ 'year': year, 'month': month}
|
|
|
+ if order_id:
|
|
|
+ combo_order_data['order_id'] = order_id
|
|
|
+ # 有效期类型 1 等于自然月,0天数
|
|
|
+ if unicom_combo['expiration_type'] == 1:
|
|
|
+ # 激活类型 1=下月激活 否则等于当月激活
|
|
|
+ if activate_type == 1:
|
|
|
+ combo_order_data['next_month_activate'] = True
|
|
|
+ next_start_time, end_time = cls.get_next_month_data_time()
|
|
|
+ combo_order_data['activation_time'] = next_start_time
|
|
|
+ combo_order_data['expire_time'] = end_time
|
|
|
+ combo_order_data['status'] = 0
|
|
|
+ else:
|
|
|
+ start_time, month_end_time = cls.get_month_start_and_end_time()
|
|
|
+ combo_order_data['activation_time'] = now_time
|
|
|
+ combo_order_data['expire_time'] = month_end_time
|
|
|
+
|
|
|
+ elif unicom_combo['expiration_type'] == 0:
|
|
|
+ days = unicom_combo['expiration_days']
|
|
|
+ zero_today, end_time = cls.get_data_time(days)
|
|
|
+ combo_order_data['activation_time'] = now_time
|
|
|
+ combo_order_data['expire_time'] = end_time
|
|
|
+ # 联通业务逻辑
|
|
|
+ unicom_api = UnicomObjeect()
|
|
|
+ if status == 1:
|
|
|
+ unicom_api.change_device_to_activate(iccid)
|
|
|
+ flow_total_usage = unicom_api.get_flow_usage_total(iccid)
|
|
|
+ if flow_total_usage > 0:
|
|
|
+ flow_total_usage = Decimal(flow_total_usage)
|
|
|
+ flow_total_usage = flow_total_usage.quantize(Decimal('0.00'))
|
|
|
+ else:
|
|
|
+ flow_total_usage = 0
|
|
|
+ combo_order_data['flow_total_usage'] = str(flow_total_usage)
|
|
|
+ UnicomComboOrderInfo.objects.create(**combo_order_data)
|
|
|
+ logger.info('保存套餐支付信息success')
|
|
|
+ return True
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return False
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_next_month_data_time():
|
|
|
+ """
|
|
|
+ 获取下个月开始时间与结束时间戳
|
|
|
+ @return: next_start_time,end_time
|
|
|
+ """
|
|
|
+ next_month_start = LocalDateTimeUtil.get_next_month_start()
|
|
|
+ next_start_time, next_end_time = LocalDateTimeUtil.get_start_and_end_time(next_month_start, '%Y-%m-%d')
|
|
|
+ next_month_end = LocalDateTimeUtil.get_next_month_end()
|
|
|
+ start_time, end_time = LocalDateTimeUtil.get_start_and_end_time(next_month_end, '%Y-%m-%d')
|
|
|
+ return next_start_time, end_time
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_data_time(days):
|
|
|
+ """
|
|
|
+ 获取今天开始时间以及days后日期结束时间戳
|
|
|
+ @return: next_start_time,end_time
|
|
|
+ """
|
|
|
+ zero_today, last_today = LocalDateTimeUtil.get_today_date(True)
|
|
|
+ now_time = int(time.time())
|
|
|
+ after_time = LocalDateTimeUtil.get_after_days_timestamp(now_time, days)
|
|
|
+ time_array = time.localtime(after_time)
|
|
|
+ start_time, end_time = LocalDateTimeUtil.get_start_and_end_time(time.strftime("%Y-%m-%d", time_array),
|
|
|
+ '%Y-%m-%d')
|
|
|
+ return zero_today, end_time
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_month_start_and_end_time():
|
|
|
+ """
|
|
|
+ 获取当天开始时间与当月结束时间戳
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ zero_today, last_today = LocalDateTimeUtil.get_today_date(True)
|
|
|
+ month_end = LocalDateTimeUtil.get_cur_month_end()
|
|
|
+ start_time, month_end_time = LocalDateTimeUtil.get_start_and_end_time(month_end, '%Y-%m-%d')
|
|
|
+ return zero_today, month_end_time
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_test_sign(cls, request_dict, response):
|
|
|
+ """
|
|
|
+ 测试盐加密解密
|
|
|
+ @param request_dict:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
+ verify = request_dict.get('verify', False)
|
|
|
+ if verify:
|
|
|
+ sign = request_dict.get('sign')
|
|
|
+ time_stamp = request_dict.get('timeStamp')
|
|
|
+ sign = CommonService.check_time_stamp_token(sign, time_stamp)
|
|
|
+ if not sign:
|
|
|
+ return response.json(13)
|
|
|
+ return response.json(0)
|
|
|
+ now_time = int(time.time())
|
|
|
+ sign = CommonService.encode_data(str(now_time))
|
|
|
+ return response.json(0, {'sign': sign, 'timeStamp': now_time})
|