<?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.asset.mapper.AmsAssetCategoryMapper">
    
    <resultMap type="AmsAssetCategory" id="AmsAssetCategoryResult">
        <result property="categoryId"    column="category_id"    />
        <result property="categoryCode"    column="category_code"    />
        <result property="categoryName"    column="category_name"    />
        <result property="defaultMaintenanceCycle"    column="default_maintenance_cycle"    />
        <result property="defaultUseLife"    column="default_use_life"    />
        <result property="enabled"    column="enabled"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectAmsAssetCategoryVo">
        select category_id, category_code, category_name, default_maintenance_cycle, default_use_life, enabled, create_by, create_time, update_by, update_time, remark, del_flag from ams_asset_category
    </sql>

    <select id="selectAmsAssetCategoryList" parameterType="AmsAssetCategory" resultMap="AmsAssetCategoryResult">
        <include refid="selectAmsAssetCategoryVo"/>
        <where>
            del_flag = '0'
            <if test="categoryCode != null  and categoryCode != ''"> and category_code = #{categoryCode}</if>
            <if test="categoryName != null  and categoryName != ''"> and category_name like concat('%', #{categoryName}, '%')</if>
            <if test="enabled != null  and enabled != ''"> and enabled = #{enabled}</if>
        </where>
    </select>
    
    <select id="selectAmsAssetCategoryByCategoryId" parameterType="Long" resultMap="AmsAssetCategoryResult">
        <include refid="selectAmsAssetCategoryVo"/>
        where category_id = #{categoryId} and del_flag = '0'
    </select>

    <select id="checkCategoryCodeUnique" parameterType="String" resultMap="AmsAssetCategoryResult">
        <include refid="selectAmsAssetCategoryVo"/>
        where category_code = #{categoryCode} and del_flag = '0' limit 1
    </select>

    <select id="countCategoryReferences" parameterType="Long" resultType="int">
        select
            (select count(1) from ams_asset where category_id = #{categoryId} and del_flag = '0')
            + (select count(1) from ams_inventory_task where category_id = #{categoryId} and del_flag = '0')
            + (select count(1) from ams_inventory_task_item where category_id = #{categoryId} and del_flag = '0')
            + (select count(1) from ams_inventory_task_surplus where category_id = #{categoryId} and del_flag = '0')
    </select>

    <insert id="insertAmsAssetCategory" parameterType="AmsAssetCategory" useGeneratedKeys="true" keyProperty="categoryId">
        insert into ams_asset_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="categoryCode != null and categoryCode != ''">category_code,</if>
            <if test="categoryName != null and categoryName != ''">category_name,</if>
            <if test="defaultMaintenanceCycle != null">default_maintenance_cycle,</if>
            <if test="defaultUseLife != null">default_use_life,</if>
            <if test="enabled != null">enabled,</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>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="categoryCode != null and categoryCode != ''">#{categoryCode},</if>
            <if test="categoryName != null and categoryName != ''">#{categoryName},</if>
            <if test="defaultMaintenanceCycle != null">#{defaultMaintenanceCycle},</if>
            <if test="defaultUseLife != null">#{defaultUseLife},</if>
            <if test="enabled != null">#{enabled},</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>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateAmsAssetCategory" parameterType="AmsAssetCategory">
        update ams_asset_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="categoryCode != null and categoryCode != ''">category_code = #{categoryCode},</if>
            <if test="categoryName != null and categoryName != ''">category_name = #{categoryName},</if>
            <if test="defaultMaintenanceCycle != null">default_maintenance_cycle = #{defaultMaintenanceCycle},</if>
            <if test="defaultUseLife != null">default_use_life = #{defaultUseLife},</if>
            <if test="enabled != null">enabled = #{enabled},</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>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where category_id = #{categoryId}
    </update>

    <update id="deleteAmsAssetCategoryByCategoryId" parameterType="Long">
        update ams_asset_category set del_flag = '1' where category_id = #{categoryId}
    </update>

    <update id="deleteAmsAssetCategoryByCategoryIds" parameterType="String">
        update ams_asset_category set del_flag = '1' where category_id in
        <foreach item="categoryId" collection="array" open="(" separator="," close=")">
            #{categoryId}
        </foreach>
    </update>

</mapper>
