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.ProEconEquipmentInfoDay1Mapper;
import com.gyee.runeconomy.model.auto.ProEconEquipmentInfoDay1;
import com.gyee.runeconomy.service.auto.IProEconEquipmentInfoDay1Service;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 五损电量 服务实现类
 * </p>
 *
 * @author shilin
 * @since 2022-11-01
 */
@Service
public class ProEconEquipmentInfoDay1ServiceImpl extends ServiceImpl<ProEconEquipmentInfoDay1Mapper, ProEconEquipmentInfoDay1> implements IProEconEquipmentInfoDay1Service {

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

        }
        return currlist;
    }
}
