package com.gyee.runeconomy.service.StudyElectricity;

import com.gyee.common.util.DateUtils;
import com.gyee.common.util.SortUtils;
import com.gyee.runeconomy.dto.CoulometricVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.service.auto.IProBasicFeasibilityStudyService;
import com.gyee.runeconomy.service.auto.IProBasicProjectPlanService;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay1Service;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay5Service;
import com.gyee.runeconomy.util.StringUtils;
import org.springframework.stereotype.Service;

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

@Service
public class StudyElectricityService {

    @Resource
    private IProEconPowerstationInfoDay1Service proEconPowerstationInfoDay1Service;

    @Resource
    private IProEconPowerstationInfoDay5Service proEconPowerstationInfoDay5Service;

    @Resource
    private IProBasicProjectPlanService proBasicProjectPlanService;

    @Resource
    private IProBasicFeasibilityStudyService proBasicFeasibilityStudyService;

    public Map<String, Object> studyelectricity(String wpId) throws Exception {

        List<ProBasicPowerstation> wplsG = CacheContext.wplsG.stream().filter(g -> wpId.equals(g.getId())).collect(Collectors.toList());
        List<CoulometricVo> vos = new ArrayList<CoulometricVo>();
        Map<String, Object> map = new LinkedHashMap<>();
        int day = 0;

        // 获取当前日期
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(new Date());
        // 设置为本年一月一号0点
        calendar2.set(Calendar.MONTH, Calendar.JANUARY); // 0表示一月
        calendar2.set(Calendar.DAY_OF_MONTH, 1);
        calendar2.set(Calendar.HOUR_OF_DAY, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);
        calendar2.set(Calendar.MILLISECOND, 0);
        Date firstDayZeroHour = calendar2.getTime();

        for (int i = 1; i <= 12; i++) {

            //本月计划电量
            double byjhdlsum = 0.0;
            //本月实际电量合计
            double bysjdlhjsum = 0.0;

            //本月可研电量合计
            double bykydlhjsum = 0.0;

            day = i;
            for (ProBasicPowerstation wpls : wplsG) {

                //计划电量-循环所有场站当前月集合
                List<ProBasicProjectPlan> currentjhls = proBasicProjectPlanService.getmonthList(String.valueOf(day));

                //当前场站循环当前月
                List<ProBasicProjectPlan> currentjh = currentjhls.stream().filter(wp -> wp.getWindpowerstationId().equals(wpls.getId())).collect(Collectors.toList());
                byjhdlsum = currentjh.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum(); //本月计划电量合

                //可研电量-循环所有场站当前月集合
                List<ProBasicFeasibilityStudy> currentkyls = proBasicFeasibilityStudyService.getmonthList(String.valueOf(day));

                //当前场站循环当前月
                List<ProBasicFeasibilityStudy> currentky = currentkyls.stream().filter(wp -> wp.getWindpowerstationId().equals(wpls.getId())).collect(Collectors.toList());
                bykydlhjsum = currentky.stream().mapToDouble(ProBasicFeasibilityStudy::getGeneratingCapacity).sum(); //本月可研电量合


                //当前场站循环月实际电量
                List<ProEconPowerstationInfoDay5> sjdlls = proEconPowerstationInfoDay5Service.getmax(wpls.getId(), firstDayZeroHour);
                if (!sjdlls.isEmpty() && sjdlls != null) {
                    bysjdlhjsum = sjdlls.get(0).getYfdldb().doubleValue();
                }

            }


            CoulometricVo vo = new CoulometricVo();

            //添加计划电量
            vo.setJhdl(StringUtils.round(byjhdlsum, 2));
            vo.setHours(i);
            //添加实际电量
            vo.setSjdl(StringUtils.round(bysjdlhjsum / 10000, 2));
            //添加完成情况
            vo.setWcqk(StringUtils.round(((bysjdlhjsum / 10000) / byjhdlsum) * 100, 0));
            //添加可研电量
            vo.setRzqd(StringUtils.round(bykydlhjsum, 2));
            vos.add(vo);

            SortUtils.sort(vos, "hours", SortUtils.ASC);
            map.put("value", vos);
            // 月份增加一个月
            firstDayZeroHour = DateUtils.addMonths(firstDayZeroHour, 1);
        }

        return map;
    }
}
