package cn.iocoder.yudao.module.energy.dal.mysql.price;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.energy.controller.admin.price.vo.EnergyStationPricePageReqVO;
import cn.iocoder.yudao.module.energy.dal.dataobject.price.EnergyStationPriceDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;

@Mapper
public interface EnergyStationPriceMapper extends BaseMapperX<EnergyStationPriceDO> {

    default PageResult<EnergyStationPriceDO> selectPage(EnergyStationPricePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<EnergyStationPriceDO>()
                .eqIfPresent(EnergyStationPriceDO::getStationId, reqVO.getStationId())
                .eqIfPresent(EnergyStationPriceDO::getCustomerId, reqVO.getCustomerId())
                .eqIfPresent(EnergyStationPriceDO::getStatus, reqVO.getStatus())
                .orderByDesc(EnergyStationPriceDO::getId));
    }

    default EnergyStationPriceDO selectEffective(Long stationId, Long customerId, LocalDate date) {
        return selectOne(new LambdaQueryWrapperX<EnergyStationPriceDO>()
                .eq(EnergyStationPriceDO::getStationId, stationId)
                .eq(EnergyStationPriceDO::getCustomerId, customerId)
                .le(EnergyStationPriceDO::getEffectiveDate, date)
                .and(w -> w.ge(EnergyStationPriceDO::getExpiryDate, date)
                        .or().isNull(EnergyStationPriceDO::getExpiryDate))
                .eq(EnergyStationPriceDO::getStatus, 0)
                .orderByDesc(EnergyStationPriceDO::getEffectiveDate)
                .last("LIMIT 1"));
    }

}
