package cn.cecep.talroad.service.impl;

import java.beans.Transient;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import cn.cecep.talroad.domain.*;
import cn.cecep.talroad.mapper.*;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cecep.talroad.service.IFactoryReportingTaskService;
import cn.cecep.talroad.mapper.FactoryReportingTaskMapper;
import cn.cecep.talroad.domain.FactoryReportingTask;
import cn.cecep.talroad.service.IFactoryReportingTaskService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 企业信息上报任务Service业务层处理
 *
 * @author szhpt
 * @date 2023-05-09*/
@Service
public class FactoryReportingTaskServiceImpl extends ServiceImpl<FactoryReportingTaskMapper,FactoryReportingTask> implements IFactoryReportingTaskService
{
    @Autowired
    private FactoryReportingTaskMapper factoryReportingTaskMapper;
    @Autowired
    private PcFactoryMapper pcFactoryMapper;
    @Autowired
    private FactoryReportingRemindersMapper factoryReportingRemindersMapper;
    @Autowired
    private FactoryReportingManifestMapper factoryReportingManifestMapper;
    @Autowired
    private BEnterpriseIrBusinessAlertsMapper enterpriseIrBusinessAlertsMapper;
    /**
     * 查询企业信息上报任务列表
     *
     * @param factoryReportingTask 企业信息上报任务
     * @return 企业信息上报任务
     */
    @Override
    public List<FactoryReportingTask> selectFactoryReportingTaskList(FactoryReportingTask factoryReportingTask)
    {
        return factoryReportingTaskMapper.selectFactoryReportingTaskList(factoryReportingTask);
    }

    @Override
    @Transactional
    public void informationReporting() {
        List<PcFactory> pcFactories = pcFactoryMapper.selectList(new QueryWrapper<>());
        Map<String, PcFactory> pcLabelsMap = pcFactories.stream().collect(Collectors.toMap(PcFactory::getId, a -> a,(k1, k2)->k1));
        List<FactoryReportingReminders> reportingReminders = factoryReportingRemindersMapper.selectList(new QueryWrapper<FactoryReportingReminders>().eq("submit_status", "0").eq("tatol_status","1"));
        List<FactoryReportingManifest> factoryReportingManifests=new ArrayList<>();
        Date createTime=new Date();
        DateTime parse = DateUtil.parse(LocalDateTime.now().format(
                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
        ), "yyyy-MM-dd HH:mm:ss");
        List<BEnterpriseIrBusinessAlerts>  businessAlertsList=new ArrayList<>();
        reportingReminders.forEach(item->{
            if(pcLabelsMap.containsKey(item.getFactoryId())){
                //任务清单
                FactoryReportingManifest factoryReportingManifest=new FactoryReportingManifest();
                PcFactory pcFactory = pcLabelsMap.get(item.getFactoryId());
                factoryReportingManifest.setFactoryName(pcFactory.getFactoryName());
                factoryReportingManifest.setUscCode(pcFactory.getUscCode());
                factoryReportingManifest.setIndustryCategory(pcFactory.getIndustryPermissionName());
                if(StringUtils.isNotBlank(pcFactory.getTechnicalMan())){
                    factoryReportingManifest.setContact(pcFactory.getTechnicalMan());
                }else {
                    factoryReportingManifest.setContact(pcFactory.getPrincipalMan());
                }
                if(StringUtils.isNotBlank(pcFactory.getMobileTelephone())){
                    factoryReportingManifest.setContactPhone(pcFactory.getMobileTelephone());
                }else {
                    factoryReportingManifest.setContactPhone(pcFactory.getTel());
                }
                factoryReportingManifest.setRegionName(pcFactory.getCountryName());
                factoryReportingManifest.setCreateTime(createTime);
                factoryReportingManifest.setFmId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
                factoryReportingManifests.add(factoryReportingManifest);
                //业务提醒
                BEnterpriseIrBusinessAlerts enterpriseIrBusinessAlerts=new BEnterpriseIrBusinessAlerts();
                enterpriseIrBusinessAlerts.setFactoryId(item.getFactoryId());
                enterpriseIrBusinessAlerts.setBusinessType("信息上报");
                enterpriseIrBusinessAlerts.setBusinessDetails("您好，企业信息上报提交截止时间是2023年5月19日，截止当前贵企业还未进行提交，请尽快完成信息填写并提交。");
                enterpriseIrBusinessAlerts.setBusinessTime(parse);
                enterpriseIrBusinessAlerts.setCreateTime(parse);
                businessAlertsList.add(enterpriseIrBusinessAlerts);
            }
        });
        //
        Map<String, List<FactoryReportingManifest>> groupedMap = factoryReportingManifests.stream()
                .collect(Collectors.groupingBy(FactoryReportingManifest::getRegionName));
        // 将分组后的结果按照key进行遍历
        groupedMap.forEach((key, value) -> {
            FactoryReportingTask factoryReportingTask=new FactoryReportingTask();
            factoryReportingTask.setTaskTime(createTime);
            factoryReportingTask.setTaskName("企业上报任务督办");
            factoryReportingTask.setTaskSource("企业端平台");
            factoryReportingTask.setRegionName(key);
            String startTime= LocalDateTime.now().format(
                    DateTimeFormatter.ofPattern("yyyy年MM月dd日")
            );
            //市级数据推送
            factoryReportingTask.setTaskDescription("截止"+startTime+"还有"+value.size()+"家企业未进行企业信息上报，请督办企业尽快完成上报。");
            String frId= UUID.randomUUID().toString().trim().replaceAll("-", "");
            factoryReportingTask.setFrId(frId);
            factoryReportingTask.setTaskUser(getTaskUser(key));
            value.forEach(item->{
                item.setFrId(frId);
            });
            //新增任务表
            this.save(factoryReportingTask);
            //新增任务清单表
            List<List<FactoryReportingManifest>> partition = ListUtil.partition(value, 2000);
            partition.forEach(i->{
                factoryReportingManifestMapper.insertBatch(i);
            });
        });
        //新增市级账号推送
        if(CollectionUtil.isNotEmpty(factoryReportingManifests)){
            FactoryReportingTask factoryReportingTask=new FactoryReportingTask();
            factoryReportingTask.setTaskTime(createTime);
            factoryReportingTask.setTaskName("企业上报任务督办");
            factoryReportingTask.setTaskSource("企业端平台");
            factoryReportingTask.setRegionName("唐山市");
            String startTime= LocalDateTime.now().format(
                    DateTimeFormatter.ofPattern("yyyy年MM月dd日")
            );
            //市级数据推送
            factoryReportingTask.setTaskDescription("截止"+startTime+"还有"+factoryReportingManifests.size()+"家企业未进行企业信息上报，请督办企业尽快完成上报。");
            String frId= UUID.randomUUID().toString().trim().replaceAll("-", "");
            factoryReportingTask.setFrId(frId);
            factoryReportingTask.setTaskUser(getTaskUser("唐山市"));
            factoryReportingManifests.forEach(item->{
                item.setFmId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
                item.setFrId(frId);
            });
            //新增任务表
            this.save(factoryReportingTask);
            //新增任务清单表
            List<List<FactoryReportingManifest>> partition = ListUtil.partition(factoryReportingManifests, 2000);
            partition.forEach(i->{
                factoryReportingManifestMapper.insertBatch(i);
            });
        }

        //新增企业提醒企业的消息
        List<List<BEnterpriseIrBusinessAlerts>> partition = ListUtil.partition(businessAlertsList, 2000);
        partition.forEach(i->{
            enterpriseIrBusinessAlertsMapper.insertBatch(i);
        });
    }
    public String getTaskUser(String name){
        //19
     String str="";
     if(name.contains("唐山市")){
         str="zfzd";
     }
     if(name.contains("丰南区")){
         str="zhglfengnan";
     }
     if(name.contains("丰润区")){
         str="zhglfengrun";
     }
     if(name.contains("乐亭县")){
         str="zhgllaoting";
     }
     if(name.contains("南堡经济开发区")){
         str="zhglnanbao";
     }
     if(name.contains("古冶区")){
         str="zhglguzhi";
     }
     if(name.contains("开平区")){
         str="zhglkaiping";
     }
     if(name.contains("曹妃甸区")){
         str="zhglcaofeidian";
     }
     if(name.contains("汉沽管理区")){
         str="zhglhangu";
     }
     if(name.contains("海港经济开发区")){
         str="zhglhaigang";
     }
     if(name.contains("滦南县")){
         str="zhglluannan";
     }
     if(name.contains("滦县")){//
         str="zhglluanzhou";
     }
     if(name.contains("玉田县")){
         str="zhglyutian";
     }
     if(name.contains("芦台经济开发区")){
         str="zhgllutai";
     }
     if(name.contains("路北区")){
         str="zhgllubei";
     }
     if(name.contains("路南区")){
         str="zhgllunan";
     }
     if(name.contains("迁安市")){
         str="zhglqianan";
     }
     if(name.contains("迁西县")){
         str="zhglqianxi";
     }
     if(name.contains("遵化市")){
         str="zhglzunhua";
     }
     if(name.contains("高新技术产业开发区")){
         str="zhglgaoxin";
     }
     if(name.contains("旅游岛")){
         str="zhgllvyoudao";
     }
        return str;
    }
}
