<?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.ygtx.worklog.mapper.CollaborateTaskMapper">

    <resultMap type="CollaborateTask" id="CollaborateTaskResult">
        <result property="id"    column="id"    />
        <result property="sourceReportId"    column="source_report_id"    />
        <result property="sourceUserId"    column="source_user_id"    />
        <result property="sourceUserName"    column="source_user_name"    />
        <result property="collaboratorId"    column="collaborator_id"    />
        <result property="collaboratorName"    column="collaborator_name"    />
        <result property="taskContent"    column="task_content"    />
        <result property="taskDate"    column="task_date"    />
        <result property="status"    column="status"    />
        <result property="targetReportId"    column="target_report_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="processTime"    column="process_time"    />
    </resultMap>

    <sql id="selectCollaborateTaskVo">
        select id, source_report_id, source_user_id, source_user_name, collaborator_id,
               collaborator_name, task_content, task_date, status, target_report_id,
               create_time, process_time
        from wl_collaborate_task
    </sql>

    <select id="selectCollaborateTaskList" parameterType="CollaborateTask" resultMap="CollaborateTaskResult">
        <include refid="selectCollaborateTaskVo"/>
        <where>
            <if test="sourceReportId != null "> and source_report_id = #{sourceReportId}</if>
            <if test="sourceUserId != null "> and source_user_id = #{sourceUserId}</if>
            <if test="collaboratorId != null "> and collaborator_id = #{collaboratorId}</if>
            <if test="taskDate != null "> and task_date = #{taskDate}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectCollaborateTaskById" parameterType="Long" resultMap="CollaborateTaskResult">
        <include refid="selectCollaborateTaskVo"/>
        where id = #{id}
    </select>

    <select id="selectPendingTasksByDate" resultMap="CollaborateTaskResult">
        <include refid="selectCollaborateTaskVo"/>
        where collaborator_id = #{collaboratorId}
        and task_date = #{taskDate}
        and status = '0'
        order by create_time asc
    </select>

    <insert id="insertCollaborateTask" parameterType="CollaborateTask" useGeneratedKeys="true" keyProperty="id">
        insert into wl_collaborate_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sourceReportId != null">source_report_id,</if>
            <if test="sourceUserId != null">source_user_id,</if>
            <if test="sourceUserName != null and sourceUserName != ''">source_user_name,</if>
            <if test="collaboratorId != null">collaborator_id,</if>
            <if test="collaboratorName != null and collaboratorName != ''">collaborator_name,</if>
            <if test="taskContent != null">task_content,</if>
            <if test="taskDate != null">task_date,</if>
            <if test="status != null">status,</if>
            <if test="targetReportId != null">target_report_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="processTime != null">process_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sourceReportId != null">#{sourceReportId},</if>
            <if test="sourceUserId != null">#{sourceUserId},</if>
            <if test="sourceUserName != null and sourceUserName != ''">#{sourceUserName},</if>
            <if test="collaboratorId != null">#{collaboratorId},</if>
            <if test="collaboratorName != null and collaboratorName != ''">#{collaboratorName},</if>
            <if test="taskContent != null">#{taskContent},</if>
            <if test="taskDate != null">#{taskDate},</if>
            <if test="status != null">#{status},</if>
            <if test="targetReportId != null">#{targetReportId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="processTime != null">#{processTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertCollaborateTask" parameterType="java.util.List">
        insert into wl_collaborate_task(source_report_id, source_user_id, source_user_name,
                                        collaborator_id, collaborator_name, task_content,
                                        task_date, status, create_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.sourceReportId}, #{item.sourceUserId}, #{item.sourceUserName},
             #{item.collaboratorId}, #{item.collaboratorName}, #{item.taskContent},
             #{item.taskDate}, #{item.status}, now())
        </foreach>
    </insert>

    <update id="updateCollaborateTask" parameterType="CollaborateTask">
        update wl_collaborate_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="sourceReportId != null">source_report_id = #{sourceReportId},</if>
            <if test="sourceUserId != null">source_user_id = #{sourceUserId},</if>
            <if test="sourceUserName != null and sourceUserName != ''">source_user_name = #{sourceUserName},</if>
            <if test="collaboratorId != null">collaborator_id = #{collaboratorId},</if>
            <if test="collaboratorName != null and collaboratorName != ''">collaborator_name = #{collaboratorName},</if>
            <if test="taskContent != null">task_content = #{taskContent},</if>
            <if test="taskDate != null">task_date = #{taskDate},</if>
            <if test="status != null">status = #{status},</if>
            <if test="targetReportId != null">target_report_id = #{targetReportId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="processTime != null">process_time = #{processTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCollaborateTaskById" parameterType="Long">
        delete from wl_collaborate_task where id = #{id}
    </delete>

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