<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hotent.baseInfo.dao.CostCatalogDao">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hotent.baseInfo.model.CostCatalog">
        <id column="catalog_id" property="catalogId"/>
        <result column="parent_id" property="parentId"/>
        <result column="node_type" property="nodeType"/>
        <result column="code" property="code"/>
        <result column="catalog_name" property="catalogName"/>
        <result column="price_level" property="priceLevel"/>
        <result column="audit_type" property="auditType"/>
        <result column="remark" property="remark"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="create_time" property="createTime"/>
        <result column="create_by" property="createBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="layer_level" property="layerLevel"/>
        <result column="pricing_dept" property="pricingDept"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        catalog_id
        , parent_id, node_type, code, catalog_name, price_level,layer_level, pricing_dept,audit_type, remark, status, is_deleted, create_time, create_by, update_time, update_by
    </sql>

    <select id="selectChildrenById" resultMap="BaseResultMap">
        select * from cost_catalog where find_in_set(#{catalogId}, layer_level)
    </select>

    <select id="selectPage" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        cost_catalog
        ${ew.customSqlSegment}
    </select>

    <select id="selectList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        cost_catalog
        ${ew.customSqlSegment}
    </select>

    <select id="selectById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        cost_catalog
        where
        catalog_id = #{catalogId}
    </select>

    <!-- 查询所有目录（包含树形结构） -->
    <select id="selectAllCatalogs" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM cost_catalog
        WHERE is_deleted = 0
        ORDER BY code ASC
    </select>

    <!-- 根据父节点ID查询子节点 -->
    <select id="selectByParentId" parameterType="string" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM cost_catalog
        WHERE is_deleted = 0
        AND parent_id = #{parentId}
        ORDER BY code ASC
    </select>

    <!-- 查询最大编码（用于生成新编码） -->
    <select id="selectMaxCodeByParentId" parameterType="string" resultType="string">
        SELECT MAX(code)
        FROM cost_catalog
        WHERE is_deleted = 0
        <choose>
            <when test="parentId == null or parentId == '' or parentId == '0'">
                AND (parent_id IS NULL OR parent_id = '0')
                AND LENGTH(code) = 3
            </when>
            <otherwise>
                AND parent_id = #{parentId}
            </otherwise>
        </choose>
    </select>

    <!-- 检查是否存在子节点 -->
    <select id="countChildren" parameterType="string" resultType="int">
        SELECT COUNT(*)
        FROM cost_catalog
        WHERE is_deleted = 0
          AND parent_id = #{catalogId}
    </select>

    <!-- 根据名称和父节点ID查询（用于重复性检查） -->
    <select id="countByNameAndParentId" resultType="int">
        SELECT COUNT(*)
        FROM cost_catalog
        WHERE is_deleted = 0
        AND catalog_name = #{name}
        AND parent_id = #{parentId}
        <if test="excludeId != null and excludeId != ''">
            AND catalog_id != #{excludeId}
        </if>
    </select>

    <!-- 批量更新状态 -->
    <update id="batchUpdateStatus">
        UPDATE cost_catalog
        SET status = #{status},
        update_time = NOW()
        WHERE catalog_id IN
        <foreach collection="catalogIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND is_deleted = 0
    </update>
    <update id="updateChildren">
        update cost_catalog set layer_level =
        <foreach collection="costCatalogs" item="item" index="index"
                 separator=" " open="case catalog_id" close="end">
            when #{item.catalogId} then #{item.layerLevel}
        </foreach>
        where catalog_id in
        <foreach collection="costCatalogs" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.catalogId}
        </foreach>
    </update>
</mapper>
