package com.huatek.busi.task;

import com.huatek.busi.common.BusiConstant;
import com.huatek.busi.common.CurveSubType;
import com.huatek.busi.common.DateUtils;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dao.RptAretMonitorDao;
import com.huatek.busi.dao.TransformerMeteorologicalDao;
import com.huatek.busi.entity.RptAretMonitor;
import com.huatek.busi.entity.TransformerMeteorological;
import com.huatek.busi.service.ArresterAssessService;
import com.huatek.busi.service.BusiEqmWarningService;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.arrester.BusiOfflineArresterBrandService;
import com.huatek.busi.task.transformer.TransformerDateOnlineService;
import com.huatek.busi.util.ArresterCal;
import com.huatek.busi.vo.BusiOfflineArresterBrandVo;
import com.huatek.busi.vo.BusiTreeNodeVo;
import com.huatek.torch.frame.exception.OwlException;
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 javax.script.ScriptException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * 避雷器评估定时任务
 */
@Slf4j
@Component("arresterAssessTask")
public class ArresterAssessTask {
    @Autowired
    private BusiSiteTreeService busiSiteTreeService;
    @Autowired
    private ArresterAssessService arresterAssessService;

    @Autowired
    private BusiOfflineArresterBrandService busiOfflineArresterBrandService;

    @Autowired
    private TransformerMeteorologicalDao transformerMeteorologicalDao;

    @Autowired
    private RptAretMonitorDao rptAretMonitorDao;

    @Autowired
    private TransformerDateOnlineService transformerDateOnlineService;

    /**
     * 避雷器评测任务
     *
     *  x1  绝缘电阻
     *  x2  泄漏电流
     *  x3  阻性电流
     *  x4  容性电流
     *  x5  雷击次数
     *  x6   环境温度
     * @throws IOException
     * @throws ScriptException
     * @throws NoSuchMethodException
     * @throws FileNotFoundException
     * @throws Exception
     */
    public void execute() {
        String  date =DateUtils.parseDateToStr( DateUtils.YYYY_MM_DD,DateUtils.addDays(new Date(),-1));
        log.info("避雷器评测开始时间------------------------------------------------{} start...", date);
        String[] mainEquipments = {SiteConstants.SiteTreeEnum.BLQ_HL_EQM.getValue()};
        List<BusiTreeNodeVo> list = busiSiteTreeService.findMainEquipmentList(mainEquipments);
        for (BusiTreeNodeVo busiTreeNodeVo:list) {
            try {
                BusiOfflineArresterBrandVo busiOfflineArresterBrandInfo = busiOfflineArresterBrandService.getBusiOfflineArresterBrandInfo(busiTreeNodeVo.getSiteId(), busiTreeNodeVo.getId());
                if(busiOfflineArresterBrandInfo.getTransportTime()==null){
                    transformerDateOnlineService.warringEvaluating(BusiConstant.EqType.EQ_TYPE3, BusiConstant.GzType.GZ_TYPE25,"避雷器评测-未设置投运时间",busiTreeNodeVo.getId(),busiTreeNodeVo.getName());
                    log.error("未查到避雷器-未设置投运时间！");
                    continue;
                }
                if(busiOfflineArresterBrandInfo.getInsulationResistance()==null){
                    transformerDateOnlineService.warringEvaluating(BusiConstant.EqType.EQ_TYPE3, BusiConstant.GzType.GZ_TYPE25,"避雷器评测-未设置绝缘电阻",busiTreeNodeVo.getId(),busiTreeNodeVo.getName());
                    log.error("未查到避雷器-未设置绝缘电阻！");
                    continue;
                }
                RptAretMonitor rptAretMonitor = getRptAretMonitor(date,busiTreeNodeVo.getId());
                BigDecimal x1=busiOfflineArresterBrandInfo.getInsulationResistance();
                BigDecimal x2=BigDecimal.ONE;
                BigDecimal x3=BigDecimal.ONE;
                BigDecimal x4=BigDecimal.ONE;
                BigDecimal x5=BigDecimal.ONE;
                List<TransformerMeteorological> transformerMeteorologicals=getTransformerMeteorological(date,busiTreeNodeVo.getId());
                if(CollectionUtils.isEmpty(transformerMeteorologicals)){
                    transformerDateOnlineService.warringEvaluating(BusiConstant.EqType.EQ_TYPE3, BusiConstant.GzType.GZ_TYPE25,"避雷器评测-环境温度无数据",busiTreeNodeVo.getId(),busiTreeNodeVo.getName());
                    log.error("未查到避雷器-环境温度无数据！");
                    continue;
                }
                List<BigDecimal> list1=new ArrayList<>();
                transformerMeteorologicals.stream().forEach(te->{
                    list1.add(new BigDecimal(te.getAirTemperature()));
                });
                BigDecimal x6=Collections.max(list1);
                if(ObjectUtils.isNotEmpty(rptAretMonitor)){
                    x2=compare(rptAretMonitor.getAFullAlc().multiply(rptAretMonitor.getAHindranceAlc()),
                            rptAretMonitor.getBFullAlc().multiply(rptAretMonitor.getBHindranceAlc()),rptAretMonitor.getCFullAlc().multiply(rptAretMonitor.getCHindranceAlc()));
                    x3=compare(rptAretMonitor.getAHindranceAlc(),rptAretMonitor.getBHindranceAlc(),rptAretMonitor.getCHindranceAlc());
                    x4=compare(rptAretMonitor.getACpacitanceAlc(),rptAretMonitor.getBCpacitanceAlc(),rptAretMonitor.getCCpacitanceAlc());
                    x5=rptAretMonitor.getArresterCount();
                }else{
                    transformerDateOnlineService.warringEvaluating(BusiConstant.EqType.EQ_TYPE3, BusiConstant.GzType.GZ_TYPE25,"避雷器评测-实时绝缘监测无数据",busiTreeNodeVo.getId(),busiTreeNodeVo.getName());
                    log.error("未查到避雷器-实时绝缘监测无数据！");
                   continue;
                }

                BigDecimal w= ArresterCal.calW(busiOfflineArresterBrandInfo.getTransportTime());
                BigDecimal β=ArresterCal.calHi1(busiOfflineArresterBrandInfo.getTransportTime(),x5.intValue());
                        arresterAssessService.assessmentForArrester(busiTreeNodeVo.getSiteId(), busiTreeNodeVo.getId(),"2",
                        x1,x2,x3,x4,x5,x6,w,β);

            } catch (Exception e) {
                log.error("避雷器评测出错【站点：{};设备Code：{}】,错误信息如下:{}", busiTreeNodeVo.getSiteId(), busiTreeNodeVo.getId(),e.getMessage());
                log.error(e.getMessage(), e);
            }
        }
        log.info("{} end!", date);
    }


    /**
     * 避雷器-界面实时监测基础信息
     * @param date
     * @param mainTreeId
     * @return
     */
    private  RptAretMonitor getRptAretMonitor(String  date,String mainTreeId ){

        RptAretMonitor rptAretMonitor = rptAretMonitorDao.get(new WherePrams()
                        .where("main_tree_id",mainTreeId)
                .and("create_time","like",date)
                );

        return rptAretMonitor;
    }

    /**
     * 环境温度
     * @param date
     * @param mainTreeId
     * @return
     */
    private  List<TransformerMeteorological> getTransformerMeteorological(String  date, String mainTreeId ){
        List<TransformerMeteorological> transformerMeteorologicals = transformerMeteorologicalDao.list(new WherePrams()
                        .where("main_tree_id", mainTreeId)
                .and("time_stamp", "like", date));

        return transformerMeteorologicals;
    }

    /**
     * 比较大小 --大值
     * 运用三目运算符
     * @param a
     * @param b
     * @param c
     * @return
     */
    private  static BigDecimal compare(BigDecimal a ,BigDecimal b,BigDecimal c){

        BigDecimal max=BigDecimal.ZERO;
            max=a.compareTo(b) > -1? a:b;
            max=max.compareTo(b) > -1 ?max:c;
            return max;
    }

    /**
     * 比较大小 -小值
     * 运用三目运算符
     * @param a
     * @param b
     * @param c
     * @return
     */
    private  static BigDecimal compareSmall(BigDecimal a ,BigDecimal b,BigDecimal c){

        BigDecimal min=BigDecimal.ZERO;
        min=a.compareTo(b)< 1 ? a:b;
        min=min.compareTo(b)< 1 ? min:c;
        return min;
    }

    public static void main(String[] args) {
        List<BigDecimal> tt=new ArrayList<>();
        tt.add(new BigDecimal(12));
        tt.add(new BigDecimal(23));
        tt.add(new BigDecimal(66));
        System.out.println(Collections.max(tt));
    }
}
