package com.gyee.generation.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.generation.mapper.auto.ProEconPowerstationInfoDay1Mapper;
import com.gyee.generation.model.auto.ProEconPowerstationInfoDay1;
import com.gyee.generation.service.auto.IProEconPowerstationInfoDay1Service;
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 ProEconPowerstationInfoDay1ServiceImpl extends ServiceImpl<ProEconPowerstationInfoDay1Mapper, ProEconPowerstationInfoDay1> implements IProEconPowerstationInfoDay1Service {
    @Override
    public List<ProEconPowerstationInfoDay1> getmax(String foreignKeyId, Date date) {
        QueryWrapper<ProEconPowerstationInfoDay1> qw = null;

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        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();
        //当前数据
        List<ProEconPowerstationInfoDay1> currlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(foreignKeyId)) {
                qw.lambda().eq(ProEconPowerstationInfoDay1::getForeignKeyId, foreignKeyId);
            }
            qw.lambda().between(ProEconPowerstationInfoDay1::getRecordDate, DateUtils.getMonthFirst(date), endOfDayOfLastDayOfMonth);
            qw.lambda().orderByDesc(ProEconPowerstationInfoDay1::getRecordDate);
            qw.last("limit 1");
            currlist = baseMapper.selectList(qw);

        }
        return currlist;
    }
}
