package com.gyee.generation.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.*;
import com.gyee.generation.service.auto.IProEconActivePowerDataService;
import com.gyee.generation.service.auto.IProEconEquipmentInfo15minuteService;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ActivePowerService {


    //    private static final Logger logger = LoggerFactory.getLogger(ActivePowerService.class);
    //    @Resource
    //    private IEdosUtil edosUtil;

    @Resource
    private IProEconEquipmentInfo15minuteService proEconEquipmentInfo15minuteService;

    @Resource
    private IProEconActivePowerDataService proEconActivePowerDataService;


    public void calcActivePowerData(Date currentDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(currentDate);
        Date beginDate = DateUtils.truncate(currentDate);
        c.add(Calendar.DAY_OF_MONTH, 1);
        Date endDate = c.getTime();
        for (ProBasicPowerstation station : CacheContext.wpls) {

            QueryWrapper<ProEconEquipmentInfo15minute> queryWrapper = new QueryWrapper<>();
            queryWrapper.ge("record_date", beginDate)
                    .le("record_date", endDate)
                    .eq("windPowerStation_Id", station.getId());
            List<ProEconEquipmentInfo15minute> ei15ls = proEconEquipmentInfo15minuteService.list(queryWrapper);
            //                    .stream()
            //                    .filter(i -> i.getWindpowerstationId().equals(station.getId())
            //                            && ( i.getRecordDate().compareTo(beginDate)==0 || i.getRecordDate().after(beginDate))
            //                            && (i.getRecordDate().compareTo(endDate)==0 || i.getRecordDate().before(endDate))
            //                    ).collect(Collectors.toList());


            Map<String/*风机编号*/, Map<Double/*风速*/, List<Double>>> map =
                    new HashMap<>();


            Map<Double/*风速*/, List<Double>> speedMap;
            for (ProEconEquipmentInfo15minute item : ei15ls) {

                if (map.containsKey(item.getWindturbineId())) {
                    speedMap = map.get(item.getWindturbineId());
                } else {
                    speedMap = new HashMap<>();
                    map.put(item.getWindturbineId(), speedMap);
                }
                Double speed = StringUtils.round(item.getFpjfs(), 2);
                if (speed > item.getFpjfs()) {

                    if (speed - item.getFpjfs() > 0.25) {
                        speed -= 0.5;
                    } else if (speed - item.getFpjfs() > 0.175) {
                        speed -= 0.25;
                    }
                } else if (speed < item.getFpjfs()) {
                    if (item.getFpjfs() - speed > 0.25) {
                        speed += 0.5;
                    } else if (item.getFpjfs() - speed > 0.175) {
                        speed += 0.25;
                    }
                }
                if ((item.getFpjgl() > 0 && item.getFpjfs() >= 3) || (item.getFpjfs() <= 3)) {
                    List<Double> powers;
                    if (speedMap.containsKey(speed)) {
                        powers = speedMap.get(speed);
                    } else {
                        powers = new ArrayList<>();
                        speedMap.put(speed, powers);
                    }
                    powers.add(item.getFpjgl());
                }

            }
            for (String windturbineId : map.keySet()) {
                //                List<ProEconActivePowerData> dataList = new ArrayList<>();
                Map<Double/*风速*/, List<Double>> intnalMap = map.get(windturbineId);
                for (Double speed : intnalMap.keySet()) {
                    List<Double> powers = intnalMap.get(speed);

                    QueryWrapper<ProEconActivePowerData> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("record_date", beginDate)
                            .eq("windturbine_id", windturbineId).eq("speed", speed);

                    Optional<ProEconActivePowerData> data = proEconActivePowerDataService.list(queryWrapper2)
                            .stream().findFirst();

                    ProEconActivePowerData apdata;
                    if (!data.isPresent()) {

                        QueryWrapper<ProEconActivePowerData> wrapper = new QueryWrapper<>();
                        wrapper.eq("id", data.get().getId());
                        proEconActivePowerDataService.remove(wrapper);

                    }

                    apdata = new ProEconActivePowerData();
                    buildActivePowerData(beginDate, station.getId(), windturbineId, speed, powers, apdata);
                    proEconActivePowerDataService.save(apdata);

                    //                    dataList.add(apdata);
                }
            }

        }
    }

    private void buildActivePowerData(Date beginDate, String station, String windturbineId, Double speed, List<Double> powers, ProEconActivePowerData data) {
        data.setWindturbineId(windturbineId);
        data.setWindpowerstationId(station);
        DoubleSummaryStatistics summaryStatistics = powers.stream().mapToDouble(n -> n).summaryStatistics();
        data.setPower(summaryStatistics.getAverage());
        data.setSpeed(speed);
        data.setFrequency(powers.size());
        data.setRecordDate(beginDate);

        if (CacheContext.wtmap.containsKey(windturbineId)) {
            ProBasicEquipment wt = CacheContext.wtmap.get(windturbineId);
            data.setModelId(wt.getModelId());
            data.setWindturbineCode(wt.getNemCode());

        }

        if (StringUtils.notEmp(data.getModelId()) && CacheContext.modelMap.containsKey(data.getModelId())) {
            ProEconEquipmentmodel em = CacheContext.modelMap.get(data.getModelId());
            data.setModelName(em.getName());

        }

    }


}

