package com.ruoyi.system.service;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.mail.MailUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.AkmDataMapper;
import com.ruoyi.system.mapper.KeyPeopleInfoMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysInterfaceForeignMapper;
import com.ruoyi.system.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class Task {

    @Resource
    private OdsQssHsjcxxService qdsQssHsjcxxService;
    @Resource
    private SysDeptMapper deptMapper;
    @Resource
    private KeyPeopleInfoMapper keyPeopleInfoMapper;
    @Resource
    private AkmDataMapper akmDataMapper;
    @Resource
    private SysInterfaceForeignMapper sysInterfaceForeignMapper;

    /***
     * 邮件发送（定时比对数据后发送邮件）
     */
    @Async
    @Scheduled(cron = "0 10,40 * * * ? ")
    //@Scheduled(cron = "0 0/2 * * * ? ")
    public void thirdNucleicAcid() {
        try {

            System.out.println("邮件发送开始========");
            long start1 = System.currentTimeMillis();

            SysDept dept = new SysDept();
            dept.setDeptName("定时任务");
            List<SysDept> depts = deptMapper.selectDeptList(dept);
            if(depts==null && depts.size()<=0){
                System.out.println("定时任务: 部门名字错误");
                return;
            }
            //获取人员信息
            KeyPeopleInfo keyPeopleInfo = new KeyPeopleInfo();
            keyPeopleInfo.setDeptId(depts.get(0).getDeptId().toString());
            List<KeyPeopleInfo> keyPeopleInfoList = keyPeopleInfoMapper.selectKeyPeopleInfoList(keyPeopleInfo);

            //获取当前时间
            Date data = DateUtils.getNowDate();
            String time = DateUtils.formatString(data);
            //往前推半个小时

            //String startTime = "2022-12-12 10:00:00";
            //String endTime = "2022-12-12 10:30:00";
            String startTime = DateUtils.plusSeconds(time, -70);
            String endTime = DateUtils.plusSeconds(time, -40);
            //SysUser user = SecurityUtils.getLoginUser().getUser();
            //UserInfo userInfo = new UserInfo();
            // startTime = "2022-09-20 00:00:00";
            //String endTime = "2022-09-27 00:00:00";
            String detectionNumber = "1";

            //计算开始时间结束时间相隔天数
            String detectionScope = "1H";

            List<String> idCardList = keyPeopleInfoList.stream().map(KeyPeopleInfo::getIdCard).collect(Collectors.toList());
            idCardList.add("342301197310201014");
            idCardList.add("340824199601181411");

            //从第三方数据库查询人员核酸计录
            List<OdsQssHsjcxx> OdsQssHsjcxxList = qdsQssHsjcxxService.thirdNucleicAcid(startTime, endTime, idCardList);

            //查询65开头的人员核酸数据
            List<OdsQssHsjcxx> OdsQssHsjcxxList65 = qdsQssHsjcxxService.thirdNucleicAcid65(startTime, endTime);
            OdsQssHsjcxxList.addAll(OdsQssHsjcxxList65);
            //查询安康码采集地点信息
            System.out.println("执行查询安康码采集地点信息sql");
            List<AkmData> akmDataList = akmDataMapper.selectAkmDataListByIdList(startTime, endTime, idCardList);

            //查询65开头的安康码扫码数据
            List<AkmData> akmDataList65 = akmDataMapper.selectAkmData65ListByIdList(startTime, endTime);
            akmDataList.addAll(akmDataList65);

            if((OdsQssHsjcxxList == null || OdsQssHsjcxxList.size() <= 0) && (akmDataList == null || akmDataList.size() <= 0)){
                System.out.println("无数据，不发送邮件");
                return;
            }


            //List<Map<String, Object>> mapList = new ArrayList<>();
            StringBuilder sb = new StringBuilder();
            sb.append("时间："+startTime+"--"+endTime);
            StringBuilder sb1 = new StringBuilder();
            sb1.append("时间："+startTime+"--"+endTime);


            boolean b = false;
            boolean b1 = false;
            if(OdsQssHsjcxxList != null && OdsQssHsjcxxList.size() > 0){

                sb.append("<br>").append("核酸采集点数据：");
                sb1.append("<br>").append("核酸采集点数据：");

                for (OdsQssHsjcxx odsQssHsjcxx : OdsQssHsjcxxList) {

                    if(odsQssHsjcxx.getSfzhm().substring(0,2).equals("65") || "342301197310201014".equals(odsQssHsjcxx.getSfzhm()) || "340824199601181411".equals(odsQssHsjcxx.getSfzhm()) ){
                        if(odsQssHsjcxx.getXm().length()==2){
                            sb.append("<br>").append(odsQssHsjcxx.getXm()).append("&nbsp;&nbsp;&nbsp;---").append(odsQssHsjcxx.getSfzhm()).append("---");
                        }else{
                            sb.append("<br>").append(odsQssHsjcxx.getXm()).append("---").append(odsQssHsjcxx.getSfzhm()).append("---");
                        }
                        if(StringUtils.isNotEmpty(odsQssHsjcxx.getSjhm())){
                            sb.append(odsQssHsjcxx.getSjhm()).append("---").append(odsQssHsjcxx.getCjsj());
                            sb.append("---").append(odsQssHsjcxx.getJcdd());
                        }else{
                            sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                            sb.append("---").append(odsQssHsjcxx.getCjsj()).append("---").append(odsQssHsjcxx.getJcdd());
                        }

                        b = true;
                    }else {
                        if(odsQssHsjcxx.getXm().length()==2){
                            sb1.append("<br>").append(odsQssHsjcxx.getXm()).append("&nbsp;&nbsp;&nbsp;---").append(odsQssHsjcxx.getSfzhm()).append("---");
                        }else{
                            sb1.append("<br>").append(odsQssHsjcxx.getXm()).append("---").append(odsQssHsjcxx.getSfzhm()).append("---");
                        }

                        if(StringUtils.isNotEmpty(odsQssHsjcxx.getSjhm())){
                            sb1.append(odsQssHsjcxx.getSjhm()).append("---").append(odsQssHsjcxx.getCjsj());
                            sb1.append("---").append(odsQssHsjcxx.getJcdd());
                        }else{
                            sb1.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                            sb1.append("---").append(odsQssHsjcxx.getCjsj()).append("---").append(odsQssHsjcxx.getJcdd());
                        }
                        b1 = true;
                    }

                /*Map<String, Object> map = new HashMap<>();
                map.put("姓名", odsQssHsjcxx.getXm());
                map.put("身份证号码", odsQssHsjcxx.getSfzhm());
                map.put("联系号码", odsQssHsjcxx.getSjhm());
                map.put("核酸采集时间", odsQssHsjcxx.getCjsj());
                map.put("核酸结果时间", odsQssHsjcxx.getJgcjss());
                map.put("采集地点", odsQssHsjcxx.getJcdd());
                if(StringUtils.isEmpty(odsQssHsjcxx.getJcjg())){
                    map.put("采集结果", odsQssHsjcxx.getJcjg());
                }else if("0".equals(odsQssHsjcxx.getJcjg()) || "negative".equals(odsQssHsjcxx.getJcjg())){
                    map.put("采集结果", "阴性");
                }else if("1".equals(odsQssHsjcxx.getJcjg()) || "positive".equals(odsQssHsjcxx.getJcjg())){
                    map.put("采集结果", "阳性");
                }
                map.put("采集结果", "");
                map.put("比对开始时间", startTime);
                map.put("比对结束时间", endTime);
                mapList.add(map);*/
                }
            }

            if(akmDataList != null && akmDataList.size() > 0){
                sb.append("<br>").append("安康码扫码地点数据：");
                sb1.append("<br>").append("安康码扫码地点数据：");

                for (AkmData akmData : akmDataList) {

                    if(akmData.getIdCard().substring(0,2).equals("65")  || "342301197310201014".equals(akmData.getIdCard()) || "340824199601181411".equals(akmData.getIdCard())){
                        if(akmData.getName().length()==2){
                            sb.append("<br>").append(akmData.getName()).append("&nbsp;&nbsp;&nbsp;---").append(akmData.getIdCard()).append("---");
                        }else{
                            sb.append("<br>").append(akmData.getName()).append("---").append(akmData.getIdCard()).append("---");
                        }
                        if(StringUtils.isNotEmpty(akmData.getPhoneNum())){
                            sb.append(akmData.getPhoneNum()).append("---").append(akmData.getCollectTime());
                            sb.append("---").append(akmData.getEquipName());
                        }else{
                            sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                            sb.append("---").append(akmData.getCollectTime()).append("---").append(akmData.getEquipName());
                        }

                        b = true;
                    }else {
                        if(akmData.getName().length()==2){
                            sb1.append("<br>").append(akmData.getName()).append("&nbsp;&nbsp;&nbsp;---").append(akmData.getIdCard()).append("---");
                        }else{
                            sb1.append("<br>").append(akmData.getName()).append("---").append(akmData.getIdCard()).append("---");
                        }

                        if(StringUtils.isNotEmpty(akmData.getPhoneNum())){
                            sb1.append(akmData.getPhoneNum()).append("---").append(akmData.getCollectTime());
                            sb1.append("---").append(akmData.getEquipName());
                        }else{
                            sb1.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                            sb1.append("---").append(akmData.getCollectTime()).append("---").append(akmData.getEquipName());
                        }
                        b1 = true;
                    }
                }
            }

            System.out.println("--------逻辑结束，开始走发送邮件接口" );
            if(b){
                ArrayList<String> tos2 = CollUtil.newArrayList(
                        "3555393474@qq.com",
                        "731020@qq.com","1176443192@qq.com");
                /*ArrayList<String> tos2 = CollUtil.newArrayList(
                        "1434745622@qq.com");*/
                MailUtil.send(tos2, "会员积分实时变动通知", sb.toString(), true);
            }
            /*if(b1){
                ArrayList<String> tos1 = CollUtil.newArrayList(
                        "qsxzhb@126.com",
                        "731020@qq.com","1176443192@qq.com");
                *//*ArrayList<String> tos1 = CollUtil.newArrayList(
                        "1434899932@qq.com",
                        "1434745622@qq.com");*//*
                MailUtil.send(tos1, "会员积分实时变动通知", sb1.toString(), true);
            }*/

            //String path = ExcelUtils.getInstance().createExcel(mapList, "核酸对比数据", "对比数据");
            /*MailUtil.send("qsxzhb@126.com", "会员积分实时变动通知", "时间："+startTime+"--"+endTime, true,
                    FileUtil.file(path));
            MailUtil.send("731020@qq.com", "会员积分实时变动通知", "时间："+startTime+"--"+endTime, true,
                    FileUtil.file(path));*/
            //删除系统文件
            //File file = new File(path);
            //file.delete();
            long end1 = System.currentTimeMillis();
            System.out.println("--------邮件发送结束耗时" + (end1 - start1) + "---------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 定时重置查询次数
     */
    @Async
    @Scheduled(cron = "0 1 0 * * ? ")
    public void reset() {
        List<SysInterfaceForeign> interfaceForeignList = sysInterfaceForeignMapper.selectSysInterfaceForeignList(new SysInterfaceForeign());
        for (SysInterfaceForeign sysInterfaceForeign : interfaceForeignList) {
            sysInterfaceForeign.setRemainingNumber(sysInterfaceForeign.getNumber());
            sysInterfaceForeignMapper.updateSysInterfaceForeign(sysInterfaceForeign);
        }
    }


}
