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.InfoPriceResourcePageReqVO;
import com.yhy.module.core.dal.dataobject.infoprice.InfoPriceResourceDO;
import java.util.Collections;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

/**
 * 信息价工料机信息 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface InfoPriceResourceMapper extends BaseMapperX<InfoPriceResourceDO> {

    default PageResult<InfoPriceResourceDO> selectPage(InfoPriceResourcePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<InfoPriceResourceDO>()
                .eqIfPresent(InfoPriceResourceDO::getCategoryTreeId, reqVO.getCategoryTreeId())
                .eqIfPresent(InfoPriceResourceDO::getSourceResourceItemId, reqVO.getSourceResourceItemId())
                .likeIfPresent(InfoPriceResourceDO::getCode, reqVO.getCode())
                .orderByAsc(InfoPriceResourceDO::getSortOrder));
    }
    
    /**
     * 分页查询（支持按categoryTreeIds过滤）
     * @param reqVO 查询条件
     * @param categoryTreeIds 允许的categoryTreeId列表（地区过滤），null表示不过滤
     * @return 分页结果
     */
    default PageResult<InfoPriceResourceDO> selectPageWithCategoryTreeIds(InfoPriceResourcePageReqVO reqVO, List<Long> categoryTreeIds) {
        LambdaQueryWrapperX<InfoPriceResourceDO> wrapper = new LambdaQueryWrapperX<InfoPriceResourceDO>()
                .eqIfPresent(InfoPriceResourceDO::getCategoryTreeId, reqVO.getCategoryTreeId())
                .eqIfPresent(InfoPriceResourceDO::getSourceResourceItemId, reqVO.getSourceResourceItemId())
                .likeIfPresent(InfoPriceResourceDO::getCode, reqVO.getCode());
        
        // 如果有categoryTreeIds过滤条件，添加IN查询
        if (categoryTreeIds != null && !categoryTreeIds.isEmpty()) {
            wrapper.in(InfoPriceResourceDO::getCategoryTreeId, categoryTreeIds);
        }
        
        wrapper.orderByAsc(InfoPriceResourceDO::getSortOrder);
        return selectPage(reqVO, wrapper);
    }

    default List<InfoPriceResourceDO> selectListByCategoryTreeId(Long categoryTreeId) {
        return selectList(new LambdaQueryWrapperX<InfoPriceResourceDO>()
                .eq(InfoPriceResourceDO::getCategoryTreeId, categoryTreeId)
                .orderByAsc(InfoPriceResourceDO::getSortOrder));
    }

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

    default InfoPriceResourceDO selectByCategoryTreeIdAndCode(Long categoryTreeId, String code) {
        return selectOne(new LambdaQueryWrapperX<InfoPriceResourceDO>()
                .eq(InfoPriceResourceDO::getCategoryTreeId, categoryTreeId)
                .eq(InfoPriceResourceDO::getCode, code));
    }

    /**
     * 批量统计多个分类树节点下的工料机数量
     *
     * @param categoryTreeIds 分类树节点ID列表
     * @return Map，key为categoryTreeId，value为工料机数量
     */
    default java.util.Map<Long, Long> countByCategoryTreeIds(List<Long> categoryTreeIds) {
        if (categoryTreeIds == null || categoryTreeIds.isEmpty()) {
            return java.util.Collections.emptyMap();
        }
        List<InfoPriceResourceDO> list = selectList(new LambdaQueryWrapperX<InfoPriceResourceDO>()
                .in(InfoPriceResourceDO::getCategoryTreeId, categoryTreeIds)
                .select(InfoPriceResourceDO::getCategoryTreeId));
        return list.stream()
                .collect(java.util.stream.Collectors.groupingBy(
                        InfoPriceResourceDO::getCategoryTreeId,
                        java.util.stream.Collectors.counting()));
    }

}
