package com.gyee.generation.service;/*
@author   谢生杰
@date   2023/4/27-10:25
*/

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.common.util.CommonUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.ProBasicEquipment;
import com.gyee.generation.model.auto.ProBasicEquipmentPoint;
import com.gyee.generation.model.auto.ProEconEquipmentDeviatPower;
import com.gyee.generation.model.auto.ProEconEquipmentDeviatSpeed;
import com.gyee.generation.model.vo.DeviationVo;
import com.gyee.generation.service.auto.IProEconEquipmentDeviatPowerService;
import com.gyee.generation.service.auto.IProEconEquipmentDeviatSpeedService;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WindDeviationService {
    @Resource
    private IEdosUtil edosUtil;
    @Resource
    private IProEconEquipmentDeviatSpeedService proEconEquipmentDeviatSpeedService;
    @Resource
    private IProEconEquipmentDeviatPowerService proEconEquipmentDeviatPowerService;
    @Value("${initialcode}")
    private String initialcode;

    public void save(String begin, String end) {

        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
        List<ProBasicEquipment> wtfdls = CacheContext.wtfdls;
        Map<String, Double> modelpower = CacheContext.modelpower;
        List<ProEconEquipmentDeviatSpeed> speedsResultList = new ArrayList<>();
        List<ProEconEquipmentDeviatPower> powersResultList = new ArrayList<>();

        List<String> days = DateUtils.getDays(begin, end);
        days.stream().forEach(day -> {
            Date date = DateUtils.parseDate(day);
            Date startOfDay = DateUtils.getStartOfDay(date);
            Date endOfDay = DateUtils.getEndOfDay(date);

            QueryWrapper<ProEconEquipmentDeviatSpeed> del = new QueryWrapper<>();
            del.lambda().eq(ProEconEquipmentDeviatSpeed::getRecordDate, date);
            proEconEquipmentDeviatSpeedService.remove(del);

            wtfdls.stream().forEach(wt -> {
                //                if (wt.getEquipmentCategory()==-1){
                Map<String, ProBasicEquipmentPoint> equipmentPointMap = wtpAimap.get(wt.getId());

                ProBasicEquipmentPoint fxPoint = equipmentPointMap.get(ContantXk.CJ_FX);
                ProBasicEquipmentPoint dfjdPoint = equipmentPointMap.get(ContantXk.CJ_DFJD);
                ProBasicEquipmentPoint fsPoint = equipmentPointMap.get(ContantXk.CJ_SSFS);
                ProBasicEquipmentPoint glPoint = equipmentPointMap.get(ContantXk.CJ_SSGL);


                List<PointData> fxDatasSnap = null;
                List<PointData> dfjdDatasSnap = null;
                List<PointData> fsDatasSnap = null;
                List<PointData> glDatasSnap = null;

                if (!fxPoint.getNemCode().equals(initialcode) &&
                        !dfjdPoint.getNemCode().equals(initialcode) &&
                        !fsPoint.getNemCode().equals(initialcode) &&
                        !glPoint.getNemCode().equals(initialcode)
                ) {
                    try {
                        fxDatasSnap = edosUtil.getHistoryDatasSnap(fxPoint.getNemCode(), startOfDay.getTime() / 1000, endOfDay.getTime() / 1000);
                        dfjdDatasSnap = edosUtil.getHistoryDatasSnap(dfjdPoint.getNemCode(), startOfDay.getTime() / 1000, endOfDay.getTime() / 1000);
                        fsDatasSnap = edosUtil.getHistoryDatasSnap(fsPoint.getNemCode(), startOfDay.getTime() / 1000, endOfDay.getTime() / 1000);
                        glDatasSnap = edosUtil.getHistoryDatasSnap(glPoint.getNemCode(), startOfDay.getTime() / 1000, endOfDay.getTime() / 1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }


                if (StringUtils.isNotEmpty(fxDatasSnap) && StringUtils.isNotEmpty(dfjdDatasSnap) && StringUtils.isNotEmpty(fsDatasSnap) && StringUtils.isNotEmpty(glDatasSnap)) {
                    List<DeviationVo> tempList = new ArrayList<>();
                    for (int i = 0; i < fxDatasSnap.size(); i++) {
                        DeviationVo vo = new DeviationVo();
                        vo.setWtid(wt.getId());
                        vo.setSpeed(fsDatasSnap.get(i).getPointValueInDouble());
                        vo.setPower(glDatasSnap.get(i).getPointValueInDouble());
                        vo.setFx(fxDatasSnap.get(i).getPointValueInDouble());
                        vo.setDfjd(dfjdDatasSnap.get(i).getPointValueInDouble());
                        tempList.add(vo);
                    }
                    for (int i = 1; i <= 25; i++) {
                        int finalI = i;
                        List<DeviationVo> vos = tempList.stream().filter(t -> (int) Math.round(t.getSpeed()) == finalI).collect(Collectors.toList());
                        int[] ints = windDeviationRatio(vos);
                        ProEconEquipmentDeviatSpeed deviatSpeed = new ProEconEquipmentDeviatSpeed();
                        int qualified = 0;
                        int zqualified = 0;
                        for (int x = 0; x < ints.length; x++) {
                            if (x >= 35 && x <= 65) {
                                qualified += ints[x];
                            }
                            zqualified += ints[x];
                        }
                        deviatSpeed.setSpeed(i);
                        deviatSpeed.setUnqualified(zqualified - qualified);
                        deviatSpeed.setQualified(qualified);
                        deviatSpeed.setId(CommonUtils.getUUID());
                        deviatSpeed.setWindturbineId(wt.getId());
                        deviatSpeed.setLineId(wt.getLineId());
                        deviatSpeed.setProjectId(wt.getProjectId());
                        deviatSpeed.setWindpowerstationId(wt.getWindpowerstationId());
                        deviatSpeed.setCompanyId(wt.getCompanyId());
                        deviatSpeed.setRegionId(wt.getRegionId());
                        deviatSpeed.setRecordDate(date);
                        deviatSpeed.setN50(ints[0]);
                        deviatSpeed.setN49(ints[1]);
                        deviatSpeed.setN48(ints[2]);
                        deviatSpeed.setN47(ints[3]);
                        deviatSpeed.setN46(ints[4]);
                        deviatSpeed.setN45(ints[5]);
                        deviatSpeed.setN44(ints[6]);
                        deviatSpeed.setN43(ints[7]);
                        deviatSpeed.setN42(ints[8]);
                        deviatSpeed.setN41(ints[9]);
                        deviatSpeed.setN40(ints[10]);
                        deviatSpeed.setN39(ints[11]);
                        deviatSpeed.setN38(ints[12]);
                        deviatSpeed.setN37(ints[13]);
                        deviatSpeed.setN36(ints[14]);
                        deviatSpeed.setN35(ints[15]);
                        deviatSpeed.setN34(ints[16]);
                        deviatSpeed.setN33(ints[17]);
                        deviatSpeed.setN32(ints[18]);
                        deviatSpeed.setN31(ints[19]);
                        deviatSpeed.setN30(ints[20]);
                        deviatSpeed.setN29(ints[21]);
                        deviatSpeed.setN28(ints[22]);
                        deviatSpeed.setN27(ints[23]);
                        deviatSpeed.setN26(ints[24]);
                        deviatSpeed.setN25(ints[25]);
                        deviatSpeed.setN24(ints[26]);
                        deviatSpeed.setN23(ints[27]);
                        deviatSpeed.setN22(ints[28]);
                        deviatSpeed.setN21(ints[29]);
                        deviatSpeed.setN20(ints[30]);
                        deviatSpeed.setN19(ints[31]);
                        deviatSpeed.setN18(ints[32]);
                        deviatSpeed.setN17(ints[33]);
                        deviatSpeed.setN16(ints[34]);
                        deviatSpeed.setN15(ints[35]);
                        deviatSpeed.setN14(ints[36]);
                        deviatSpeed.setN13(ints[37]);
                        deviatSpeed.setN12(ints[38]);
                        deviatSpeed.setN11(ints[39]);
                        deviatSpeed.setN10(ints[40]);
                        deviatSpeed.setN9(ints[41]);
                        deviatSpeed.setN8(ints[42]);
                        deviatSpeed.setN7(ints[43]);
                        deviatSpeed.setN6(ints[44]);
                        deviatSpeed.setN5(ints[45]);
                        deviatSpeed.setN4(ints[46]);
                        deviatSpeed.setN3(ints[47]);
                        deviatSpeed.setN2(ints[48]);
                        deviatSpeed.setN1(ints[49]);
                        deviatSpeed.setP0(ints[50]);
                        deviatSpeed.setP1(ints[51]);
                        deviatSpeed.setP2(ints[52]);
                        deviatSpeed.setP3(ints[53]);
                        deviatSpeed.setP4(ints[54]);
                        deviatSpeed.setP5(ints[55]);
                        deviatSpeed.setP6(ints[56]);
                        deviatSpeed.setP7(ints[57]);
                        deviatSpeed.setP8(ints[58]);
                        deviatSpeed.setP9(ints[59]);
                        deviatSpeed.setP10(ints[60]);
                        deviatSpeed.setP11(ints[61]);
                        deviatSpeed.setP12(ints[62]);
                        deviatSpeed.setP13(ints[63]);
                        deviatSpeed.setP14(ints[64]);
                        deviatSpeed.setP15(ints[65]);
                        deviatSpeed.setP16(ints[66]);
                        deviatSpeed.setP17(ints[67]);
                        deviatSpeed.setP18(ints[68]);
                        deviatSpeed.setP19(ints[69]);
                        deviatSpeed.setP20(ints[70]);
                        deviatSpeed.setP21(ints[71]);
                        deviatSpeed.setP22(ints[72]);
                        deviatSpeed.setP23(ints[73]);
                        deviatSpeed.setP24(ints[74]);
                        deviatSpeed.setP25(ints[75]);
                        deviatSpeed.setP26(ints[76]);
                        deviatSpeed.setP27(ints[77]);
                        deviatSpeed.setP28(ints[78]);
                        deviatSpeed.setP29(ints[79]);
                        deviatSpeed.setP30(ints[80]);
                        deviatSpeed.setP31(ints[81]);
                        deviatSpeed.setP32(ints[82]);
                        deviatSpeed.setP33(ints[83]);
                        deviatSpeed.setP34(ints[84]);
                        deviatSpeed.setP35(ints[85]);
                        deviatSpeed.setP36(ints[86]);
                        deviatSpeed.setP37(ints[87]);
                        deviatSpeed.setP38(ints[88]);
                        deviatSpeed.setP39(ints[89]);
                        deviatSpeed.setP40(ints[90]);
                        deviatSpeed.setP41(ints[91]);
                        deviatSpeed.setP42(ints[92]);
                        deviatSpeed.setP43(ints[93]);
                        deviatSpeed.setP44(ints[94]);
                        deviatSpeed.setP45(ints[95]);
                        deviatSpeed.setP46(ints[96]);
                        deviatSpeed.setP47(ints[97]);
                        deviatSpeed.setP48(ints[98]);
                        deviatSpeed.setP49(ints[99]);
                        deviatSpeed.setP50(ints[100]);
                        speedsResultList.add(deviatSpeed);
                    }

                    double zjrl = modelpower.get(wt.getModelId());
                    int round = (int) zjrl / 100;
                    for (int i = 1; i <= round; i++) {
                        int finalI = i;
                        List<DeviationVo> vos = tempList.stream().filter(t -> (int) Math.round(t.getPower()) == finalI).collect(Collectors.toList());
                        int[] ints = windDeviationRatio(vos);
                        ProEconEquipmentDeviatPower deviatPower = new ProEconEquipmentDeviatPower();
                        int qualified = 0;
                        int zqualified = 0;
                        for (int x = 0; x < ints.length; x++) {
                            if (x >= 35 && x <= 65) {
                                qualified += ints[x];
                            }
                            zqualified += ints[x];
                        }
                        deviatPower.setPower(i * 100);
                        deviatPower.setUnqualified(zqualified - qualified);
                        deviatPower.setQualified(qualified);
                        deviatPower.setId(CommonUtils.getUUID());
                        deviatPower.setWindturbineId(wt.getId());
                        deviatPower.setLineId(wt.getLineId());
                        deviatPower.setProjectId(wt.getProjectId());
                        deviatPower.setWindpowerstationId(wt.getWindpowerstationId());
                        deviatPower.setCompanyId(wt.getCompanyId());
                        deviatPower.setRegionId(wt.getRegionId());
                        deviatPower.setRecordDate(date);
                        deviatPower.setN50(ints[0]);
                        deviatPower.setN49(ints[1]);
                        deviatPower.setN48(ints[2]);
                        deviatPower.setN47(ints[3]);
                        deviatPower.setN46(ints[4]);
                        deviatPower.setN45(ints[5]);
                        deviatPower.setN44(ints[6]);
                        deviatPower.setN43(ints[7]);
                        deviatPower.setN42(ints[8]);
                        deviatPower.setN41(ints[9]);
                        deviatPower.setN40(ints[10]);
                        deviatPower.setN39(ints[11]);
                        deviatPower.setN38(ints[12]);
                        deviatPower.setN37(ints[13]);
                        deviatPower.setN36(ints[14]);
                        deviatPower.setN35(ints[15]);
                        deviatPower.setN34(ints[16]);
                        deviatPower.setN33(ints[17]);
                        deviatPower.setN32(ints[18]);
                        deviatPower.setN31(ints[19]);
                        deviatPower.setN30(ints[20]);
                        deviatPower.setN29(ints[21]);
                        deviatPower.setN28(ints[22]);
                        deviatPower.setN27(ints[23]);
                        deviatPower.setN26(ints[24]);
                        deviatPower.setN25(ints[25]);
                        deviatPower.setN24(ints[26]);
                        deviatPower.setN23(ints[27]);
                        deviatPower.setN22(ints[28]);
                        deviatPower.setN21(ints[29]);
                        deviatPower.setN20(ints[30]);
                        deviatPower.setN19(ints[31]);
                        deviatPower.setN18(ints[32]);
                        deviatPower.setN17(ints[33]);
                        deviatPower.setN16(ints[34]);
                        deviatPower.setN15(ints[35]);
                        deviatPower.setN14(ints[36]);
                        deviatPower.setN13(ints[37]);
                        deviatPower.setN12(ints[38]);
                        deviatPower.setN11(ints[39]);
                        deviatPower.setN10(ints[40]);
                        deviatPower.setN9(ints[41]);
                        deviatPower.setN8(ints[42]);
                        deviatPower.setN7(ints[43]);
                        deviatPower.setN6(ints[44]);
                        deviatPower.setN5(ints[45]);
                        deviatPower.setN4(ints[46]);
                        deviatPower.setN3(ints[47]);
                        deviatPower.setN2(ints[48]);
                        deviatPower.setN1(ints[49]);
                        deviatPower.setP0(ints[50]);
                        deviatPower.setP1(ints[51]);
                        deviatPower.setP2(ints[52]);
                        deviatPower.setP3(ints[53]);
                        deviatPower.setP4(ints[54]);
                        deviatPower.setP5(ints[55]);
                        deviatPower.setP6(ints[56]);
                        deviatPower.setP7(ints[57]);
                        deviatPower.setP8(ints[58]);
                        deviatPower.setP9(ints[59]);
                        deviatPower.setP10(ints[60]);
                        deviatPower.setP11(ints[61]);
                        deviatPower.setP12(ints[62]);
                        deviatPower.setP13(ints[63]);
                        deviatPower.setP14(ints[64]);
                        deviatPower.setP15(ints[65]);
                        deviatPower.setP16(ints[66]);
                        deviatPower.setP17(ints[67]);
                        deviatPower.setP18(ints[68]);
                        deviatPower.setP19(ints[69]);
                        deviatPower.setP20(ints[70]);
                        deviatPower.setP21(ints[71]);
                        deviatPower.setP22(ints[72]);
                        deviatPower.setP23(ints[73]);
                        deviatPower.setP24(ints[74]);
                        deviatPower.setP25(ints[75]);
                        deviatPower.setP26(ints[76]);
                        deviatPower.setP27(ints[77]);
                        deviatPower.setP28(ints[78]);
                        deviatPower.setP29(ints[79]);
                        deviatPower.setP30(ints[80]);
                        deviatPower.setP31(ints[81]);
                        deviatPower.setP32(ints[82]);
                        deviatPower.setP33(ints[83]);
                        deviatPower.setP34(ints[84]);
                        deviatPower.setP35(ints[85]);
                        deviatPower.setP36(ints[86]);
                        deviatPower.setP37(ints[87]);
                        deviatPower.setP38(ints[88]);
                        deviatPower.setP39(ints[89]);
                        deviatPower.setP40(ints[90]);
                        deviatPower.setP41(ints[91]);
                        deviatPower.setP42(ints[92]);
                        deviatPower.setP43(ints[93]);
                        deviatPower.setP44(ints[94]);
                        deviatPower.setP45(ints[95]);
                        deviatPower.setP46(ints[96]);
                        deviatPower.setP47(ints[97]);
                        deviatPower.setP48(ints[98]);
                        deviatPower.setP49(ints[99]);
                        deviatPower.setP50(ints[100]);
                        powersResultList.add(deviatPower);
                    }
                }

                //                }
            });

        });
        proEconEquipmentDeviatSpeedService.saveBatch(speedsResultList);
        proEconEquipmentDeviatPowerService.saveBatch(powersResultList);
    }

    private static int[] windDeviationRatio(List<DeviationVo> list) {
        int[] pc = new int[101];  //正负偏差 [-50,-49,....,0,1,2,.....50]
        //次数统计
        for (int i = 0; i < list.size(); i++) {
            DeviationVo item = list.get(i);
            int ele = (int) (Math.abs(item.getFx()) + Math.abs(item.getDfjd()));
            int index = ele - 180;
            if (index >= -50 && index <= 50)
                pc[50 - (index > 0 ? -index : Math.abs(index))]++;
        }

        return pc;
    }

}
