<?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.projectV2.ZsyzSkcyMapper">
    
    <resultMap type="ZsyzSkcy" id="ZsyzSkcyResult">
        <result property="skcyId"    column="skcy_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="deptName"    column="dept_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="selectZsyzSkcyVo">
        select skcy_id, parent_id, ancestors, dept_name, order_num, status, del_flag, create_by, create_time, update_by, update_time from zsyz_skcy
    </sql>

    <select id="selectZsyzSkcyList" parameterType="ZsyzSkcy" resultMap="ZsyzSkcyResult">
        <include refid="selectZsyzSkcyVo"/>
        <where>  
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectZsyzSkcyBySkcyId" parameterType="Long" resultMap="ZsyzSkcyResult">
        <include refid="selectZsyzSkcyVo"/>
        where skcy_id = #{skcyId}
    </select>
    <select id="selectChildrenSkcyById" resultType="com.ruoyi.system.domain.projectV2.ZsyzSkcy">
        select * from zsyz_skcy where find_in_set(#{skcyId}, ancestors)
    </select>

    <insert id="insertZsyzSkcy" parameterType="ZsyzSkcy" useGeneratedKeys="true" keyProperty="skcyId">
        insert into zsyz_skcy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="deptName != null">dept_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="deptName != null">#{deptName},</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="updateZsyzSkcy" parameterType="ZsyzSkcy">
        update zsyz_skcy
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="deptName != null">dept_name = #{deptName},</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 skcy_id = #{skcyId}
    </update>
    <update id="updateZsyzSkcyChildren" parameterType="java.util.List">
            update zsyz_skcy set ancestors =
            <foreach collection="zsyzSkcys" item="item" index="index"
                     separator=" " open="case skcy_id" close="end">
                when #{item.skcyId} then #{item.ancestors}
            </foreach>
            where skcy_id in
            <foreach collection="zsyzSkcys" item="item" index="index"
                     separator="," open="(" close=")">
                #{item.skcyId}
            </foreach>

    </update>
    <update id="updateZsyzSkcyStatusNormal" parameterType="Long">
        update zsyz_skcy set status = '0' where skcy_id in
        <foreach collection="array" item="skcyId" open="(" separator="," close=")">
            #{skcyId}
        </foreach>
    </update>

    <select id="hasChildBySkcyId" parameterType="Long" resultType="int">
		select count(1) from zsyz_skcy
		where del_flag = '0' and parent_id = #{SkcyId} limit 1
	</select>

    <delete id="deleteZsyzSkcyBySkcyId" parameterType="Long">
        delete from zsyz_skcy where skcy_id = #{skcyId}
    </delete>

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