import datetime
import json
import time

import boto3
import botocore
import oss2
import redis
from botocore import client
from django.http import JsonResponse
from django.views.generic.base import View
from obs import ObsClient

from Ansjer.config import DETECT_PUSH_DOMAIN, DETECT_PUSH_DOMAINS, DETECT_PUSH_DOMAIN_JIUAN, DETECT_PUSH_DOMAINS_JIUAN, \
    OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, LOGGER, ALGORITHM_COMBO_TYPES, \
    HUAWEICLOUD_AK, HUAWEICLOUD_SK, HUAWEICLOUD_OBS_SERVER, HUAWEICLOUD_PUSH_BUKET
from Ansjer.config import PUSH_BUCKET, CONFIG_INFO, CONFIG_CN, CONFIG_EUR, CONFIG_US
from Ansjer.config import PUSH_REDIS_ADDRESS
from Model.models import Device_Info, Equipment_Info, UidSetModel, UidPushModel, CompanyModel, SysMsgModel, \
    AiService, VodBucketModel
from Object.ETkObject import ETkObject
from Object.OCIObjectStorage import OCIObjectStorage
from Object.RedisObject import RedisObject
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject
from Object.utils import LocalDateTimeUtil
from Service.CommonService import CommonService
from Service.EquipmentInfoService import EquipmentInfoService
from Service.VodHlsService import SplitVodHlsObject


class DetectControllerViewV2(View):

    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation')
        api_version = kwargs.get('apiVersion')
        # self.ip = CommonService.get_ip_address(request)
        return self.validation(request, request.GET, operation, api_version)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation')
        api_version = kwargs.get('apiVersion')
        # self.ip = CommonService.get_ip_address(request)
        return self.validation(request, request.POST, operation, api_version)

    def validation(self, request, request_dict, operation, api_version):
        response = ResponseObject()
        if operation is None:
            return response.json(444, 'error path')
        token = request_dict.get('token', None)
        lang = request_dict.get('lang', None)
        if lang:
            response = ResponseObject(lang)
        tko = TokenObject(token)
        if tko.code == 0:
            userID = tko.userID
            # 修改推送设置
            if operation == 'changeStatus':
                return self.do_change_status(userID, request_dict, response)
            # 查询推送信息
            elif operation == 'queryInfo':
                return self.do_query(request_dict, response, userID)
            # 点击推送信息跳转到列表信息
            elif operation == 'transferInfo':
                return self.do_transfer(request_dict, response, userID)
            # 更新推送延迟
            elif operation == 'updateInterval':
                return self.do_update_interval(userID, request_dict, response)
            # 消息提醒配置
            elif operation == 'messageNotificationSet':
                return self.message_notification_set(api_version, request_dict, response)
            else:
                return response.json(414)
        else:
            return response.json(tko.code)

    @classmethod
    def message_notification_set(cls, api_version, request_dict, response):
        """
        消息提醒设置
        @param api_version: 版本号
        @param request_dict: 参数json格式
        @param response: 响应数据
        """
        try:
            msg_data = request_dict.get('msgData', None)
            uid = request_dict.get('uid', None)
            channel = int(request_dict.get('channel', 0))
            is_nvr = int(request_dict.get('isNVR', 0))
            event_types = request_dict.get('eventTypes', None)
            LOGGER.info('*****DetectControllerViewV2.message_notification_set*****api_version:{},uid:{}'
                        .format(api_version, uid))
            if not all([msg_data, uid]):
                return response.json(444)
            data = json.loads(msg_data)
            uid_set_qs = UidSetModel.objects.filter(uid=uid)
            if not uid_set_qs.exists():
                return response.json(173)
            data = cls.update_nvr_app_msg(channel, is_nvr, uid_set_qs[0].msg_notify, data, event_types)

            uid_set_qs.update(msg_notify=data, updTime=int(time.time()))
            return response.json(0)
        except Exception as e:
            LOGGER.error('*****DetectControllerViewV2.message_notification_set:errLine:{}, errMsg:{}'
                         .format(e.__traceback__.tb_lineno, repr(e)))
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @classmethod
    def update_nvr_app_msg(cls, channel, is_nvr, msg_notify, data, event_types):
        try:
            # 检查是否是 NVR 且通道大于 0
            if is_nvr == 1 and channel > 0:
                # 构建目标键，例如 'channel1' 或 'channel2'
                target_key = f'channel{channel}'

                # 使用条件表达式构建 new_data
                new_data = {target_key: list(map(int, event_types.split(','))) if event_types else []}

                # 获取 nvr 列表并准备标志
                nvr_list = msg_notify.get('nvr', [])

                # 尝试在 nvr 列表中找到匹配的通道
                for item in nvr_list:
                    if target_key in item:
                        item[target_key] = new_data[target_key]
                        return msg_notify  # 找到匹配后直接返回

                # 如果没有找到匹配，添加 new_data
                nvr_list.append(new_data)
                msg_notify['nvr'] = nvr_list
                return msg_notify
            elif is_nvr == 0:
                # 检查 data 中是否存在 nvr 键，如果存在则删除
                if 'nvr' in data:
                    del data['nvr']  # 删除 nvr 键
            return data  # 返回更新后的 data
        except Exception as e:
            # 记录异常信息
            LOGGER.error('修改消息提醒params: {}: errLine: {}, errMsg: {}'.format(
                data, e.__traceback__.tb_lineno, repr(e)))
            return data

    def do_change_status(self, userID, request_dict, response):
        token_val = request_dict.get('token_val', None)
        jg_token_val = request_dict.get('jg_token_val', '')
        appBundleId = request_dict.get('appBundleId', None)
        app_type = request_dict.get('app_type', None)
        push_type = request_dict.get('push_type', None)
        status = request_dict.get('status', None)
        m_code = request_dict.get('m_code', None)
        uid = request_dict.get('uid', None)
        lang = request_dict.get('lang', 'en')
        tz = request_dict.get('tz', '0')
        company_secrete = request_dict.get('company_secrete', None)
        region = request_dict.get('region', None)  # app必须传：1：国外,2：国内
        electricity_status = request_dict.get('electricity_status', None)
        domain_name = request_dict.get('domain_name', None)
        is_nvr = int(request_dict.get('isNVR', 0))
        if not region:
            return response.json(444, 'region')
        region = int(region)
        # 消息提醒功能新增

        # 如果传空上来，就默认为0
        if tz == '':
            tz = 0
        else:
            tz = tz.replace("GMT", "")
        detect_group = request_dict.get('detect_group', None)
        interval = request_dict.get('interval', None)
        if not status and not electricity_status:
            return response.json(444, 'status and electricity_status')
        if not company_secrete:
            return response.json(444, 'company_secrete')
        company = CompanyModel.objects.filter(secret=company_secrete)
        if not company.exists():
            return response.json(444, 'company_secrete')
        # 关闭推送
        if not all([appBundleId, app_type, token_val, uid, m_code]):
            return response.json(444, 'appBundleId,app_type,token_val,uid,m_code')

        try:
            # 判断用户是否拥有设备
            device_info_qs = Device_Info.objects.filter(userID_id=userID, UID=uid)
            if not device_info_qs.exists():
                device_info_qs = Device_Info.objects.filter(userID_id=userID, serial_number=uid)
                if not device_info_qs.exists():
                    return response.json(14)

            # 更新或创建uid_set数据
            nowTime = int(time.time())
            uid_set_data = {
                'device_type': device_info_qs[0].Type
            }
            # 设置开关状态,0:关闭,1:开启
            if status:
                status = int(status)
                uid_set_data['detect_status'] = status
                device_info_qs.update(NotificationMode=status)

            # 检测类型
            if detect_group:
                uid_set_data['detect_group'] = detect_group
            uid_set_qs = UidSetModel.objects.filter(uid=uid)
            # 设置消息推送间隔
            if interval:
                interval = int(interval)
                if uid_set_qs.exists() and status == 1 and uid_set_qs.first().detect_status == 0:
                    interval = 60
                uid_set_data['detect_interval'] = interval
                # 开通了ai服务的设备,通过mqtt通知设备修改消息推送间隔
                ai_service_qs = AiService.objects.filter(uid=uid, use_status=1, endTime__gte=nowTime)
                if ai_service_qs.exists():
                    topic_name = 'ansjer/generic/{}'.format(uid)
                    msg = {
                        'commandType': 'AIState',
                        'payload': {
                            'IntervalTime': interval
                        }
                    }
                    CommonService.req_publish_mqtt_msg(uid, topic_name, msg)

            if uid_set_qs.exists():
                msg_data = uid_set_qs.first().msg_notify
                if status == 0 and msg_data:
                    msg_data['appPush'] = -1
                    uid_set_data['msg_notify'] = msg_data
                elif status == 1 and uid_set_qs.first().detect_status == 0:
                    uid_set_data['detect_interval'] = 60

                    types = ALGORITHM_COMBO_TYPES
                    device_type = device_info_qs[0].Type
                    ai_type = uid_set_qs.first().ai_type
                    # IPC默认关闭移动侦测APP通知
                    if ai_type > 0 and device_type and device_type not in [1, 2, 3, 4, 10001]:
                        types = [x for x in types if x != 51]
                    msg_data = {'appPush': 1,
                                'pushTime': {'allDay': 1, 'repeat': 127, 'endTime': 0, 'timeZone': '+08.00',
                                             'startTime': 0},
                                'eventTypes': {'device': types, 'aiCloud': 1}
                                }

                    if is_nvr == 1:  # 检查NVR版本是否存在

                        # 确定通道数量，如果channel大于0则使用其值，否则默认使用4
                        channel_count = uid_set_qs[0].channel if uid_set_qs[0].channel > 0 else 4
                        # 生成通道的列表，每个频道对应一个字典，格式为 "channelX": [types]
                        channel_list = [{f'channel{index + 1}': ALGORITHM_COMBO_TYPES} for index in range(channel_count)]
                        # 将NVR字典赋值给msg_data中的'nvr'键
                        msg_data['nvr'] = channel_list
                        msg_data['eventTypes']['device'] = []  # 将IPC允许推送类型设为空

                    uid_set_data['msg_notify'] = msg_data

                uid_set_id = uid_set_qs[0].id
                uid_set_data['updTime'] = nowTime
                uid_set_qs.update(**uid_set_data)
            else:
                uid_set_data['uid'] = uid
                uid_set_data['addTime'] = nowTime
                uid_set_data['updTime'] = nowTime
                uid_set_qs = UidSetModel.objects.create(**uid_set_data)
                uid_set_id = uid_set_qs.id

            # 初始化UidPushModel推送表
            if electricity_status:
                if m_code != 0 and m_code != '0':
                    uid_push_qs = UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid)
                    if uid_push_qs.exists():
                        uid_push_update_dict = {
                            'appBundleId': appBundleId,
                            'app_type': app_type,
                            'push_type': push_type,
                            'token_val': token_val,
                            'jg_token_val': jg_token_val,
                            'updTime': nowTime,
                            'lang': lang,
                            'tz': tz
                        }
                        uid_push_qs.update(**uid_push_update_dict)
                    else:
                        uid_push_create_dict = {
                            'uid_set_id': uid_set_id,
                            'userID_id': userID,
                            'appBundleId': appBundleId,
                            'app_type': app_type,
                            'push_type': push_type,
                            'token_val': token_val,
                            'jg_token_val': jg_token_val,
                            'm_code': m_code,
                            'addTime': nowTime,
                            'updTime': nowTime,
                            'lang': lang,
                            'tz': tz
                        }
                        # 绑定设备推送
                        UidPushModel.objects.create(**uid_push_create_dict)
                return response.json(0)

            if status == 0:
                # 状态为0的时候删除redis缓存数据
                self.do_delete_redis(uid)
                return response.json(0)
            elif status == 1:
                if m_code != 0 and m_code != '0':
                    uid_push_qs = UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid)
                    if uid_push_qs.exists():
                        uid_push_update_dict = {
                            'appBundleId': appBundleId,
                            'app_type': app_type,
                            'push_type': push_type,
                            'token_val': token_val,
                            'jg_token_val': jg_token_val,
                            'updTime': nowTime,
                            'lang': lang,
                            'tz': tz
                        }
                        uid_push_qs.update(**uid_push_update_dict)
                    else:
                        uid_push_create_dict = {
                            'uid_set_id': uid_set_id,
                            'userID_id': userID,
                            'appBundleId': appBundleId,
                            'app_type': app_type,
                            'push_type': push_type,
                            'token_val': token_val,
                            'jg_token_val': jg_token_val,
                            'm_code': m_code,
                            'addTime': nowTime,
                            'updTime': nowTime,
                            'lang': lang,
                            'tz': tz
                        }
                        # 绑定设备推送
                        UidPushModel.objects.create(**uid_push_create_dict)
                if interval:
                    self.do_delete_redis(uid, interval)
                else:
                    self.do_delete_redis(uid)
                etkObj = ETkObject(etk='')
                etk = etkObj.encrypt(uid)

                if company_secrete == 'MTEyMTNB':
                    d_type = device_info_qs[0].Type
                    url = DETECT_PUSH_DOMAIN
                    if d_type in [103, 26]:
                        url = DETECT_PUSH_DOMAINS
                    urls = DETECT_PUSH_DOMAINS
                else:
                    url = DETECT_PUSH_DOMAIN_JIUAN
                    urls = DETECT_PUSH_DOMAINS_JIUAN
                if domain_name in ['api.zositeche.com', 'api.loocam3.com', 'common.neutral3.com']:
                    url = 'http://push.zositeche.com/'
                    urls = 'https://push.zositeche.com/'
                detectUrl = "{DETECT_PUSH_DOMAIN}notifyV2/push?etk={etk}&company_secrete={company_secrete}&region={region}". \
                    format(etk=etk, company_secrete=company_secrete, DETECT_PUSH_DOMAIN=url, region=region)
                detectUrls = "{DETECT_PUSH_DOMAIN_V2}notifyV2/push?etk={etk}&company_secrete={company_secrete}&region={region}". \
                    format(etk=etk, company_secrete=company_secrete, DETECT_PUSH_DOMAIN_V2=urls, region=region)
                return response.json(0, {'detectUrl': detectUrl, 'detectUrls': detectUrls})
            else:
                return response.json(173)
        except Exception as e:
            LOGGER.info('消息推送设置error,uid:{},line:{},msg:{}'.format(uid, e.__traceback__.tb_lineno, repr(e)))
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    def do_delete_redis(self, uid, detect_interval=0):
        keyPattern = '{uid}*'.format(uid=uid)
        redisObj = RedisObject(host=PUSH_REDIS_ADDRESS)
        keys = redisObj.get_keys(keyPattern)
        if keys:
            for key in keys:
                key = key.decode()
                if detect_interval == 0:
                    redisObj.del_data(key=key)
                elif key.find('plt') != -1:
                    continue
                elif key.find('flag') != -1:
                    redisObj.set_data(key=key, val=1, expire=detect_interval)
                else:
                    redisObj.del_data(key=key)

    def do_query(self, request_dict, response, userID):
        page = int(request_dict.get('page', None))
        line = int(request_dict.get('line', None))
        start_time = request_dict.get('startTime', None)
        end_time = request_dict.get('endTime', None)
        event_type = request_dict.get('eventType', None)
        region = int(request_dict.get('region', None))
        uids = request_dict.get('uids', None)
        channel = request_dict.get('channel', None)

        try:
            uid_list = []
            if uids:
                uid_list = uids.split(',')

            if not start_time and not end_time:
                # 默认查询近七天内数据
                end_time = int(time.time())
                start_time = LocalDateTimeUtil.get_before_days_timestamp(end_time, 7)
            equipment_info_qs, count = EquipmentInfoService. \
                union_equipment_info(userID, uid_list, event_type, start_time, end_time, page, line, channel=channel)

            # 查询设备类型,昵称
            if uid_list:
                dvqs = Device_Info.objects.filter(UID__in=uid_list, userID_id=userID).values('UID', 'Type', 'NickName')
                uid_type_dict = {}
                for dv in dvqs:
                    uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
            else:
                dvqs = Device_Info.objects.filter(userID_id=userID).values('UID', 'Type', 'NickName')
                uid_type_dict = {}
                for dv in dvqs:
                    uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}

            # 没有推送数据返回空列表
            if count == 0:
                return response.json(0, {'datas': [], 'count': 0})

            res = []
            # 华为云
            obs_client = ObsClient(
                access_key_id=HUAWEICLOUD_AK, secret_access_key=HUAWEICLOUD_SK, server=HUAWEICLOUD_OBS_SERVER)

            # 阿里云
            auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
            oss_img_bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')

            # aws
            aws_s3 = boto3.client(
                's3',
                aws_access_key_id=AWS_ACCESS_KEY_ID[1],
                aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
                config=botocore.client.Config(signature_version='s3v4'),
                region_name='us-east-1'
            )
            aws_s3_cn = boto3.client(
                's3',
                aws_access_key_id=AWS_ACCESS_KEY_ID[0],
                aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
                config=botocore.client.Config(signature_version='s3v4'),
                region_name='cn-northwest-1'
            )

            # oci
            # 国内生产环境默认不实例OCI对象
            oci = self.get_oci_client()
            redis_obj = RedisObject(3)

            # ai消息标识所有组合标签
            ai_all_event_type = EquipmentInfoService.get_all_comb_event_type()
            for equipment_info in equipment_info_qs:
                uid = equipment_info['devUid']
                event_time = equipment_info['eventTime']
                channel = equipment_info['Channel']
                storage_location = equipment_info['storage_location']
                border_coords = equipment_info['borderCoords']
                event_type = equipment_info['eventType']
                event_tag = equipment_info['eventTag']

                if equipment_info['is_st'] == 1:
                    thumbspng = '{}/{}/{}.jpeg'.format(uid, channel, event_time)
                    if storage_location == 1:  # 阿里云oss
                        img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
                    elif storage_location == 5:  # 华为云
                        create_res = obs_client.createSignedUrl(
                            method='GET', bucketName=HUAWEICLOUD_PUSH_BUKET, objectKey=thumbspng, expires=300)
                        img_url = create_res.signedUrl
                    elif storage_location in [3, 4]:
                        prefix_name = f'{uid}/'
                        img_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
                        if img_url:
                            img_url = img_url + thumbspng
                    else:
                        params = {'Key': thumbspng}
                        if region == 1:  # AWS国外
                            params['Bucket'] = 'foreignpush'
                            img_url = aws_s3.generate_presigned_url(
                                'get_object', Params=params, ExpiresIn=300)
                        else:  # AWS国内
                            params['Bucket'] = 'push'
                            img_url = aws_s3_cn.generate_presigned_url(
                                'get_object', Params=params, ExpiresIn=300)

                    equipment_info['img'] = img_url
                    equipment_info['img_list'] = [img_url]

                elif equipment_info['is_st'] == 2:
                    # 列表装载回放时间戳标记
                    split_vod_hls_obj = SplitVodHlsObject()
                    vodqs = split_vod_hls_obj.get_vod_hls_data(
                        uid=uid, channel=channel, start_time=int(event_time)).values('bucket_id')
                    if not vodqs.exists():
                        return response.json(173)
                    vod_bucket_qs = VodBucketModel.objects.filter(id=vodqs[0]['bucket_id']).values('bucket', 'endpoint')
                    if not vod_bucket_qs.exists():
                        return response.json(173)
                    bucket_name = vod_bucket_qs[0]['bucket']
                    endpoint = vod_bucket_qs[0]['endpoint']
                    bucket = oss2.Bucket(auth, endpoint, bucket_name)
                    ts = '{}/vod{}/{}/ts0.ts'.format(uid, channel, event_time)
                    if storage_location == 1:  # 阿里云oss
                        thumb0 = bucket.sign_url('GET', ts, 3600,
                                                 params={'x-oss-process': 'video/snapshot,t_0000,w_700'})
                        thumb1 = bucket.sign_url('GET', ts, 3600,
                                                 params={'x-oss-process': 'video/snapshot,t_1000,w_700'})
                        thumb2 = bucket.sign_url('GET', ts, 3600,
                                                 params={'x-oss-process': 'video/snapshot,t_2000,w_700'})
                        equipment_info['img_list'] = [thumb0, thumb1, thumb2]
                    else:
                        params = {'Key': ts}
                        if region == 1:  # AWS国外
                            params['Bucket'] = 'foreignpush'
                            img_url = aws_s3.generate_presigned_url(
                                'get_object', Params=params, ExpiresIn=300)
                        else:  # AWS国内
                            params['Bucket'] = 'push'
                            img_url = aws_s3_cn.generate_presigned_url(
                                'get_object', Params=params, ExpiresIn=300)
                        equipment_info['img_list'] = [img_url]

                elif equipment_info['is_st'] == 3 or equipment_info['is_st'] == 4:
                    # 列表装载回放时间戳标记
                    equipment_info['img_list'] = []

                    for i in range(equipment_info['is_st']):
                        thumbspng = '{}/{}/{}_{}.jpeg'.format(uid, channel, event_time, i)
                        if storage_location == 1:  # 阿里云oss
                            img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
                        elif storage_location == 5:  # 华为云
                            create_res = obs_client.createSignedUrl(
                                method='GET', bucketName=HUAWEICLOUD_PUSH_BUKET, objectKey=thumbspng, expires=300)
                            img_url = create_res.signedUrl
                        elif storage_location in [3, 4]:  # 国外OCI云
                            prefix_name = f'{uid}/'
                            img_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
                            if img_url:
                                img_url = img_url + thumbspng
                        else:
                            params = {'Key': thumbspng}
                            if region == 1:  # 国外AWS
                                params['Bucket'] = 'foreignpush'
                                img_url = aws_s3.generate_presigned_url(
                                    'get_object', Params=params, ExpiresIn=300)
                            else:  # 国内AWS
                                params['Bucket'] = 'push'
                                img_url = aws_s3_cn.generate_presigned_url(
                                    'get_object', Params=params, ExpiresIn=300)
                        equipment_info['img_list'].append(img_url)

                if uid in uid_type_dict.keys():
                    equipment_info['uid_type'] = uid_type_dict[uid]['type']
                    equipment_info['devNickName'] = uid_type_dict[uid]['NickName']
                else:
                    equipment_info['uid_type'] = ''

                equipment_info['borderCoords'] = '' if border_coords == '' else eval(border_coords)  # ai消息坐标信息
                equipment_info['ai_event_type_list'] = []
                # 如果是ai消息类型，则分解eventType, 如：123 -> [1,2,3]
                if border_coords and event_type in ai_all_event_type:
                    equipment_info['ai_event_type_list'] = list(map(int, str(event_type)))
                if EquipmentInfoService.is_combo_tag(event_type, event_tag):
                    equipment_info['ai_event_type_list'] += EquipmentInfoService.get_combo_types(event_type, event_tag)
                res.append(equipment_info)
            return response.json(0, {'datas': res, 'count': count})
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @staticmethod
    def get_oci_client():
        """
        获取所在区域客户端
        @return:
        """
        if CONFIG_INFO == CONFIG_CN:
            return None
        oci = OCIObjectStorage(CONFIG_EUR) if CONFIG_INFO == CONFIG_EUR else OCIObjectStorage(CONFIG_US)
        return oci

    @staticmethod
    def oci_object_url(oci, redis_obj, uid, obj_name):
        """
        获取OCI对象存储URL 有效期5分钟
        @param uid: 设备UID
        @param redis_obj: 缓存客户端
        @param oci: oci客户端
        @param obj_name: 对象名称或前缀
        @return: url
        """
        try:
            if not oci:
                return ''
            uid_key = f'PUSH:MSG:OCI:URL:{uid}'
            oci_url = redis_obj.get_data(uid_key)
            if oci_url:
                return oci_url
            time_expires = datetime.datetime.utcnow() + datetime.timedelta(minutes=60)
            result = oci.get_preauthenticated_request_url(PUSH_BUCKET, 'ociPush', obj_name, time_expires,
                                                          'AnyObjectRead')  # 授权到指定uid文件夹
            full_url = result.full_path if result else ''
            redis_obj.set_data(uid_key, full_url, 3580)
            return full_url
        except Exception as e:
            LOGGER.error('oci查询消息列表异常error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
            return

    # redis_obj,is_st,storage_location,uid,channel,event_type,event_time,event_tag)
    def get_redis_url(self, **params):
        try:
            oss_img_bucket = params['oss_img_bucket']

            # 国内生产环境默认不实例OCI对象
            oci = self.get_oci_client()
            # 华为云
            obs_client = ObsClient(
                access_key_id=HUAWEICLOUD_AK, secret_access_key=HUAWEICLOUD_SK, server=HUAWEICLOUD_OBS_SERVER)

            uid = params['uid']
            is_st = params['is_st']
            storage_location = params['storage_location']
            region = params['region']
            aws_s3 = params['aws_s3']
            aws_s3_cn = params['aws_s3_cn']
            redis_obj = params['redis_obj']
            channel = params['channel']
            event_time = params['event_time']
            event_type = int(params['event_type'])
            event_tag = params['event_tag']
            img_list = []
            img_url = ''
            if is_st == 1:
                thumbspng = '{}/{}/{}.jpeg'.format(uid, channel, event_time)
                if storage_location == 1:  # 阿里云oss
                    img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
                elif storage_location == 5:  # 华为云
                    create_res = obs_client.createSignedUrl(
                        method='GET', bucketName=HUAWEICLOUD_PUSH_BUKET, objectKey=thumbspng, expires=300)
                    img_url = create_res.signedUrl
                elif storage_location in [3, 4]:
                    prefix_name = f'{uid}/'
                    img_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
                    if img_url:
                        img_url = img_url + thumbspng
                else:
                    params = {'Key': thumbspng}
                    if region == 1:  # AWS国外
                        params['Bucket'] = 'foreignpush'
                        img_url = aws_s3.generate_presigned_url(
                            'get_object', Params=params, ExpiresIn=300)
                    else:  # AWS国内
                        params['Bucket'] = 'push'
                        img_url = aws_s3_cn.generate_presigned_url(
                            'get_object', Params=params, ExpiresIn=300)
                img_list = [img_url]
            elif is_st == 3 or is_st == 4:
                # 列表装载回放时间戳标记
                img_list = []
                for i in range(is_st):
                    thumbspng = '{}/{}/{}_{}.jpeg'.format(uid, channel, event_time, i)
                    if storage_location == 1:  # 阿里云oss
                        img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
                    elif storage_location == 5:  # 华为云
                        create_res = obs_client.createSignedUrl(
                            method='GET', bucketName=HUAWEICLOUD_PUSH_BUKET, objectKey=thumbspng, expires=300)
                        img_url = create_res.signedUrl
                    elif storage_location in [3, 4]:
                        prefix_name = f'{uid}/'
                        img_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
                        if img_url:
                            img_url = img_url + thumbspng
                    else:
                        params = {'Key': thumbspng}
                        if region == 1:  # 国外AWS
                            params['Bucket'] = 'foreignpush'
                            img_url = aws_s3.generate_presigned_url(
                                'get_object', Params=params, ExpiresIn=300)
                        else:  # 国内AWS
                            params['Bucket'] = 'push'
                            img_url = aws_s3_cn.generate_presigned_url(
                                'get_object', Params=params, ExpiresIn=300)
                    img_list.append(img_url)
            ai_event_type_list = EquipmentInfoService.get_combo_types(event_type, event_tag)
            msg_data = {
                "id": "",
                "status": False,
                "answer_status": False,
                "alarm": "",
                "is_st": is_st,
                "storage_location": storage_location,
                "devUid": uid,
                "devNickName": "",
                "Channel": channel,
                "eventType": event_type,
                "eventTime": event_time,
                "receiveTime": 0,
                "addTime": 0,
                "borderCoords": "",
                "eventTag": event_tag,
                "img": img_url,
                "img_list": img_list,
                "uid_type": 0,
                "ai_event_type_list": ai_event_type_list
            }
            datas = [msg_data]
            return datas
        except Exception as e:
            LOGGER.error('消息跳转异常:, errLine:{}, errMsg:{}'
                         .format(e.__traceback__.tb_lineno, repr(e)))
            return []

    def do_transfer(self, request_dict, response, userID):
        event_time = request_dict.get('eventTime', None)
        event_type = request_dict.get('eventType', None)
        region = int(request_dict.get('region', None))
        channel = int(request_dict.get('channel', 1))
        uid = request_dict.get('uid', None)

        if not all([event_time, region, uid]):
            return response.json(444)

        try:
            msg_key = 'PUSH:MSG:IMAGE:{}:{}:{}'.format(uid, channel, event_time)
            redis_client = redis.Redis(connection_pool=redis.ConnectionPool(host=PUSH_REDIS_ADDRESS, port=6379, db=3))
            msg_data = redis_client.get(msg_key)

            auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
            oss_img_bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
            aws_s3 = boto3.client(
                's3',
                aws_access_key_id=AWS_ACCESS_KEY_ID[1],
                aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
                config=botocore.client.Config(signature_version='s3v4'),
                region_name='us-east-1'
            )
            aws_s3_cn = boto3.client(
                's3',
                aws_access_key_id=AWS_ACCESS_KEY_ID[0],
                aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
                config=botocore.client.Config(signature_version='s3v4'),
                region_name='cn-northwest-1'
            )

            # 国内生产环境默认不实例OCI对象
            oci = self.get_oci_client()
            # 华为云
            obs_client = ObsClient(
                access_key_id=HUAWEICLOUD_AK, secret_access_key=HUAWEICLOUD_SK, server=HUAWEICLOUD_OBS_SERVER)
            redis_obj = RedisObject(3)

            if msg_data:
                msg_dict = json.loads(msg_data)
                LOGGER.info(f'缓存数据:{msg_dict}')
                params = {'redis_obj': redis_obj, 'is_st': msg_dict['is_st'], 'region': region, 'aws_s3': aws_s3,
                          'storage_location': msg_dict['storage_location'], 'aws_s3_cn': aws_s3_cn,
                          'uid': uid, 'channel': channel, 'event_type': event_type, 'oss_img_bucket': oss_img_bucket,
                          'event_time': event_time, 'event_tag': msg_dict['event_tag']}
                res = self.get_redis_url(**params)
                return response.json(0, {'datas': res, 'count': 1})
            kwargs = {'device_user_id': userID, 'device_uid': uid, 'event_time': event_time, 'event_type': event_type}
            equipment_info_qs, count = EquipmentInfoService.get_equipment_info(**kwargs)

            # 查询设备类型,昵称
            dvqs = Device_Info.objects.filter(UID=uid, userID_id=userID).values('UID', 'Type', 'NickName')
            uid_type_dict = {}
            for dv in dvqs:
                uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}

            # 没有推送数据返回空列表
            if count == 0:
                return response.json(0, {'datas': [], 'count': 0})

            res = []

            # ai消息标识所有组合标签
            ai_all_event_type = EquipmentInfoService.get_all_comb_event_type()
            for equipment_info in equipment_info_qs:
                uid = equipment_info['devUid']
                event_time = equipment_info['eventTime']
                channel = equipment_info['Channel']
                storage_location = equipment_info['storage_location']
                border_coords = equipment_info['borderCoords']
                event_type = equipment_info['eventType']
                event_tag = equipment_info['eventTag']

                if equipment_info['is_st'] == 1:
                    thumbspng = '{}/{}/{}.jpeg'.format(uid, channel, event_time)
                    if storage_location == 1:  # 阿里云oss
                        img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
                    elif storage_location == 5:  # 华为云
                        create_res = obs_client.createSignedUrl(
                            method='GET', bucketName=HUAWEICLOUD_PUSH_BUKET, objectKey=thumbspng, expires=300)
                        img_url = create_res.signedUrl
                    elif storage_location in [3, 4]:
                        prefix_name = f'{uid}/'
                        img_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
                        if img_url:
                            img_url = img_url + thumbspng
                    else:
                        params = {'Key': thumbspng}
                        if region == 1:  # AWS国外
                            params['Bucket'] = 'foreignpush'
                            img_url = aws_s3.generate_presigned_url(
                                'get_object', Params=params, ExpiresIn=300)
                        else:  # AWS国内
                            params['Bucket'] = 'push'
                            img_url = aws_s3_cn.generate_presigned_url(
                                'get_object', Params=params, ExpiresIn=300)

                    equipment_info['img'] = img_url
                    equipment_info['img_list'] = [img_url]

                elif equipment_info['is_st'] == 2:
                    # 列表装载回放时间戳标记
                    split_vod_hls_obj = SplitVodHlsObject()
                    vodqs = split_vod_hls_obj.get_vod_hls_data(
                        uid=uid, channel=channel, start_time=int(event_time)).values('bucket_id')
                    if not vodqs.exists():
                        return response.json(173)
                    vod_bucket_qs = VodBucketModel.objects.filter(id=vodqs[0]['bucket_id']).values('bucket', 'endpoint')
                    if not vod_bucket_qs.exists():
                        return response.json(173)
                    bucket_name = vod_bucket_qs[0]['bucket']
                    endpoint = vod_bucket_qs[0]['endpoint']
                    bucket = oss2.Bucket(auth, endpoint, bucket_name)
                    ts = '{}/vod{}/{}/ts0.ts'.format(uid, channel, event_time)
                    if storage_location == 1:  # 阿里云oss
                        thumb0 = bucket.sign_url('GET', ts, 3600,
                                                 params={'x-oss-process': 'video/snapshot,t_0000,w_700'})
                        thumb1 = bucket.sign_url('GET', ts, 3600,
                                                 params={'x-oss-process': 'video/snapshot,t_1000,w_700'})
                        thumb2 = bucket.sign_url('GET', ts, 3600,
                                                 params={'x-oss-process': 'video/snapshot,t_2000,w_700'})
                        equipment_info['img_list'] = [thumb0, thumb1, thumb2]
                    else:
                        params = {'Key': ts}
                        if region == 1:  # AWS国外
                            params['Bucket'] = 'foreignpush'
                            img_url = aws_s3.generate_presigned_url(
                                'get_object', Params=params, ExpiresIn=300)
                        else:  # AWS国内
                            params['Bucket'] = 'push'
                            img_url = aws_s3_cn.generate_presigned_url(
                                'get_object', Params=params, ExpiresIn=300)
                        equipment_info['img_list'] = [img_url]

                elif equipment_info['is_st'] == 3 or equipment_info['is_st'] == 4:
                    # 列表装载回放时间戳标记
                    equipment_info['img_list'] = []
                    for i in range(equipment_info['is_st']):
                        thumbspng = '{}/{}/{}_{}.jpeg'.format(uid, channel, event_time, i)
                        if storage_location == 1:  # 阿里云oss
                            img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
                        elif storage_location == 5:  # 华为云
                            create_res = obs_client.createSignedUrl(
                                method='GET', bucketName=HUAWEICLOUD_PUSH_BUKET, objectKey=thumbspng, expires=300)
                            img_url = create_res.signedUrl
                        elif storage_location in [3, 4]:
                            prefix_name = f'{uid}/'
                            img_url = DetectControllerViewV2.oci_object_url(oci, redis_obj, uid, prefix_name)
                            if img_url:
                                img_url = img_url + thumbspng
                        else:
                            params = {'Key': thumbspng}
                            if region == 1:  # 国外AWS
                                params['Bucket'] = 'foreignpush'
                                img_url = aws_s3.generate_presigned_url(
                                    'get_object', Params=params, ExpiresIn=300)
                            else:  # 国内AWS
                                params['Bucket'] = 'push'
                                img_url = aws_s3_cn.generate_presigned_url(
                                    'get_object', Params=params, ExpiresIn=300)
                        equipment_info['img_list'].append(img_url)

                if uid in uid_type_dict.keys():
                    equipment_info['uid_type'] = uid_type_dict[uid]['type']
                    equipment_info['devNickName'] = uid_type_dict[uid]['NickName']
                else:
                    equipment_info['uid_type'] = ''

                equipment_info['borderCoords'] = '' if border_coords == '' else eval(border_coords)  # ai消息坐标信息
                equipment_info['ai_event_type_list'] = []
                # 如果是ai消息类型，则分解eventType, 如：123 -> [1,2,3]
                if border_coords and event_type in ai_all_event_type:
                    equipment_info['ai_event_type_list'] = list(map(int, str(event_type)))
                if EquipmentInfoService.is_combo_tag(event_type, event_tag):
                    equipment_info['ai_event_type_list'] += EquipmentInfoService.get_combo_types(event_type, event_tag)

                res.append(equipment_info)
            return response.json(0, {'datas': res, 'count': count})
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    def do_update_interval(self, userID, request_dict, response):
        uid = request_dict.get('uid', None)
        interval = request_dict.get('interval', None)
        dvqs = Device_Info.objects.filter(userID_id=userID, UID=uid)
        if dvqs.exists():
            uid_set_qs = UidSetModel.objects. \
                filter(uid=uid, uidpushmodel__userID_id=userID)
            if uid_set_qs.exists():
                uid_set_qs.update(detect_interval=int(interval))
            else:
                return response.json(173)
        else:
            return response.json(0)


# 这个接口没有调用过，不敢动
# http://test.dvema.com/detect/add?uidToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJQMldOR0pSRDJFSEE1RVU5MTExQSJ9.xOCI5lerk8JOs5OcAzunrKCfCrtuPIZ3AnkMmnd-bPY&n_time=1526845794&channel=1&event_type=51&is_st=0
# 移动侦测接口
class PushNotificationView(View):

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

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

    def validation(self, request_dict):
        etk = request_dict.get('etk', None)
        channel = request_dict.get('channel', '1')
        n_time = request_dict.get('n_time', None)
        event_type = request_dict.get('event_type', None)
        is_st = request_dict.get('is_st', None)
        region = request_dict.get('region', '2')
        region = int(region)
        eto = ETkObject(etk)
        uid = eto.uid
        if len(uid) == 20:
            redisObj = RedisObject()
            # pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel)
            pkey = '{uid}_ptl'.format(uid=uid)
            ykey = '{uid}_redis_qs'.format(uid=uid)
            if redisObj.get_data(key=pkey):
                res_data = {'code': 0, 'msg': 'success,!33333333333'}
                return JsonResponse(status=200, data=res_data)
            else:
                redisObj.set_data(key=pkey, val=1, expire=60)
            ##############
            redis_data = redisObj.get_data(key=ykey)
            if redis_data:
                redis_list = eval(redis_data)
            else:
                # 设置推送时间为60秒一次
                redisObj.set_data(key=pkey, val=1, expire=60)
                print("从数据库查到数据")
                # 从数据库查询出来
                uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
                    values('token_val', 'app_type', 'appBundleId',
                           'push_type', 'userID_id', 'lang', 'm_code',
                           'tz', 'uid_set__nickname')
                # 新建一个list接收数据
                redis_list = []
                # 把数据库数据追加进redis_list
                for qs in uid_push_qs:
                    redis_list.append(qs)
                # 修改redis数据，并设置过期时间为10分钟
            if redis_list:
                redisObj.set_data(key=ykey, val=str(redis_list), expire=600)
                # auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
                # bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
                aws_s3_guonei = boto3.client(
                    's3',
                    aws_access_key_id=AWS_ACCESS_KEY_ID[0],
                    aws_secret_access_key=AWS_SECRET_ACCESS_KEY[0],
                    config=botocore.client.Config(signature_version='s3v4'),
                    region_name='cn-northwest-1'
                )
                aws_s3_guowai = boto3.client(
                    's3',
                    aws_access_key_id=AWS_ACCESS_KEY_ID[1],
                    aws_secret_access_key=AWS_SECRET_ACCESS_KEY[1],
                    config=botocore.client.Config(signature_version='s3v4'),
                    region_name='us-east-1'
                )
                self.do_bulk_create_info(redis_list, n_time, channel, event_type, is_st, uid)
                if is_st == '0' or is_st == '2':
                    return JsonResponse(status=200, data={'code': 0, 'msg': 'success44444444444444444'})
                elif is_st == '1':
                    # Endpoint以杭州为例，其它Region请按实际情况填写。
                    # obj = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
                    # 设置此签名URL在60秒内有效。
                    # url = bucket.sign_url('PUT', obj, 7200)
                    thumbspng = '{uid}/{channel}/{filename}.jpeg'.format(uid=uid, channel=channel, filename=n_time)
                    if region == 2:  # 2:国内
                        response_url = aws_s3_guonei.generate_presigned_url(
                            ClientMethod='put_object',
                            Params={
                                'Bucket': 'push',
                                'Key': thumbspng
                            },
                            ExpiresIn=3600
                        )
                    else:  # 1：国外
                        response_url = aws_s3_guowai.generate_presigned_url(
                            ClientMethod='put_object',
                            Params={
                                'Bucket': 'foreignpush',
                                'Key': thumbspng
                            },
                            ExpiresIn=3600
                        )
                    # res_data = {'code': 0, 'img_push': url, 'msg': 'success'}
                    # response_url = response_url[:4] + response_url[5:]
                    res_data = {'code': 0, 'img_push': response_url, 'msg': 'success'}
                    return JsonResponse(status=200, data=res_data)
                elif is_st == '3':
                    # 人形检测带动图
                    img_url_list = []
                    for i in range(int(is_st)):
                        # obj = '{uid}/{channel}/{filename}_{st}.jpeg'. \
                        #     format(uid=uid, channel=channel, filename=n_time, st=i)
                        # 设置此签名URL在60秒内有效。
                        # url = bucket.sign_url('PUT', obj, 7200)

                        thumbspng = '{uid}/{channel}/{filename}_{st}.jpeg'. \
                            format(uid=uid, channel=channel, filename=n_time, st=i)
                        if region == 2:  # 2:国内
                            response_url = aws_s3_guonei.generate_presigned_url(
                                ClientMethod='put_object',
                                Params={
                                    'Bucket': 'push',
                                    'Key': thumbspng
                                },
                                ExpiresIn=3600
                            )
                        else:  # 1：国外
                            response_url = aws_s3_guowai.generate_presigned_url(
                                ClientMethod='put_object',
                                Params={
                                    'Bucket': 'foreignpush',
                                    'Key': thumbspng
                                },
                                ExpiresIn=3600
                            )
                        # response_url = response_url[:4] + response_url[5:]
                        img_url_list.append(response_url)

                        # img_url_list.append(url)
                    res_data = {'code': 0, 'img_url_list': img_url_list, 'msg': 'success'}
                    return JsonResponse(status=200, data=res_data)
            else:
                return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'})
        else:
            return JsonResponse(status=200, data={'code': 404, 'msg': 'wrong etk'})

    def do_bulk_create_info(self, uaqs, n_time, channel, event_type, is_st, uid):
        now_time = int(time.time())
        # 设备昵称
        userID_ids = []
        sys_msg_list = []
        is_sys_msg = self.is_sys_msg(int(event_type))
        is_st = int(is_st)
        eq_list = []
        nickname = uaqs[0]['uid_set__nickname']
        if not nickname:
            nickname = uid
        for ua in uaqs:
            lang = ua['lang']
            tz = ua['tz']
            userID_id = ua["userID_id"]
            if userID_id not in userID_ids:
                eq_list.append(Equipment_Info(
                    userID_id=userID_id,
                    eventTime=n_time,
                    eventType=event_type,
                    devUid=uid,
                    devNickName=nickname,
                    Channel=channel,
                    alarm='Motion \tChannel:{channel}'.format(channel=channel),
                    is_st=is_st,
                    receiveTime=n_time,
                    addTime=now_time,
                    storage_location=2
                ))
                if is_sys_msg:
                    sys_msg_text = self.get_msg_text(channel=channel, n_time=n_time, lang=lang, tz=tz,
                                                     event_type=event_type, is_sys=1)
                    sys_msg_list.append(SysMsgModel(
                        userID_id=userID_id,
                        msg=sys_msg_text,
                        addTime=now_time,
                        updTime=now_time,
                        uid=uid,
                        eventType=event_type))
        if eq_list:
            print('eq_list')
            Equipment_Info.objects.bulk_create(eq_list)
        if is_sys_msg:
            print('sys_msg')
            SysMsgModel.objects.bulk_create(sys_msg_list)
        return True

    def is_sys_msg(self, event_type):
        event_type_list = [702, 703, 704]
        if event_type in event_type_list:
            return True
        return False

    def get_msg_text(self, channel, n_time, lang, tz, event_type, is_sys=0):
        n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz)
        etype = int(event_type)
        if lang == 'cn':
            if etype == 704:
                msg_type = '电量过低'
            elif etype == 702:
                msg_type = '摄像头休眠'
            elif etype == 703:
                msg_type = '摄像头唤醒'
            else:
                msg_type = ''
            if is_sys:
                send_text = '{msg_type} 通道:{channel}'.format(msg_type=msg_type, channel=channel)
            else:
                send_text = '{msg_type} 通道:{channel} 日期:{date}'.format(msg_type=msg_type, channel=channel, date=n_date)
        else:
            if etype == 704:
                msg_type = 'Low battery'
            elif etype == 702:
                msg_type = 'Camera sleep'
            elif etype == 703:
                msg_type = 'Camera wake'
            else:
                msg_type = ''
            if is_sys:
                send_text = '{msg_type} channel:{channel}'. \
                    format(msg_type=msg_type, channel=channel)
            else:
                send_text = '{msg_type} channel:{channel} date:{date}'. \
                    format(msg_type=msg_type, channel=channel, date=n_date)
        return send_text
