package com.gyee.generation.task.thread;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.model.StringUtils;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.*;
import com.gyee.generation.service.auto.IProEconWtAlysisDayService;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.SnowflakeGenerator;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import com.gyee.generation.util.statisticcs.Initial;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.Callable;

public class EconWtAlysisDayThreadPool implements Callable<String>, Serializable {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final long serialVersionUID = 0;

    // 保存任务所需要的数据

    private Object threadPoolTaskData;
    private ProBasicEquipment wt;
    private Map<String, ProEconEquipmentInfoDay1> eqday1map;
    private Map<String, ProEconEquipmentInfoDay2> eqday2map;
    private Map<String, ProEconEquipmentInfoDay3> eqday3map;
    private Map<String, ProEconEquipmentInfoDay4> eqday4map;
    private Date recordDate;
    private Date end;

    private Date begin;
    private List<ProEconWtAlysisDay> dayls;
    private IEdosUtil edosUtil;
    private Calendar c;

    private IProEconWtAlysisDayService proEconWtAlysisDayService;

    public EconWtAlysisDayThreadPool(Object tasks, ProBasicEquipment wt, Map<String, ProEconEquipmentInfoDay1> eqday1map,
                                     Map<String, ProEconEquipmentInfoDay2> eqday2map,
                                     Map<String, ProEconEquipmentInfoDay3> eqday3map,
                                     Map<String, ProEconEquipmentInfoDay4> eqday4map,
                                     Date recordDate, Date end, Date begin, List<ProEconWtAlysisDay> dayls,
                                     IEdosUtil edosUtil, Calendar c, IProEconWtAlysisDayService proEconWtAlysisDayService
    ) {

        this.threadPoolTaskData = tasks;
        this.wt = wt;
        this.eqday1map = eqday1map;
        this.eqday2map = eqday2map;
        this.eqday3map = eqday3map;
        this.eqday4map = eqday4map;
        this.recordDate = recordDate;
        this.end = end;
        this.begin = begin;
        this.dayls = dayls;
        this.edosUtil = edosUtil;
        this.c = c;
        this.proEconWtAlysisDayService = proEconWtAlysisDayService;
    }

    public synchronized String call() throws Exception {


        // 处理一个任务，这里的处理方式太简单了，仅仅是一个打印语句

        //System.out.println("开始执行任务：" + threadPoolTaskData);

        String result = "";

        // //便于观察，等待一段时间

        try {
            ProEconWtAlysisDay pewp = new ProEconWtAlysisDay();
            Initial.initial(pewp);
            pewp.setId(SnowflakeGenerator.generateId());
            pewp.setRecordDate(DateUtils.truncate(recordDate));
            pewp.setWindturbineId(wt.getId());

            if (eqday1map.containsKey(wt.getId())) {
                ProEconEquipmentInfoDay1 dayinfo = eqday1map.get(wt.getId());

                pewp.setRfdl(dayinfo.getRfdl());
                pewp.setRpjfs(dayinfo.getRpjfs());
                pewp.setRpjgl(dayinfo.getRpjgl());
                pewp.setRyfdl(dayinfo.getRllfdl());


                double gzss = dayinfo.getRgzssdl();
                double jxss = dayinfo.getRjxssdl();
                double xnss = dayinfo.getRxnssdl();
                double xdss = dayinfo.getRxdtjssdl() + dayinfo.getRxdjclssdl();
                double slss = dayinfo.getRcwsltqssdl() + dayinfo.getRcwsldwssdl();

                double ssdl = gzss + jxss + xnss + xdss + slss;
                pewp.setRssdl(StringUtils.round(ssdl, 2));

                pewp.setYfdl(dayinfo.getYfdl());
                pewp.setYpjfs(dayinfo.getYpjfs());
                pewp.setYpjgl(dayinfo.getYpjgl());
                pewp.setYyfdl(dayinfo.getYllfdl());


                gzss = dayinfo.getYgzssdl();
                jxss = dayinfo.getYjxssdl();
                xnss = dayinfo.getYxnssdl();
                xdss = dayinfo.getYxdtjssdl() + dayinfo.getYxdjclssdl();
                slss = dayinfo.getYcwsltqssdl() + dayinfo.getYcwsldwssdl();

                ssdl = gzss + jxss + xnss + xdss + slss;
                pewp.setYssdl(StringUtils.round(ssdl, 2));

                pewp.setNfdl(dayinfo.getNfdl());
                pewp.setNpjfs(dayinfo.getNpjfs());
                pewp.setNpjgl(dayinfo.getNpjgl());
                pewp.setNyfdl(dayinfo.getNllfdl());


                gzss = dayinfo.getNgzssdl();
                jxss = dayinfo.getNjxssdl();
                xnss = dayinfo.getNxnssdl();
                xdss = dayinfo.getNxdtjssdl() + dayinfo.getNxdjclssdl();
                slss = dayinfo.getNcwsltqssdl() + dayinfo.getNcwsldwssdl();

                ssdl = gzss + jxss + xnss + xdss + slss;
                pewp.setNssdl(StringUtils.round(ssdl, 2));

            }


            if (eqday2map.containsKey(wt.getId())) {
                ProEconEquipmentInfoDay2 dayinfo = eqday2map.get(wt.getId());
                pewp.setRyxxs(dayinfo.getRyxxs());
                pewp.setRgzxs(dayinfo.getRgztjxs());
                pewp.setRjxxs(dayinfo.getRjxtjxs());
                pewp.setRzdxs(dayinfo.getRxdxs());
                pewp.setRzdxs(dayinfo.getRtxzdxs());

                int dayTimes = 24;
                double sbklyl = new BigDecimal(dayTimes - dayinfo.getRgztjxs()).divide(new BigDecimal(dayTimes), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();//风机平均设备可利用率
                pewp.setRsbklyl(StringUtils.round(sbklyl, 2));

                pewp.setYyxxs(dayinfo.getYyxxs());
                pewp.setYgzxs(dayinfo.getYgztjxs());
                pewp.setYjxxs(dayinfo.getYjxtjxs());
                pewp.setYzdxs(dayinfo.getYxdxs());
                pewp.setYzdxs(dayinfo.getYtxzdxs());
                c.setTime(recordDate);
                int days = c.get(Calendar.DAY_OF_MONTH);
                dayTimes = 24 * days;
                sbklyl = new BigDecimal(dayTimes - dayinfo.getYgztjxs()).divide(new BigDecimal(dayTimes), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();//风机平均设备可利用率
                pewp.setYsbklyl(StringUtils.round(sbklyl, 2));

                pewp.setNyxxs(dayinfo.getNyxxs());
                pewp.setNgzxs(dayinfo.getNgztjxs());
                pewp.setNjxxs(dayinfo.getNjxtjxs());
                pewp.setNzdxs(dayinfo.getNxdxs());
                pewp.setNzdxs(dayinfo.getNtxzdxs());

                days = c.get(Calendar.DAY_OF_YEAR);
                dayTimes = 24 * days;
                sbklyl = new BigDecimal(dayTimes - dayinfo.getNgztjxs()).divide(new BigDecimal(dayTimes), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();//风机平均设备可利用率
                pewp.setNsbklyl(StringUtils.round(sbklyl, 2));
            }

            if (eqday3map.containsKey(wt.getId())) {
                ProEconEquipmentInfoDay3 dayinfo = eqday3map.get(wt.getId());
                pewp.setRtjcs(dayinfo.getRgztjcs() + dayinfo.getRjxtjcs());
                pewp.setYtjcs(dayinfo.getYgztjcs() + dayinfo.getYjxtjcs());
                pewp.setNtjcs(dayinfo.getNgztjcs() + dayinfo.getNjxtjcs());

            }

            if (eqday4map.containsKey(wt.getId())) {
                ProEconEquipmentInfoDay4 dayinfo = eqday4map.get(wt.getId());
                pewp.setRnhyd(dayinfo.getRnhyd());
                pewp.setRsjbz(dayinfo.getRqxpcbbzsj());
                pewp.setRyxfs(dayinfo.getRyxfss());

                pewp.setYnhyd(dayinfo.getYnhyd());
                pewp.setYsjbz(dayinfo.getYqxpcbbzsj());
                pewp.setYyxfs(dayinfo.getYyxfss());

                pewp.setNnhyd(dayinfo.getNnhyd());
                pewp.setNsjbz(dayinfo.getNqxpcbbzsj());
                pewp.setNyxfs(dayinfo.getNyxfss());

            }
            //昨日的统计结果
            List<ProEconWtAlysisDay> pepidls = new ArrayList<>();
            Calendar cl = Calendar.getInstance();
            cl.setTime(recordDate);
            cl.add(Calendar.DAY_OF_MONTH, -1);
            if (cl.get(Calendar.DAY_OF_MONTH) != 1) {
                QueryWrapper<ProEconWtAlysisDay> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("record_date", DateUtils.truncate(cl.getTime())).eq("windTurbine_Id", wt.getId());
                pepidls = proEconWtAlysisDayService.list(queryWrapper2);
                //                        .stream()
                //                        .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(cl.getTime())) == 0
                //                                && i.getWindturbineId().equals(wt.getId())
                //                        )
                //                        .collect(Collectors.toList());
            }
            calSimple(pewp, end, begin, wt, pepidls);
            dayls.add(pewp);


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        threadPoolTaskData = null;

        return result;

    }

    private void calSimple(ProEconWtAlysisDay pewp, Date end, Date begin, ProBasicEquipment wt, List<ProEconWtAlysisDay> pepidls) throws Exception {

        //*******************************************日信息统计*********************************************************/

        double lastState = -1;//上一分钟状态


        double bbhcs = 0;//不饱和次数

        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;


        Map<String, ProBasicEquipmentPoint> aimap = wtpAimap.get(wt.getId());
        if (aimap.containsKey(ContantXk.SSQFZT)) {

            ProBasicEquipmentPoint point = aimap.get(ContantXk.SSQFZT);
            //按照分钟时间进行统计状态快照值
            List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
            if (!pointls.isEmpty()) {
                for (PointData po : pointls) {

                    if (po.getPointValueInDouble() >= 3) {
                        if (lastState != po.getPointValueInDouble()) {

                            //将当前状态保存到上一分钟状态
                            lastState = po.getPointValueInDouble();
                            //实时欠发大于等于3的次数加1
                            bbhcs++;
                        }

                    }

                }

            }

        }
        //日不饱和次数
        pewp.setRbbhcs(bbhcs);

        //*******************************************日信息统计*********************************************************/

        //*******************************************月信息统计*********************************************************/
        if (pepidls.isEmpty()) {
            pewp.setYbbhcs(pewp.getRbbhcs());

        } else {
            ProEconWtAlysisDay pepid = pepidls.get(0);

            pewp.setYbbhcs(StringUtils.round(pepid.getYbbhcs() + pewp.getRbbhcs(), 2));

        }
        //*******************************************月信息统计*********************************************************/

        //*******************************************年信息统计*********************************************************/
        if (pepidls.isEmpty()) {
            pewp.setNbbhcs(pewp.getRbbhcs());

        } else {


            ProEconWtAlysisDay pepid = pepidls.get(0);
            pewp.setNbbhcs(StringUtils.round(pepid.getNbbhcs() + pewp.getRbbhcs(), 2));

        }
        //*******************************************年信息统计*********************************************************/
    }

}