package com.sooka.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.business.domain.ImputationData;
import com.business.mapper.ImputationDataMapper;
import com.sooka.apply.domain.IntDetailed;
import com.sooka.apply.mapper.IntRecordMapper;
import com.sooka.common.core.text.Convert;
import com.sooka.common.utils.DateUtils;
import com.sooka.common.utils.uuid.UUID;
import com.sooka.framework.util.ShiroUtils;
import com.util.NumberUtils;
import com.util.SecretKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sooka.system.mapper.TUInterfaceinfoMapper;
import com.sooka.system.domain.TUInterfaceinfo;
import com.sooka.system.service.ITUInterfaceinfoService;

import javax.annotation.Resource;


/**
 * 【接口应用审批管理】Service业务层处理
 *
 * @author lei
 * @date 2021-07-01
 */
@Service
public class TUInterfaceinfoServiceImpl implements ITUInterfaceinfoService
{
    @Resource
    private TUInterfaceinfoMapper tUInterfaceinfoMapper;
    @Resource
    private ImputationDataMapper imputationDataMapper;
    @Resource
    private IntRecordMapper intRecordMapper;

    /**
     * 查询【接口应用审批管理】
     *
     * @param id 【接口应用审批管理】ID
     * @return 【接口应用审批管理】
     */
    @Override
    public TUInterfaceinfo selectTUInterfaceinfoById(String id)
    {
//        String str = SecretKeyUtil.AESEncode("3fc674da58", "123/政数局/2022-10-20 - 2022-10-29/b29c94d72ee74c6d94b573d90020ea59");
//        System.out.println("解密后的明文是:" + SecretKeyUtil.AESDncode("3fc674da58", str));
        Long deptId = ShiroUtils.getSysUser().getDept().getDeptId();
        String deptName = ShiroUtils.getSysUser().getDept().getDeptName();
        TUInterfaceinfo tuInterfaceinfo = tUInterfaceinfoMapper.selectTUInterfaceinfoById(id);
        List<IntDetailed> intDetailedList = intRecordMapper.selectIntDetailedByIntId(id);
        if(intDetailedList.size() == 0){
            IntDetailed detailed = new IntDetailed();
            String purpose = "";
            String shrareType = tuInterfaceinfo.getShareType();
            String interfaceName = tuInterfaceinfo.getInterfaceName();
            switch (shrareType){
                case "share_type_2":
                    purpose = "市归集接口申请密钥：" + interfaceName;
                    break;
                case "share_type_3":
                    purpose = "省上报接口申请密钥：" + interfaceName;
            }
            String secretKey = SecretKeyUtil.AESEncode("3fc674da58", deptId + "/" + deptName + "/2022-12-02 - 2099-12-31/" + id);
            detailed.setIntId(id);
            detailed.setPurpose(purpose);
            detailed.setSecretKey(secretKey);
            detailed.setCode(tuInterfaceinfo.getCode());
            if(!purpose.equals("")){
                intRecordMapper.insertIntDetailed(detailed);
                tuInterfaceinfo.setSecretKey(secretKey);
            }
        }else {
            tuInterfaceinfo.setSecretKey(intDetailedList.get(0).getSecretKey());
        }
        return tuInterfaceinfo;
    }

    /**
     * 查询【接口应用审批管理】列表
     *
     * @param tUInterfaceinfo 【接口应用审批管理】
     * @return 【接口应用审批管理】
     */
    @Override
    public List<TUInterfaceinfo> selectTUInterfaceinfoList(TUInterfaceinfo tUInterfaceinfo)
    {
        List<TUInterfaceinfo> list = tUInterfaceinfoMapper.selectTUInterfaceinfoList(tUInterfaceinfo);
        list.forEach((i) -> i.setCount(sum(i.getCallsuccnum(),i.getCallfailnum())));
        return list;
    }

    private String sum(Long a, Long b){
        Long count = a + b;
        return NumberUtils.amountConversion(Integer.parseInt(count.toString()));
    }

    /**
     * 根据接口IDS获取接口LIST
     * */
    @Override
    public List<TUInterfaceinfo> selectTUInterfaceinfoList(String[] ids){
        return tUInterfaceinfoMapper.selectTUInterfaceinfoListByIds(ids);
    }

    /**
     * 根据数据应用IDS获取数据应用LIST
     * */
    @Override
    public List<TUInterfaceinfo> selectTUInterfaceDataList(String[] ids){
        return tUInterfaceinfoMapper.selectTUInterfaceDataListByIds(ids);
    }

    /**
     * 新增【接口应用审批管理】
     *
     * @param tUInterfaceinfo 【接口应用审批管理】
     * @return 结果
     */
    @Override
    public int insertTUInterfaceinfo(TUInterfaceinfo tUInterfaceinfo)
    {
        tUInterfaceinfo.setId(UUID.fastUUID().toString());
        tUInterfaceinfo.setCreateTime(DateUtils.getNowDate());
        //计算code 5位
        Integer integer = tUInterfaceinfoMapper.getMaxCode();
        if(integer == null){
            tUInterfaceinfo.setCode("00001");
        }else{
            Integer i = integer+1;
            int w =getW(i);
            String r = String.valueOf(i);
            for(int k=0;k<5-w;k++){
                r = "0"+r;
            }
            tUInterfaceinfo.setCode(r);

        }


        return tUInterfaceinfoMapper.insertTUInterfaceinfo(tUInterfaceinfo);
    }


    public static int getW(Integer i){
        for(int k=1;k<=5;k++){
            if(i/ (int)Math.pow(10,k) == 0){
                return k;
            }
        }
        return  0;
    }

    /**
     * 修改【接口应用审批管理】
     *
     * @param tUInterfaceinfo 【接口应用审批管理】
     * @return 结果
     */
    @Override
    public int updateTUInterfaceinfo(TUInterfaceinfo tUInterfaceinfo)
    {
        tUInterfaceinfo.setUpdateTime(DateUtils.getNowDate());
        return tUInterfaceinfoMapper.updateTUInterfaceinfo(tUInterfaceinfo);
    }

    /**
     * 删除【接口应用审批管理】对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTUInterfaceinfoByIds(String ids)
    {
        return tUInterfaceinfoMapper.deleteTUInterfaceinfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【接口应用审批管理】信息
     *
     * @param id 【接口应用审批管理】ID
     * @return 结果
     */
    @Override
    public int deleteTUInterfaceinfoById(String id)
    {
        return tUInterfaceinfoMapper.deleteTUInterfaceinfoById(id);
    }

    /**
     * 刷新数据
     */
    @Override
    public void refreshData(){
        //(164-57) t_guiji_gas_zenner_gas_purchase_center_infor - 归集-市燃气（真兰收费系统）-中心计费购气信息 - 467184 ms total:[12746163]
        //(123-29) t_guiji_gas_zenner_meter_reading_infor - 归集-市燃气（真兰收费系统）-抄表信息 - 324043 ms total:[12703733]
        //t_guiji_naturalresources_bdcdjxx - 归集-市自然资源（不动产）-个人基本信息 - updating database... id = 672238a3-3687-433c-888e-5db0c81eb8a8
        TUInterfaceinfo param = new TUInterfaceinfo();
        param.setNeedRefresh("1");
        List<TUInterfaceinfo> tuInterfaceinfoList = tUInterfaceinfoMapper.selectTUInterfaceinfoList(param);
        for (int i = 0; i < tuInterfaceinfoList.size(); i++) {
            TUInterfaceinfo tuInterfaceinfo = tuInterfaceinfoList.get(i);
            String tableName = tuInterfaceinfo.getTableName();
            if(tableName == null || tableName.equals("")){
                System.out.println("(" + tuInterfaceinfoList.size() + "-" + (i + 1) + ") " + tuInterfaceinfo.getTableName() + " - " + tuInterfaceinfo.getInterfaceName() + " - " + 0 + " ms");
                continue;
            }
            System.out.println(tuInterfaceinfo.getTableName() + " - " + tuInterfaceinfo.getInterfaceName() + " - " + "updating database... id = " + tuInterfaceinfo.getId());
            //select count(id) callsuccnum from ${tableName}
            Date dateBegin = new Date();
            TUInterfaceinfo result = tUInterfaceinfoMapper.selectCountByTableName(tuInterfaceinfo);
            Long interval = new Date().getTime() - dateBegin.getTime();//接口查询总数所用时间
            System.out.println("(" + tuInterfaceinfoList.size() + "-" + (i + 1) + ") " + tuInterfaceinfo.getTableName() + " - " + tuInterfaceinfo.getInterfaceName() + " - " + interval + " ms total:[" + result.getCallsuccnum() + "]");
            tuInterfaceinfo.setCallsuccnum(result.getCallsuccnum());
            tuInterfaceinfo.setRefreshTime(new Date());
            tuInterfaceinfo.setNeedRefresh("2");
            tUInterfaceinfoMapper.updateInterfaceCallsuccnum(tuInterfaceinfo);
            //update t_u_interfaceinfo set callsuccnum = #{callsuccnum} where id = #{id}
        }
    }

    /**
     * 刷新归集数据分析
     */
    @Override
    public void imputationData(){
        String year = "2022";
        TUInterfaceinfo param = new TUInterfaceinfo();
//        param.setNeedRefresh("1");
        //(164-57) t_guiji_gas_zenner_gas_purchase_center_infor - 归集-市燃气（真兰收费系统）-中心计费购气信息 - 237962 ms
        //(164-70) t_guiji_gas_zenner_meter_reading_infor - 归集-市燃气（真兰收费系统）-抄表信息 - 352968 ms
        List<TUInterfaceinfo> tuInterfaceinfoList = tUInterfaceinfoMapper.selectTUInterfaceinfoList(param);
        for (int i = 0; i < tuInterfaceinfoList.size(); i++) {
            TUInterfaceinfo tuInterfaceinfo = tuInterfaceinfoList.get(i);
            String tableName = tuInterfaceinfo.getTableName();
            if (tableName == null || tableName.equals("") || tableName.equals("t_u_interfaceinfo_emptytable")) {
                System.out.println("(" + tuInterfaceinfoList.size() + "-" + (i + 1) + ") " + tuInterfaceinfo.getTableName() + " - " + tuInterfaceinfo.getInterfaceName() + " - " + 0 + " ms");
                continue;
            }
            ImputationData imputationDataParam = new ImputationData();
            imputationDataParam.setYear(year);
            imputationDataParam.setTableName(tableName);
            boolean isExist = !(imputationDataMapper.selectImputationInterface(imputationDataParam).size() == 0);
            if(isExist){
                continue;
            }
            Map map = new HashMap();
            map.put("year", year);
            map.put("tableName", tableName);
            System.out.println(tuInterfaceinfo.getTableName() + " - " + tuInterfaceinfo.getInterfaceName() + " - " + "updating database... id = " + tuInterfaceinfo.getId());
            Date dateBegin = new Date();
            ImputationData imputationData = tUInterfaceinfoMapper.imputationData(map);
            Long interval = new Date().getTime() - dateBegin.getTime();//接口查询总数所用时间
            ImputationData imputationDataInsert = new ImputationData();
            imputationDataInsert.setYear(year);
            imputationDataInsert.setTableName(tableName);
            imputationDataInsert.setInterfaceName(tuInterfaceinfo.getInterfaceName());
            imputationDataInsert.setJan(imputationData.getJan());
            imputationDataInsert.setFeb(imputationData.getFeb());
            imputationDataInsert.setMar(imputationData.getMar());
            imputationDataInsert.setApr(imputationData.getApr());
            imputationDataInsert.setMay(imputationData.getMay());
            imputationDataInsert.setJun(imputationData.getJun());
            imputationDataInsert.setJul(imputationData.getJul());
            imputationDataInsert.setAug(imputationData.getAug());
            imputationDataInsert.setSep(imputationData.getSep());
            imputationDataInsert.setOct(imputationData.getOct());
            imputationDataInsert.setNov(imputationData.getNov());
            imputationDataInsert.setDecb(imputationData.getDecb());
            imputationDataMapper.insertImputationInterface(imputationDataInsert);
            System.out.println("(" + tuInterfaceinfoList.size() + "-" + (i + 1) + ") " + tuInterfaceinfo.getTableName() + " - " + tuInterfaceinfo.getInterfaceName() + " - " + interval + " ms");
        }
    }

    /**
     * 按月刷新归集数据分析
     */
    @Override
    public void imputationDataByMonth(){
        String year = new SimpleDateFormat("yyyy").format(new Date());//String year = "2022";
        String month = new SimpleDateFormat("M").format(new Date());
        TUInterfaceinfo param = new TUInterfaceinfo();
        param.setShareType("share_type_2");//市归集
//        param.setNeedRefresh("1");
        //(164-57) t_guiji_gas_zenner_gas_purchase_center_infor - 归集-市燃气（真兰收费系统）-中心计费购气信息 - 237962 ms
        //(164-70) t_guiji_gas_zenner_meter_reading_infor - 归集-市燃气（真兰收费系统）-抄表信息 - 352968 ms
        //t_guiji_naturalresources_qlrjbxx - 归集-市自然资源（不动产）-权利人基本信息 - updating database... id = 1de6752d-19c2-4010-9b61-9450a9d718b6
        List<TUInterfaceinfo> tuInterfaceinfoList = tUInterfaceinfoMapper.selectTUInterfaceinfoList(param);
        for (int i = 0; i < tuInterfaceinfoList.size(); i++) {
            TUInterfaceinfo tuInterfaceinfo = tuInterfaceinfoList.get(i);
            String tableName = tuInterfaceinfo.getTableName();
            if (tableName == null || tableName.equals("") || tableName.equals("t_u_interfaceinfo_emptytable")) {
                System.out.println("(" + tuInterfaceinfoList.size() + "-" + (i + 1) + ") " + tuInterfaceinfo.getTableName() + " - " + tuInterfaceinfo.getInterfaceName() + " - " + 0 + " ms");
                continue;
            }
            ImputationData imputationDataParam = new ImputationData();
            imputationDataParam.setYear(year);
            imputationDataParam.setTableName(tableName);
            boolean isExist = !(imputationDataMapper.selectImputationInterface(imputationDataParam).size() == 0);
            if(!isExist){
                continue;
            }
            Map map = new HashMap();
            map.put("year", year);
//            month = "1";
            map.put("month", month);
            System.out.println("month=" + month);
            map.put("tableName", tableName);
            System.out.println(tuInterfaceinfo.getTableName() + " - " + tuInterfaceinfo.getInterfaceName() + " - " + "updating database... id = " + tuInterfaceinfo.getId());
            Map column = new HashMap();
            column.put("tableSchema", tableName.contains("\\.") ? tableName.split("\\.")[0] : "thsjzt");//thgjj / thsjzt
            column.put("tableName", tableName.contains("\\.") ? tableName.split("\\.")[1] : tableName);//thgjj.tgjj_gtjkrxx / t_guiji_gas_zenner_meter_reading_infor
            column.put("columnName", "create_time");//create_time / LAST_UPDATE_TIME
            System.out.println(column.get("tableSchema"));
            System.out.println(column.get("tableName"));
            System.out.println(column.get("columnName"));
            List<Object> objectList = tUInterfaceinfoMapper.checkColumnExists(column);
            System.out.println(objectList.size() + "=object");
            Date dateBegin = new Date();
            ImputationData imputationData;
            if(objectList.size() != 0){
                imputationData = tUInterfaceinfoMapper.imputationDataByMonth(map);
            }else {
                imputationData = tUInterfaceinfoMapper.imputationDataByMonthLastUpdateTime(map);
            }
            Long interval = new Date().getTime() - dateBegin.getTime();//接口查询总数所用时间
            ImputationData imputationDataInsert = new ImputationData();
            imputationDataInsert.setYear(year);
            imputationDataInsert.setMonth(month);
            imputationDataInsert.setTableName(tableName);
//            imputationDataInsert.setInterfaceName(tuInterfaceinfo.getInterfaceName());//接口名称可能会变
            imputationDataInsert.setCount(imputationData.getCount());
            imputationDataMapper.updateImputationInterfaceByYearMonthTableName(imputationDataInsert);
            System.out.println("(" + tuInterfaceinfoList.size() + "-" + (i + 1) + ") " + tuInterfaceinfo.getTableName() + " - " + tuInterfaceinfo.getInterfaceName() + " - " + interval + " ms");
        }
    }

    /**
     * 查询接口数量
     * @return 结果
     */
    @Override
    public List<TUInterfaceinfo> getCount(){
        return tUInterfaceinfoMapper.getCount();
    }

    /**
     * 查询接口占比
     * @return 结果
     */
    @Override
    public List<TUInterfaceinfo> getPercent(String year){
        return tUInterfaceinfoMapper.getPercent(year);
    }

    /**
     * 查询接口占比
     * @return 结果
     */
    @Override
    public List<TUInterfaceinfo> getSubPercent(String deptId, String year){
        return tUInterfaceinfoMapper.getSubPercent(deptId,year);
    }

    /**
     * 接口审批分析
     * @return 结果
     */
    @Override
    public TUInterfaceinfo getSubJkspfx(String deptId, String year){
        return tUInterfaceinfoMapper.getSubJkspfx(deptId, year);
    }

    /**
     * 审批分析
     * @return 结果
     */
    @Override
    public TUInterfaceinfo getSubSpfx(String deptId, String year){
        return tUInterfaceinfoMapper.getSubSpfx(deptId, year);
    }

    /**
     * 查询省接口调用频次TOP5
     * @return 结果
     */
    @Override
    public List<TUInterfaceinfo> getProvinceTop5Frequency(){
        return tUInterfaceinfoMapper.getProvinceTop5Frequency();
    }

    /**
     * 查询市接口调用频次TOP5
     * @return 结果
     */
    @Override
    public List<TUInterfaceinfo> getCityTop5Frequency(){
        return tUInterfaceinfoMapper.getCityTop5Frequency();
    }

    /**
     * 查询省接口调用频次TOP20
     * @return 结果
     */
    @Override
    public List<TUInterfaceinfo> getProvinceTop20Frequency(){
        return tUInterfaceinfoMapper.getProvinceTop20Frequency();
    }

    /**
     * 查询市接口调用频次TOP20
     * @return 结果
     */
    @Override
    public List<TUInterfaceinfo> getCityTop20Frequency(){
        return tUInterfaceinfoMapper.getCityTop20Frequency();
    }

    /**
     * 查询归集接口排名
     */
    @Override
    public List<TUInterfaceinfo> getImputationInterfaceRanking(){
        return tUInterfaceinfoMapper.getImputationInterfaceRanking();
    }

    /**
     * 查询归集数据排名
     */
    @Override
    public List<TUInterfaceinfo> getImputationDataRanking(){
        return tUInterfaceinfoMapper.getImputationDataRanking();
    }

    /**
     * 查询故障接口数量
     * @return 结果
     * */
    @Override
    public Integer getInterfaceCount(String status){
        return tUInterfaceinfoMapper.getInterfaceCount(status);
    }

    /**
     * 根据部门ID查询分类数量
     * @return 结果
     * */
    @Override
    public TUInterfaceinfo getShareCountByDeptId(String deptId){
        return tUInterfaceinfoMapper.getShareCountByDeptId(deptId);
    }

    @Override
    public Long getGuijiCountByDeptId(String deptId){
        new Thread() {
            public void run() {
                try{
                    TUInterfaceinfo tuInterfaceinfo = new TUInterfaceinfo();
                    tuInterfaceinfo.setDeptId(deptId);
                    tuInterfaceinfo.setStatus("0");//状态(0可用，2禁用)
                    tuInterfaceinfo.setShareType("share_type_2");//数据来源 只统计市归集类型接口对应表中数据条数综合
                    /*
                    t_guiji_gas_zenner_gas_purchase_center_infor 真兰中心计费信息
                     */
                    List<TUInterfaceinfo> tuInterfaceinfoList = tUInterfaceinfoMapper.selectTUInterfaceinfoDataNumList(tuInterfaceinfo);
                    for (int i = 0; i < tuInterfaceinfoList.size(); i ++) {
                        TUInterfaceinfo interfaceinfo = tuInterfaceinfoList.get(i);
                        boolean uninitialized = interfaceinfo.getUpdateTime() == null;//未初始化
                        boolean oldData =  interfaceinfo.getUpdateTime() != null && new Date().getTime() - interfaceinfo.getUpdateTime().getTime() > 10 * 60 * 1000;//最多10分钟刷新一次最新数据
                        boolean isValid = interfaceinfo.getStatus().equals("0");//有效数据
                        if(uninitialized || (oldData && isValid)){
                            interfaceinfo.setUpdateTime(new Date());
                            Date dateBegin = new Date();
                            tUInterfaceinfoMapper.updateTUInterfaceinfoDataNum(interfaceinfo);//更新更新时间 防止多次操作
                            System.out.println("(" + tuInterfaceinfoList.size() + "-" + (i + 1) + ") " + interfaceinfo.getInterfaceName() + "更新更新时间 防止多次操作");
                            tUInterfaceinfoMapper.updateDataNum(interfaceinfo);//更新更新时间为最新的以及数据条数
                            Long interval = new Date().getTime() - dateBegin.getTime();//接口查询总数所用时间
                            System.out.println("(" + tuInterfaceinfoList.size() + "-" + (i + 1) + ") " + interfaceinfo.getTableName() + " - " + interfaceinfo.getInterfaceName() + " - " + interval + " ms");
                            // update t_u_interfaceinfo_datanum set datanum = 0, update_time = null 重置数据总数重新查询 使用updateTime控制min分钟内只能查询一次
                            // select count(*) from t_guiji_gas_zenner_gas_purchase_center_infor - 归集-真兰收费系统-中心计费购气信息 12452820
                            // (17-4) t_guiji_gas_zenner_gas_purchase_center_infor - 归集-真兰收费系统-中心计费购气信息 - 32424 ms
                            // (17-5) t_guiji_gas_zenner_meter_reading_infor - 归集-真兰收费系统-抄表信息 - 222659 ms
                            /*
                            update t_u_interfaceinfo_datanum set datanum = 0, update_time = null;
                            update t_u_interfaceinfo_datanum set status = 2 where table_name = 't_guiji_gas_zenner_gas_purchase_center_infor'; -- 12489514 17ms
                            update t_u_interfaceinfo_datanum set status = 2 where table_name = 't_guiji_gas_zenner_meter_reading_infor'; -- 12389252 23ms
                             */
                            // 燃气 25010409 26114283
                        }
                    }
                    System.out.println((tuInterfaceinfoList.size()>0?tuInterfaceinfoList.get(0).getDeptName():deptId)+": 查询接口数据结束，共" + tuInterfaceinfoList.size() + "个接口");
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
        Map<String, Object> param = new HashMap<>();
        param.put("deptId", deptId);
        tUInterfaceinfoMapper.getGuijiCountByDeptId(param);
        return Long.valueOf(String.valueOf(param.get("count"))).longValue();
    }

    /**
     * 接口调用频次
     * @return 结果
     * */
    @Override
    public int callFrequency(){
        return tUInterfaceinfoMapper.callFrequency();
    }

    /**
     * 接口归集频次
     * @return 结果
     * */
    @Override
    public int pushFrequency(){
        return tUInterfaceinfoMapper.pushFrequency();
    }

    /**
     * 接口共享频次
     * @return 结果
     * */
    @Override
    public int pullFrequency(){
        return tUInterfaceinfoMapper.pullFrequency();
    }

    /**
     * 查询接口日志台账
     * @return 结果
     */
    @Override
    public List<TUInterfaceinfo> interfaceLogList(TUInterfaceinfo interfaceinfo){
        return tUInterfaceinfoMapper.interfaceLogList(interfaceinfo);
    }

    /**
     * 查询共享应用分析
     * @return 结果
     */
    @Override
    public List<TUInterfaceinfo> getCallNumByDept(Long deptId){
        return tUInterfaceinfoMapper.getCallNumByDept(deptId);
    }
}
