<?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"/>
        <result column="class_type" property="class_type" jdbcType="INTEGER"/>
        <result column="period_id" property="period_id" jdbcType="INTEGER"/>
        <result column="monitor_user_id" property="monitor_user_id" jdbcType="INTEGER"/>
        <result column="appeal_result" property="appeal_result" jdbcType="INTEGER"/>
        <result column="invalid_from" property="invalidFrom" jdbcType="INTEGER"/>
    </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>
    <resultMap id="userCountMap" type="com.xintong.visualinspection.bean.UserClassCount">
        <id property="id" column="ID"/>
        <result property="task_id" column="id"/>
        <result property="user_id" column="checked_person"/>
        <result property="class_type" column="class_type"/>
        <result property="period_id" column="period_id"/>
    </resultMap>

    <select id="getAll" resultMap="BaseResultMap">
        SELECT *
        FROM check_task
        order by start_time desc
    </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="getTaskListByDept" parameterType="com.xintong.visualinspection.pojo.TaskListByDeptAppeal"
            resultMap="BaseResultMap">
        select * from check_task where id in
        (select task_id from check_appeal where appeal_dept = #{deptId}
        <if test="start_time != null">AND appeal_time &gt;= #{start_time}</if>
        <if test="end_time != null">AND appeal_time &lt;= #{end_time}</if>
        <if test="status != null">AND appeal_result = #{status}</if>
        )
        order by start_time desc
    </select>


    <select id="getList" parameterType="com.xintong.visualinspection.bean.Task" resultMap="BaseResultMap">
        SELECT t.*,
        CONCAT(substr(t.start_time,1,16),'-',substr(t.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=t.check_status) as
        check_status_name,
        ca.appeal_result as appeal_result
        FROM check_task t left join t_sys_users u on t.checked_person=u.id
        left join check_appeal ca on t.id=ca.task_id
        WHERE 1=1
        <if test="check_status != null">AND t.check_status = #{check_status}</if>
        <if test="check_status_end != null">AND t.check_status &lt;= #{check_status_end}</if>
        <if test="start_time != null">AND t.start_time &gt;= #{start_time}</if>
        <if test="end_time != null">AND t.end_time &lt;= #{end_time}</if>
        <if test="checked_dept != null">AND t.checked_dept = #{checked_dept}</if>
        <if test="checkman != null">AND t.checkman = #{checkman}</if>
        <if test="recheckman != null">AND t.recheckman = #{recheckman}</if>
        <if test="checked_person != null">AND t.checked_person = #{checked_person}</if>
        <if test="position_id != null">AND u.positionid = #{position_id}</if>
        <if test="appeal_result != null">AND ca.appeal_result = #{appeal_result}</if>
        <if test="hasScore == 1">AND
            t.id in (select task_id from check_score)
        </if>
        <if test="hasScore == 2">AND
            t.id not in (select task_id from check_score)
        </if>
        <if test="dispatch_ids != null and dispatch_ids.length > 0">AND
            t.id in
            <foreach collection="dispatch_ids" item="dispatch_id" index="index"
                     open="(" close=")" separator=",">
                #{dispatch_id}
            </foreach>
        </if>
        <if test="invalidFrom == -1">AND
            ( t.invalid_from = 0 OR t.invalid_from = 1)
        </if>

        <if test="invalidFrom != null  and invalidFrom != -1">AND
            t.invalid_from = #{invalidFrom}
        </if>

        order by t.start_time desc
    </select>

    <select id="getListInStatus" parameterType="com.xintong.visualinspection.bean.Task" resultMap="BaseResultMap">
        SELECT t.*,
        CONCAT(substr(t.start_time,1,16),'-',substr(t.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=t.check_status) as
        check_status_name,
        ca.appeal_result as appeal_result
        FROM check_task t left join t_sys_users u on t.checked_person=u.id
        left join check_appeal ca on t.id=ca.task_id
        WHERE 1=1
        <if test="check_status_arr != null and check_status_arr.length > 0">AND
            t.check_status in
            <foreach collection="check_status_arr" item="status" index="index"
                     open="(" close=")" separator=",">
                #{status}
            </foreach>
        </if>
        <if test="start_time != null">AND t.start_time &gt;= #{start_time}</if>
        <if test="end_time != null">AND t.end_time &lt;= #{end_time}</if>
        <if test="checked_dept != null">AND t.checked_dept = #{checked_dept}</if>
        <if test="checkman != null">AND t.checkman = #{checkman}</if>
        <if test="recheckman != null">AND t.recheckman = #{recheckman}</if>
        <if test="checked_person != null">AND t.checked_person = #{checked_person}</if>
        <if test="position_id != null">AND u.positionid = #{position_id}</if>
        <if test="appeal_result != null">AND ca.appeal_result = #{appeal_result}</if>
        <if test="hasScore == 1">AND
            t.id in (select task_id from check_score)
        </if>
        <if test="hasScore == 2">AND
            t.id not in (select task_id from check_score)
        </if>
        <if test="dispatch_ids != null and dispatch_ids.length > 0">AND
            t.id in
            <foreach collection="dispatch_ids" item="dispatch_id" index="index"
                     open="(" close=")" separator=",">
                #{dispatch_id}
            </foreach>
        </if>
        <if test="invalidFrom == -1">AND
            ( t.invalid_from = 0 OR t.invalid_from = 1)
        </if>

        <if test="invalidFrom != null  and invalidFrom != -1">AND
            t.invalid_from = #{invalidFrom}
        </if>
        order by t.start_time desc
    </select>
    <select id="getListByChecked" parameterType="com.xintong.visualinspection.bean.Task" resultMap="BaseResultMap">
        SELECT t.*,
        CONCAT(substr(t.start_time,1,16),'-',substr(t.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=t.check_status) as
        check_status_name,
        ca.appeal_result as appeal_result
        FROM check_task t left join t_sys_users u on t.checked_person=u.id
        left join check_appeal ca on t.id=ca.task_id
        WHERE 1=1
        <if test="check_status != null">AND t.check_status &gt;= #{check_status}</if>
        <if test="start_time != null">AND t.start_time &gt;= #{start_time}</if>
        <if test="end_time != null">AND t.end_time &lt;= #{end_time}</if>
        <if test="checked_dept != null">AND t.checked_dept = #{checked_dept}</if>
        <if test="checkman != null">AND t.checkman = #{checkman}</if>
        <if test="recheckman != null">AND t.recheckman = #{recheckman}</if>
        <if test="period_id != null">AND t.period_id = #{period_id}</if>
        <if test="checked_person != null">AND t.checked_person = #{checked_person}</if>
        <if test="appeal_result != null">AND ca.appeal_result = #{appeal_result}</if>
        <if test="invalidFrom == -1">AND
            ( t.invalid_from = 0 OR t.invalid_from = 1)
        </if>

        <if test="invalidFrom != null  and invalidFrom != -1">AND
            t.invalid_from = #{invalidFrom}
        </if>
        order by t.start_time desc
    </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>
        <if test="invalidFrom == -1">AND
            ( t.invalid_from = 0 OR t.invalid_from = 1)
        </if>

        <if test="invalidFrom != null  and invalidFrom != -1">AND
            t.invalid_from = #{invalidFrom}
        </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>
        <if test="invalidFrom != null">
            invalid_from = #{invalidFrom},
        </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>
        <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>
    </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>
    <select id="getUserCount" parameterType="com.xintong.visualinspection.bean.Task" resultMap="userCountMap">
        SELECT *
        FROM check_task
        WHERE check_status!=22
        and checked_person = #{checked_person}
        <if test="class_type != null">and class_type = #{class_type}</if>
        and period_id = (
        select period_id
        from check_task
        where id = #{id}
        )
    </select>
    <select id="getTaskStatusByType" parameterType="map" resultMap="taskStatusMap">
        SELECT *
        FROM check_status
        WHERE task_id = #{task_id}
          and check_status = #{check_status}
        order by update_time desc
    </select>
</mapper>