import json
import logging
import threading
import time
from datetime import datetime

import requests
from django.db import transaction
from django.views import View

from Ansjer.config import CRCKey, CONFIG_INFO, CONFIG_US, CONFIG_EUR, \
    CONFIG_CN, USED_SERIAL_REDIS_LIST, UNUSED_SERIAL_REDIS_LIST, SERVER_DOMAIN_US, REGION_ID_LIST, SERVER_DOMAIN_TEST, \
    SERVER_DOMAIN_LIST, SERVER_DOMAIN_CN, SERVER_DOMAIN_EUR, RESET_REGION_ID_SERIAL_REDIS_LIST, LOGGER
from Controller.UnicomCombo.UnicomComboController import UnicomComboView
from Model.models import SerialNumberModel, CompanySerialModel, UIDCompanySerialModel, UIDModel, Device_Info, \
    iotdeviceInfoModel, LogModel, UidSetModel, UID_Bucket, \
    Unused_Uid_Meal, Order_Model, StsCrdModel, VodHlsModel, ExperienceContextModel, UidUserModel, ExperienceAiModel, \
    AiService, DeviceDomainRegionModel, RegionModel, UidPushModel, UnicomDeviceInfo
from Object.AWS.S3Email import S3Email
from Object.RedisObject import RedisObject
from Object.TokenObject import TokenObject
from Object.UnicomObject import UnicomObjeect
from Object.uidManageResponseObject import uidManageResponseObject
from Service.AlgorithmService import AlgorithmBaseOn35
from Service.CommonService import CommonService
from Service.EquipmentInfoService import EquipmentInfoService
from Service.VodHlsService import SplitVodHlsObject


class SerialNumberView(View):

    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation', None)
        request_dict = request.GET
        return self.validate(request_dict, operation, request)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation', None)
        request_dict = request.POST
        return self.validate(request_dict, operation, request)

    def validate(self, request_dict, operation, request):
        response = uidManageResponseObject()

        if operation == 'attachUID':  # 绑定uid
            return self.do_attach_uid(request_dict, response, request)
        elif operation == 'detachUID':  # 解绑uid
            return self.do_detach_uid(request, request_dict, response)
        elif operation == 'get-uid':  # app获取uid
            return self.get_uid(request_dict, response, request)
        elif operation == 'create':  # 创建序列号
            return self.do_create(request_dict, response)
        elif operation == 'getUID':  # 根据序列号获取uid
            return self.do_get_uid(request_dict, response)
        elif operation == 'getRegionInfo':  # 根据序列号状态确认uid地区(PC工具使用)
            return self.get_region_info(request_dict, response)
        elif operation == 'saveRegion':  # 保存设备地区信息(app使用)
            return self.save_region(request, request_dict, response)
        elif operation == 'getDomain':  # 获取域名(设备使用)
            return self.get_domain(request_dict, response)
        elif operation == 'resetRegionId':  # 重置地区id
            return self.reset_region_id(request_dict, response)
        elif operation == 'get-status':  # 获取序列号绑定信息
            return self.check_serial_status(request_dict, response)
        elif operation == 'getUidRegion':  # 根据序列号获取uid地区
            return self.get_uid_region(request_dict, response)
        elif operation == 'getGlobalUidRegion':  # 获取序列号在全球服绑定uid的地区
            return self.get_global_uid_region(request_dict, response)
        elif operation == 'getIoTCoreBySerialNumber':  # 根据序列号获取iot core
            return self.get_iot_core_by_serial_number(request_dict, response)
        else:
            return response.json(414)

    @staticmethod
    def do_create(request_dict, response):
        quantity = int(request_dict.get('quantity', 0))

        if not quantity:
            return response.json(444)

        try:
            try:
                number = SerialNumberModel.objects.last().id
            except:
                number = 0
            serial_number_bulk = []
            now_time = int(time.time())
            algorithm = AlgorithmBaseOn35()
            for i in range(quantity):
                serial_number = algorithm.getLetter(number)
                number += 1  # sum每次递增1
                # 前面补0至六位
                serial_number = (6 - len(serial_number)) * '0' + serial_number
                serial_number_bulk.append(SerialNumberModel(serial_number=serial_number, add_time=now_time))
            # 开启事务写入
            with transaction.atomic():
                SerialNumberModel.objects.bulk_create(serial_number_bulk)
            return response.json(0)
        except Exception as e:
            print(e)
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    def do_attach_uid(self, request_dict, response, request):
        serial_number = request_dict.get('serial_number', None)
        token = request_dict.get('token', None)
        time_stamp = request_dict.get('time_stamp', None)

        if not all([serial_number, token, time_stamp]):
            return response.json(444)

        # 时间戳token校验
        if not CommonService.check_time_stamp_token(token, time_stamp):
            return response.json(13)

        now_time = int(time.time())
        serial = serial_number[0:6]
        company_identity = serial_number[6:9]
        full_serial = serial_number[0:9]

        # 根据企业标识确认企业秘钥
        company_secret = ''
        if company_identity == '11A':
            company_secret = 'MTEyMTNB'
        elif company_identity == '11L':
            company_secret = 'VmXEWnBR'
        elif company_identity == '11Z':
            company_secret = 'ZsKWcxdD'

        try:
            # 判断序列号是否刚解绑，刚解绑1分钟内不能进行绑定
            redisObj = RedisObject()
            unused_serial_redis_list = redisObj.lrange(UNUSED_SERIAL_REDIS_LIST, 0, -1)
            unused_serial_redis_list = [str(i, 'utf-8') for i in unused_serial_redis_list]
            if serial in unused_serial_redis_list:
                return response.json(5)
            # 判断序列号是否已和企业关联
            company_serial_qs = CompanySerialModel.objects.filter(company__secret=company_secret, serial_number=serial)
            if not company_serial_qs.exists():
                return response.json(173)
            company_serial = company_serial_qs[0]

            if company_serial.status == 0:  # 该序列号未绑定企业
                return response.json(173)
            elif company_serial.status == 1:  # 绑定uid
                # redis加锁，防止同一个序列号重复绑定
                key = serial + 'do_attach_uid'
                isLock = redisObj.CONN.setnx(key, 1)
                redisObj.CONN.expire(key, 60)
                if not isLock:
                    return response.json(5)

                # 获取并判断region_id
                region_id = CommonService.confirm_region_id()
                if region_id not in REGION_ID_LIST:
                    return response.json(444, {'invalid region_id': region_id})

                # 获取p2p类型
                p2p_type = request_dict.get('p2ptype', None)
                if serial_number[9:10]:
                    p2p_type = serial_number[9:10]
                elif not p2p_type:
                    return response.json(444, {'param': 'p2ptype'})
                p2p_type = int(p2p_type)

                with transaction.atomic():
                    count = 0
                    while count < 3:
                        # 查询是否存在未绑定序列号的uid
                        uid_qs = UIDModel.objects.filter(vpg__company_id=company_serial.company.id,
                                                         vpg__region_id=region_id, status=0, p2p_type=p2p_type). \
                            order_by('update_time')
                        if not uid_qs.exists():
                            return response.json(375)

                        uid = uid_qs[0]
                        # 判断uid是否已绑定过序列号
                        uid_company_serial_qs = UIDCompanySerialModel.objects.filter(uid_id=uid.id)
                        if uid_company_serial_qs.exists():
                            return response.json(377)

                        result = UIDModel.objects.filter(id=uid.id, status=0).update(status=2, update_time=now_time)
                        if int(result) <= 0:  # 更新失败
                            count += 1
                            continue

                        # UID关联【企业关联序列号】表创建数据
                        UIDCompanySerialModel.objects.create(uid_id=uid.id, company_serial_id=company_serial.id,
                                                             add_time=now_time, update_time=now_time)

                        company_serial.status = 2
                        company_serial.update_time = now_time
                        company_serial.save()

                        dev = Device_Info.objects.filter(UID=uid.uid)
                        if dev.exists():
                            dev.update(serial_number=full_serial)

                        full_uid_code = uid.full_uid_code
                        if uid.platform in CRCKey.keys():
                            full_uid_code += ':' + CRCKey[uid.platform]

                        res = {
                            'full_uid_code': CommonService.encode_data(full_uid_code),
                            'uid': CommonService.encode_data(uid.uid),
                            'extra': uid.uid_extra,
                            'platform': uid.platform,
                            'initString': uid.init_string,
                            'initStringApp': uid.init_string_app,
                        }

                        # 记录操作日志
                        ip = CommonService.get_ip_address(request)
                        content = json.loads(json.dumps(request_dict))
                        log = {
                            'ip': ip,
                            'user_id': 1,
                            'status': 200,
                            'time': now_time,
                            'content': json.dumps(content),
                            'url': 'serialNumber/attachUID',
                            'operation': '序列号{}绑定uid: {}'.format(serial, uid.uid),
                        }
                        LogModel.objects.create(**log)
                        redisObj.del_data(key=key)

                        # 处理序列号状态和计算剩余uid数量线程
                        thread = threading.Thread(target=self.rpush_serial_and_count_uid, args=(serial, p2p_type,
                                                                                                redisObj))
                        thread.start()

                        return response.json(0, res)
                    return response.json(5)
            elif company_serial.status == 2:  # 返回uid
                res = self.get_uid_info_by_serial(company_serial.id)
                return response.json(0, res)
            elif company_serial.status == 3:  # 已占用
                res = self.log_and_send_email(request, company_serial.id, serial_number, now_time)
                if not res:
                    return response.json(10042)
                return response.json(0, self.get_uid_info_by_serial(company_serial.id))
        except Exception as e:
            djangoLogger = logging.getLogger('django')
            djangoLogger.exception(repr(e))
            return response.json(176, str(e))

    @classmethod
    def get_uid_info_by_serial(cls, company_serial_id):
        """
        根据企业关联序列号ID返回UID信息
        """
        uid_qs = UIDCompanySerialModel.objects.filter(company_serial_id=company_serial_id)
        if not uid_qs.exists():
            return {}
        uid = uid_qs.values('uid__uid', 'uid__mac', 'uid__uid_extra', 'uid__full_uid_code',
                            'uid__platform', 'uid__init_string', 'uid__init_string_app')[0]
        full_uid_code = uid['uid__full_uid_code']
        if uid['uid__platform'] in CRCKey.keys():
            full_uid_code += ':' + CRCKey[uid['uid__platform']]
        res = {
            'full_uid_code': CommonService.encode_data(full_uid_code),
            'uid': CommonService.encode_data(uid['uid__uid']),
            'extra': uid['uid__uid_extra'],
            'platform': uid['uid__platform'],
            'initString': uid['uid__init_string'],
            'initStringApp': uid['uid__init_string_app'],
        }
        return res

    @classmethod
    def check_serial_status(cls, request_dict, response):
        serial_number = request_dict.get('serial_number', None)
        if not serial_number:
            return response.json(444)
        serial = serial_number[0:6]
        company_identity = serial_number[6:9]
        # 根据企业标识确认企业秘钥
        company_secret = ''
        if company_identity == '11A':
            company_secret = 'MTEyMTNB'
        elif company_identity == '11L':
            company_secret = 'VmXEWnBR'
        elif company_identity == '11Z':
            company_secret = 'ZsKWcxdD'

        try:
            # 判断序列号是否已和企业关联
            company_serial_qs = CompanySerialModel.objects.filter(company__secret=company_secret, serial_number=serial)
            if not company_serial_qs.exists():
                return response.json(173)
            company_serial = company_serial_qs[0]
            # 获取企业序列号是否绑定UID
            uid_company_qs = UIDCompanySerialModel.objects.filter(company_serial_id=company_serial.id) \
                .values('uid__uid', 'uid__mac', 'uid__uid_extra', 'uid__status', 'uid__area', 'uid__vpg_id',
                        'uid__p2p_type', 'uid__full_uid_code', 'uid__platform',
                        'uid__init_string', 'uid__init_string_app')
            uid_info = {}
            if uid_company_qs.exists():
                uid_info['uid'] = uid_company_qs[0]['uid__uid']
                uid_info['mac'] = uid_company_qs[0]['uid__mac']
                uid_info['uid_extra'] = uid_company_qs[0]['uid__uid_extra']
                uid_info['status'] = uid_company_qs[0]['uid__status']
                uid_info['area'] = uid_company_qs[0]['uid__area']
                uid_info['vpg_id'] = uid_company_qs[0]['uid__vpg_id']
                uid_info['p2p_type'] = uid_company_qs[0]['uid__p2p_type']
                uid_info['full_uid_code'] = uid_company_qs[0]['uid__full_uid_code']
                uid_info['platform'] = uid_company_qs[0]['uid__platform']
                uid_info['init_string'] = uid_company_qs[0]['uid__init_string']
                uid_info['init_string_app'] = uid_company_qs[0]['uid__init_string_app']
            results = {'serial': company_serial.serial_number, 'status': company_serial.status,
                       'uidInfo': uid_info}
            return response.json(0, results)
        except Exception as e:
            LOGGER.info('查询序列号异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
            return response.json(176, str(e))

    @staticmethod
    def rpush_serial_and_count_uid(serial, p2p_type, redis_obj):
        """
        处理序列号状态和计算剩余uid数量线程
        @param serial: 序列号
        @param p2p_type: p2p类型
        @param redis_obj: redis对象
        @return:
        """
        # 写入已使用序列号redis列表
        redis_obj.rpush(USED_SERIAL_REDIS_LIST, serial)

        redis_obj = RedisObject()
        time_limit = redis_obj.get_data('uid_count_warning_time_limit')
        if not time_limit:
            vpg_id = 1
            if CONFIG_INFO == CONFIG_US:
                vpg_id = 3
            elif CONFIG_INFO == CONFIG_EUR:
                vpg_id = 4
            p2p_type = int(p2p_type)

            try:
                # 剩余uid数量少于2000邮件提醒
                unused_uid_count = UIDModel.objects.filter(vpg_id=vpg_id, p2p_type=p2p_type, status=0).count()
                LOGGER.info('uid剩余数量:{}'.format(unused_uid_count))
                warning_count = 2000
                if unused_uid_count < warning_count:
                    # 限制一天提醒一次
                    redis_obj.set_data('uid_count_warning_time_limit', 1, 60 * 60 * 24)
                    platform = '尚云' if p2p_type == 1 else 'tutk'
                    email_content = '{}服{}的uid数量少于{}个,请及时处理'.format(CONFIG_INFO, platform, warning_count)
                    S3Email().faEmail(email_content, 'servers@ansjer.com')
            except Exception as e:
                LOGGER.info('发送提醒邮件异常: error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def log_and_send_email(request, company_serial_id, serial, now_time):
        """
        记录序列号占用操作日志及发送邮件通知,（美服、欧服已占用序列号将执行数据同步）
        @param company_serial_id: 企业关联序列号 ID
        @param request: 请求
        @param serial: 序列号
        @param now_time: 当前时间
        """
        send_email = S3Email()
        try:
            if CONFIG_INFO == 'cn' or CONFIG_INFO == 'test':
                return {}
            # 判断当前序列号是否绑定UID
            uid_serial_qs = UIDCompanySerialModel.objects.filter(company_serial_id=company_serial_id)
            if uid_serial_qs.exists():
                email_content = '{}序列号已占用当前数据库已绑定UID: {}'.format(CONFIG_INFO, serial)
                send_email.faEmail(email_content, 'servers@ansjer.com')
                return {}
            if CONFIG_INFO == 'eur':  # 查美服
                response = requests.get("https://www.dvema.com/serialNumber/get-status",
                                        params={'serial_number': serial}, timeout=3)
            else:  # 查国服
                response = requests.get("https://www.zositechc.cn/serialNumber/get-status",
                                        params={'serial_number': serial}, timeout=3)
            ip = CommonService.get_ip_address(request)
            operation = '{}序列号占用:{}'.format(CONFIG_INFO, serial)
            log = {
                'ip': ip,
                'user_id': 1,
                'status': 200,
                'time': now_time,
                'operation': operation,
                'url': 'serialNumber/attachUID',
            }
            if not response.status_code == 200:
                operation += '查询其它服UID数据异常响应状态：{}'.format(response.status_code)
                log['operation'] = operation
                LogModel.objects.create(**log)
                return {}
            results = json.loads(response.text)
            if not results['result_code'] == 0:
                operation += '其它服绑定UID数据result_code: {}'.format(results['result_code'])
                log['operation'] = operation
                LogModel.objects.create(**log)
                return {}
            # 解析从其它服获取的UID数据
            uid_info = results['result']['uidInfo']
            if uid_info:
                if uid_info['p2p_type'] == 1:  # 尚云UID不进行同步只进行 邮寄警报
                    # 邮件通知
                    email_content = '国内uid同步国外服类型为尚云不操作: {}'.format(serial)
                    send_email.faEmail(email_content, 'servers@ansjer.com')
                else:
                    # 同步UID数据
                    uid_id = ''
                    uid_qs = UIDModel.objects.filter(uid=uid_info['uid'])
                    if uid_qs.exists():
                        if uid_qs.first().status == 2:  # 判断uid是否被使用
                            email_content = '{}序列号已占用且{}已被使用:{}'.format(CONFIG_INFO, uid_info['uid'], serial)
                            send_email.faEmail(email_content, 'servers@ansjer.com')
                            return {}
                        else:
                            uid_qs.update(status=2, update_time=now_time)
                            uid_id = uid_qs.first().id
                    if not uid_id:
                        uid_id = UIDModel.objects.create(**uid_info).id
                    # 企业序列号关联 uid
                    UIDCompanySerialModel.objects.create(uid_id=uid_id, company_serial_id=company_serial_id,
                                                         add_time=now_time, update_time=now_time)
                    # 成功后 修改企业序列号状态为2（已分配）
                    CompanySerialModel.objects.filter(id=company_serial_id) \
                        .update(status=2, update_time=now_time)
                    SerialNumberView.sync_iot_core_data(serial)
                    email_content = '{}序列号已占用已将其它服UID数据同步完成: {}'.format(CONFIG_INFO, serial)
                    send_email.faEmail(email_content, 'servers@ansjer.com')
                    operation += '同步成功'
                    log['operation'] = operation
                    LogModel.objects.create(**log)
                    return 'success'
            email_content = '{}序列号已占用其它服没有绑定UID: {}'.format(CONFIG_INFO, serial)
            send_email.faEmail(email_content, 'servers@ansjer.com')
            return {}
        except Exception as e:
            email_content = '序列号占用同步数据异常: {},errLine:{}, errMsg:{}' \
                .format(serial, e.__traceback__.tb_lineno, repr(e))
            send_email.faEmail(email_content, 'servers@ansjer.com')
            send_email.faEmail(email_content, 'antony@ansjer.com')
            return {}

    def do_get_uid(self, request_dict, response):
        serial_number = request_dict.get('serial_number', None)
        token = request_dict.get('token', None)
        time_stamp = request_dict.get('time_stamp', None)

        if token and time_stamp and serial_number:
            # 时间戳token校验
            if not CommonService.check_time_stamp_token(token, time_stamp):
                return response.json(13)

            mark = serial_number[6:9]
            serial = serial_number[0:6]
            uid_company_serial_qs = UIDCompanySerialModel.objects.filter(company_serial__company__mark=mark,
                                                                         company_serial__serial_number__serial_number=serial)

            if uid_company_serial_qs.exists():
                uid = uid_company_serial_qs.values('uid__uid', 'uid__mac', 'uid__uid_extra')[0]

                res = {
                    'uid': CommonService.encode_data(uid['uid__uid']),
                    'mac': CommonService.encode_data(uid['uid__mac']),
                    'extra': uid['uid__uid_extra']
                }
                return response.json(0, res)
            else:
                return response.json(173)
        else:
            return response.json(444)

    @staticmethod
    def do_detach_uid(request, request_dict, response):
        token = request_dict.get('token', None)
        time_stamp = request_dict.get('time_stamp', None)
        serial_number = request_dict.get('serial_number', None)

        if not all([token, time_stamp, serial_number]):
            return response.json(444)

        # 时间戳token校验
        if not CommonService.check_time_stamp_token(token, time_stamp):
            return response.json(13)

        now_time = int(time.time())
        serial = serial_number[0:6]
        company_serial_qs = CompanySerialModel.objects.filter(serial_number=serial).values('status')
        if not company_serial_qs.exists():
            return response.json(379)
        status = company_serial_qs[0]['status']
        if status == 1:
            return response.json(0, {'success': '序列号未绑定uid'})
        elif status == 3:
            return response.json(10043)
        uid_serial_qs = UIDCompanySerialModel.objects.filter(company_serial__serial_number=serial)
        if not uid_serial_qs.exists():
            return response.json(173)
        uid_serial = uid_serial_qs[0]
        # 判断序列号是否刚绑定uid，刚绑定需要过1分钟才能解绑
        redisObj = RedisObject()
        used_serial_redis_list = redisObj.lrange(USED_SERIAL_REDIS_LIST, 0, -1)
        used_serial_redis_list = [str(i, 'utf-8') for i in used_serial_redis_list]
        if serial in used_serial_redis_list:
            return response.json(5)
        try:
            with transaction.atomic():
                uid = uid_serial.uid.uid
                # 更新序列号状态
                company_serial_qs.update(status=1, update_time=now_time)
                # 删除设备相关数据,参考后台的设备重置删除的数据
                Device_Info.objects.filter(UID=uid).delete()
                UidSetModel.objects.filter(uid=uid).delete()
                UidUserModel.objects.filter(UID=uid).delete()
                UidPushModel.objects.filter(uid_set__uid=uid).delete()
                iotdeviceInfoModel.objects.filter(serial_number=serial).delete()
                # 删除推送消息
                for val in range(1, 8):
                    EquipmentInfoService.get_equipment_info_model('', val).filter(device_uid=uid).delete()

                # 重置设备云存
                UID_Bucket.objects.filter(uid=uid).delete()
                Unused_Uid_Meal.objects.filter(uid=uid).delete()
                Order_Model.objects.filter(UID=uid).delete()
                StsCrdModel.objects.filter(uid=uid).delete()
                VodHlsModel.objects.filter(uid=uid).delete()
                # 删除vod_hls分表数据
                split_vod_hls_obj = SplitVodHlsObject()
                split_vod_hls_obj.del_vod_hls_data(uid=uid)
                ExperienceContextModel.objects.filter(uid=uid).delete()

                # 重置AI
                ExperienceAiModel.objects.filter(uid=uid).delete()
                AiService.objects.filter(uid=uid).delete()

                # 写入未使用序列号redis列表
                redisObj.rpush(UNUSED_SERIAL_REDIS_LIST, serial)

                # 重置region_id,不为美洲服,则写入redis列表
                if CONFIG_INFO == CONFIG_US:
                    DeviceDomainRegionModel.objects.filter(serial_number=serial).update(region_id=0)
                else:
                    redisObj.rpush(RESET_REGION_ID_SERIAL_REDIS_LIST, serial)

                # 重置uid的使用状态为未使用,更新时间
                UIDModel.objects.filter(uid=uid).update(status=0, mac='', update_time=now_time)
                uid_serial.delete()

                # 记录操作日志
                ip = CommonService.get_ip_address(request)
                content = json.loads(json.dumps(request_dict))
                log = {
                    'ip': ip,
                    'user_id': 1,
                    'status': 200,
                    'time': now_time,
                    'content': json.dumps(content),
                    'url': 'serialNumber/detachUID',
                    'operation': '序列号{}解绑uid: {}'.format(serial, uid),
                }
                LogModel.objects.create(**log)
            return response.json(0)
        except Exception as e:
            # 记录操作日志
            ip = CommonService.get_ip_address(request)
            content = json.loads(json.dumps(request_dict))
            log = {
                'ip': ip,
                'user_id': 1,
                'status': 200,
                'time': now_time,
                'content': json.dumps(content) + '异常:{}'.format(repr(e)),
                'url': 'serialNumber/detachUID',
                'operation': '序列号{}解绑uid{}异常'.format(serial, uid),
            }
            LogModel.objects.create(**log)
            return response.json(176, str(e))

    def get_uid(self, request_dict, response, request):
        """
        app获取序列号
        @param request_dict:
        @param response:
        @param request:
        @return:
        """
        token = request_dict.get('token', None)
        time_stamp = request_dict.get('time_stamp', None)
        company_secret = request_dict.get('company_id', None)
        serial_number = request_dict.get('serial_number', None)

        if not all([token, time_stamp, company_secret, serial_number]):
            return response.json(444)

        # 时间戳token校验
        if not CommonService.check_time_stamp_token(token, time_stamp):
            return response.json(13)

        now_time = int(time.time())
        serial = serial_number[0:6]

        # 判断序列号是否已和企业关联
        company_serial_qs = CompanySerialModel.objects.filter(company__secret=company_secret, serial_number=serial)
        if not company_serial_qs.exists():
            return response.json(173)
        company_serial = company_serial_qs[0]

        try:
            if company_serial.status == 0 or company_serial.status == 1:  # 未使用
                self.is_4g_device(serial_number, request_dict, request)
                return response.json(173)
            elif company_serial.status == 2:  # 返回uid
                res = self.get_uid_info_by_serial(company_serial.id)
                return response.json(0, res)
            elif company_serial.status == 3:  # 已占用
                res = self.log_and_send_email(request, company_serial.id, serial_number, now_time)
                if not res:
                    return response.json(10042)
                return response.json(0, self.get_uid_info_by_serial(company_serial.id))
        except Exception as e:
            error_logger = logging.getLogger('django')
            error_logger.exception(repr(e))
            return response.json(176, str(e))

    @staticmethod
    def get_region_info(request_dict, response):
        """
        根据序列号状态确认uid地区
        @param request_dict: 请求参数
        @param response: 响应对象
        @request_dict serial_number: 序列号
        @return: response
        """
        serial_number = request_dict.get('serial_number', None)
        if not serial_number:
            return response(444)
        company_serial_qs = CompanySerialModel.objects.filter(serial_number=serial_number[:6]).values('status')
        if not company_serial_qs.exists():
            return response.json(173)
        status = company_serial_qs[0]['status']
        if status == 2:
            if CONFIG_INFO == CONFIG_CN:
                return response.json(0, {'region': 1})
            elif CONFIG_INFO == CONFIG_US:
                return response.json(0, {'region': 3})
        elif status == 3:
            if CONFIG_INFO == CONFIG_CN:
                return response.json(0, {'region': 3})
            elif CONFIG_INFO == CONFIG_US:
                return response.json(0, {'region': 1})
        return response.json(0)

    @staticmethod
    def save_region(request, request_dict, response):
        """
        保存设备地区信息
        @param request: 请求体
        @param request_dict: 请求参数
        @param response: 响应对象
        @request_dict token: 令牌
        @request_dict time_stamp: 时间戳
        @return: response
        """
        token = request_dict.get('token', None)
        serial_number = request_dict.get('serial_number', None)
        if not all([serial_number, token]):
            return response(444)

        try:
            serial_number = serial_number[:6]

            # 不是美洲服,请求美洲域名保存数据
            if CONFIG_INFO != CONFIG_US:
                # token认证
                token_obj = TokenObject(token)
                if token_obj.code != 0:
                    return response.json(token_obj.code)
                response.lang = token_obj.lang

                url = SERVER_DOMAIN_US + 'serialNumber/saveRegion'
                data = request_dict.dict()
                data['ip'] = CommonService.get_ip_address(request)
                data['region_id'] = CommonService.confirm_region_id()
                try:
                    r = requests.post(url=url, data=data, timeout=3)
                    assert r.status_code == 200
                    res = r.json()
                    assert res['result_code'] == 0
                except (TimeoutError, AssertionError):
                    return response.json(5)
            # 美洲服,处理请求和保存数据
            else:
                data = {}

                # 处理其他服务器发起请求的情况
                region_id = request_dict.get('region_id', None)
                if region_id is not None:
                    region_id = int(region_id)
                    data['region_id'] = region_id
                    data['ip'] = request_dict.get('ip', '')
                else:
                    # token认证
                    token_obj = TokenObject(token)
                    if token_obj.code != 0:
                        return response.json(token_obj.code)
                    response.lang = token_obj.lang

                    region_id = CommonService.confirm_region_id()
                    data['region_id'] = region_id
                    data['ip'] = CommonService.get_ip_address(request)

                # 写入数据
                device_domain_region_qs = DeviceDomainRegionModel.objects.filter(serial_number=serial_number). \
                    values('region_id')
                if not device_domain_region_qs.exists():
                    data['serial_number'] = serial_number
                    DeviceDomainRegionModel.objects.create(**data)
                else:
                    # 设备解绑过uid,更新region_id
                    if device_domain_region_qs[0]['region_id'] == 0:
                        device_domain_region_qs.update(**data)
            return response.json(0)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def get_domain(request_dict, response):
        """
        设备获取域名
        @param request_dict: 请求参数
        @param response: 响应对象
        @request_dict serial_number: 序列号
        @request_dict time_stamp_token: 时间戳token
        @request_dict time_stamp: 时间戳
        @return: response
        """
        serial_number = request_dict.get('serial_number', None)
        time_stamp_token = request_dict.get('time_stamp_token', None)
        time_stamp = request_dict.get('time_stamp', None)
        if not all([serial_number, time_stamp_token, time_stamp]):
            return response.json(444)
        # 时间戳token校验
        if not CommonService.check_time_stamp_token(time_stamp_token, time_stamp):
            return response.json(13)

        try:
            serial_number = serial_number[:6]

            device_domain_region_qs = DeviceDomainRegionModel.objects.filter(serial_number=serial_number).values(
                'region_id')
            if not device_domain_region_qs.exists():
                return response.json(173)
            region_id = device_domain_region_qs[0]['region_id']
            region_qs = RegionModel.objects.filter(id=region_id).values('api', 'push_api')
            if not region_qs.exists():
                return response.json(173)
            res = {
                'api': region_qs[0]['api'],
                'push_api': region_qs[0]['push_api']
            }
            return response.json(0, res)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def reset_region_id(request_dict, response):
        """
        重置地区id
        @param request_dict: 请求参数
        @param response: 响应对象
        @request_dict serial_number: 序列号
        @return: response
        """
        serial_number = request_dict.get('serial_number', None)
        if not serial_number:
            return response(444)
        try:
            serial_number = serial_number[:6]
            DeviceDomainRegionModel.objects.filter(serial_number=serial_number).update(region_id=0)
            return response.json(0)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def get_uid_region(request_dict, response):
        """
        根据序列号获取uid地区
        @param request_dict: 请求参数
        @param response: 响应对象
        @request_dict serial_number: 序列号
        @return: response
        """
        serial_number = request_dict.get('serial_number', None)
        if not serial_number:
            return response(444)
        try:
            serial_number = serial_number[:6]
            uid_company_serial_qs = UIDCompanySerialModel.objects.filter(
                company_serial__serial_number=serial_number).values('uid__uid')
            res = []
            for item in uid_company_serial_qs:
                res.append({'uid': item['uid__uid']})
            return response.json(0, res)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def get_global_uid_region(request_dict, response):
        """
        获取序列号在全球服绑定uid的地区
        @param request_dict: 请求参数
        @param response: 响应对象
        @request_dict serial_number: 序列号
        @return: response
        """
        orders_domain_name_list = SERVER_DOMAIN_LIST
        if SERVER_DOMAIN_TEST in orders_domain_name_list:
            orders_domain_name_list.remove(SERVER_DOMAIN_TEST)
        uid_list = []
        try:
            for orders_domain_name in orders_domain_name_list:
                url = orders_domain_name + 'serialNumber/getUidRegion'
                res = requests.post(url=url, data=request_dict)
                result = res.json()
                if result['result_code'] != 0:
                    return response.json(result['result_code'])
                if orders_domain_name == SERVER_DOMAIN_CN:
                    for item in result['result']:
                        item['region'] = 1
                        uid_list.append(item)
                elif orders_domain_name == SERVER_DOMAIN_US:
                    for item in result['result']:
                        item['region'] = 3
                        uid_list.append(item)
                elif orders_domain_name == SERVER_DOMAIN_EUR:
                    for item in result['result']:
                        item['region'] = 4
                        uid_list.append(item)
            return response.json(0, uid_list)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @classmethod
    def get_iot_core_by_serial_number(cls, request_dict, response):
        """
        根据序列号查询iot core
        """
        try:
            serial_number = request_dict.get('serialNumber', None)
            if not serial_number:
                return response.json(444)
            serial = serial_number[0:6]
            LOGGER.info('根据序列号查询IoTC:{}'.format(serial_number))
            device_iot_qs = iotdeviceInfoModel.objects.filter(serial_number=serial)
            if not device_iot_qs.exists():
                return response.json(173)
            iot_vo = device_iot_qs.first()
            iot_dto = {
                'serial_number': iot_vo.serial_number,
                'uid': iot_vo.uid,
                'certificate_id': iot_vo.certificate_id,
                'certificate_pem': iot_vo.certificate_pem,
                'public_key': iot_vo.public_key,
                'private_key': iot_vo.private_key,
                'thing_name': iot_vo.thing_name,
                'thing_groups': iot_vo.thing_groups,
                'endpoint': iot_vo.endpoint,
                'token_iot_number': iot_vo.token_iot_number,
                'add_time': iot_vo.add_time.strftime("%Y-%m-%d %H:%M:%S"),
                'update_time': iot_vo.update_time.strftime("%Y-%m-%d %H:%M:%S")
            }
            return response.json(0, {'iotInfo': iot_dto})
        except Exception as e:
            LOGGER.info('查询序列号异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
            return response.json(176, str(e))

    @staticmethod
    def sync_iot_core_data(serial_number):
        """
        同步iot core 数据，目前只同步美服到欧服
        @param serial_number: 序列号
        """
        try:
            if not CONFIG_INFO == 'eur':
                return False
            serial = serial_number[0:6]
            device_iot_qs = iotdeviceInfoModel.objects.filter(serial_number=serial)
            if device_iot_qs.exists():
                return False
            response = requests.get("https://www.dvema.com/serialNumber/getIoTCoreBySerialNumber",
                                    params={'serialNumber': serial_number}, timeout=3)
            if response.status_code != 200:
                return False
            results = json.loads(response.text)
            if results['result_code'] != 0:
                return False
            iot_vo = results['result']['iotInfo']
            iot_vo['add_time'] = datetime.strptime(iot_vo['add_time'], "%Y-%m-%d %H:%M:%S")
            iot_vo['update_time'] = datetime.strptime(iot_vo['update_time'], "%Y-%m-%d %H:%M:%S")
            iotdeviceInfoModel.objects.create(**iot_vo)
            return True
        except Exception as e:
            LOGGER.info('{}同步iot异常,errLine:{}, errMsg:{}'.format(serial_number, e.__traceback__.tb_lineno, repr(e)))
            return False

    @staticmethod
    def is_4g_device(serial_number, request_dict, request):
        """
        判断是否4G设备
        """
        try:
            serial_no = serial_number[0:9]
            key = f'ASJ:UNICOM:CARD:ACTIVATE:{serial_no}'
            redis = RedisObject()
            if redis.get_data(key):
                return True
            # 根据序列号查询联通iccid
            unicom_qs = UnicomDeviceInfo.objects.filter(serial_no=serial_no, status=1, card_type=0) \
                .values('iccid', 'status')
            if not unicom_qs.exists():
                return False
            unicom_qs = unicom_qs.first()
            unicom_api = UnicomObjeect()
            unicom_api.change_device_to_activate(unicom_qs['iccid'])
            redis.CONN.setnx(key, str(unicom_qs['iccid']))
            redis.CONN.expire(key, 7200)  # 已调用过激活两个小时内不可调用
            ip = CommonService.get_ip_address(request)
            describe = '{}获取uid请求激活4G卡,{}'.format(serial_no, unicom_qs['iccid'])
            UnicomComboView().create_operation_log('serialNumber/get-uid', ip, request_dict, describe)
            return True
        except Exception as e:
            LOGGER.info('{}判断是否4G设备异常,errLine:{}, errMsg:{}'.format(serial_number, e.__traceback__.tb_lineno, repr(e)))
            return False
