<?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.visuallnspection_fjq.dao.FwqTaskDao">
    <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>

    <insert id="addtask" useGeneratedKeys="true"  keyProperty="id"  parameterType="com.xintong.visualinspection.bean.FwqCheckTask">
        INSERT INTO check_task
        (name,check_status,area_id,create_time,update_time,start_time,
        	end_time,checkman,checked_dept,rule_id,remark,period_id
        )
        VALUES
        (#{name}, #{check_status},#{area_id},now(),now(),#{start_time},
        	#{end_time},#{checkman},#{checked_dept},#{rule_id},#{remark},#{period_id}
        )
    </insert>


    <select id="getone" parameterType="com.xintong.visualinspection.bean.FwqCheckTask" resultType="com.xintong.visualinspection.bean.FwqCheckTask">
        select *,CONCAT(substr(start_time,1,16),'-',substr(end_time,12,5)) as check_period from check_task where id = #{id}
        ORDER BY start_time DESC
    </select>

    <select id="getall" parameterType="com.xintong.visualinspection.bean.FwqCheckTask" resultType="com.xintong.visualinspection.bean.FwqCheckTask">
        select t.*,CONCAT(substr(t.start_time,1,16),'-',substr(t.end_time,12,5)) as check_period,(select name from area_info where id = t.area_id) as area_name
        ,ca.appeal_result as appeal_result
        from check_task t
        left join check_appeal ca on t.id=ca.task_id
        where 1=1
        <if test="period_id != null">AND t.period_id = #{period_id}</if>
        <if test="checked_dept != null">AND t.checked_dept = #{checked_dept}</if>
        <if test="start_time != null and end_time != null">AND t.start_time >= #{start_time} <![CDATA[ AND t.end_time < #{end_time}]]> </if>
        <choose>
            <when test="check_status_end != null and check_status != null">
                AND t.check_status &gt;= #{check_status} AND t.check_status &lt;= #{check_status_end}
            </when>
            <otherwise>
                <if test="check_status != null"> AND t.check_status = #{check_status}</if>
            </otherwise>
        </choose>

        <if test="checkman != null">AND t.checkman = #{checkman}</if>
        <if test="area_id != null">AND t.area_id = #{area_id}</if>
        <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="area_id != null">AND t.area_id = #{area_id}</if>
        <if test="hasScore != null ">

            <!--有扣分-->
            <if test="hasScore == 1 ">
                AND t.id in (select task_id from check_score where check_item_score>0 )
            </if>
            <!--无扣分-->
            <if test="hasScore == 2 ">
                AND t.id not in (select task_id from check_score where check_item_score>0 )
            </if>

        </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>
        <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>
        ORDER BY t.start_time DESC
    </select>

    <update id="updateStatus" parameterType="com.xintong.visualinspection.bean.FwqCheckTask" >
        update check_task set
        <if test="check_status != null">check_status = #{check_status},</if>
        <if test="recheckman != null">recheckman = #{recheckman},</if>
        update_time=now()
        WHERE
        id = #{id}
    </update>






    <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>

    <select id="getTaskStatusList" parameterType="java.lang.Long" resultMap="taskStatusMap">
        SELECT *
        FROM check_task_status
        WHERE task_id = #{id}
        order by update_time
    </select>

    <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>

</mapper>


