<?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.ems.mapper.FacsSubCategoryMapper">
    
    <resultMap type="com.ruoyi.ems.domain.FacsSubCategory" id="subCategoryResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="parentCode"    column="parent_code"    />
        <result property="desc"    column="desc"    />
        <result property="version"    column="version"    />
    </resultMap>

    <sql id="selectSubCategoryVo">
        select `id`, `code`, `name`, `parent_code`, `desc`, `version` from dim_ems_facs_subcategory
    </sql>

    <select id="selectSubCategoryList" parameterType="com.ruoyi.ems.domain.FacsSubCategory" resultMap="subCategoryResult">
        <include refid="selectSubCategoryVo"/>
        <where>  
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="parentCode != null  and parentCode != ''"> and parent_code = #{parentCode}</if>
            <if test="desc != null  and desc != ''"> and desc = #{desc}</if>
            <if test="version != null  and version != ''"> and version = #{version}</if>
        </where>
    </select>
    
    <select id="selectSubCategoryById" parameterType="Long" resultMap="subCategoryResult">
        <include refid="selectSubCategoryVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSubCategory" parameterType="com.ruoyi.ems.domain.FacsSubCategory" useGeneratedKeys="true" keyProperty="id">
        insert into dim_ems_facs_subcategory
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null and code != ''">`code`,</if>
            <if test="name != null and name != ''">`name`,</if>
            <if test="parentCode != null and parentCode != ''">`parent_code`,</if>
            <if test="desc != null">`desc`,</if>
            <if test="version != null">`version`,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null and code != ''">#{code},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="parentCode != null and parentCode != ''">#{parentCode},</if>
            <if test="desc != null">#{desc},</if>
            <if test="version != null">#{version},</if>
         </trim>
    </insert>

    <insert id="insertSubCategoryBatch" parameterType="java.util.List">
        insert into dim_ems_facs_subcategory (`code`, `name`, `parent_code`, `desc`, `version`) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.code},#{item.name},#{item.parentCode},#{item.desc},#{item.version})
        </foreach>
    </insert>

    <update id="updateSubCategory" parameterType="com.ruoyi.ems.domain.FacsSubCategory">
        update dim_ems_facs_subcategory
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null and code != ''">`code` = #{code},</if>
            <if test="name != null and name != ''">`name` = #{name},</if>
            <if test="parentCode != null and parentCode != ''">`parent_code` = #{parentCode},</if>
            <if test="desc != null">`desc` = #{desc},</if>
            <if test="version != null">`version` = #{version},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSubCategoryById" parameterType="Long">
        delete from dim_ems_facs_subcategory where `id` = #{id}
    </delete>

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

    <delete id="deleteSubCategoryByParentCode" parameterType="String">
        delete from dim_ems_facs_subcategory where `parent_code` = #{code}
    </delete>
</mapper>