<?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.xintong.visualinspection.dao.master.TaskDao" >
    <resultMap id="BaseResultMap" type="com.xintong.visualinspection.bean.Task" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="check_status" property="check_status" jdbcType="INTEGER" />
        <result column="check_status_name" property="check_status_name" jdbcType="VARCHAR" />
        <result column="video_id" property="video_id" jdbcType="VARCHAR" />
        <result column="create_time" property="create_time" jdbcType="VARCHAR" />
        <result column="update_time" property="update_time" jdbcType="VARCHAR" />
        <result column="start_time" property="start_time" jdbcType="VARCHAR" />
        <result column="end_time" property="end_time" jdbcType="VARCHAR" />
        <result column="checkman" property="checkman" jdbcType="INTEGER" />
        <result column="recheckman" property="recheckman" jdbcType="INTEGER" />
        <result column="checked_person" property="checked_person" jdbcType="INTEGER" />
        <result column="checked_dept" property="checked_dept" jdbcType="INTEGER" />
        <result column="rule_id" property="rule_id" jdbcType="INTEGER" />
        <result column="checked_location" property="checked_location" jdbcType="VARCHAR" />
        <result column="checked_location_type" property="checked_location_type" jdbcType="INTEGER" />
        <result column="remark" property="remark" jdbcType="VARCHAR" />
        <result column="check_period" property="check_period" jdbcType="VARCHAR" />
    </resultMap>
    <resultMap id="taskStatusMap" type="com.xintong.visualinspection.bean.TaskStatus">
        <id property="id" column="ID"/>
        <result property="task_id" column="task_id"/>
        <result property="update_time" column="update_time"/>
        <result property="update_user" column="update_user"/>
        <result property="update_username" column="update_username"/>
        <result property="check_status" column="check_status"/>
        <result property="check_status_name" column="check_status_name"/>
    </resultMap>

    <select id="getAll" resultMap="BaseResultMap"  >
        SELECT *
        FROM check_task
    </select>

    <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT *
        FROM check_task
        WHERE id = #{id}
    </select>
    
    <select id="getTaskStatusList" parameterType="java.lang.Long" resultMap="taskStatusMap" >
        SELECT *
        FROM check_task_status
        WHERE task_id = #{id}
        order by update_time 
    </select>
    
    <select id="getList" parameterType="com.xintong.visualinspection.bean.Task" resultMap="BaseResultMap" >
        SELECT *,
        		CONCAT(substr(start_time,1,16),'-',substr(end_time,12,5)) as check_period,
              (select a.code_name from sys_code a where a.code_flag='task_status' and a.code_value=check_status) as check_status_name
        FROM check_task
        WHERE 1=1
        	<if test="check_status != null">AND check_status = #{check_status}</if>
        	<if test="check_status_end != null">AND check_status &lt;= #{check_status_end}</if>
        	<if test="start_time != null">AND start_time &gt;= #{start_time}</if>
        	<if test="end_time != null">AND end_time &lt;= #{end_time}</if>
        	<if test="checked_dept != null">AND checked_dept = #{checked_dept}</if>
        	<if test="checkman != null">AND checkman = #{checkman}</if>
        	<if test="recheckman != null">AND recheckman = #{recheckman}</if>
        	<if test="checked_person != null">AND checked_person = #{checked_person}</if>
        	<if test="hasScore == 1">AND 
        		id in (select task_id from check_score)
        	</if>
        	<if test="hasScore == 2">AND 
        		id not in (select task_id from check_score)
        	</if>
        	<if test="dispatch_ids != null and dispatch_ids.length > 0">AND
        		id in 
        		<foreach collection="dispatch_ids" item="dispatch_id" index="index"
		            open="(" close=")" separator=",">
		            #{dispatch_id}
		        </foreach>
        	</if>
    </select>
    
    <select id="getListInStatus" parameterType="com.xintong.visualinspection.bean.Task" resultMap="BaseResultMap" >
        SELECT *,
        		CONCAT(substr(start_time,1,16),'-',substr(end_time,12,5)) as check_period,
              (select a.code_name from sys_code a where a.code_flag='task_status' and a.code_value=check_status) as check_status_name
        FROM check_task
        WHERE 1=1
       		<if test="check_status_arr != null and check_status_arr.length > 0">AND
        		check_status in 
        		<foreach collection="check_status_arr" item="status" index="index"
		            open="(" close=")" separator=",">
		            #{status}
		        </foreach>
        	</if>
        	<if test="start_time != null">AND start_time &gt;= #{start_time}</if>
        	<if test="end_time != null">AND end_time &lt;= #{end_time}</if>
        	<if test="checked_dept != null">AND checked_dept = #{checked_dept}</if>
        	<if test="checkman != null">AND checkman = #{checkman}</if>
        	<if test="recheckman != null">AND recheckman = #{recheckman}</if>
        	<if test="checked_person != null">AND checked_person = #{checked_person}</if>
        	<if test="hasScore == 1">AND 
        		id in (select task_id from check_score)
        	</if>
        	<if test="hasScore == 2">AND 
        		id not in (select task_id from check_score)
        	</if>
        	<if test="dispatch_ids != null and dispatch_ids.length > 0">AND
        		id in 
        		<foreach collection="dispatch_ids" item="dispatch_id" index="index"
		            open="(" close=")" separator=",">
		            #{dispatch_id}
		        </foreach>
        	</if>
    </select>
    <select id="getListByChecked" parameterType="com.xintong.visualinspection.bean.Task" resultMap="BaseResultMap" >
        SELECT *,
        	  CONCAT(substr(start_time,1,16),'-',substr(end_time,11,6)) as check_period,
              (select a.code_name from sys_code a where a.code_flag='task_status' and a.code_value=check_status) as check_status_name
        FROM check_task
        WHERE 1=1
        	<if test="check_status != null">AND check_status &gt;= #{check_status}</if>
        	<if test="start_time != null">AND start_time &gt;= #{start_time}</if>
        	<if test="end_time != null">AND end_time &lt;= #{end_time}</if>
        	<if test="checked_dept != null">AND checked_dept = #{checked_dept}</if>
        	<if test="checkman != null">AND checkman = #{checkman}</if>
        	<if test="recheckman != null">AND recheckman = #{recheckman}</if>
        	<if test="checked_person != null">AND checked_person = #{checked_person}</if>
    </select>

	<select id="getListBySubmited" parameterType="com.xintong.visualinspection.bean.Task" resultMap="BaseResultMap" >
        SELECT *,
        	  CONCAT(substr(start_time,1,16),'-',substr(end_time,11,6)) as check_period,
              (select a.code_name from sys_code a where a.code_flag='task_status' and a.code_value=check_status) as check_status_name
        FROM check_task
        WHERE 1=1
        	<if test="check_status != null">AND check_status &gt;= #{check_status}</if>
        	<if test="check_status_end != null">AND check_status &lt;= #{check_status_end}</if>
        	<if test="start_time != null">AND start_time &gt;= #{start_time}</if>
        	<if test="end_time != null">AND end_time &lt;= #{end_time}</if>
        	<if test="checked_dept != null">AND checked_dept = #{checked_dept}</if>
        	<if test="checkman != null">AND checkman = #{checkman}</if>
        	<if test="recheckman != null">AND recheckman = #{recheckman}</if>
        	<if test="checked_person != null">AND checked_person = #{checked_person}</if>
    </select>
    <insert id="insert" parameterType="com.xintong.visualinspection.bean.Task" >
        INSERT INTO check_task
        (name,check_status,video_id,create_time,update_time,start_time,
        	end_time,checkman,checked_person,checked_dept,rule_id,checked_location,
        	checked_location_type,remark
        )
        VALUES
        (#{name}, #{check_status},#{video_id},#{create_time},#{update_time},#{start_time},
        	#{end_time},#{checkman},#{checked_person},#{checked_dept},#{rule_id},#{checked_location},
        	#{checked_location_type},#{remark}
        )
    </insert>
    
    <insert id="insertStatus" parameterType="com.xintong.visualinspection.bean.TaskStatus" >
        INSERT INTO check_task_status
        (task_id,update_time,update_user,update_username,check_status,
        	check_status_name
        )
        VALUES
        (#{task_id}, now(),#{update_user},#{update_username},#{check_status},
        	#{check_status_name}
        )
    </insert>
    <insert id="insertStatusBatch" parameterType="java.util.List" >
        INSERT INTO check_task_status
        (task_id,update_time,update_user,update_username,check_status,
        	check_status_name
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator="," >  
	        (#{item.task_id}, #{item.update_time},#{item.update_user},#{item.update_username},#{item.check_status},
	        	#{item.check_status_name}
	        ) 
	    </foreach>  
    </insert>

    <update id="update" parameterType="com.xintong.visualinspection.bean.Task" >
        UPDATE
        check_task
        SET
        <if test="name != null">name = #{name},</if>
        <if test="check_status != null">check_status = #{check_status},</if>
        <if test="video_id != null">video_id = #{video_id},</if>
        <if test="update_time != null">update_time = #{update_time},</if>
        <if test="start_time != null">start_time = #{start_time},</if>
        <if test="end_time != null">end_time = #{end_time},</if>
        <if test="checkman != null">checkman = #{checkman},</if>
        <if test="recheckman != null">recheckman = #{recheckman},</if>
        <if test="checked_person != null">checked_person = #{checked_person},</if>
        <if test="checked_dept != null">checked_dept = #{checked_dept},</if>
        <if test="rule_id != null">rule_id = #{rule_id},</if>
        <if test="checked_location != null">checked_location = #{checked_location},</if>
        <if test="checked_location_type != null">checked_location_type = #{checked_location_type},</if>
        remark = #{remark}
        WHERE
        id = #{id}
    </update>
	
	<update id="dispatch" parameterType="com.xintong.visualinspection.bean.Task" >
        UPDATE
        	check_task
        SET
	        <if test="update_check_status != null">check_status = #{update_check_status},</if>
	        update_time = now()
        WHERE 1=1
        	<if test="check_status != null">AND check_status = #{check_status}</if>
        	<if test="start_time != null">AND create_time &gt;= #{start_time}</if>
        	<if test="end_time != null">AND create_time &lt;= #{end_time}</if>
        	<if test="checked_dept != null">AND checked_dept = #{checked_dept}</if>
        	<if test="checkman != null">AND checkman = #{checkman}</if>
        	<if test="checked_person != null">AND checked_person = #{checked_person}</if>
        	<if test="dispatch_ids != null and dispatch_ids.length > 0">AND
        		id in 
        		<foreach collection="dispatch_ids" item="dispatch_id" index="index"
		            open="(" close=")" separator=",">
		            #{dispatch_id}
		        </foreach>
        	</if>
    </update>
    
    <update id="dispatchById" parameterType="com.xintong.visualinspection.bean.Task" >
        UPDATE
        	check_task
        SET
	        <if test="check_status != null">check_status = #{check_status},</if>
	        update_time = now()
        WHERE id=#{id}
    </update>
    
    <delete id="delete" parameterType="java.lang.Long" >
        DELETE FROM
        check_task
        WHERE
        id =#{id}
    </delete>
    <delete id="deleteInvalidTaskCount" parameterType="java.lang.Long" >
        DELETE FROM
        	check_user_count
        WHERE
        	task_id =#{id}
    </delete>
    <update id="updateInvalidTaskCount" parameterType="com.xintong.visualinspection.bean.Task" >
        UPDATE
        	check_user_count
        SET
	        <if test="checked_person != null">user_id = #{checked_person}</if>
        WHERE task_id=#{id}
    </update>
</mapper>