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

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xujuanning
 * @since 2024-11-28
 */
@Service
public class StationInfoDayServiceImpl extends ServiceImpl<StationInfoDayMapper, StationInfoDay> implements IStationInfoDayService {

    @Override
    public List<StationInfoDay> getmax(String foreignKeyId, Date date) {
        QueryWrapper<StationInfoDay> 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<StationInfoDay> currlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(foreignKeyId)) {
                qw.lambda().eq(StationInfoDay::getStationId, foreignKeyId);
            }
            qw.lambda().between(StationInfoDay::getRecordDate, DateUtils.getMonthFirst(date), endOfDayOfLastDayOfMonth);
            qw.lambda().orderByDesc(StationInfoDay::getRecordDate);
            qw.last("limit 1");
            currlist = baseMapper.selectList(qw);

        }
        return currlist;
    }

    @Override
    public List<StationInfoDay> getSum(String foreignKeyId, Date date) {
        QueryWrapper<StationInfoDay> 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<StationInfoDay> currlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(foreignKeyId)) {
                qw.lambda().eq(StationInfoDay::getStationId, foreignKeyId);
            }
            qw.lambda().between(StationInfoDay::getRecordDate, DateUtils.getMonthFirst(date), endOfDayOfLastDayOfMonth);
            qw.select("COALESCE(sum(rfdl_fj),0) as rfdl_fj,COALESCE(sum(swdl),0) as swdl,COALESCE(sum(gwdl),0) as gwdl,COALESCE(sum(zhcydl),0) as zhcydl");
            currlist = baseMapper.selectList(qw);

        }
        return currlist;
    }

    @Override
    public List<StationInfoDay> getTurbineList(String wpid, String kssj, String jssj) throws ParseException {
        QueryWrapper<StationInfoDay> qw = new QueryWrapper<>();
        Date kssjDate = new SimpleDateFormat("yyyy-MM-dd").parse(kssj);
        Date jssjDate = new SimpleDateFormat("yyyy-MM-dd").parse(jssj);
        qw.lambda().eq(StationInfoDay::getStationId,wpid);
        qw.lambda().between(StationInfoDay::getRecordDate,kssjDate,jssjDate);
        List<StationInfoDay> list = baseMapper.selectList(qw);
        return list;
    }

}
