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

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import com.yhy.module.core.dal.dataobject.resource.ResourceItemDO;
import java.math.BigDecimal;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface ResourceItemMapper extends BaseMapperX<ResourceItemDO> {

    /**
     * 根据工料机机类树节点ID查询工料机列表
     * 
     * 用于定额子目工料机的范围过滤
     * 通过 catalog_item_id 关联 yhy_catalog_item 表，再过滤 category_tree_id
     */
    @Select("SELECT ri.* FROM yhy_resource_item ri " +
            "INNER JOIN yhy_catalog_item rci ON ri.catalog_item_id = rci.id " +
            "WHERE rci.category_tree_id = #{categoryTreeId} " +
            "AND ri.deleted = 0 " +
            "AND rci.deleted = 0 " +
            "ORDER BY ri.id")
    List<ResourceItemDO> selectByCategoryTreeId(@Param("categoryTreeId") Long categoryTreeId);

    /**
     * 根据工料机机类树节点ID和查询条件查询工料机列表（支持模糊查询）
     * 
     * @param categoryTreeId 工料机机类树节点ID
     * @param code 编码（模糊查询）
     * @param name 名称（模糊查询）
     * @param spec 型号规格（模糊查询）
     * @return 工料机列表
     */
    @Select("<script>" +
            "SELECT ri.* FROM yhy_resource_item ri " +
            "INNER JOIN yhy_catalog_item rci ON ri.catalog_item_id = rci.id " +
            "WHERE rci.category_tree_id = #{categoryTreeId} " +
            "AND ri.deleted = 0 " +
            "AND rci.deleted = 0 " +
            "<if test='code != null and code != \"\"'>" +
            "AND ri.code LIKE CONCAT('%', #{code}, '%') " +
            "</if>" +
            "<if test='name != null and name != \"\"'>" +
            "AND ri.name LIKE CONCAT('%', #{name}, '%') " +
            "</if>" +
            "<if test='spec != null and spec != \"\"'>" +
            "AND ri.spec LIKE CONCAT('%', #{spec}, '%') " +
            "</if>" +
            "ORDER BY ri.id" +
            "</script>")
    List<ResourceItemDO> selectByCategoryTreeIdWithFilter(
            @Param("categoryTreeId") Long categoryTreeId,
            @Param("code") String code,
            @Param("name") String name,
            @Param("spec") String spec);

    /**
     * 批量查询工料机项（根据ID列表）
     *
     * @param ids ID列表
     * @return 工料机项列表
     */
    default List<ResourceItemDO> selectBatchIds(List<Long> ids) {
        return selectList("id", ids);
    }

    /**
     * 更新工料机的4个价格字段
     *
     * @param id 工料机ID
     * @param taxExclBasePrice 除税基价
     * @param taxInclBasePrice 含税基价
     * @param taxExclCompilePrice 除税编制价
     * @param taxInclCompilePrice 含税编制价
     */
    @Update("UPDATE yhy_resource_item SET " +
            "tax_excl_base_price = #{taxExclBasePrice}, " +
            "tax_incl_base_price = #{taxInclBasePrice}, " +
            "tax_excl_compile_price = #{taxExclCompilePrice}, " +
            "tax_incl_compile_price = #{taxInclCompilePrice}, " +
            "update_time = now() " +
            "WHERE id = #{id} AND deleted = 0")
    void updatePrices(@Param("id") Long id,
                      @Param("taxExclBasePrice") BigDecimal taxExclBasePrice,
                      @Param("taxInclBasePrice") BigDecimal taxInclBasePrice,
                      @Param("taxExclCompilePrice") BigDecimal taxExclCompilePrice,
                      @Param("taxInclCompilePrice") BigDecimal taxInclCompilePrice);
}
