<?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.shzg.project.worn.mapper.WornMaterialTypeMapper">
    
    <resultMap type="WornMaterialType" id="WornMaterialTypeResult">
        <result property="id"    column="id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="typeCode"    column="type_code"    />
        <result property="typeName"    column="type_name"    />
        <result property="firstLetter"    column="first_letter"    />
        <result property="description"    column="description"    />
        <result property="level"    column="level"    />
        <result property="orderNum"    column="order_num"    />
        <result property="status"    column="status"    />
        <result property="isDelete"    column="is_delete"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectWornMaterialTypeVo">
        select id, parent_id, ancestors, type_code, type_name, first_letter, description, level, order_num, status, is_delete, create_by, create_time, update_by, update_time from worn_material_type
    </sql>

    <select id="selectWornMaterialTypeList" parameterType="WornMaterialType" resultMap="WornMaterialTypeResult">
        <include refid="selectWornMaterialTypeVo"/>
        <where>
            is_delete = '0'
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="typeCode != null  and typeCode != ''"> and type_code = #{typeCode}</if>
            <if test="typeName != null  and typeName != ''"> and type_name like concat('%', #{typeName}, '%')</if>
            <if test="firstLetter != null  and firstLetter != ''"> and first_letter = #{firstLetter}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="level != null "> and level = #{level}</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="isDelete != null  and isDelete != ''"> and is_delete = #{isDelete}</if>

            order by order_num</where>
    </select>
    
    <select id="selectWornMaterialTypeById" parameterType="Long" resultMap="WornMaterialTypeResult">
        <include refid="selectWornMaterialTypeVo"/>
        where id = #{id}
    </select>

    <insert id="insertWornMaterialType" parameterType="WornMaterialType" useGeneratedKeys="true" keyProperty="id">
        insert into worn_material_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="typeCode != null and typeCode != ''">type_code,</if>
            <if test="typeName != null and typeName != ''">type_name,</if>
            <if test="firstLetter != null">first_letter,</if>
            <if test="description != null">description,</if>
            <if test="level != null">level,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="status != null">status,</if>
            <if test="isDelete != null">is_delete,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="typeCode != null and typeCode != ''">#{typeCode},</if>
            <if test="typeName != null and typeName != ''">#{typeName},</if>
            <if test="firstLetter != null">#{firstLetter},</if>
            <if test="description != null">#{description},</if>
            <if test="level != null">#{level},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="status != null">#{status},</if>
            <if test="isDelete != null">#{isDelete},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateWornMaterialType" parameterType="WornMaterialType">
        update worn_material_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="typeCode != null and typeCode != ''">type_code = #{typeCode},</if>
            <if test="typeName != null and typeName != ''">type_name = #{typeName},</if>
            <if test="firstLetter != null">first_letter = #{firstLetter},</if>
            <if test="description != null">description = #{description},</if>
            <if test="level != null">level = #{level},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWornMaterialTypeById" parameterType="Long">
        delete from worn_material_type where id = #{id}
    </delete>

    <delete id="deleteWornMaterialTypeByIds" parameterType="String">
        delete from worn_material_type where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectMaxTypeCode" resultType="java.lang.String">
        SELECT type_code
        FROM worn_material_type
        WHERE type_code LIKE 'MT%'
        ORDER BY type_code DESC
            LIMIT 1
    </select>

    <select id="selectChildCountByParentId" parameterType="Long" resultType="int">
        SELECT COUNT(1)
        FROM worn_material_type
        WHERE parent_id = #{parentId}
          AND is_delete = '0'
    </select>

    <!-- 查询所有子节点 -->
    <select id="selectChildrenById" parameterType="Long" resultMap="WornMaterialTypeResult">
        SELECT *
        FROM worn_material_type
        WHERE ancestors LIKE CONCAT('%,', #{id}, ',%')
          AND is_delete = '0'
    </select>

    <!-- 更新子节点 ancestors -->
    <update id="updateChildrenAncestors" parameterType="WornMaterialType">
        UPDATE worn_material_type
        SET ancestors = #{ancestors}
        WHERE id = #{id}
    </update>

</mapper>