<?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.ruoyi.inventory.mapper.MaterialsCategoryMapper">
    
    <resultMap type="MaterialsCategory" id="MaterialsCategoryResult">
        <result property="id"    column="id"    />
        <result property="categoryCode"    column="category_code"    />
        <result property="categoryName"    column="category_name"    />
        <result property="sortNo"    column="sort_no"    />
        <result property="createTime"    column="create_time"    />
        <result property="createUserCode"    column="create_user_code"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateUserCode"    column="update_user_code"    />
        <result property="parentId" column="parent_id" />
        <result property="isUsed" column="is_used" />
    </resultMap>

    <sql id="selectMaterialsCategoryVo">
        select id, category_code, category_name, sort_no, create_time, create_user_code, update_time, update_user_code, parent_id, is_used from materials_category
    </sql>

    <select id="selectMaterialsCategoryList" parameterType="MaterialsCategory" resultMap="MaterialsCategoryResult">
        <include refid="selectMaterialsCategoryVo"/>
        <where>
            is_used != 0
            <if test="id != null  and id != ''"> and id = #{id}</if>
            <if test="categoryCode != null  and categoryCode != ''"> and category_code like concat('%', #{categoryCode}, '%')</if>
            <if test="categoryName != null  and categoryName != ''"> and category_name like concat('%', #{categoryName}, '%')</if>
            <if test="sortNo != null "> and sort_no = #{sortNo}</if>
            <if test="createTime != null "> and create_time like concat('%', #{createTime}, '%')</if>
            <if test="updateTime != null "> and update_time like concat('%', #{updateTime}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
<!--            <if test="isUsed != null "> and is_used = #{isUsed}</if>-->
        </where>
        order by sort_no asc
    </select>
    
    <select id="selectMaterialsCategoryById" parameterType="String" resultMap="MaterialsCategoryResult">
        <include refid="selectMaterialsCategoryVo"/>
        where id = #{id}
        and is_used != 0
        order by sort_no asc
    </select>
    <insert id="insertMaterialsCategory" parameterType="MaterialsCategory">
        insert into materials_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="categoryCode != null">category_code,</if>
            <if test="categoryName != null">category_name,</if>
            <if test="sortNo != null">sort_no,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUserCode != null">create_user_code,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateUserCode != null">update_user_code,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="isUsed != null">is_used,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="categoryCode != null">#{categoryCode},</if>
            <if test="categoryName != null">#{categoryName},</if>
            <if test="sortNo != null">#{sortNo},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createUserCode != null">#{createUserCode},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateUserCode != null">#{updateUserCode},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="isUsed != null">#{isUsed},</if>
         </trim>
    </insert>

    <update id="updateMaterialsCategory" parameterType="MaterialsCategory">
        update materials_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="categoryCode != null">category_code = #{categoryCode},</if>
            <if test="categoryName != null">category_name = #{categoryName},</if>
            <if test="sortNo != null">sort_no = #{sortNo},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createUserCode != null">create_user_code = #{createUserCode},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateUserCode != null">update_user_code = #{updateUserCode},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="isUsed != null">is_used = #{isUsed},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateCategoryIsUsedByIds" parameterType="String">
        update materials_category
        set is_used = 0 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <delete id="deleteMaterialsCategoryById" parameterType="String">
        delete from materials_category where id = #{id}
    </delete>

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