<?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.project.benyi.mapper.ByDayFlowDetailMapper">

    <resultMap type="ByDayFlowDetail" id="ByDayFlowDetailResult">
        <result property="id" column="id"/>
        <result property="sortNumber" column="sort_number"/>
        <result property="parentId" column="parent_id"/>
        <result property="targetCount" column="target_count"/>
        <result property="name" column="name"/>
        <result property="content" column="content"/>
        <result property="note" column="note"/>
        <result property="flowType" column="flow_type"/>
        <result property="createUser" column="create_user"/>
        <result property="updateUser" column="update_user"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectByDayFlowDetailVo">
        select id, sort_number, (select count(*) from by_day_flow_task where detail_id=id) target_count, name, content, note, flow_type, create_user, update_user, create_time, update_time
        from by_day_flow_detail
    </sql>

    <sql id="selectByDayFlowDetailsVo">
        select * from(
select dict_value+9999 id,dict_label name,0 parent_id,dict_sort sort_number from sys_dict_data where dict_type='sys_dayflow_type'
union all
select id,name,flow_type+9999,sort_number from by_day_flow_detail ) t
    </sql>

    <select id="selectByDayFlowDetailList" parameterType="ByDayFlowDetail" resultMap="ByDayFlowDetailResult">
        <include refid="selectByDayFlowDetailVo"/>
        <where>
            <if test="sortNumber != null ">and sort_number = #{sortNumber}</if>
            <if test="targetCount != null ">and target_count = #{targetCount}</if>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="content != null  and content != ''">and content = #{content}</if>
            <if test="note != null  and note != ''">and note = #{note}</if>
            <if test="flowType != null ">and flow_type = #{flowType}</if>
            <if test="createUser != null ">and create_user = #{createUser}</if>
            <if test="updateUser != null ">and update_user = #{updateUser}</if>
        </where>
    </select>

    <select id="selectByDayFlowDetailListTree" parameterType="ByDayFlowDetail" resultMap="ByDayFlowDetailResult">
        <include refid="selectByDayFlowDetailsVo"/>
        <where>
            <if test="sortNumber != null ">and sort_number = #{sortNumber}</if>
            <if test="targetCount != null ">and target_count = #{targetCount}</if>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="content != null  and content != ''">and content = #{content}</if>
            <if test="note != null  and note != ''">and note = #{note}</if>
            <if test="flowType != null ">and flow_type = #{flowType}</if>
            <if test="createUser != null ">and create_user = #{createUser}</if>
            <if test="updateUser != null ">and update_user = #{updateUser}</if>
        </where>
    </select>

    <select id="selectByDayFlowDetailById" parameterType="Long" resultMap="ByDayFlowDetailResult">
        <include refid="selectByDayFlowDetailVo"/>
        where id = #{id}
    </select>

    <select id="selectChildrenByDayFlowDetailById" parameterType="Long" resultMap="ByDayFlowDetailResult">
		select * from sys_dept where find_in_set(#{deptId})
	</select>

    <insert id="insertByDayFlowDetail" parameterType="ByDayFlowDetail" useGeneratedKeys="true" keyProperty="id">
        insert into by_day_flow_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sortNumber != null ">sort_number,</if>
            <if test="targetCount != null ">target_count,</if>
            <if test="name != null  and name != ''">name,</if>
            <if test="content != null  and content != ''">content,</if>
            <if test="note != null  and note != ''">note,</if>
            <if test="flowType != null ">flow_type,</if>
            <if test="createUser != null ">create_user,</if>
            <if test="updateUser != null ">update_user,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateTime != null ">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sortNumber != null ">#{sortNumber},</if>
            <if test="targetCount != null ">#{targetCount},</if>
            <if test="name != null  and name != ''">#{name},</if>
            <if test="content != null  and content != ''">#{content},</if>
            <if test="note != null  and note != ''">#{note},</if>
            <if test="flowType != null ">#{flowType},</if>
            <if test="createUser != null ">#{createUser},</if>
            <if test="updateUser != null ">#{updateUser},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateTime != null ">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateByDayFlowDetail" parameterType="ByDayFlowDetail">
        update by_day_flow_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="sortNumber != null ">sort_number = #{sortNumber},</if>
            <if test="targetCount != null ">target_count = #{targetCount},</if>
            <if test="name != null  and name != ''">name = #{name},</if>
            <if test="content != null  and content != ''">content = #{content},</if>
            <if test="note != null  and note != ''">note = #{note},</if>
            <if test="flowType != null ">flow_type = #{flowType},</if>
            <if test="createUser != null ">create_user = #{createUser},</if>
            <if test="updateUser != null ">update_user = #{updateUser},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteByDayFlowDetailById" parameterType="Long">
        delete from by_day_flow_detail where id = #{id}
    </delete>

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

</mapper>