package com.ruoyi.quartz.task;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ImageUtil;
import com.ruoyi.common.utils.http.restTemplate.InvokingService;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.doubledefense.domain.CheckTaskInventory;
import com.ruoyi.doubledefense.domain.CheckTaskInventoryRecord;
import com.ruoyi.doubledefense.dtoGov.*;
import com.ruoyi.doubledefense.service.*;
import com.ruoyi.doubledefense.service.impl.*;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.impl.SysDictDataServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 定时同步：重预防汇聚计算节点
 * 预计是往“重预防汇聚计算节点”推送数据
 *
 * @author ruoyi
 */
@Slf4j
@Component("govSyncTask")
public class GovSyncTask {
    @Autowired
    private InvokingService invokingService;

    /**
     * 定时清理巡检任务清单
     * 定时检查漏检任务清单
     * <p>
     */
    public void sync() {
        log.info("【重预防汇聚计算节点同步定时器】推送数据开始：{}", DateUtils.getTime());

        //1、查询字典表配置的URL
        SysDictData dictData = new SysDictData();
        dictData.setDictType("gov_sync");
        ISysDictDataService dictDataService = SpringUtils.getBean(SysDictDataServiceImpl.class);
        List<SysDictData> sysDictDataList = dictDataService.selectDictDataList(dictData);

        for (SysDictData sysDictData : sysDictDataList) {
            String dictLabel = sysDictData.getDictLabel();
            String url = sysDictData.getDictValue();
            System.out.println(dictLabel + " = " + url);

            if (dictLabel.contains("-四色图")) {
                govSelectFourColorDiagramList(url);//1、企业安全风险空间分布接口->四色图
            } else if (dictLabel.contains("分析单元")) {
                govSelectRiskUnitList(url);//2、测试分析单元
            } else if (dictLabel.contains("风险事件接口")) {
                govSelectRiskEventList(url);//3、测试风险事件
            } else if (dictLabel.contains("安全风险管控措施接口\n")) {
                govSelectRiskMeasureList(url);//4、安全风险管控措施
            } else if (dictLabel.contains("-隐患排查要求")) {
                govSelectCheckTaskList(url);//5、隐患排查任务 -> 隐患排查要求
            } else if (dictLabel.contains("-巡检打卡记录")) {
                govSelectCheckTaskInventoryRecordList(url);// 6、隐患排查记录 -> 巡检记录
            } else if (dictLabel.contains("-隐患列表")) {
                govSelectDangerList(url);//7、隐患排查信息->隐患列表（APP和PC都可以添加隐患）
            }
        }
        log.info("【重预防汇聚计算节点同步定时器】推送数据结束：{}", DateUtils.getTime());
    }

    /**
     * 1、企业安全风险空间分布接口->四色图
     */
    private void govSelectFourColorDiagramList(String url) {
        IFourColorDiagramService fourColorDiagramService = SpringUtils.getBean(FourColorDiagramServiceImpl.class);
        FourColorDiagramDTOGov fourColorDiagramDTOGov = new FourColorDiagramDTOGov();
        fourColorDiagramDTOGov.setPageNo(1);
        fourColorDiagramDTOGov.setPageSize(20);//一个企业做多上传20张
        PageInfo<FourColorDiagramDTOGov> govSelectFourColorDiagramList = fourColorDiagramService.govSelectFourColorDiagramList(fourColorDiagramDTOGov);
        List<FourColorDiagramDTOGov> fourColorDiagramDTOGovList = govSelectFourColorDiagramList.getList();
        Iterator<FourColorDiagramDTOGov> fourColorDiagramDTOGovIterator = fourColorDiagramDTOGovList.iterator();
        while (fourColorDiagramDTOGovIterator.hasNext()) {
            FourColorDiagramDTOGov colorDiagramDTOGov = fourColorDiagramDTOGovIterator.next();
            String colorImg = colorDiagramDTOGov.getColorImg();
            String imageToBase64Str = null;
            try {
                imageToBase64Str = ImageUtil.convertImageToBase64Str(colorImg);

                byte[] imageBytes = Base64.getDecoder().decode(imageToBase64Str.split(",")[1]);
                InputStream in = new ByteArrayInputStream(imageBytes);
                BufferedImage image = ImageIO.read(in);
                if (image != null) {
                    // 校验成功
                    System.out.println("SUCCESS----------");
                } else {
                    fourColorDiagramDTOGovIterator.remove();
                    // 校验失败
                    System.out.println("ERROR-----");
                }
            } catch (Exception e) {
                fourColorDiagramDTOGovIterator.remove();
                e.printStackTrace();
            }
            colorDiagramDTOGov.setColorImg(imageToBase64Str);
        }
        System.out.println("1、企业安全风险空间分布接口->四色图：" + govSelectFourColorDiagramList);

        //TODO 发送HTTP请求，推送数据到数字化平台
        String type = "GET";
        String paramType = null;
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55Q29kZSI6IlFZMDAwMSIsImxlYWRlciI6IjEwIiwicGNVc2VySWQiOiIxIiwibmlja05hbWUiOiLnrqHnkIblkZgiLCJwb3N0SWRMaXN0IjoiMSwyLDQiLCJpZCI6IjExIiwidXNlclR5cGUiOiIxIiwidXNlck5hbWUiOiJhZG1pbiIsImV4cCI6MTY5ODczMzQzMX0.xt7FkxMQTmgmLKUqdsCBY2lJq3VZvcfjhpXlZuvt75s");
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("token", "PC Token");
        String resultData = invokingService.invServiceForResultData(url, type, paramType, headerMap, paramMap);
        System.out.println("接口返回数据 = " + resultData);
    }


    /**
     * 2、分析单元
     */
    private void govSelectRiskUnitList(String url) {
        IRiskUnitService riskUnitService = SpringUtils.getBean(RiskUnitServiceImpl.class);
        RiskUnitDTOGov ristUnit = new RiskUnitDTOGov();
        ristUnit.setPageNo(1);
        ristUnit.setPageSize(10);
        PageInfo<RiskUnitDTOGov> govSelectRiskUnitList = riskUnitService.govSelectRiskUnitList(ristUnit);
//        List<RiskUnitDTOGov> list = govSelectRiskUnitList.getList();
//        for (RiskUnitDTOGov data : list) {
//            System.out.println();
//            System.out.println(data);
//            System.out.println(JSON.toJSONString(data));
//            System.out.println();
//        }
        System.out.println("2、测试分析单元：" + JSON.toJSONString(govSelectRiskUnitList));

        //TODO 发送HTTP请求，推送数据到数字化平台
        String type = "GET";
        String paramType = null;
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55Q29kZSI6IlFZMDAwMSIsImxlYWRlciI6IjEwIiwicGNVc2VySWQiOiIxIiwibmlja05hbWUiOiLnrqHnkIblkZgiLCJwb3N0SWRMaXN0IjoiMSwyLDQiLCJpZCI6IjExIiwidXNlclR5cGUiOiIxIiwidXNlck5hbWUiOiJhZG1pbiIsImV4cCI6MTY5ODczMzQzMX0.xt7FkxMQTmgmLKUqdsCBY2lJq3VZvcfjhpXlZuvt75s");
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("dictKey", "gov_sync");
        String resultData = invokingService.invServiceForResultData(url, type, paramType, headerMap, paramMap);
        System.out.println("接口返回数据 = " + resultData);

    }


    /**
     * 3、风险事件
     */
    private void govSelectRiskEventList(String url) {
        IRiskEventService riskEventService = SpringUtils.getBean(RiskEventServiceImpl.class);
        RiskEventDTOGov riskEvent = new RiskEventDTOGov();
        riskEvent.setPageNo(1);
        riskEvent.setPageSize(10);
        PageInfo<RiskEventDTOGov> govSelectRiskEventList = riskEventService.govSelectRiskEventList(riskEvent);
        System.out.println("3、测试风险事件：" + JSON.toJSONString(govSelectRiskEventList));

        //TODO 发送HTTP请求，推送数据到数字化平台
        String type = "GET";
        String paramType = null;
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55Q29kZSI6IlFZMDAwMSIsImxlYWRlciI6IjEwIiwicGNVc2VySWQiOiIxIiwibmlja05hbWUiOiLnrqHnkIblkZgiLCJwb3N0SWRMaXN0IjoiMSwyLDQiLCJpZCI6IjExIiwidXNlclR5cGUiOiIxIiwidXNlck5hbWUiOiJhZG1pbiIsImV4cCI6MTY5ODczMzQzMX0.xt7FkxMQTmgmLKUqdsCBY2lJq3VZvcfjhpXlZuvt75s");
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("dictKey", "gov_sync");
        String resultData = invokingService.invServiceForResultData(url, type, paramType, headerMap, paramMap);
        System.out.println("接口返回数据 = " + resultData);
    }


    /**
     * 4、安全风险管控措施
     */
    private void govSelectRiskMeasureList(String url) {


        IRiskMeasureService riskMeasureService = SpringUtils.getBean(RiskMeasureServiceImpl.class);
        RiskMeasureDTOGov riskMeasureDTOGov = new RiskMeasureDTOGov();
        riskMeasureDTOGov.setPageNo(1);
        riskMeasureDTOGov.setPageSize(10);
        PageInfo<RiskMeasureDTOGov> govSelectRiskMeasureList = riskMeasureService.govSelectRiskMeasureList(riskMeasureDTOGov);

        System.out.println("：" + JSON.toJSONString(govSelectRiskMeasureList));

        //TODO 发送HTTP请求，推送数据到数字化平台
        String type = "GET";
        String paramType = null;
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55Q29kZSI6IlFZMDAwMSIsImxlYWRlciI6IjEwIiwicGNVc2VySWQiOiIxIiwibmlja05hbWUiOiLnrqHnkIblkZgiLCJwb3N0SWRMaXN0IjoiMSwyLDQiLCJpZCI6IjExIiwidXNlclR5cGUiOiIxIiwidXNlck5hbWUiOiJhZG1pbiIsImV4cCI6MTY5ODczMzQzMX0.xt7FkxMQTmgmLKUqdsCBY2lJq3VZvcfjhpXlZuvt75s");
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("dictKey", "gov_sync");
        String resultData = invokingService.invServiceForResultData(url, type, paramType, headerMap, paramMap);
        System.out.println("接口返回数据 = " + resultData);
    }


    /**
     * 5、隐患排查任务 -> 隐患排查要求
     */
    private void govSelectCheckTaskList(String url) {
        //
        ICheckTaskService checkTaskService = SpringUtils.getBean(CheckTaskServiceImpl.class);
        CheckTaskDTOGov checkTaskDTOGov = new CheckTaskDTOGov();
        checkTaskDTOGov.setPageNo(1);
        checkTaskDTOGov.setPageSize(10);
        PageInfo<CheckTaskDTOGov> govSelectCheckTaskList = checkTaskService.govSelectCheckTaskList(checkTaskDTOGov);

        System.out.println("5、隐患排查任务 -> 隐患排查要求：" + JSON.toJSONString(govSelectCheckTaskList));

        //TODO 发送HTTP请求，推送数据到数字化平台
        String type = "GET";
        String paramType = null;
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55Q29kZSI6IlFZMDAwMSIsImxlYWRlciI6IjEwIiwicGNVc2VySWQiOiIxIiwibmlja05hbWUiOiLnrqHnkIblkZgiLCJwb3N0SWRMaXN0IjoiMSwyLDQiLCJpZCI6IjExIiwidXNlclR5cGUiOiIxIiwidXNlck5hbWUiOiJhZG1pbiIsImV4cCI6MTY5ODczMzQzMX0.xt7FkxMQTmgmLKUqdsCBY2lJq3VZvcfjhpXlZuvt75s");
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("dictKey", "gov_sync");
        String resultData = invokingService.invServiceForResultData(url, type, paramType, headerMap, paramMap);
        System.out.println("接口返回数据 = " + resultData);
    }


    /**
     * 6、隐患排查记录 -> 巡检记录
     */
    private void govSelectCheckTaskInventoryRecordList(String url) {
        ICheckTaskInventoryRecordService checkTaskInventoryRecordService = SpringUtils.getBean(CheckTaskInventoryRecordServiceImpl.class);
        CheckTaskInventoryRecordDTOGov checkTaskInventoryRecordDTOGov = new CheckTaskInventoryRecordDTOGov();
        checkTaskInventoryRecordDTOGov.setPageNo(1);
        checkTaskInventoryRecordDTOGov.setPageSize(20);
        PageInfo<CheckTaskInventoryRecordDTOGov> govSelectCheckTaskInventoryRecordList = checkTaskInventoryRecordService.govSelectCheckTaskInventoryRecordList(checkTaskInventoryRecordDTOGov);

        System.out.println("6、隐患排查记录 -> 巡检记录：" + JSON.toJSONString(govSelectCheckTaskInventoryRecordList));

        //TODO 发送HTTP请求，推送数据到数字化平台
        String type = "GET";
        String paramType = null;
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55Q29kZSI6IlFZMDAwMSIsImxlYWRlciI6IjEwIiwicGNVc2VySWQiOiIxIiwibmlja05hbWUiOiLnrqHnkIblkZgiLCJwb3N0SWRMaXN0IjoiMSwyLDQiLCJpZCI6IjExIiwidXNlclR5cGUiOiIxIiwidXNlck5hbWUiOiJhZG1pbiIsImV4cCI6MTY5ODczMzQzMX0.xt7FkxMQTmgmLKUqdsCBY2lJq3VZvcfjhpXlZuvt75s");
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("dictKey", "gov_sync");
        String resultData = invokingService.invServiceForResultData(url, type, paramType, headerMap, paramMap);
        System.out.println("接口返回数据 = " + resultData);
    }


    /**
     * 7、隐患排查信息->隐患列表（APP和PC都可以添加隐患）
     */
    private void govSelectDangerList(String url) {
        //
        IDangerService dangerService = SpringUtils.getBean(DangerServiceImpl.class);
        DangerDTOGov dangerDTOGov = new DangerDTOGov();
        dangerDTOGov.setPageNo(1);
        dangerDTOGov.setPageSize(10);
        PageInfo<DangerDTOGov> govSelectDangerList = dangerService.govSelectDangerList(dangerDTOGov);

        System.out.println("7、隐患排查信息->隐患列表（APP和PC都可以添加隐患）：" + JSON.toJSONString(govSelectDangerList));

        //TODO 发送HTTP请求，推送数据到数字化平台
        String type = "GET";
        String paramType = null;
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55Q29kZSI6IlFZMDAwMSIsImxlYWRlciI6IjEwIiwicGNVc2VySWQiOiIxIiwibmlja05hbWUiOiLnrqHnkIblkZgiLCJwb3N0SWRMaXN0IjoiMSwyLDQiLCJpZCI6IjExIiwidXNlclR5cGUiOiIxIiwidXNlck5hbWUiOiJhZG1pbiIsImV4cCI6MTY5ODczMzQzMX0.xt7FkxMQTmgmLKUqdsCBY2lJq3VZvcfjhpXlZuvt75s");
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("dictKey", "gov_sync");
        String resultData = invokingService.invServiceForResultData(url, type, paramType, headerMap, paramMap);
        System.out.println("接口返回数据 = " + resultData);
    }

}
