package com.gyee.runeconomy.service.monitor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.util.DateUtils;
import com.gyee.common.util.SortUtils;
import com.gyee.runeconomy.dto.PowercurveVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.service.TokenService;
import com.gyee.runeconomy.service.auto.*;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

@Service
public class MonitorvalueService {

    @Resource
    private IProBasicProjectPlanService proBasicProjectPlanService;
    @Resource
    private IProBasicPowerstationPointService proBasicPowerstationPointService;
    @Resource
    private IProBasicEquipmentPointService proBasicEquipmentPointService;
    @Resource
    private IProBasicCompanyService proBasicCompanyService;
    @Resource
    private IProEconTargetdataService proEconTargetdataService;
    @Resource
    private IProEconTargetdataFristService proEconTargetdataFristService;

    @Resource
    private IProBasicPowerPointService powerPointService;

    @Resource
    private IProEconPowerstationInfoDay5Service proEconPowerstationInfoDay5Service;

    private final String QS = "0";
    private final String FD = "-1";
    private final String GF = "-2";

    @Resource
    private IEdosUtil edosUtil;
    Map<String, Object> map = new HashMap<>();
    @Resource
    private TokenService tokenService;

    //利用小时
    public List<PowercurveVo> hoursvalue(String wpId, String timetype) throws Exception {


        //        Calendar twoyear = Calendar.getInstance();
        //        twoyear.add(Calendar.YEAR, -2);
        //        Date twoDate = twoyear.getTime();
        ////        Date beginDate = DateUtils.truncate(beginDate1);
        //
        //        Calendar oneyear = Calendar.getInstance();
        //        oneyear.add(Calendar.YEAR, -1);
        //        Date oneDate = oneyear.getTime();
        //
        //        Calendar currentyear = Calendar.getInstance();
        //        currentyear.add(Calendar.YEAR, 0);
        //        Date currentDate = currentyear.getTime();


        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);

        // 设置开始时间为本年最后一天早上六点
        calendar.set(Calendar.YEAR, currentYear);
        calendar.set(Calendar.MONTH, Calendar.DECEMBER);
        calendar.set(Calendar.DAY_OF_MONTH, 31);
        calendar.set(Calendar.HOUR_OF_DAY, 6);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        Date startDate = calendar.getTime();

        // 设置结束时间为本年最后一天晚上23点59分59秒
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        Date endDate = calendar.getTime();

        // 判断是否是本年
        boolean isCurrentYear = currentYear == Calendar.getInstance().get(Calendar.YEAR);

        if (isCurrentYear) {
            // 如果是本年，最后一天就是当天
            //            Calendar today = Calendar.getInstance();
            //            today.set(Calendar.HOUR_OF_DAY, 6);
            //            today.set(Calendar.MINUTE, 0);
            //            today.set(Calendar.SECOND, 0);
            //            startDate = today.getTime();
            //            endDate = new Date();
            Calendar today = Calendar.getInstance();
            today.add(Calendar.DAY_OF_MONTH, -1);
            today.set(Calendar.HOUR_OF_DAY, 6);
            today.set(Calendar.MINUTE, 0);
            today.set(Calendar.SECOND, 0);
            startDate = today.getTime();

            Calendar end = Calendar.getInstance();
            end.setTime(new Date());
            end.add(Calendar.DAY_OF_MONTH, -1);
            end.set(Calendar.HOUR_OF_DAY, 23);
            end.set(Calendar.MINUTE, 59);
            end.set(Calendar.SECOND, 59);
            endDate = end.getTime();
        }


        double zjrl = 0.0;

        Map<String, Object> zjrlmap = new HashMap<>();
        List<PowercurveVo> vos = new ArrayList<PowercurveVo>();
        if (wpId.endsWith(QS) || wpId.endsWith(FD) || wpId.endsWith(GF) || CacheContext.wpmapls.containsKey(wpId)) {

            List<ProBasicPowerstation> wplist = new ArrayList<>();
            if (CacheContext.wpmapls.containsKey(wpId)) {
                wplist = CacheContext.wpmapls.get(wpId);
            }
            HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
            //            List<ProBasicPowerstation> wplsToken = tokenService.getWpls(request);
            List<ProBasicPowerstation> wplsToken = wplist;
            for (ProBasicPowerstation wp : wplsToken) {
                if (wpId.contains(FD)) {
                    if (wp.getId().endsWith("FDC_STA")) {
                        wplist.add(wp);
                    }
                } else if (wpId.contains(GF)) {
                    if (wp.getId().endsWith("GDC_STA")) {
                        wplist.add(wp);
                    }
                } else if (wpId.contains(QS)) {
                    wplist.add(wp);
                }
            }


            if (wpId.contains(QS) || wpId.contains(FD) || wpId.contains(GF)) {
                for (ProBasicRegion rg : CacheContext.rgls) {
                    if (wpId.contains(rg.getId())) {
                        zjrl += rg.getJrCapacity() + rg.getJrwindCapacity();
                    }
                }
                for (ProBasicCompany cp : CacheContext.cpls) {
                    if (wpId.contains(cp.getId())) {
                        zjrl += cp.getJrCapacity() + cp.getJrwindCapacity();
                    }
                }
            }

            for (ProBasicPowerstation wp : wplsToken) {
                if (wpId.equals(wp.getId())) {
                    zjrl += wp.getJrCapacity() + wp.getJrwindCapacity();
                }
            }
            for (ProBasicProject pj : CacheContext.pjls) {
                if (wpId.equals(pj.getId())) {
                    zjrl += pj.getCapacity();
                }
            }
            for (ProBasicLine ln : CacheContext.lnls) {
                if (wpId.equals(ln.getId())) {
                    zjrl += ln.getCapacity();
                }
            }


            zjrlmap.put("zjrl", zjrl);

            List<ProEconPowerstationInfoDay5> qwt1 = null;
            QueryWrapper<ProEconPowerstationInfoDay5> qw1 = new QueryWrapper<>();

            if (timetype.contains("year")) {

                for (int i = 0; i < 3; i++) {


                    if (wpId.contains("RGN")) {
                        if (wpId.equals("SXJ_RGN0")) {
                            qw1.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1", "SXJ_RGN-2");
                        } else if (wpId.equals("SXJ_RGN-1")) {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1");
                        } else if (wpId.equals("SXJ_RGN-2")) {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-2");
                        }
                        qw1.lambda().between(ProEconPowerstationInfoDay5::getRecordDate, startDate, endDate);
                    } else {
                        qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, wpId);
                        qw1.lambda().between(ProEconPowerstationInfoDay5::getRecordDate, startDate, endDate);
                    }
                    qwt1 = proEconPowerstationInfoDay5Service.list(qw1);


                    if (!qwt1.isEmpty()) {

                        //                    vos.get(0).setNfdl(StringUtils.round((temp0 / 1000) / (zjrl / 1000), 2));
                        //                    vos.get(1).setNfdl(StringUtils.round((temp1 / 1000) / (zjrl / 1000), 2));
                        //                    vos.get(2).setNfdl(StringUtils.round((temp2 / 1000) / (zjrl / 1000), 2));
                        PowercurveVo vo = new PowercurveVo();


                    }
                }
            }

            if (timetype.contains("month")) {

                int month1 = DateUtils.getMonth(new Date());//4
                DateUtils.getMonthLast(new Date());//4.30
                Date date = DateUtils.getCurrentDate();

                for (int i = month1; i > 0; i--) {

                    //                    PointData data = edosUtil.getSectionData(month, DateUtils.getEndOfDay(DateUtils.getMonthLast(date)).getTime());
                    PowercurveVo vo1 = new PowercurveVo();
                    //                    vo1.setYfdl(DoubleUtils.keepPrecision((data.getPointValueInDouble() / 1000) / (zjrl / 1000), 2));
                    vo1.setHours(i);
                    vos.add(vo1);
                    date = DateUtils.addMonths(date, -1);
                }
                SortUtils.sort(vos, "hours", SortUtils.ASC);


            }
        }
        return vos;
    }

}
