<?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.CheckApplyDao" >
    <resultMap id="BaseResultMap" type="com.xintong.visualinspection.bean.CheckApply" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="apply_reason" property="apply_reason" jdbcType="VARCHAR" />
        <result column="check_status" property="check_status" jdbcType="INTEGER" />
        <result column="apply_time" property="apply_time" jdbcType="DATE" />
        <result column="update_time" property="update_time" jdbcType="DATE" />
        <result column="apply_man" property="apply_man" jdbcType="INTEGER" />
        <result column="verify_person" property="verify_person" jdbcType="INTEGER" />
        <result column="apply_dept" property="apply_dept" jdbcType="INTEGER" />
        <result column="task_id" property="task_id" jdbcType="INTEGER" />
        <result column="remark" property="remark" jdbcType="VARCHAR" />
        
        
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="start_time" property="start_time" jdbcType="DATE" />
        <result column="end_time" property="end_time" jdbcType="DATE" />
        
        <result column="code_name" property="code_name" jdbcType="VARCHAR" />
    </resultMap>

    <select id="getList" resultMap="BaseResultMap" parameterType="com.xintong.visualinspection.bean.CheckApply" >
        SELECT ca.*,ct.name,ct.start_time,ct.end_time,sc.code_name 
        FROM check_apply ca LEFT JOIN check_task ct ON ca.task_id=ct.id 
        LEFT JOIN sys_code sc ON ct.check_status=sc.code_value
        WHERE 1=1 
        <if test="start_time != null">AND ct.start_time >= #{start_time}</if>
        <if test="end_time != null">
        	<![CDATA[ AND ct.end_time < #{end_time}]]>
        </if>
        <if test="apply_dept != null">AND ca.apply_dept = #{apply_dept}</if>
        <if test="apply_man != null">AND ct.checkman = #{apply_man}</if>
        <if test="apply_ids != null and apply_ids.length > 0">AND
       		ct.id in 
       		<foreach collection="apply_ids" item="apply_id" index="index"
	            open="(" close=")" separator=",">
	            #{apply_id}
	        </foreach>
       	</if>
        ORDER BY ca.apply_time DESC
    </select>

    <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT *
        FROM check_apply
        WHERE id = #{id}
    </select>
    
    <select id="getOneByTaskId" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT *
        FROM check_apply
        WHERE task_id = #{id}
    </select>
    
    <insert id="insert" parameterType="com.xintong.visualinspection.bean.CheckAppeal" >
        INSERT INTO
        check_apply
        (apply_reason,check_status,apply_time,update_time,apply_man,verify_person,apply_dept,task_id,remark)
        VALUES
        (#{apply_reason}, #{check_status},now(),now(),#{apply_man},#{verify_person},#{apply_dept},#{task_id},#{remark})
    </insert>

    <update id="update" parameterType="com.xintong.visualinspection.bean.CheckAppeal" >
        UPDATE
        	check_apply
        SET
	        <if test="check_status != null">check_status = #{check_status},</if>
	        <if test="verify_person != null">verify_person = #{verify_person},</if>
	        update_time = now()
        WHERE
        id = #{id}
    </update>

    <delete id="delete" parameterType="java.lang.Long" >
        DELETE FROM
        check_apply
        WHERE
        id =#{id}
    </delete>
</mapper>