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

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.detail.vo.HydrogenDetailPageReqVO;
import cn.iocoder.yudao.module.energy.dal.dataobject.detail.EnergyHydrogenDetailDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface EnergyHydrogenDetailMapper extends BaseMapperX<EnergyHydrogenDetailDO> {

    default PageResult<EnergyHydrogenDetailDO> selectPage(HydrogenDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<EnergyHydrogenDetailDO>()
                .eqIfPresent(EnergyHydrogenDetailDO::getStationId, reqVO.getStationId())
                .eqIfPresent(EnergyHydrogenDetailDO::getCustomerId, reqVO.getCustomerId())
                .likeIfPresent(EnergyHydrogenDetailDO::getPlateNumber, reqVO.getPlateNumber())
                .eqIfPresent(EnergyHydrogenDetailDO::getAuditStatus, reqVO.getAuditStatus())
                .eqIfPresent(EnergyHydrogenDetailDO::getDeductionStatus, reqVO.getDeductionStatus())
                .eqIfPresent(EnergyHydrogenDetailDO::getSettlementStatus, reqVO.getSettlementStatus())
                .eqIfPresent(EnergyHydrogenDetailDO::getBillId, reqVO.getBillId())
                .betweenIfPresent(EnergyHydrogenDetailDO::getHydrogenDate, reqVO.getHydrogenDate())
                .orderByDesc(EnergyHydrogenDetailDO::getId));
    }

    default List<EnergyHydrogenDetailDO> selectListByBillId(Long billId) {
        return selectList(new LambdaQueryWrapperX<EnergyHydrogenDetailDO>()
                .eq(EnergyHydrogenDetailDO::getBillId, billId));
    }

    default List<EnergyHydrogenDetailDO> selectUnbilledByCondition(Long customerId, Long contractId, Long stationId, LocalDate startDate, LocalDate endDate) {
        return selectList(new LambdaQueryWrapperX<EnergyHydrogenDetailDO>()
                .eq(EnergyHydrogenDetailDO::getCustomerId, customerId)
                .eq(EnergyHydrogenDetailDO::getContractId, contractId)
                .eqIfPresent(EnergyHydrogenDetailDO::getStationId, stationId)
                .ge(EnergyHydrogenDetailDO::getHydrogenDate, startDate)
                .le(EnergyHydrogenDetailDO::getHydrogenDate, endDate)
                .eq(EnergyHydrogenDetailDO::getAuditStatus, 1)
                .eq(EnergyHydrogenDetailDO::getDeductionStatus, 1)
                .isNull(EnergyHydrogenDetailDO::getBillId));
    }

    default List<EnergyHydrogenDetailDO> selectUnbilledByPeriod(LocalDate start, LocalDate end) {
        return selectList(new LambdaQueryWrapperX<EnergyHydrogenDetailDO>()
                .eq(EnergyHydrogenDetailDO::getAuditStatus, 1) // APPROVED
                .isNull(EnergyHydrogenDetailDO::getBillId)
                .between(EnergyHydrogenDetailDO::getHydrogenDate, start, end));
    }

}
