<?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.system.mapper.IndustryCategoryMapper">
    
    <resultMap type="IndustryCategory" id="IndustryCategoryResult">
        <result property="categoryId"    column="category_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="categoryName"    column="category_name"    />
        <result property="orderNum"    column="order_num"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <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="selectIndustryCategoryVo">
        select category_id, parent_id, ancestors, category_name, order_num, status, del_flag, create_by, create_time, update_by, update_time from industry_category
    </sql>

    <select id="selectIndustryCategoryList" parameterType="IndustryCategory" resultMap="IndustryCategoryResult">
        <include refid="selectIndustryCategoryVo"/>
        <where>  
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="categoryName != null  and categoryName != ''"> and category_name like concat('%', #{categoryName}, '%')</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectIndustryCategoryByCategoryId" parameterType="Long" resultMap="IndustryCategoryResult">
        <include refid="selectIndustryCategoryVo"/>
        where category_id = #{categoryId}
    </select>
    <select id="selectChildrenIndustryById" resultMap="IndustryCategoryResult" parameterType="java.lang.Long">
        select * from industry_category where find_in_set(#{id}, ancestors)
    </select>

    <insert id="insertIndustryCategory" parameterType="IndustryCategory" useGeneratedKeys="true" keyProperty="categoryId">
        insert into industry_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="categoryName != null">category_name,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</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="categoryName != null">#{categoryName},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</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="updateIndustryCategory" parameterType="IndustryCategory">
        update industry_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="categoryName != null">category_name = #{categoryName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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 category_id = #{categoryId}
    </update>
    <update id="updateIndustryChildren" parameterType="java.util.List">
        update industry_category set ancestors =
        <foreach collection="industrys" item="item" index="index"
                 separator=" " open="case category_id" close="end">
            when #{item.categoryId} then #{item.ancestors}
        </foreach>
        where category_id in
        <foreach collection="industrys" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.categoryId}
        </foreach>
    </update>
    <update id="updateDeptStatusNormal" parameterType="Long">
        update industry_category set status = '0' where category_id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <delete id="deleteIndustryCategoryByCategoryId" parameterType="Long">
        delete from industry_category where category_id = #{categoryId}
    </delete>

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