<?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.ibs.task.mapper.WorkTemplateMapper">

    <sql id="selectTemplateVo">
        select id, post_id, work_type, work_detail, frequency_type, create_time, create_by, is_enabled, feedback
        from work_template
    </sql>

    <insert id="insertWorkTemplate" parameterType="WorkTemplate" useGeneratedKeys="true" keyProperty="id">
        insert into work_template(
            <if test="postId != null">post_id,</if>
            <if test="workType != null">work_type,</if>
            <if test="workDetail != null">work_detail,</if>
            <if test="feedback != null">feedback,</if>
            <if test="frequencyType != null">frequency_type,</if>
            <if test="createBy != null">create_by,</if>
            <if test="isEnabled != null">is_enabled,</if>
            del_flag,
            create_time
        )values (
            <if test="postId != null">#{postId},</if>
            <if test="workType != null">#{workType},</if>
            <if test="workDetail != null">#{workDetail},</if>
            <if test="feedback != null">#{feedback},</if>
            <if test="frequencyType != null">#{frequencyType},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="isEnabled != null">#{isEnabled},</if>
            0,
            sysdate()
        )
    </insert>

    <select id="selectWorkTemplateList" parameterType="WorkTemplateDTO" resultType="WorkTemplateVO">
        select
            t.id,
            t.post_id,
            t.work_type,
            t.work_detail,
            t.frequency_type,
            t.create_time,
            t.create_by,
            t.is_enabled,
            t.feedback,
            p.post_name
        from work_template t
        left join sys_post p on t.post_id = p.post_id
        <where>
            t.del_flag = 0
            and t.post_id in
            <foreach collection="postIds" item="postId" open="(" separator="," close=")">
                #{postId}
            </foreach>
            <if test="dto.postId != null">
                and t.post_id = #{dto.postId}
            </if>
            <if test="dto.frequencyType != null and dto.frequencyType != ''">
                and t.frequency_type = #{dto.frequencyType}
            </if>
        </where>
        order by t.frequency_type, t.post_id
    </select>

    <update id="updateWorkTemplate">
        update work_template
        <set>
            <if test="postId != null">post_id = #{postId},</if>
            <if test="workType != null">work_type = #{workType},</if>
            <if test="workDetail != null">work_detail = #{workDetail},</if>
            <if test="feedback != null">feedback = #{feedback},</if>
            <if test="frequencyType != null">frequency_type = #{frequencyType},</if>
            <if test="isEnabled != null">is_enabled = #{isEnabled},</if>
        </set>
        where id = #{id}
    </update>

    <update id="deleteWorkTemplateByIds" >
        update work_template
        set del_flag = 2
        where id in
        <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>