package com.gyee.frame.service.contrast;

import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.BenchMarkVo;
import com.gyee.frame.model.custom.ConditionVo;
import com.gyee.frame.model.custom.ShutdowneventVo;
import com.gyee.frame.service.FaultEliminateDefectsService;
import com.gyee.frame.service.ShutdowneventService;
import com.gyee.frame.service.StatetransitionratesService;
import com.gyee.frame.service.Windpowerinfoday3Service;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;


@Service
public class ContrastZjService {
    @Resource
    private Windpowerinfoday3Service windpowerinfoday3Service;
    @Resource
    private FaultEliminateDefectsService faultEliminateDefectsService;
    @Resource
    private StatetransitionratesService statetransitionratesService;
    @Resource
    private ShutdowneventService shutdowneventService;



    public void addBenchmarkZj(HttpServletRequest request,  String wpId, String year, String month, String zjs) {


        String pattern = "yyyy-MM-dd";
        if (StringUtils.notEmp(wpId) && StringUtils.notEmp(zjs)) {
            Map<String, Windpowerstation> wpmap = InitialRunner.wpmap;
            List<ConditionVo> ctvos = new ArrayList<ConditionVo>();

            if (zjs.indexOf(",") > 0) {
                String[] strs = zjs.split(",");

                for (String str : strs) {
                    BenchMarkVo bm = new BenchMarkVo();
                    if (wpmap.containsKey(wpId)) {
                        Windpowerstation wp = wpmap.get(wpId);
                        bm.setWpId(wp.getId());
                        bm.setWpName(wp.getName());
                    }
                    intialBenchMarkTemp(bm);

                    Date beginDate=new Date();
                    Date endDate=new Date();
                    // 获得开始结束时间
                    int yearnumber = Integer.valueOf(year);
                    int monthnumber = Integer.valueOf(month)-1;


                    Calendar cal = Calendar.getInstance();

                    cal.set(Calendar.YEAR,yearnumber);
                    cal.set(Calendar.MONTH,monthnumber);

                    int maxday = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

                    StringBuilder sb = new StringBuilder();


                    switch (str) {
                        case "1":
                            sb.append(year).append("-").append(month).append("-").append("01");
                            beginDate = StringUtils.toDate(String.valueOf(sb), pattern);
                            sb.setLength(0);
                            sb.append(year).append("-").append(month).append("-").append("10");
                            endDate = StringUtils.toDate(String.valueOf(sb), pattern);

                            bm.setZjDate(String.valueOf(sb));

                            break;
                        case "2":

                            sb.append(year).append("-").append(month).append("-").append("11");
                            beginDate = StringUtils.toDate(String.valueOf(sb), pattern);
                            sb.setLength(0);
                            sb.append(year).append("-").append(month).append("-").append("20");
                            endDate = StringUtils.toDate(String.valueOf(sb), pattern);

                            bm.setZjDate(String.valueOf(sb));
                            break;
                        case "3":

                            sb.append(year).append("-").append(month).append("-").append("21");
                            beginDate = StringUtils.toDate(String.valueOf(sb), pattern);
                            sb.setLength(0);
                            sb.append(year).append("-").append(month).append("-").append(maxday);
                            endDate = StringUtils.toDate(String.valueOf(sb), pattern);

                            bm.setZjDate(String.valueOf(sb));
                            break;
                        default:
                            break;
                    }

                    ConditionVo vo = new ConditionVo();
                    vo.setBeginDate(DateUtils.format(beginDate,"yyyy-MM-dd"));
                    vo.setEndDate(DateUtils.format(endDate,"yyyy-MM-dd"));
                    vo.setWpId(wpId);
                    ctvos.add(vo);

                    setBenchMarkInfo(bm,wpId, beginDate, endDate);// 统计值际数据
                    setBenchMark(bm, ctvos, str,request);
                }
            } else {
                BenchMarkVo bm = new BenchMarkVo();
                if (wpmap.containsKey(wpId)) {
                    Windpowerstation wp = wpmap.get(wpId);
                    bm.setWpId(wp.getId());
                    bm.setWpName(wp.getName());
                }
                intialBenchMarkTemp(bm);
                Date beginDate=new Date();
                Date endDate=new Date();
               // 获得开始结束时间
                int yearnumber = Integer.valueOf(year);
                int monthnumber = Integer.valueOf(month)-1;


                Calendar cal = Calendar.getInstance();

                cal.set(Calendar.YEAR,yearnumber);
                cal.set(Calendar.MONTH,monthnumber);

                int maxday = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

                StringBuilder sb = new StringBuilder();


                switch (zjs) {
                    case "1":
                        sb.append(year).append("-").append(month).append("-").append("01");
                        beginDate = StringUtils.toDate(String.valueOf(sb), pattern);
                        sb.setLength(0);
                        sb.append(year).append("-").append(month).append("-").append("10");
                        endDate = StringUtils.toDate(String.valueOf(sb), pattern);

                        bm.setZjDate(String.valueOf(sb));

                        break;
                    case "2":

                        sb.append(year).append("-").append(month).append("-").append("11");
                        beginDate = StringUtils.toDate(String.valueOf(sb), pattern);
                        sb.setLength(0);
                        sb.append(year).append("-").append(month).append("-").append("20");
                        endDate = StringUtils.toDate(String.valueOf(sb), pattern);

                        bm.setZjDate(String.valueOf(sb));
                        break;
                    case "3":

                        sb.append(year).append("-").append(month).append("-").append("21");
                        beginDate = StringUtils.toDate(String.valueOf(sb), pattern);
                        sb.setLength(0);
                        sb.append(year).append("-").append(month).append("-").append(maxday);
                        endDate = StringUtils.toDate(String.valueOf(sb), pattern);

                        bm.setZjDate(String.valueOf(sb));
                        break;
                    default:
                        break;
                }
                ConditionVo vo = new ConditionVo();
                vo.setBeginDate(DateUtils.format(beginDate,"yyyy-MM-dd"));
                vo.setEndDate(DateUtils.format(endDate,"yyyy-MM-dd"));
                vo.setWpId(wpId);
                ctvos.add(vo);

                setBenchMarkInfo(bm,wpId, beginDate, endDate);// 统计值际数据
                setBenchMark(bm, ctvos, zjs,request);
            }

        }

    }


    private void setBenchMark(BenchMarkVo bm, List<ConditionVo> ctvos, String str,HttpServletRequest request) {
        totalBenchmarkInfo(bm);// 统计值际计算指标

        bm.setCid(StringUtils.getUUID());
        List<BenchMarkVo> vos=null;


        switch (str) {
            case "1":
                bm.setZjName("一值");
                break;
            case "2":
                bm.setZjName("二值");
                break;
            case "3":
                bm.setZjName("三值");
                break;
            default:
                break;
        }

        if (null != request.getSession().getAttribute("bmvos")) {
            vos = (List<BenchMarkVo>) request.getSession().getAttribute("bmvos");
        } else {
            vos = new ArrayList<BenchMarkVo>();
        }
        vos.add(bm);

        request.getSession().setAttribute("bmvos", vos);

        Map<String, List<ConditionVo>> ctmap = null;
        if (null != request.getSession().getAttribute("bmwtvos")) {
            ctmap = (Map<String, List<ConditionVo>>) request.getSession().getAttribute("bmwtvos");

            ctmap.put(bm.getCid(), ctvos);
        } else {
            ctmap = new HashMap<String, List<ConditionVo>>();

            ctmap.put(bm.getCid(), ctvos);
        }

        request.getSession().setAttribute("bmwtvos", ctmap);
    }

    private void totalBenchmarkInfo(BenchMarkVo bm) {
        // double scadafdl = bm.getScadafdl(); // scada发电量
        // double qfss = bm.getQfss();// 欠发损失发电量
        // double gzss = bm.getGzss();// 故障损失发电量
        // double whss = bm.getWhss();// 维护损失发电量
        // double xdss = bm.getXdss();// 限电损失发电量

        double scadafdl = null != bm.getScadafdl() ? (new BigDecimal(bm.getScadafdl()).divide(new BigDecimal(1), 2, RoundingMode.HALF_UP).doubleValue()) : 0; // scada发电量
        double qfss = null != bm.getQfss() ? (new BigDecimal(bm.getQfss()).divide(new BigDecimal(1), 2, RoundingMode.HALF_UP).doubleValue()) : 0;// 欠发损失发电量
        double gzss = null != bm.getGzss() ? (new BigDecimal(bm.getGzss()).divide(new BigDecimal(1), 2, RoundingMode.HALF_UP).doubleValue()) : 0;// 故障损失发电量
        double whss = null != bm.getWhss() ? (new BigDecimal(bm.getWhss()).divide(new BigDecimal(1), 2, RoundingMode.HALF_UP).doubleValue()) : 0;// 维护损失发电量
        double xdss = null != bm.getXdss() ? (new BigDecimal(bm.getXdss()).divide(new BigDecimal(1), 2, RoundingMode.HALF_UP).doubleValue()) : 0;// 限电损失发电量

        double llfdl = scadafdl + qfss + gzss + whss + xdss;// 理论发电量

        if (llfdl != 0) {
            double temp = xdss;

            BigDecimal b1 = new BigDecimal(temp);
            BigDecimal b2 = new BigDecimal(llfdl);

            temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
            // temp 弃风率
            bm.setQfl(temp);
            temp = qfss;

            b1 = new BigDecimal(temp);
            b2 = new BigDecimal(llfdl);

            temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
            // temp 性能损失率
            bm.setXnssl(temp);

            temp = gzss;

            b1 = new BigDecimal(temp);
            b2 = new BigDecimal(llfdl);

            temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
            // temp 故障损失率
            bm.setGzssl(temp);

            temp = whss;

            b1 = new BigDecimal(temp);
            b2 = new BigDecimal(llfdl);

            temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
            // temp 检修损失率
            bm.setJxssl(temp);
            temp = 0;

            b1 = new BigDecimal(temp);
            b2 = new BigDecimal(llfdl);

            temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
            // temp 受累损失率
            bm.setSlssl(temp);
            temp = scadafdl;

            b1 = new BigDecimal(temp);
            b2 = new BigDecimal(llfdl);

            temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
            // temp 风能利用率
            bm.setFnlyl(temp);
            // 受累损失电量
            bm.setSlssl(0.0);
        } else {
            // temp 风能利用率
            bm.setFnlyl(0.0);
            // temp 故障损失率
            bm.setGzssl(0.0);
            // temp 检修损失率
            bm.setJxssl(0.0);
            // temp 弃风率
            bm.setQfl(0.0);
            // temp 受累损失率
            bm.setSlssl(0.0);
            // 受累损失电量
            bm.setSlssl(0.0);
            // temp 性能损失率
            bm.setXnssl(0.0);
        }

        if (bm.getFwzcs() != 0) {

            double temp1 = bm.getFwcs();// 5分钟复位及时次数
            double temp2 = bm.getFwzcs();// 总复位次数

            double temp = 0;

            BigDecimal b1 = new BigDecimal(temp1);
            BigDecimal b2 = new BigDecimal(temp2);

            temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
            // temp 复位及时率
            bm.setFwjsl(temp);
        } else {
            bm.setFwjsl(100.0);
        }

        if (bm.getZtzcs() != 0) {

            double temp1 = bm.getZtcs();// 小于3米5分钟转换及时次数
            double temp2 = bm.getZtzcs();// 小于3米状态转换总次数

            double temp = 0;

            BigDecimal b1 = new BigDecimal(temp1);
            BigDecimal b2 = new BigDecimal(temp2);

            temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();

            // temp 状态转换及时率
            bm.setZtzhjsl(temp);
        } else {
            bm.setZtzhjsl(100.0);
        }

        if (bm.getXqzcs() != 0) {

            double temp1 = bm.getXqcs();// 消缺及时次数
            double temp2 = bm.getXqzcs();// 故障总次数

            double temp = 0;

            BigDecimal b1 = new BigDecimal(temp1);
            BigDecimal b2 = new BigDecimal(temp2);

            temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();

            // temp 消缺及时率
            bm.setXqjsl(temp);
        } else {
            bm.setXqjsl(100.0);
        }
    }



    private void intialBenchMarkTemp(BenchMarkVo bm) {
        bm.setScadafdl(0.0);
        bm.setGzss(0.0);
        bm.setWhss(0.0);
        bm.setQfss(0.0);
        bm.setXdss(0.0);
        bm.setSlss(0.0);
        bm.setFwcs(0);
        bm.setFwzcs(0);
        bm.setZtcs(0);
        bm.setZtzcs(0);
        bm.setXqcs(0);
        bm.setXqzcs(0);
        bm.setGzcs(0);
        bm.setFjts(0);
    }


    private void setBenchMarkInfo(BenchMarkVo bm,String wpId, Date beginDate, Date endDate) {
        // 获得复位及时率、状态转换及时率数据
        Map<String, Statetransitionrates> stmap = statetransitionratesService.getStatetransitionratesByWpTotal(wpId, beginDate, endDate);
        // 获得消缺及时率数据
        Map<String, FaultEliminateDefects> fdmap = faultEliminateDefectsService.getFaultEliminateDefectsByWpTotal(wpId, beginDate, endDate);

        int gzcs = 0;// 故障次数
        int fjts = 0;// 风机台数
        double gzcsradio = 0.0;// 故障率

        Map<String, List<Windturbine>> wtsmap = InitialRunner.wp_wtmap;
        if (wtsmap.containsKey(wpId)) {
            List<Windturbine> wtls = wtsmap.get(wpId);
            fjts = wtls.size();
            // 获得故障次数集合
            List<ShutdowneventVo> sels = shutdowneventService.queryEvents(wpId, null, beginDate, endDate, "2");
            int daynum = DateUtils.daysDiff(beginDate, endDate);
            gzcs = sels.size();

            bm.setGzcs(bm.getGzcs() + gzcs);
            bm.setFjts(bm.getFjts() + fjts * daynum);
            // bm.setFjts(bm.getFjts() + fjts);
            if (bm.getFjts() != 0) {
                gzcsradio = new BigDecimal(bm.getGzcs()).divide(new BigDecimal(bm.getFjts()), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
                if (gzcsradio > 100.0) {
                    gzcsradio = 100.0;
                }
                bm.setGzcsradio(gzcsradio);
            }

        }


        List<Windpowerinfoday3> ls = windpowerinfoday3Service.getWindpowerinfoday3(wpId, beginDate, endDate,"windpower");

        Windpowerinfoday3 w = new Windpowerinfoday3();
        w.setDaynhxdssdl(0.0);
        w.setDaynhgzssdl(0.0);
        w.setDaynhwhssdl(0.0);
        w.setDaynhqfdl(0.0);
        w.setDaydl2(0.0);

        if (!ls.isEmpty()) {
            for (Windpowerinfoday3 wd : ls) {

                w.setWindpowerstationid(wd.getWindpowerstationid());
                double losspower1 = null != wd.getDaynhxdssdl() ? wd.getDaynhxdssdl() : 0.0;
                double losspower2 = null != wd.getDaynhgzssdl() ? wd.getDaynhgzssdl() : 0.0;
                double losspower3 = null != wd.getDaynhwhssdl() ? wd.getDaynhwhssdl() : 0.0;
                double losspower4 = null != wd.getDaynhqfdl() ? wd.getDaynhqfdl() : 0.0;
                double scadafdl = null != wd.getDaydl2() ? wd.getDaydl2() : 0; // scada发电量

                w.setDaynhxdssdl(w.getDaynhxdssdl() + losspower1);
                w.setDaynhgzssdl(w.getDaynhgzssdl() + losspower2);
                w.setDaynhwhssdl(w.getDaynhwhssdl() + losspower3);
                w.setDaynhqfdl(w.getDaynhqfdl() + losspower4);
                w.setDaydl2(w.getDaydl2() + scadafdl);
            }
        }

        bm.setScadafdl(bm.getScadafdl() + (new BigDecimal(w.getDaydl2()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()));
        bm.setGzss(bm.getGzss() + (new BigDecimal(w.getDaynhgzssdl()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()));
        bm.setWhss(bm.getWhss() + (new BigDecimal(w.getDaynhwhssdl()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()));
        bm.setQfss(bm.getQfss() + (new BigDecimal(w.getDaynhqfdl()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()));
        bm.setXdss(bm.getXdss() + (new BigDecimal(w.getDaynhxdssdl()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()));
        if (stmap.containsKey(wpId)) {
            Statetransitionrates st = stmap.get(wpId);

            Double temp1 = st.getTimerate1();// 小于3米5分钟转换及时次数
            Double temp2 = st.getTimerate13();// 小于3米状态转换总次数
            bm.setZtcs(bm.getZtcs() + temp1.intValue());
            bm.setZtzcs(bm.getZtzcs() + temp2.intValue());
            temp1 = st.getTimerate16();// 5分钟复位及时次数
            temp2 = st.getTimerate20();// 总复位次数
            bm.setFwcs(bm.getFwcs() + temp1.intValue());
            bm.setFwzcs(bm.getFwzcs() + temp2.intValue());
        }
        if (fdmap.containsKey(wpId)) {
            FaultEliminateDefects fd = fdmap.get(wpId);

            Integer temp1 = fd.getRightcount();// 消缺及时次数
            Integer temp2 = fd.getFaultcount();// 故障次数

            bm.setXqcs(bm.getXqcs() + temp1.intValue());
            bm.setXqzcs(bm.getXqzcs() + temp2.intValue());

        }
    }
}
