package com.yhy.module.core.dal.mysql.infoprice;

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 com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceResourcePricePageReqVO;
import com.yhy.module.core.dal.dataobject.infoprice.InfoPriceResourcePriceDO;
import java.util.Collections;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

/**
 * 信息价工料机价格历史 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface InfoPriceResourcePriceMapper extends BaseMapperX<InfoPriceResourcePriceDO> {

    default List<InfoPriceResourcePriceDO> selectListByResourceId(Long resourceId) {
        return selectList(new LambdaQueryWrapperX<InfoPriceResourcePriceDO>()
                .eq(InfoPriceResourcePriceDO::getResourceId, resourceId)
                .orderByDesc(InfoPriceResourcePriceDO::getStartTime));
    }

    default InfoPriceResourcePriceDO selectLastByResourceId(Long resourceId) {
        return selectOne(new LambdaQueryWrapperX<InfoPriceResourcePriceDO>()
                .eq(InfoPriceResourcePriceDO::getResourceId, resourceId)
                .orderByDesc(InfoPriceResourcePriceDO::getEndTime)
                .last("LIMIT 1"));
    }

    default List<InfoPriceResourcePriceDO> selectListByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }
        return selectList("id", ids);
    }

    default PageResult<InfoPriceResourcePriceDO> selectPage(InfoPriceResourcePricePageReqVO reqVO) {
        LambdaQueryWrapperX<InfoPriceResourcePriceDO> wrapper = new LambdaQueryWrapperX<InfoPriceResourcePriceDO>()
                .eq(InfoPriceResourcePriceDO::getResourceId, reqVO.getResourceId());
        
        // 日期范围过滤：查询时间段与指定范围有交集的记录
        if (reqVO.getStartTime() != null) {
            wrapper.ge(InfoPriceResourcePriceDO::getEndTime, reqVO.getStartTime());
        }
        if (reqVO.getEndTime() != null) {
            wrapper.le(InfoPriceResourcePriceDO::getStartTime, reqVO.getEndTime());
        }
        
        wrapper.orderByDesc(InfoPriceResourcePriceDO::getStartTime);
        return selectPage(reqVO, wrapper);
    }

}
