package com.gyee.runeconomy.service.auto.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.common.model.StringUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.runeconomy.mapper.auto.ProEconPowerstationInfoDay5Mapper;
import com.gyee.runeconomy.model.auto.ProEconPowerstationInfoDay5;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay5Service;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 电表发电量 服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-11-22
 */
@Service
public class ProEconPowerstationInfoDay5ServiceImpl extends ServiceImpl<ProEconPowerstationInfoDay5Mapper, ProEconPowerstationInfoDay5> implements IProEconPowerstationInfoDay5Service {

    @Override
    public Map<String, ProEconPowerstationInfoDay5> getEconElectricQuantity(String foreignKeyId, String staType, String dateType, Date date) {
        Map<String, ProEconPowerstationInfoDay5> resultmap = new HashMap<>();

        QueryWrapper<ProEconPowerstationInfoDay5> qw = null;


        //当前数据
        List<ProEconPowerstationInfoDay5> currlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(staType)) {
                qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, foreignKeyId + staType);
            }
            qw.lambda().eq(ProEconPowerstationInfoDay5::getRecordDate, date);
            currlist = getBaseMapper().selectList(qw);
            resultmap.put("currDlInfo", currlist.size() > 0 ? currlist.get(0) : null);
        }


        //环比数据
        List<ProEconPowerstationInfoDay5> ringlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(staType)) {
                qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, foreignKeyId + staType);
            }
            Date ring = DateUtils.addMonths(date, -1);
            qw.lambda().eq(ProEconPowerstationInfoDay5::getRecordDate, ring);
            ringlist = getBaseMapper().selectList(qw);
            resultmap.put("ringDlInfo", ringlist.size() > 0 ? ringlist.get(0) : null);
        }

        //同比
        List<ProEconPowerstationInfoDay5> withlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(staType)) {
                qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, foreignKeyId + staType);
            }
            Date with = DateUtils.addYears(new Date(), -1);
            qw.lambda().eq(ProEconPowerstationInfoDay5::getRecordDate, with);
            withlist = getBaseMapper().selectList(qw);
            resultmap.put("withDlInfo", withlist.size() > 0 ? withlist.get(0) : null);
        }

        return resultmap;
    }

    @Override
    public List<ProEconPowerstationInfoDay5> getEconZhcydldb(String foreignKeyId, String staType, String dateType, Date date) {

        QueryWrapper<ProEconPowerstationInfoDay5> qw = new QueryWrapper<>();


        if (StringUtils.isNotEmpty(staType)) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, foreignKeyId + staType);
        }

        if (null != date) {
            Date bingdate = DateUtils.addDays(date, -6);

            qw.lambda().ge(ProEconPowerstationInfoDay5::getRecordDate, bingdate);
            qw.lambda().le(ProEconPowerstationInfoDay5::getRecordDate, date);

        }
        qw.lambda().orderByAsc(ProEconPowerstationInfoDay5::getRecordDate);
        List<ProEconPowerstationInfoDay5> list = baseMapper.selectList(qw);

        return list;
    }

    @Override
    public List<ProEconPowerstationInfoDay5> getmax(String foreignKeyId, Date date) {
        QueryWrapper<ProEconPowerstationInfoDay5> qw = null;


        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int specifiedYear = calendar.get(Calendar.YEAR);
        int specifiedMonth = calendar.get(Calendar.MONTH) + 1;
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        Date endOfDayOfLastDayOfMonth = calendar.getTime();

        // 获取当前日期的年份和月份
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;


        // 判断指定日期是否是当前月份
        if (currentYear == specifiedYear && currentMonth == specifiedMonth) {
            // 如果是当前月份，则将endOfDayOfLastDayOfMonth减去一天
            Calendar calendar2 = Calendar.getInstance();  // 创建 Calendar 对象，表示当前时间
            calendar2.add(Calendar.DAY_OF_MONTH, -1);  // 减去一天

            // 设置时、分、秒为最后时刻
            calendar2.set(Calendar.HOUR_OF_DAY, 23);
            calendar2.set(Calendar.MINUTE, 59);
            calendar2.set(Calendar.SECOND, 59);

            endOfDayOfLastDayOfMonth = calendar2.getTime();  // 获取计算后的时间
        }

        //当前数据
        List<ProEconPowerstationInfoDay5> currlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(foreignKeyId)) {
                qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, foreignKeyId);
            }
            qw.lambda().between(ProEconPowerstationInfoDay5::getRecordDate, DateUtils.getMonthFirst(date), endOfDayOfLastDayOfMonth);
            qw.lambda().orderByDesc(ProEconPowerstationInfoDay5::getRecordDate);
            qw.last("limit 1");
            currlist = baseMapper.selectList(qw);

        }
        return currlist;
    }

    @Override
    public List<ProEconPowerstationInfoDay5> getdrlist(String wpId) {

        //当前日期
        long longDate = System.currentTimeMillis();
        Date nowDate = new Date(longDate);
        List<ProEconPowerstationInfoDay5> qwt = null;

        QueryWrapper<ProEconPowerstationInfoDay5> qw = new QueryWrapper<>();

        if (wpId.equals("KGDL_FGS0")) {
            qw.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS-2", "SXJ_KGDL_FLFD_ZGS-1");

        } else if (wpId.equals("KGDL_FGS-1")) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_FLFD_ZGS-1");

        } else if (wpId.equals("KGDL_FGS-2")) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS-2");

        } else if (wpId.equals("SXJ_RGN0")) {
            qw.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1", "SXJ_RGN-2");

        } else if (wpId.equals("SXJ_RGN-1")) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1");

        } else if (wpId.equals("SXJ_RGN-2")) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-2");

        } else if (wpId.endsWith("STA")) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, wpId);

        }

        qw.lambda().eq(ProEconPowerstationInfoDay5::getRecordDate, nowDate);
        qwt = baseMapper.selectList(qw);
        return qwt;
    }

    @Override
    public List<ProEconPowerstationInfoDay5> getzrlist(String wpId) {

        // 获取当前日期 暂时
        Date currentDate8 = new Date();

        // 创建 Calendar 对象，并设置为当前日期
        Calendar calendar8 = Calendar.getInstance();
        calendar8.setTime(currentDate8);
        // 设置 beginDate 为前一天的早上六点
        calendar8.add(Calendar.DAY_OF_MONTH, -1);
        calendar8.set(Calendar.HOUR_OF_DAY, 6);
        calendar8.set(Calendar.MINUTE, 0);
        calendar8.set(Calendar.SECOND, 0);
        calendar8.set(Calendar.MILLISECOND, 0);
        Date beginDate = calendar8.getTime();

        // 设置 endDate 为当天的23点59分59秒
        calendar8.set(Calendar.HOUR_OF_DAY, 23);
        calendar8.set(Calendar.MINUTE, 59);
        calendar8.set(Calendar.SECOND, 59);
        calendar8.set(Calendar.MILLISECOND, 999);
        Date endDate = calendar8.getTime();


        List<ProEconPowerstationInfoDay5> qwt = null;
        QueryWrapper<ProEconPowerstationInfoDay5> qw = new QueryWrapper<>();
        if (wpId.equals("KGDL_FGS0")) {
            qw.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS-2", "SXJ_KGDL_FLFD_ZGS-1");
        } else if (wpId.equals("KGDL_FGS-1")) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_FLFD_ZGS-1");
        } else if (wpId.equals("KGDL_FGS-2")) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS-2");
        } else if (wpId.equals("SXJ_RGN0")) {
            qw.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1", "SXJ_RGN-2");
        } else if (wpId.equals("SXJ_RGN-1")) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1");
        } else if (wpId.equals("SXJ_RGN-2")) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-2");
        } else if (wpId.endsWith("STA")) {
            qw.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, wpId);
        }
        qw.lambda().between(ProEconPowerstationInfoDay5::getRecordDate, beginDate, endDate);
        qwt = baseMapper.selectList(qw);
        return qwt;
    }
}
