<?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.zehong.system.mapper.TWorkTaskPatrolMapper">
    
    <resultMap type="TWorkTaskPatrol" id="TWorkTaskPatrolResult">
        <result property="patrolId"    column="patrol_id"    />
        <result property="taskId"    column="task_id"    />
        <result property="memberId"    column="member_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="memberName"    column="member_name"    />
    </resultMap>

    <sql id="selectTWorkTaskPatrolVo">
        select patrol_id, task_id, member_id, create_time, end_time,(select person_name from t_line_patrol_person where person_id = member_id) as member_name from t_work_task_patrol
    </sql>

    <select id="selectTWorkTaskPatrolList" parameterType="TWorkTaskPatrol" resultMap="TWorkTaskPatrolResult">
        <include refid="selectTWorkTaskPatrolVo"/>
        <where>  
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="memberId != null "> and member_id = #{memberId}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectTWorkTaskPatrolById" parameterType="Long" resultMap="TWorkTaskPatrolResult">
        <include refid="selectTWorkTaskPatrolVo"/>
        where patrol_id = #{patrolId}
    </select>
        
    <insert id="insertTWorkTaskPatrol" parameterType="TWorkTaskPatrol" useGeneratedKeys="true" keyProperty="patrolId">
        insert into t_work_task_patrol
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="memberId != null">member_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="endTime != null">end_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="memberId != null">#{memberId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="endTime != null">#{endTime},</if>
         </trim>
    </insert>

    <update id="updateTWorkTaskPatrol" parameterType="TWorkTaskPatrol">
        update t_work_task_patrol
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="memberId != null">member_id = #{memberId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
        </trim>
        where patrol_id = #{patrolId}
    </update>
    <update id="updatebytmid" parameterType="TWorkTaskPatrol">
         update t_work_task_patrol set end_time =#{endTime}
         where task_id =#{taskId} and member_id = #{memberId} and end_time is null
    </update>
    <delete id="deleteTWorkTaskPatrolById" parameterType="Long">
        delete from t_work_task_patrol where patrol_id = #{patrolId}
    </delete>

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

    <select id="patrolStatistic" parameterType="TWorkTaskPatrol" resultType="java.util.HashMap">
        SELECT
            DATE_FORMAT(patrol.create_time, "%Y-%m-%d") AS timeType,
            COUNT(1) AS countNum
        FROM
            t_work_task_patrol patrol
        LEFT JOIN t_work_task task ON task.task_id = patrol.task_id
        <where>
            patrol.create_time BETWEEN concat(YEAR(now()),'-01-01 00:00:00') AND concat(YEAR(now()),'-12-31 23:59:59')
            <if test="taskTitle != null">
                and task.task_title like concat('%', #{taskTitle}, '%')
            </if>
            <if test="createEnterpriseId != null "> and task.create_enterprise_id = #{createEnterpriseId}</if>
            <if test="taskStatus != null "> and task.status = #{taskStatus}</if>
        </where>
        GROUP BY timeType
    </select>
</mapper>