package com.gyee.generation.service;//package com.gyee.generation.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.model.StringUtils;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.*;
import com.gyee.generation.service.auto.IProEconEquipmentInfoDay1Service;
import com.gyee.generation.service.auto.IProEconEquipmentInfoDay2Service;
import com.gyee.generation.service.auto.IProEconEquipmentInfoDay4Service;
import com.gyee.generation.service.auto.IProEconEquipmentInfoDayTopService;
import com.gyee.generation.util.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class EquipmentInfoDayTopService {

    //    private static final Logger logger = LoggerFactory.getLogger(EquipmentInfoDayTopService.class);

    @Resource
    private IProEconEquipmentInfoDay1Service proEconEquipmentInfoDay1Service;
    @Resource
    private IProEconEquipmentInfoDay2Service proEconEquipmentInfoDay2Service;

    @Resource
    private IProEconEquipmentInfoDay4Service proEconEquipmentInfoDay4Service;

    @Resource
    private IProEconEquipmentInfoDayTopService proEconEquipmentInfoDayTopService;

    public void calEquipmentInfoDayTop(Date recordDate) throws Exception {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);

        //        QueryWrapper<ProEconEquipmentInfoDayTop> queryWrapper = new QueryWrapper<>();
        //        queryWrapper.ge("record_date",DateUtils.truncate(recordDate)).eq("types",1);
        //        //判断是否有重复记录，先删除重复记录
        //        List<Long> idls = proEconEquipmentInfoDayTopService.list(queryWrapper).stream()
        //                .map(ProEconEquipmentInfoDayTop::getId)
        //                .collect(Collectors.toList());
        //
        //        List<Long> tempids=new ArrayList<>();
        //
        //        for(int i=0;i<idls.size();i++)
        //        {
        //            tempids.add(idls.get(i));
        //            if(tempids.size()==1000)
        //            {
        //                proEconEquipmentInfoDayTopService.removeByIds(tempids);
        //                tempids=new ArrayList<>();
        //            }
        //        }
        //
        //        if(!tempids.isEmpty())
        //        {
        //            proEconEquipmentInfoDayTopService.removeByIds(tempids);
        //        }


        for (ProBasicPowerstation wp : CacheContext.wpls) {

            if (wp.getId().contains("GDC")) {
                continue;
            }

            List<ProEconEquipmentInfoDayTop> topls = new ArrayList<>();

            for (ProBasicEquipment wt : CacheContext.wpwtmap.get(wp.getId())) {

                ProEconEquipmentInfoDayTop peeidt = getEquipmentInfoDayTop(recordDate, c, wt);

                topls.add(peeidt);

            }


            List<String> filedls = initialDayFiled();
            //遍历循环排序，将序号赋值给rank，再通过风机编号转为，MAP
            orderObject(topls, filedls);


            for (ProEconEquipmentInfoDayTop top : topls) {

                calLevels(top, topls.size());

            }

            topls = topls.stream()
                    .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDayScore).reversed()).collect(Collectors.toList());

            for (int i = 0; i < topls.size(); i++) {
                ProEconEquipmentInfoDayTop top = topls.get(i);
                top.setDayRank(i + 1);
            }

            QueryWrapper<ProEconEquipmentInfoDayTop> queryWrapper = new QueryWrapper<>();
            queryWrapper.ge("record_date", DateUtils.truncate(recordDate)).eq("types", 1).eq("windpowerstation_id", wp.getId());
            //判断是否有重复记录，先删除重复记录
            List<Long> idls = proEconEquipmentInfoDayTopService.list(queryWrapper).stream()
                    .map(ProEconEquipmentInfoDayTop::getId)
                    .collect(Collectors.toList());

            List<Long> tempids = new ArrayList<>();

            for (int i = 0; i < idls.size(); i++) {
                tempids.add(idls.get(i));
                if (tempids.size() == 1000) {
                    proEconEquipmentInfoDayTopService.removeByIds(tempids);
                    tempids = new ArrayList<>();
                }
            }

            if (!tempids.isEmpty()) {
                proEconEquipmentInfoDayTopService.removeByIds(tempids);
            }


            List<ProEconEquipmentInfoDayTop> templs = new ArrayList<>();
            for (ProEconEquipmentInfoDayTop vo : topls) {
                templs.add(vo);
                if (templs.size() == 1000) {
                    proEconEquipmentInfoDayTopService.saveBatch(templs);
                    templs = new ArrayList<>();
                }
            }

            if (!templs.isEmpty()) {
                proEconEquipmentInfoDayTopService.saveBatch(templs);
            }
            //            proEconEquipmentInfoDayTopService.saveBatch(topls);
        }

    }


    public void calEquipmentInfoMonthTop(Date recordDate) throws Exception {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);


        for (ProBasicPowerstation wp : CacheContext.wpls) {

            List<ProEconEquipmentInfoDayTop> topls = new ArrayList<>();

            for (ProBasicEquipment wt : CacheContext.wpwtmap.get(wp.getId())) {

                ProEconEquipmentInfoDayTop peeidt = getEquipmentInfoMonthTop(recordDate, c, wt);

                topls.add(peeidt);

            }


            List<String> filedls = initialMonthFiled();
            //遍历循环排序，将序号赋值给rank，再通过风机编号转为，MAP
            orderObject(topls, filedls);


            for (ProEconEquipmentInfoDayTop top : topls) {

                calLevels(top, topls.size());
            }

            topls = topls.stream()
                    .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDayScore).reversed()).collect(Collectors.toList());

            for (int i = 0; i < topls.size(); i++) {
                ProEconEquipmentInfoDayTop top = topls.get(i);
                top.setDayRank(i + 1);
            }


            //判断是否有重复记录，先删除重复记录
            QueryWrapper<ProEconEquipmentInfoDayTop> queryWrapper = new QueryWrapper<>();
            queryWrapper.ge("record_date", DateUtils.truncate(recordDate)).eq("types", 2).eq("windpowerstation_id", wp.getId());
            List<Long> idls = proEconEquipmentInfoDayTopService.list(queryWrapper).stream()
                    //                .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
                    //                        && i.getTypes() ==2
                    //                        && CacheContext.wtmap.containsKey(i.getWindturbineId()))
                    .map(ProEconEquipmentInfoDayTop::getId)
                    .collect(Collectors.toList());

            //        if (idls.size() > 0) {
            //
            //            proEconEquipmentInfoDayTopService.removeByIds(idls);
            //        }

            List<Long> tempids = new ArrayList<>();

            for (int i = 0; i < idls.size(); i++) {
                tempids.add(idls.get(i));
                if (tempids.size() == 1000) {
                    proEconEquipmentInfoDayTopService.removeByIds(tempids);
                    tempids = new ArrayList<>();
                }
            }

            if (!tempids.isEmpty()) {
                proEconEquipmentInfoDayTopService.removeByIds(tempids);
            }

            List<ProEconEquipmentInfoDayTop> templs = new ArrayList<>();
            for (ProEconEquipmentInfoDayTop vo : topls) {
                templs.add(vo);
                if (templs.size() == 1000) {
                    proEconEquipmentInfoDayTopService.saveBatch(templs);
                    templs = new ArrayList<>();
                }
            }

            if (!templs.isEmpty()) {
                proEconEquipmentInfoDayTopService.saveBatch(templs);
            }
            //            proEconEquipmentInfoDayTopService.saveBatch(topls);
        }


    }

    public void calEquipmentInfoYearTop(Date recordDate) throws Exception {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);


        for (ProBasicPowerstation wp : CacheContext.wpls) {

            List<ProEconEquipmentInfoDayTop> topls = new ArrayList<>();

            for (ProBasicEquipment wt : CacheContext.wpwtmap.get(wp.getId())) {

                ProEconEquipmentInfoDayTop peeidt = getEquipmentInfoYearTop(recordDate, c, wt);

                topls.add(peeidt);

            }


            List<String> filedls = initialYearFiled();
            //遍历循环排序，将序号赋值给rank，再通过风机编号转为，MAP
            orderObject(topls, filedls);


            for (ProEconEquipmentInfoDayTop top : topls) {

                calLevels(top, topls.size());
            }

            topls = topls.stream()
                    .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDayScore).reversed()).collect(Collectors.toList());

            for (int i = 0; i < topls.size(); i++) {
                ProEconEquipmentInfoDayTop top = topls.get(i);
                top.setDayRank(i + 1);
            }


            //判断是否有重复记录，先删除重复记录
            QueryWrapper<ProEconEquipmentInfoDayTop> queryWrapper = new QueryWrapper<>();
            queryWrapper.ge("record_date", DateUtils.truncate(recordDate)).eq("types", 3).eq("windpowerstation_id", wp.getId());
            List<Long> idls = proEconEquipmentInfoDayTopService.list(queryWrapper).stream()
                    //                .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
                    //                        && i.getTypes() ==3
                    //                        && CacheContext.wtmap.containsKey(i.getWindturbineId()))
                    .map(ProEconEquipmentInfoDayTop::getId)
                    .collect(Collectors.toList());
            //
            //        if (idls.size() > 0) {
            //
            //            proEconEquipmentInfoDayTopService.removeByIds(idls);
            //        }
            List<Long> tempids = new ArrayList<>();

            for (int i = 0; i < idls.size(); i++) {
                tempids.add(idls.get(i));
                if (tempids.size() == 1000) {
                    proEconEquipmentInfoDayTopService.removeByIds(tempids);
                    tempids = new ArrayList<>();
                }
            }

            if (!tempids.isEmpty()) {
                proEconEquipmentInfoDayTopService.removeByIds(tempids);
            }

            List<ProEconEquipmentInfoDayTop> templs = new ArrayList<>();
            for (ProEconEquipmentInfoDayTop vo : topls) {
                templs.add(vo);
                if (templs.size() == 1000) {
                    proEconEquipmentInfoDayTopService.saveBatch(templs);
                    templs = new ArrayList<>();
                }
            }

            if (!templs.isEmpty()) {
                proEconEquipmentInfoDayTopService.saveBatch(templs);
            }
            //            proEconEquipmentInfoDayTopService.saveBatch(topls);
        }


    }

    private ProEconEquipmentInfoDayTop getEquipmentInfoDayTop(Date recordDate, Calendar c, ProBasicEquipment wt) {
        ProEconEquipmentInfoDayTop peeidt = new ProEconEquipmentInfoDayTop();

        peeidt.setWindpowerstationId(wt.getWindpowerstationId());
        peeidt.setTypes(1);
        peeidt.setLineId(wt.getLineId());
        peeidt.setProjectId(wt.getProjectId());
        peeidt.setWindturbineId(wt.getId());
        peeidt.setRecordDate(DateUtils.truncate(recordDate));
        peeidt.setYear(DateUtils.getYear(recordDate));
        peeidt.setMonth(DateUtils.getMonth(recordDate) + 1);
        initial(peeidt);
        QueryWrapper<ProEconEquipmentInfoDay1> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("record_date", DateUtils.truncate(recordDate)).eq("windturbine_id", wt.getId());
        List<ProEconEquipmentInfoDay1> pepid1ls = proEconEquipmentInfoDay1Service.list(queryWrapper);

        if (!pepid1ls.isEmpty()) {
            ProEconEquipmentInfoDay1 pepid1 = pepid1ls.get(0);
            //发电量
            peeidt.setDayfdl(pepid1.getRfdl());

            //理论发电量
            peeidt.setDayllfdl(pepid1.getRllfdl());

            //平均风速
            peeidt.setDayfs(pepid1.getRpjfs());

            //平均功率
            peeidt.setDaygl(pepid1.getRpjgl());

            //故障损失电量
            peeidt.setDaygzssdl(pepid1.getRgzssdl());

            //限电损失电量
            peeidt.setDayxdssdl(pepid1.getRxdtjssdl());

            //维护损失电量
            peeidt.setDaywhssdl(pepid1.getRjxssdl());

            //性能损失电量
            peeidt.setDayxnssdl(pepid1.getRqxjclssdl());

        }

        QueryWrapper<ProEconEquipmentInfoDay2> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ge("record_date", DateUtils.truncate(recordDate)).eq("windturbine_id", wt.getId());
        List<ProEconEquipmentInfoDay2> pepid2ls = proEconEquipmentInfoDay2Service.list(queryWrapper2);


        if (!pepid2ls.isEmpty()) {
            ProEconEquipmentInfoDay2 pepid2 = pepid2ls.get(0);
            //故障时间
            peeidt.setDaygzsj(pepid2.getRgzxsmx());

            //维护时间
            peeidt.setDaywhsj(pepid2.getRjxxsmx());

            //运行时间
            peeidt.setDayyxsj(pepid2.getRyxxs());

            //停机时间
            peeidt.setDaytjsj(pepid2.getRgzxsmx());

            //利用小时数

            double defaultpower = 1500.0;
            if (CacheContext.modelMap.containsKey(wt.getModelId())) {
                defaultpower = CacheContext.modelMap.get(wt.getModelId()).getPowerProduction();
            }

            peeidt.setDaylyxs(BigDecimal.valueOf(peeidt.getDayfdl()).divide(new BigDecimal(defaultpower), 2, RoundingMode.HALF_EVEN).doubleValue());


            //设备可利用率
            int dayhours = 24;


            //            double temp = StringUtils.round(((dayhours - peeidt.getDaygzsj()/dayhours)*100), 2);
            double temp = new BigDecimal(dayhours - peeidt.getDaygzsj()).divide(new BigDecimal(dayhours), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
            peeidt.setDaysbklyl(temp);


            //等效可用系数
            dayhours = 24;
            //            temp = StringUtils.round(((dayhours - peeidt.getDaygzsj() - peeidt.getDaywhsj())/dayhours)*100, 2);
            temp = new BigDecimal(dayhours - peeidt.getDaygzsj() - peeidt.getDaywhsj()).divide(new BigDecimal(dayhours), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
            peeidt.setDaydxkyxs(temp);


        }
        QueryWrapper<ProEconEquipmentInfoDay4> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.ge("record_date", DateUtils.truncate(recordDate)).eq("windturbine_id", wt.getId());
        List<ProEconEquipmentInfoDay4> pepid4ls = proEconEquipmentInfoDay4Service.list(queryWrapper4);


        if (!pepid4ls.isEmpty()) {
            ProEconEquipmentInfoDay4 pepid4 = pepid4ls.get(0);
            //拟合优度
            peeidt.setDaynhyd(pepid4.getRnhyd());

            //有效风时数
            peeidt.setDayyxfss(pepid4.getRyxfss());

            //小风切入
            peeidt.setDayxfqr(pepid4.getRxfqrfs());

            //静风频率

            int dayhours = 24;
            double temp = BigDecimal.valueOf(pepid4.getRjfsc()).divide(new BigDecimal(dayhours), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
            peeidt.setDayjfpl(temp);


            //功率一致性系数
            peeidt.setDayglyzxxs(pepid4.getRglyzxxs());

        }
        return peeidt;
    }

    private ProEconEquipmentInfoDayTop getEquipmentInfoMonthTop(Date recordDate, Calendar c, ProBasicEquipment wt) {
        ProEconEquipmentInfoDayTop peeidt = new ProEconEquipmentInfoDayTop();
        peeidt.setTypes(2);
        peeidt.setWindpowerstationId(wt.getWindpowerstationId());
        peeidt.setLineId(wt.getLineId());
        peeidt.setProjectId(wt.getProjectId());
        peeidt.setWindturbineId(wt.getId());
        peeidt.setRecordDate(DateUtils.truncate(recordDate));
        peeidt.setYear(DateUtils.getYear(recordDate));
        peeidt.setMonth(DateUtils.getMonth(recordDate) + 1);
        initial(peeidt);

        Calendar cal = Calendar.getInstance();
        cal.setTime(recordDate);
        int days = cal.get(Calendar.DAY_OF_MONTH);

        QueryWrapper<ProEconEquipmentInfoDay1> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("record_date", DateUtils.truncate(recordDate)).eq("windturbine_id", wt.getId());
        List<ProEconEquipmentInfoDay1> pepid1ls = proEconEquipmentInfoDay1Service.list(queryWrapper);


        if (!pepid1ls.isEmpty()) {
            ProEconEquipmentInfoDay1 pepid1 = pepid1ls.get(0);
            //发电量
            peeidt.setDayfdl(pepid1.getYfdl());

            //理论发电量
            peeidt.setDayllfdl(pepid1.getYllfdl());

            //平均风速
            peeidt.setDayfs(pepid1.getYpjfs());

            //平均功率
            peeidt.setDaygl(pepid1.getYpjgl());

            //故障损失电量
            peeidt.setDaygzssdl(pepid1.getYgzssdl());

            //限电损失电量
            peeidt.setDayxdssdl(pepid1.getYxdtjssdl());

            //维护损失电量
            peeidt.setDaywhssdl(pepid1.getYjxssdl());

            //性能损失电量
            peeidt.setDayxnssdl(pepid1.getYqxjclssdl());

        }

        QueryWrapper<ProEconEquipmentInfoDay2> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ge("record_date", DateUtils.truncate(recordDate)).eq("windturbine_id", wt.getId());
        List<ProEconEquipmentInfoDay2> pepid2ls = proEconEquipmentInfoDay2Service.list(queryWrapper2);

        if (!pepid2ls.isEmpty()) {
            ProEconEquipmentInfoDay2 pepid2 = pepid2ls.get(0);
            //故障时间
            peeidt.setDaygzsj(pepid2.getYgzxsmx());

            //维护时间
            peeidt.setDaywhsj(pepid2.getYjxxsmx());

            //运行时间
            peeidt.setDayyxsj(pepid2.getYyxxs());

            //停机时间
            peeidt.setDaytjsj(pepid2.getYgzxsmx());

            //利用小时数

            double defaultpower = 1500.0;
            if (CacheContext.modelMap.containsKey(wt.getModelId())) {
                defaultpower = CacheContext.modelMap.get(wt.getModelId()).getPowerProduction();
            }

            peeidt.setDaylyxs(BigDecimal.valueOf(peeidt.getDayfdl()).divide(new BigDecimal(defaultpower), 2, RoundingMode.HALF_EVEN).doubleValue());


            //设备可利用率
            int dayhours = 24 * days;
            //            double temp = StringUtils.round(((dayhours - peeidt.getDaygzsj())/dayhours)*100, 2);
            double temp = dayhours - peeidt.getDaygzsj();
            temp = new BigDecimal(temp).divide(new BigDecimal(dayhours), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

            peeidt.setDaysbklyl(temp);


            //等效可用系数
            dayhours = 24 * days;
            //            temp = StringUtils.round(((dayhours - peeidt.getDaygzsj() - peeidt.getDaywhsj())/dayhours)*100, 2);
            temp = new BigDecimal(dayhours - peeidt.getDaygzsj() - peeidt.getDaywhsj()).divide(new BigDecimal(dayhours), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

            peeidt.setDaydxkyxs(temp);


        }
        QueryWrapper<ProEconEquipmentInfoDay4> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.ge("record_date", DateUtils.truncate(recordDate)).eq("windturbine_id", wt.getId());
        List<ProEconEquipmentInfoDay4> pepid4ls = proEconEquipmentInfoDay4Service.list(queryWrapper4);

        if (!pepid4ls.isEmpty()) {
            ProEconEquipmentInfoDay4 pepid4 = pepid4ls.get(0);
            //拟合优度
            peeidt.setDaynhyd(pepid4.getYnhyd());

            //有效风时数
            peeidt.setDayyxfss(pepid4.getYyxfss());

            //小风切入
            peeidt.setDayxfqr(pepid4.getYxfqrfs());

            //静风频率

            int dayhours = 24 * days;
            double temp = BigDecimal.valueOf(pepid4.getYjfsc()).divide(new BigDecimal(dayhours), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
            peeidt.setDayjfpl(temp);


            //功率一致性系数
            peeidt.setDayglyzxxs(pepid4.getYglyzxxs());

        }
        return peeidt;
    }

    private ProEconEquipmentInfoDayTop getEquipmentInfoYearTop(Date recordDate, Calendar c, ProBasicEquipment wt) {
        ProEconEquipmentInfoDayTop peeidt = new ProEconEquipmentInfoDayTop();
        peeidt.setTypes(3);
        peeidt.setWindpowerstationId(wt.getWindpowerstationId());
        peeidt.setLineId(wt.getLineId());
        peeidt.setProjectId(wt.getProjectId());
        peeidt.setWindturbineId(wt.getId());
        peeidt.setRecordDate(DateUtils.truncate(recordDate));
        peeidt.setYear(DateUtils.getYear(recordDate));
        peeidt.setMonth(DateUtils.getMonth(recordDate) + 1);
        initial(peeidt);

        Calendar cal = Calendar.getInstance();
        cal.setTime(recordDate);
        int days = cal.get(Calendar.DAY_OF_YEAR);

        QueryWrapper<ProEconEquipmentInfoDay1> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("record_date", DateUtils.truncate(recordDate)).eq("windturbine_id", wt.getId());

        List<ProEconEquipmentInfoDay1> pepid1ls = proEconEquipmentInfoDay1Service.list(queryWrapper);


        if (!pepid1ls.isEmpty()) {
            ProEconEquipmentInfoDay1 pepid1 = pepid1ls.get(0);
            //发电量
            peeidt.setDayfdl(pepid1.getNfdl());

            //理论发电量
            peeidt.setDayllfdl(pepid1.getNllfdl());

            //平均风速
            peeidt.setDayfs(pepid1.getNpjfs());

            //平均功率
            peeidt.setDaygl(pepid1.getNpjgl());

            //故障损失电量
            peeidt.setDaygzssdl(pepid1.getNgzssdl());

            //限电损失电量
            peeidt.setDayxdssdl(pepid1.getNxdtjssdl());

            //维护损失电量
            peeidt.setDaywhssdl(pepid1.getNjxssdl());

            //性能损失电量
            peeidt.setDayxnssdl(pepid1.getNqxjclssdl());

        }

        QueryWrapper<ProEconEquipmentInfoDay2> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ge("record_date", DateUtils.truncate(recordDate)).eq("windturbine_id", wt.getId());
        List<ProEconEquipmentInfoDay2> pepid2ls = proEconEquipmentInfoDay2Service.list(queryWrapper2);

        if (!pepid2ls.isEmpty()) {
            ProEconEquipmentInfoDay2 pepid2 = pepid2ls.get(0);
            //故障时间
            peeidt.setDaygzsj(pepid2.getNgzxsmx());

            //维护时间
            peeidt.setDaywhsj(pepid2.getNjxxsmx());

            //运行时间
            peeidt.setDayyxsj(pepid2.getNyxxs());

            //停机时间
            peeidt.setDaytjsj(pepid2.getNgzxsmx());

            //利用小时数

            double defaultpower = 1500.0;
            if (CacheContext.modelMap.containsKey(wt.getModelId())) {
                defaultpower = CacheContext.modelMap.get(wt.getModelId()).getPowerProduction();
            }

            peeidt.setDaylyxs(BigDecimal.valueOf(peeidt.getDayfdl()).divide(new BigDecimal(defaultpower), 2, RoundingMode.HALF_EVEN).doubleValue());


            //            //设备可利用率
            //            int dayhours = 24*days;
            //            double temp = StringUtils.round(((dayhours - peeidt.getDaygzsj())/dayhours)*100, 2);
            //            peeidt.setDaysbklyl(temp);
            //
            //
            //
            //            //等效可用系数
            //            dayhours = 24*days;
            //            temp = StringUtils.round(((dayhours - peeidt.getDaygzsj() - peeidt.getDaywhsj())/dayhours)*100, 2);
            //            peeidt.setDaydxkyxs(temp);

            //设备可利用率
            int dayhours = 24 * days;
            //            double temp = StringUtils.round(((dayhours - peeidt.getDaygzsj())/dayhours)*100, 2);
            double temp = new BigDecimal(dayhours - peeidt.getDaygzsj()).divide(new BigDecimal(dayhours), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

            peeidt.setDaysbklyl(temp);


            //等效可用系数
            dayhours = 24 * days;
            //            temp = StringUtils.round(((dayhours - peeidt.getDaygzsj() - peeidt.getDaywhsj())/dayhours)*100, 2);
            temp = new BigDecimal(dayhours - peeidt.getDaygzsj() - peeidt.getDaywhsj()).divide(new BigDecimal(dayhours), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

            peeidt.setDaydxkyxs(temp);


        }
        QueryWrapper<ProEconEquipmentInfoDay4> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.ge("record_date", DateUtils.truncate(recordDate)).eq("windturbine_id", wt.getId());
        List<ProEconEquipmentInfoDay4> pepid4ls = proEconEquipmentInfoDay4Service.list(queryWrapper4);


        if (!pepid4ls.isEmpty()) {
            ProEconEquipmentInfoDay4 pepid4 = pepid4ls.get(0);
            //拟合优度
            peeidt.setDaynhyd(pepid4.getNnhyd());

            //有效风时数
            peeidt.setDayyxfss(pepid4.getNyxfss());

            //小风切入
            peeidt.setDayxfqr(pepid4.getNxfqrfs());

            //静风频率
            int dayhours = 24 * days;
            double temp = BigDecimal.valueOf(pepid4.getNjfsc()).divide(new BigDecimal(dayhours), 4, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
            peeidt.setDayjfpl(temp);


            //功率一致性系数
            peeidt.setDayglyzxxs(pepid4.getNglyzxxs());

        }
        return peeidt;
    }

    private void orderObject(List<ProEconEquipmentInfoDayTop> topls, List<String> filedls) {


        //遍历循环排序，将序号赋值给rank，再通过风机编号转为，MAP
        for (String str : filedls) {


            List<ProEconEquipmentInfoDayTop> ls = new ArrayList<>();


            switch (str) {
                case "dayfs":
                case "monthfs":
                case "yearfs":
                    ls = topls.stream()
                            .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDayfs).reversed()).collect(Collectors.toList());
                    for (int i = 0; i < ls.size(); i++) {
                        ProEconEquipmentInfoDayTop po = ls.get(i);
                        po.setMonthfs((double) (i + 1));
                    }
                    break;
                case "dayxnssdl":
                case "monthxnssdl":
                case "yearxnssdl":
                    ls = topls.stream()
                            .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDayxnssdl)).collect(Collectors.toList());
                    for (int i = 0; i < ls.size(); i++) {
                        ProEconEquipmentInfoDayTop po = ls.get(i);
                        po.setMonthxnssdl((double) (i + 1));
                    }
                    break;

                case "daylyxs":
                case "monthlyxs":
                case "yearlyxs":
                    ls = topls.stream()
                            .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDaylyxs).reversed()).collect(Collectors.toList());
                    for (int i = 0; i < ls.size(); i++) {
                        ProEconEquipmentInfoDayTop po = ls.get(i);
                        po.setMonthlyxs((double) (i + 1));
                    }
                    break;
                case "daynhyd":
                case "monthnhyd":
                case "yearnhyd":
                    ls = topls.stream()
                            .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDaynhyd)).collect(Collectors.toList());
                    for (int i = 0; i < ls.size(); i++) {
                        ProEconEquipmentInfoDayTop po = ls.get(i);
                        po.setMonthnhyd((double) (i + 1));
                    }
                    break;
                case "daysbklyl":
                case "monthsbklyl":
                case "yearsbklyl":
                    ls = topls.stream()
                            .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDaysbklyl).reversed()).collect(Collectors.toList());
                    for (int i = 0; i < ls.size(); i++) {
                        ProEconEquipmentInfoDayTop po = ls.get(i);
                        po.setMonthsbklyl((double) (i + 1));
                    }
                    break;
                case "daydxkyxs":
                case "monthdxkyxs":
                case "yeardxkyxs":
                    ls = topls.stream()
                            .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDaydxkyxs).reversed()).collect(Collectors.toList());
                    for (int i = 0; i < ls.size(); i++) {
                        ProEconEquipmentInfoDayTop po = ls.get(i);
                        po.setMonthdxkyxs((double) (i + 1));
                    }
                    break;
                case "dayyxfss":
                case "monthyxfss":
                case "yearyxfss":
                    ls = topls.stream()
                            .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDayyxfss).reversed()).collect(Collectors.toList());
                    for (int i = 0; i < ls.size(); i++) {
                        ProEconEquipmentInfoDayTop po = ls.get(i);
                        po.setMonthyxfss((double) (i + 1));
                    }
                    break;
                case "dayxfqr":
                case "monthxfqr":
                case "yearxfqr":
                    ls = topls.stream()
                            .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDayxfqr)).collect(Collectors.toList());
                    for (int i = 0; i < ls.size(); i++) {
                        ProEconEquipmentInfoDayTop po = ls.get(i);
                        po.setMonthxfqr((double) (i + 1));
                    }
                    break;
                case "dayjfpl":
                case "monthjfpl":
                case "yearjfpl":
                    ls = topls.stream()
                            .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDayjfpl)).collect(Collectors.toList());
                    for (int i = 0; i < ls.size(); i++) {
                        ProEconEquipmentInfoDayTop po = ls.get(i);
                        po.setMonthjfpl((double) (i + 1));
                    }
                    break;
                case "dayglyzxxs":
                case "monthglyzxxs":
                case "yearglyzxxs":
                    ls = topls.stream()
                            .sorted(Comparator.comparing(ProEconEquipmentInfoDayTop::getDayglyzxxs)).collect(Collectors.toList());
                    for (int i = 0; i < ls.size(); i++) {
                        ProEconEquipmentInfoDayTop po = ls.get(i);
                        po.setMonthglyzxxs((double) (i + 1));
                    }
                    break;
                default:

            }


        }


    }

    private void calLevels(ProEconEquipmentInfoDayTop top, Integer size) {
        double m = 10;
        double d = m / (double) size;
        double temp = 0;

        //切入风速
        double xfqrValue = 0;
        //性能损失电量
        double xnssdlValue = 0;
        //拟合优度
        double nhydValue = 0;
        //功率一致性系数
        double glyzxxsValue = 0;
        //利用小时
        double lyxsValue = 0;
        //设备可利用率
        double sbklylValue = 0;
        //等效可利用系数
        double dxkyxsValue = 0;
        //有效风时数
        double yxfssValue = 0;
        //风速
        double fsValue = 0;
        //静风频率
        double jfplValue = 0;

        //切入风速
        double value = 0.0;
        value = top.getDayxfqr();

        if (value >= 5) {
            xfqrValue = 0;
        } else if (value <= 3.5) {
            xfqrValue = m;
        } else {

            temp = d * (size - top.getMonthxfqr());

            xfqrValue = StringUtils.round(temp, 0);
            top.setYearxfqr(xfqrValue);
        }
        //性能损失电量
        value = 0.0;
        value = top.getDayxnssdl();
        if (value >= top.getDayfdl()) {
            xnssdlValue = 0;
        } else if (value <= 0) {
            xnssdlValue = m;
        } else {
            temp = d * (size - top.getMonthxnssdl());
            xnssdlValue = StringUtils.round(temp, 0);
            top.setYearxnssdl(xnssdlValue);

        }

        value = 0.0;
        value = top.getDaynhyd();
        //拟合优度
        if (value <= 15) {
            nhydValue = 0;
        } else if (value >= 80) {
            nhydValue = m;
        } else {
            temp = d * (size - top.getMonthnhyd());
            nhydValue = StringUtils.round(temp, 0);
            top.setYearnhyd(nhydValue);
        }


        //功率一致性系数
        value = 0.0;
        value = top.getDayglyzxxs();

        if (value <= 15) {
            glyzxxsValue = 0;
        } else if (value >= 80) {
            glyzxxsValue = m;
        } else {
            temp = d * (size - top.getMonthglyzxxs());
            glyzxxsValue = StringUtils.round(temp, 0);
            top.setYearglyzxxs(glyzxxsValue);
        }

        //利用小时
        value = 0.0;
        value = top.getDaylyxs();

        if (value <= 0) {
            lyxsValue = 0;
        } else {
            temp = d * (size - top.getMonthlyxs());
            lyxsValue = StringUtils.round(temp, 0);
            top.setYearlyxs(lyxsValue);
        }


        //设备可利用率
        value = 0.0;
        value = top.getDaysbklyl();

        if (value < 90) {
            sbklylValue = 0;
        } else if (value >= 99.8) {
            sbklylValue = m;
        } else {
            temp = d * (size - top.getMonthsbklyl());
            sbklylValue = StringUtils.round(temp, 0);
            top.setYearsbklyl(sbklylValue);
        }


        //等效可用系数
        value = 0.0;
        value = top.getDaydxkyxs();

        if (value < 90) {
            dxkyxsValue = 0;
        } else if (value >= 99.8) {
            dxkyxsValue = m;
        } else {
            temp = d * (size - top.getMonthdxkyxs());
            dxkyxsValue = StringUtils.round(temp, 0);
            top.setYeardxkyxs(dxkyxsValue);
        }


        //有效风时数
        value = 0.0;
        value = top.getDayyxfss();

        if (value <= 0) {
            yxfssValue = 0;
        } else {
            temp = d * (size - top.getMonthyxfss());
            yxfssValue = StringUtils.round(temp, 0);
            top.setYearyxfss(yxfssValue);
        }

        //平均风速
        value = 0.0;
        value = top.getDayfs();

        if (value <= 0) {
            fsValue = 0;
        } else {
            temp = d * (size - top.getMonthfs());
            fsValue = StringUtils.round(temp, 0);
            top.setYearfs(fsValue);
        }

        //静风频率
        value = 0.0;
        value = top.getDayjfpl();

        if (value <= 0) {
            jfplValue = 0;
        } else {
            temp = d * (size - top.getMonthjfpl());
            top.setYearjfpl(temp);
            jfplValue = StringUtils.round(temp, 0);
        }

        Double total = xfqrValue + xnssdlValue + nhydValue + glyzxxsValue + lyxsValue + sbklylValue + dxkyxsValue + yxfssValue + fsValue + jfplValue;

        if (total >= 95) {

            top.setDayLevel("AAA");
            top.setDayScore(total);
        } else if (total >= 85) {
            top.setDayLevel("AA");
            top.setDayScore(total);
        } else if (total >= 80) {

            top.setDayLevel("A");
            top.setDayScore(total);
        } else if (total >= 75) {
            top.setDayLevel("BBB");
            top.setDayScore(total);
        } else if (total >= 65) {
            top.setDayLevel("BB");
            top.setDayScore(total);

        } else if (total >= 60) {
            top.setDayLevel("B");
            top.setDayScore(total);
        } else if (total >= 40) {
            top.setDayLevel("C");
            top.setDayScore(total);

        } else {
            top.setDayLevel("C-");
            top.setDayScore(total);
        }
    }


    private List<String> initialDayFiled() {
        List<String> filedls = new ArrayList<>();


        filedls.add("dayfs");


        filedls.add("dayxnssdl");


        filedls.add("daylyxs");


        filedls.add("daynhyd");


        filedls.add("daysbklyl");


        filedls.add("daydxkyxs");


        filedls.add("dayyxfss");


        filedls.add("dayxfqr");


        filedls.add("dayjfpl");


        filedls.add("dayglyzxxs");


        return filedls;
    }

    private List<String> initialMonthFiled() {
        List<String> filedls = new ArrayList<>();


        filedls.add("monthfs");


        filedls.add("monthxnssdl");


        filedls.add("monthlyxs");


        filedls.add("monthnhyd");


        filedls.add("monthsbklyl");


        filedls.add("monthdxkyxs");


        filedls.add("monthyxfss");


        filedls.add("monthxfqr");


        filedls.add("monthjfpl");


        filedls.add("monthglyzxxs");


        return filedls;
    }

    private List<String> initialYearFiled() {
        List<String> filedls = new ArrayList<>();


        filedls.add("yearfs");


        filedls.add("yearxnssdl");


        filedls.add("yearlyxs");


        filedls.add("yearnhyd");


        filedls.add("yearsbklyl");


        filedls.add("yeardxkyxs");


        filedls.add("yearyxfss");

        filedls.add("yearxfqr");


        filedls.add("yearjfpl");

        filedls.add("yearglyzxxs");

        return filedls;
    }

    private void initial(ProEconEquipmentInfoDayTop po) {
        /**
         * 发电量
         */
        po.setDayfdl(0.0);

        /**
         * 发电量月
         */
        po.setMonthfdl(0.0);

        /**
         * 发电量年
         */
        po.setYearfdl(0.0);

        /**
         * 理论发电量
         */
        po.setDayllfdl(0.0);

        /**
         * 理论发电量月
         */
        po.setMonthllfdl(0.0);

        /**
         * 理论发电量年
         */
        po.setYearllfdl(0.0);

        /**
         * 平均风速
         */
        po.setDayfs(0.0);

        /**
         * 月平均风速
         */
        po.setMonthfs(0.0);

        /**
         * 年平均风速
         */
        po.setYearfs(0.0);

        /**
         * 平均功率
         */
        po.setDaygl(0.0);

        /**
         * 月平均功率
         */
        po.setMonthgl(0.0);

        /**
         * 年平均功率
         */
        po.setYeargl(0.0);

        /**
         * 故障损失电量
         */
        po.setDaygzssdl(0.0);

        /**
         * 月故障损失电量
         */
        po.setMonthgzssdl(0.0);

        /**
         * 年故障损失电量
         */
        po.setYeargzssdl(0.0);

        /**
         * 日限电损失电量
         */
        po.setDayxdssdl(0.0);

        /**
         * 月限电损失电量
         */
        po.setMonthxdssdl(0.0);

        /**
         * 年限电损失电量
         */
        po.setYearxdssdl(0.0);

        /**
         * 日维护损失电量
         */
        po.setDaywhssdl(0.0);

        /**
         * 月维护损失电量
         */
        po.setMonthwhssdl(0.0);

        /**
         * 年维护损失电量
         */
        po.setYearwhssdl(0.0);

        /**
         * 日性能损失电量
         */
        po.setDayxnssdl(0.0);

        /**
         * 月性能损失电量
         */
        po.setMonthxnssdl(0.0);

        /**
         * 年性能损失电量
         */
        po.setYearxnssdl(0.0);

        /**
         * 日故障时间
         */
        po.setDaygzsj(0.0);

        /**
         * 月故障时间
         */
        po.setMonthgzsj(0.0);

        /**
         * 年故障时间
         */
        po.setYeargzsj(0.0);

        /**
         * 日维护时间
         */
        po.setDaywhsj(0.0);

        /**
         * 月维护时间
         */
        po.setMonthwhsj(0.0);

        /**
         * 年维护时间
         */
        po.setYearwhsj(0.0);

        /**
         * 日运行时间
         */
        po.setDayyxsj(0.0);

        /**
         * 月运行时间
         */
        po.setMonthyxsj(0.0);

        /**
         * 年运行时间
         */
        po.setYearyxsj(0.0);

        /**
         * 日停机时间
         */
        po.setDaytjsj(0.0);

        /**
         * 月停机时间
         */
        po.setMonthtjsj(0.0);

        /**
         * 年停机时间
         */
        po.setYeartjsj(0.0);

        /**
         * 日利用小时数
         */
        po.setDaylyxs(0.0);

        /**
         * 月利用小时数
         */
        po.setMonthlyxs(0.0);

        /**
         * 年利用小时数
         */
        po.setYearlyxs(0.0);

        /**
         * 日拟合优度
         */
        po.setDaynhyd(0.0);

        /**
         * 月拟合优度
         */
        po.setMonthnhyd(0.0);

        /**
         * 年拟合优度
         */
        po.setYearnhyd(0.0);

        /**
         * 日设备可利用率
         */
        po.setDaysbklyl(0.0);

        /**
         * 月设备可利用率
         */
        po.setMonthsbklyl(0.0);

        /**
         * 年设备可利用率
         */
        po.setYearsbklyl(0.0);

        /**
         * 日等效可用系数
         */
        po.setDaydxkyxs(0.0);

        /**
         * 月等效可用系数
         */
        po.setMonthdxkyxs(0.0);

        /**
         * 年等效可用系数
         */
        po.setYeardxkyxs(0.0);

        /**
         * 有效风时数
         */
        po.setDayyxfss(0.0);

        /**
         * 月有效风时数
         */
        po.setMonthyxfss(0.0);

        /**
         * 年有效风时数
         */
        po.setYearyxfss(0.0);

        /**
         * 日小风切入
         */
        po.setDayxfqr(0.0);

        /**
         * 月小风切入
         */
        po.setMonthxfqr(0.0);

        /**
         * 年小风切入
         */
        po.setYearxfqr(0.0);

        /**
         * 日静风频率
         */
        po.setDayjfpl(0.0);

        /**
         * 月静风频率
         */
        po.setMonthjfpl(0.0);

        /**
         * 年静风频率
         */
        po.setYearjfpl(0.0);

        /**
         * 日功率一致性系数
         */
        po.setDayglyzxxs(0.0);

        /**
         * 月功率一致性系数
         */
        po.setMonthglyzxxs(0.0);

        /**
         * 年功率一致性系数
         */
        po.setYearglyzxxs(0.0);


        /**
         * 综合排名
         */
        po.setDayRank(0);


        /**
         * 分数
         */
        po.setDayScore(0.0);
    }
}







