<?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.CheckAppealDao" >
    <resultMap id="BaseResultMap" type="com.xintong.visualinspection.bean.CheckAppeal" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="file_src" property="file_src" jdbcType="VARCHAR" />
        <result column="check_status" property="check_status" jdbcType="INTEGER" />
        <result column="appeal_time" property="appeal_time" jdbcType="DATE" />
        <result column="update_time" property="update_time" jdbcType="DATE" />
        <result column="appeal_man" property="appeal_man" jdbcType="INTEGER" />
        <result column="verify_person" property="verify_person" jdbcType="INTEGER" />
        <result column="appeal_dept" property="appeal_dept" jdbcType="INTEGER" />
        <result column="task_id" property="task_id" jdbcType="INTEGER" />
        <result column="appeal_reason" property="appeal_reason" jdbcType="VARCHAR" />
        <result column="recheck_result" property="recheck_result" jdbcType="VARCHAR" />
        <result column="remark" property="remark" jdbcType="VARCHAR" />
        <result column="remark" property="appeal_reason" jdbcType="VARCHAR" />
        <result column="remark" property="recheck_result" 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="check_period" property="check_period" jdbcType="VARCHAR" />
        <result column="checkman" property="checkman" jdbcType="INTEGER" />
        <result column="recheckman" property="recheckman" jdbcType="INTEGER" />
        
        <result column="code_name" property="code_name" jdbcType="VARCHAR" />
        <result column="code_flag" property="code_flag" jdbcType="VARCHAR" />
    </resultMap>

    <select id="getList" resultMap="BaseResultMap" parameterType="com.xintong.visualinspection.bean.CheckAppeal" >
        SELECT ca.*,ct.name,ct.start_time,ct.end_time,ct.checkman,ct.recheckman,ct.check_status as check_status,
        		CONCAT(substr(ct.start_time,1,16),'-',substr(ct.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=ct.check_status) as check_status_name    		
        FROM check_appeal ca LEFT JOIN check_task ct ON ca.task_id=ct.id 
        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="appeal_dept != null">AND ca.appeal_dept = #{appeal_dept}</if>
        <if test="check_status != null">AND ct.check_status = #{check_status}</if>
        ORDER BY ca.appeal_time DESC
    </select>

    <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT *
        FROM check_appeal
        WHERE id = #{id}
    </select>
    
    <select id="getByTaskId" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT *
        FROM check_appeal
        WHERE task_id = #{task_id}
    </select>
    
    <insert id="insert" parameterType="com.xintong.visualinspection.bean.CheckAppeal" >
        INSERT INTO
        check_appeal
        (file_src,check_status1,appeal_time,update_time,appeal_man,verify_person,appeal_dept,task_id,remark,appeal_reason,recheck_result)
        VALUES
        (#{file_src}, #{check_status},now(),now(),#{appeal_man},#{verify_person},#{appeal_dept},#{task_id},#{remark},#{appeal_reason},#{recheck_result})
    </insert>

    <update id="update" parameterType="com.xintong.visualinspection.bean.CheckAppeal" >
        UPDATE
        check_appeal
        SET
        <if test="check_status != null">check_status = #{check_status}</if>
        WHERE
        id = #{id}
    </update>

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