/**
 * Copyright
 * All right reserved.
 * 项目名称:运维系统
 * 创建日期:2022/5/22
 */
package org.springblade.modules.etl.task;


import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.ConcurrentDateFormat;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.baseinfo.monitorpoint.entity.MonitorPointInfoEntity;
import org.springblade.modules.baseinfo.monitorpoint.entity.MonitorPointRelEntity;
import org.springblade.modules.baseinfo.monitorpoint.service.IMonitorPointRelService;
import org.springblade.modules.baseinfo.monitorpoint.service.IMonitorPointService;
import org.springblade.modules.baseinfo.monitorsite.entity.MonitorSiteInfoEntity;
import org.springblade.modules.baseinfo.monitorsite.entity.MonitorSiteRelEntity;
import org.springblade.modules.baseinfo.monitorsite.service.IMonitorSiteRelService;
import org.springblade.modules.baseinfo.monitorsite.service.IMonitorSiteService;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.baseinfo.wateruseconsumer.entity.WaterUseConsumerInfoEntity;
import org.springblade.modules.baseinfo.wateruseconsumer.service.IWaterUseConsumerService;
import org.springblade.modules.etl.entity.*;
import org.springblade.modules.etl.service.*;

import java.util.List;
import java.util.concurrent.Callable;

/***
 * Date:2022/5/22
 * Title: 基础测站信息抽取
 * Description：
 * @author swp
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class EtlBaseRtuInfoTask implements Callable<Integer> {

    private static IEtlMpDataService etlMpDataService;

    private static IEtlMpDataService getEtlMpDataService() {
        if (etlMpDataService == null) {
            etlMpDataService = SpringUtil.getBean(IEtlMpDataService.class);
        }
        return etlMpDataService;
    }

    private static IEtlWiuDataService etlWiuDataService;

    private static IEtlWiuDataService getEtlWiuDataService() {
        if (etlWiuDataService == null) {
            etlWiuDataService = SpringUtil.getBean(IEtlWiuDataService.class);
        }
        return etlWiuDataService;
    }


    private static IEtlWiustDataService etlWiustDataService;

    private static IEtlWiustDataService getEtlWiustDataService() {
        if (etlWiustDataService == null) {
            etlWiustDataService = SpringUtil.getBean(IEtlWiustDataService.class);
        }
        return etlWiustDataService;
    }


    private static IEtlWiustMpDataService etlWiustMpDataService;

    private static IEtlWiustMpDataService getEtlWiustMpDataService() {
        if (etlWiustMpDataService == null) {
            etlWiustMpDataService = SpringUtil.getBean(IEtlWiustMpDataService.class);
        }
        return etlWiustMpDataService;
    }

    private static IEtlWiuWiustDataService etlWiuWiustDataService;

    private static IEtlWiuWiustDataService getEtlWiuWiustDataService() {
        if (etlWiuWiustDataService == null) {
            etlWiuWiustDataService = SpringUtil.getBean(IEtlWiuWiustDataService.class);
        }
        return etlWiuWiustDataService;
    }


    private static IMonitorPointService monitorPointService;

    private static IMonitorPointService getMonitorPointService() {
        if (monitorPointService == null) {
            monitorPointService = SpringUtil.getBean(IMonitorPointService.class);
        }
        return monitorPointService;
    }

    private static IMonitorPointRelService monitorPointRelService;

    private static IMonitorPointRelService getMonitorPointRelService() {
        if (monitorPointRelService == null) {
            monitorPointRelService = SpringUtil.getBean(IMonitorPointRelService.class);
        }
        return monitorPointRelService;
    }

    private static IMonitorSiteService monitorSiteService;

    private static IMonitorSiteService getMonitorSiteService() {
        if (monitorSiteService == null) {
            monitorSiteService = SpringUtil.getBean(IMonitorSiteService.class);
        }
        return monitorSiteService;
    }

    private static IMonitorSiteRelService monitorSiteRelService;

    private static IMonitorSiteRelService getMonitorSiteRelService() {
        if (monitorSiteRelService == null) {
            monitorSiteRelService = SpringUtil.getBean(IMonitorSiteRelService.class);
        }
        return monitorSiteRelService;
    }

    private static IWaterUseConsumerService waterUseConsumerService;

    private static IWaterUseConsumerService getWaterUseConsumerService() {
        if (waterUseConsumerService == null) {
            waterUseConsumerService = SpringUtil.getBean(IWaterUseConsumerService.class);
        }
        return waterUseConsumerService;
    }

    private static IRtuBaseInfoService rtuBaseInfoService;

    private static IRtuBaseInfoService getRtuBaseInfoService() {
        if (rtuBaseInfoService == null) {
            rtuBaseInfoService = SpringUtil.getBean(IRtuBaseInfoService.class);
        }
        return rtuBaseInfoService;
    }


    public EtlBaseRtuInfoTask() {
    }

    @Override
    public Integer call() {
        try {
            log.info("RTU导入任务开始执行*********************");
            //取水户
            EtlWiuEntity dto = new EtlWiuEntity();
            List<EtlWiuEntity> wiuList = getEtlWiuDataService().selectList(dto);
            for (EtlWiuEntity etlWiuEntity : wiuList) {
                log.info("用水户 {}", etlWiuEntity.getWiuNm());
                WaterUseConsumerInfoEntity waterUseConsumerInfoEntity = new WaterUseConsumerInfoEntity();
                waterUseConsumerInfoEntity.setAddr(etlWiuEntity.getAddr());
                waterUseConsumerInfoEntity.setWiuTp(etlWiuEntity.getWiuTp());
                waterUseConsumerInfoEntity.setMonLevel(etlWiuEntity.getMong());
                waterUseConsumerInfoEntity.setEcoTp(etlWiuEntity.getEcoTp());
                waterUseConsumerInfoEntity.setTradTp(etlWiuEntity.getTradTp());
                waterUseConsumerInfoEntity.setLrNm(etlWiuEntity.getLrNm());
                waterUseConsumerInfoEntity.setWiuNm(etlWiuEntity.getWiuNm());
                waterUseConsumerInfoEntity.setWiuCd(etlWiuEntity.getWiuCd());
                if ("00".equals(etlWiuEntity.getOrgCode())) {
                    waterUseConsumerInfoEntity.setOrgId(1123598813738675201L);
                } else if ("11".equals(etlWiuEntity.getOrgCode())) {
                    waterUseConsumerInfoEntity.setOrgId(1715215595639267330L);
                } else if ("12".equals(etlWiuEntity.getOrgCode())) {
                    waterUseConsumerInfoEntity.setOrgId(1715215690510229506L);
                } else if ("10".equals(etlWiuEntity.getOrgCode())) {
                    waterUseConsumerInfoEntity.setOrgId(1715215884710699010L);
                } else if ("13".equals(etlWiuEntity.getOrgCode())) {
                    waterUseConsumerInfoEntity.setOrgId(1715215952884916226L);
                } else if ("14".equals(etlWiuEntity.getOrgCode())) {
                    waterUseConsumerInfoEntity.setOrgId(1715216007264067585L);
                } else if ("15".equals(etlWiuEntity.getOrgCode())) {
                    waterUseConsumerInfoEntity.setOrgId(1715216066533777409L);
                } else if ("713".equals(etlWiuEntity.getOrgCode())) {
                    waterUseConsumerInfoEntity.setOrgId(1715215779899236354L);
                } else if ("111".equals(etlWiuEntity.getOrgCode())) {
                    waterUseConsumerInfoEntity.setOrgId(1715216136150835201L);
                }

                //waterUseConsumerInfoEntity.setOrgId(Func.toLong(etlWiuEntity.getOrgCode()));
                if (getWaterUseConsumerService().save(waterUseConsumerInfoEntity)) {
                    //取水户关联测站
                    EtlWiuWiustDataEntity wiuRelDto = new EtlWiuWiustDataEntity();
                    wiuRelDto.setWiuCd(etlWiuEntity.getWiuCd());
                    List<EtlWiuWiustDataEntity> wiuRelList = getEtlWiuWiustDataService().selectList(wiuRelDto);
                    for (EtlWiuWiustDataEntity wiuWiustDataEntity : wiuRelList) {
                        //测站
                        EtlWiustEntity etlWiustDto = new EtlWiustEntity();
                        etlWiustDto.setWiustCd(wiuWiustDataEntity.getWiustCd());
                        List<EtlWiustEntity> etlWiustEntityList = getEtlWiustDataService().selectList(etlWiustDto);
                        for (EtlWiustEntity etlWiustEntity : etlWiustEntityList) {

                            log.info("测站 {}", etlWiustEntity.getWiustNm());

                            MonitorSiteInfoEntity monitorSiteInfoEntity = new MonitorSiteInfoEntity();
                            monitorSiteInfoEntity.setWiustCd(etlWiustEntity.getWiustCd());
                            monitorSiteInfoEntity.setWiustNm(etlWiustEntity.getWiustNm());
                            monitorSiteInfoEntity.setWiustTp(etlWiustEntity.getWiustTp());
                            monitorSiteInfoEntity.setLoc(etlWiustEntity.getLoc());
                            monitorSiteInfoEntity.setDatTp(etlWiustEntity.getDatTp());
                            monitorSiteInfoEntity.setWellTp(etlWiustEntity.getWellTp());
                            String tm = etlWiustEntity.getEstStYm() + "01120000";
                            monitorSiteInfoEntity.setEstStYm(Func.parse(tm, ConcurrentDateFormat.of("yyyyMMddHHmmss")));
                            if (getMonitorSiteService().save(monitorSiteInfoEntity)) {

                                RtuInfoEntity rtuInfoEntity = new RtuInfoEntity();
                                rtuInfoEntity.setRtuCode(monitorSiteInfoEntity.getWiustCd());
                                rtuInfoEntity.setAdCode(etlWiuEntity.getOrgCode());
                                rtuInfoEntity.setOrgId(waterUseConsumerInfoEntity.getOrgId());
                                rtuInfoEntity.setRtuName(monitorSiteInfoEntity.getWiustNm());
                                getRtuBaseInfoService().save(rtuInfoEntity);

                                MonitorSiteRelEntity siteRelEntity = new MonitorSiteRelEntity();
                                siteRelEntity.setWiuCd(waterUseConsumerInfoEntity.getWiuCd());
                                siteRelEntity.setWiustCd(monitorSiteInfoEntity.getWiustCd());
                                getMonitorSiteRelService().save(siteRelEntity);

                                //测站关联站点
                                EtlWiustMpDataEntity wiustRelDto = new EtlWiustMpDataEntity();
                                wiustRelDto.setWiustCd(monitorSiteInfoEntity.getWiustCd());
                                List<EtlWiustMpDataEntity> wiustMpRelList = getEtlWiustMpDataService().selectList(wiustRelDto);
                                for (EtlWiustMpDataEntity wiustMpDataEntity : wiustMpRelList) {
                                    //点位
                                    EtlMpEntity mpDto = new EtlMpEntity();
                                    mpDto.setMpCd(wiustMpDataEntity.getMpCd());
                                    List<EtlMpEntity> mpList = getEtlMpDataService().selectList(mpDto);
                                    for (EtlMpEntity mpEntity : mpList) {
                                        log.info("点位 {}", mpEntity.getMpNm());
                                        MonitorPointInfoEntity pointInfoEntity = new MonitorPointInfoEntity();
                                        pointInfoEntity.setMpCd(mpEntity.getMpCd());
                                        pointInfoEntity.setMpNm(mpEntity.getMpNm());
                                        pointInfoEntity.setMpAddr(mpEntity.getMpAddr());
                                        pointInfoEntity.setMpItem(mpEntity.getMpItem());
                                        pointInfoEntity.setIsAuto(mpEntity.getIsAuto());
                                        pointInfoEntity.setSourTp(mpEntity.getSourTp());
                                        pointInfoEntity.setLat(mpEntity.getWeiDu());
                                        pointInfoEntity.setLng(mpEntity.getJingDu());

                                        if (getMonitorPointService().save(pointInfoEntity)) {
                                            MonitorPointRelEntity pointRelEntity = new MonitorPointRelEntity();
                                            pointRelEntity.setMpCd(pointInfoEntity.getMpCd());
                                            pointRelEntity.setWiustCd(monitorSiteInfoEntity.getWiustCd());
                                            getMonitorPointRelService().save(pointRelEntity);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("RTU导入任务异常 {}", e.getMessage());
        }
        return 0;
    }

}
