package com.huatek.busi.task.cablecurrent;

import com.huatek.busi.common.DateUtils;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dao.CwzzMonitoringStatisticsDao;
import com.huatek.busi.dao.SubstationMicBlqDao;
import com.huatek.busi.dao.SubstationMicGisDao;
import com.huatek.busi.dao.SubstationMicStatisticsDao;
import com.huatek.busi.dto.cablecurrent.CableCurrentResultDto;
import com.huatek.busi.entity.*;
import com.huatek.busi.mapper.IecBydworkParamMapper;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.cablecurrent.ICableCurrentResultService;
import com.huatek.busi.task.base.AbstractBusiTaskExecutor;
import com.huatek.busi.util.CableCurrentCal;
import com.huatek.busi.vo.BusiTreeNodeVo;
import com.huatek.torch.sql.beans.WherePrams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: cablecurrent 电缆载流能力
 * @FullClassPath: CableCurrentTask
 * @Description: 中标任务
 * @author: vent
 * @date:
 * @version: 1.0
 */
@Slf4j
@Component("cableCurrentTask")
public class CableCurrentTask extends AbstractBusiTaskExecutor {

    @Autowired
    private ICableCurrentResultService cableCurrentResultService;

    @Autowired
    private IecBydworkParamMapper iecBydworkParamMapper;

    @Autowired
    private BusiSiteTreeService busiSiteTreeService;
    @Autowired
    private CwzzMonitoringStatisticsDao cwzzMonitoringStatisticsDao;

    @Autowired
    private SubstationMicGisDao substationMicGisDao;

    @Autowired
    private SubstationMicStatisticsDao substationMicStatisticsDao;


    @Autowired
    private SubstationMicBlqDao substationMicBlqDao;


    long nd = 1000 * 24 * 60 * 60;

    long nh = 1000 * 60 * 60;

    long nm = 1000 * 60;


    @Override
    public void execute() {

        String day = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.addDays(new Date(), -1));
        log.info("电缆载流能力与敷设环境 开始------------------------------------------------{} start...", day);
        String[] mainEquipments = {SiteConstants.SiteTreeEnum.DL_EQM.getValue()};
        List<BusiTreeNodeVo> list = busiSiteTreeService.findMainEquipmentList(mainEquipments);
        if(CollectionUtils.isEmpty(list)){
            log.info("没有电缆数据");
        }

        List<IecBydworkParam> iecBydworkParamWorksCal = iecBydworkParamMapper.getIecBydworkParamWorksCal("2", day);
        List<IecBydworkParam> iecBydworkParamWorksCal1 = iecBydworkParamMapper.getIecBydworkParamWorksCal("1", day);
        for (BusiTreeNodeVo busiTreeNodeVo : list) {
            CwzzMonitoringStatistics cwzzMonitoringStatistics = cwzzMonitoringStatisticsDao.get(new WherePrams()
                    .where("main_tree_id", busiTreeNodeVo.getId())
                    .and("d_time", " like", day));
            if(ObjectUtils.isEmpty(cwzzMonitoringStatistics)){
                log.info("没有查到电缆光纤测温温度");
                continue;
            }
            BigDecimal airTemperature=null;
            List<SubstationMicGis> substationMicGiss = substationMicGisDao.list(new WherePrams()
                    .where("d_time", " like", day)
                    .orderBy("air_temperature desc"));
            if(CollectionUtils.isEmpty(substationMicGiss)){
                List<SubstationMicStatistics> substationMicStatisticss = substationMicStatisticsDao.list(new WherePrams()
                     .where("d_time", " like", day)
                        .orderBy("air_temperature desc"));
                if(CollectionUtils.isEmpty(substationMicStatisticss)){
                    List<SubstationMicBlq> substationMicBlqs = substationMicBlqDao.list(new WherePrams()
                            .where("d_time", " like", day)
                            .orderBy("air_temperature desc"));
                    if(CollectionUtils.isEmpty(substationMicBlqs)){
                        log.info("没有查到电缆环境温度");
                        continue;
                    }else{
                        airTemperature = substationMicBlqs.get(0).getAirTemperature();
                    }
                }else{
                    airTemperature = substationMicStatisticss.get(0).getAirTemperature();
                }
            }else{
                 airTemperature = substationMicGiss.get(0).getAirTemperature();
            }

            BigDecimal avgTmp = cwzzMonitoringStatistics.getAvgTmp();
            //抽水
            CableCurrentResultDto build = build(busiTreeNodeVo.getName(),avgTmp, airTemperature, "1",calTime(iecBydworkParamWorksCal));
            cableCurrentResultService.calculateByInputItemDate("",build);
            //发电

            CableCurrentResultDto build1 = build(busiTreeNodeVo.getName(),avgTmp, airTemperature, "0", calTime(iecBydworkParamWorksCal1));
            cableCurrentResultService.calculateByInputItemDate("",build1);
        }


        log.info("{} end!", day);
    }

    public BigDecimal calTime(List<IecBydworkParam> iecBydworkParamWorksCal){
        if(CollectionUtils.isEmpty(iecBydworkParamWorksCal)){
            return BigDecimal.ZERO;
        }
        Long timefiff=0L;
        for (IecBydworkParam iecBydworkParam:iecBydworkParamWorksCal) {
            timefiff+= iecBydworkParam.getStopTime().getTime()-iecBydworkParam.getStartTime().getTime();
        }
        BigDecimal decimal = BigDecimal.valueOf(timefiff).divide(BigDecimal.valueOf(nh), 2, BigDecimal.ROUND_HALF_UP);
        return decimal;
    }

    /**
     * 组装数据
     * @param meaTemp
     * @param ambientTemp
     * @param flag
     * @param chichang
     * @return
     */
    public  CableCurrentResultDto build(String name,BigDecimal meaTemp,BigDecimal ambientTemp,String flag ,BigDecimal chichang){
        CableCurrentResultDto cableCurrentResultDto =new CableCurrentResultDto();
        cableCurrentResultDto.setFlag(flag);
        cableCurrentResultDto.setName(name);
        if("0".equals(flag)){
            cableCurrentResultDto.setElectricPowerDuration(chichang);
        }else{
            cableCurrentResultDto.setPumpingDuration(chichang);
        }
        cableCurrentResultDto.setCablesNumber(new BigDecimal(1));
        cableCurrentResultDto.setAmbientTemp(ambientTemp);
        cableCurrentResultDto.setMeaTemp(meaTemp);
        cableCurrentResultDto.setConductivity(CableCurrentCal.ρ);
        cableCurrentResultDto.setDiameter(CableCurrentCal.Diameter);
        cableCurrentResultDto.setDielectricConstant(CableCurrentCal.e);
        cableCurrentResultDto.setDielectricLossFactor(CableCurrentCal.DielectricLossFactor);
        cableCurrentResultDto.setExternalDiameterCC(CableCurrentCal.ExternalDiameterCC);
        cableCurrentResultDto.setExternalDiameterCI(CableCurrentCal.ExternalDiameterC);
        cableCurrentResultDto.setExternalDiameterDC(CableCurrentCal.ExternalDiameterDC);
        cableCurrentResultDto.setInsulationThick(CableCurrentCal.InsulationThick);
        cableCurrentResultDto.setRatedVoltage(CableCurrentCal.RatedVoltage);
        cableCurrentResultDto.setResistanceTempCoefficient(CableCurrentCal.ResistanceTempCoefficient);
        cableCurrentResultDto.setT2(CableCurrentCal.t2);
        cableCurrentResultDto.setType("2");
        cableCurrentResultDto.setT3(CableCurrentCal.t3);
        cableCurrentResultDto.setT4(CableCurrentCal.t4);
        return cableCurrentResultDto;
    }

}