<?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.CheckOtherDao">

	<insert id="insert" parameterType="com.xintong.visualinspection.bean.CheckOther">
		INSERT INTO
		other_check
		(dept_id,user_id,check_person_id,check_score,check_item_id,remark,check_type,file_ids,check_num,check_time,check_time_name,update_time,wrecker_check_type, special_check_type)
		VALUES
		(#{dept_id},#{user_id},#{check_person_id},#{check_score},#{check_item_id},#{remark},#{check_type},#{file_ids},#{check_num},#{check_time},#{check_time_name},now(),#{wrecker_check_type},#{special_check_type})
	</insert>
	
	<select id="getCheckByDeptId" parameterType="com.xintong.visualinspection.bean.CheckOther"
	resultType="com.xintong.visualinspection.bean.CheckOther" >

        select ck.* from (

        SELECT
        o.id,o.dept_id,o.user_id,o.check_person_id,o.check_score,o.check_item_id,o.remark,o.check_type,o.file_ids,o.check_num,o.check_time,
        o.check_time_name,o.delete_flag,o.update_time , t.ORGANNAME organname,u.TRUENAME username,c.name
        check_item_name, o.wrecker_check_type,o.special_check_type,(select if(p.score is NULL,p.name,(select name from
        check_item where id = p.parent_id)) from check_item p where p.id =o.check_item_id) pcheckname
        FROM
        (SELECT * FROM other_check
        where 1=1 and check_type = #{check_type}
        <if test="check_time_name != null and check_time_name != ''">AND check_time_name = #{check_time_name}</if>
        <if test="dept_id != null">AND dept_id = #{dept_id}</if>
        <if test="special_check_type != null">AND special_check_type = #{special_check_type}</if>
        )o
        LEFT JOIN t_sys_organ t ON o.dept_id = t.id
        LEFT JOIN t_sys_users u ON o.user_id = u.id
        LEFT JOIN check_item c ON o.check_item_id = c.id
        ORDER BY o.check_time desc

        ) ck
        <if test="check_type == 4 and scoretype!=null and scoretype!='all' ">where ck.pcheckname = '${scoretype}'</if>

    </select>
    <select id="getCheckById" parameterType="java.lang.Long" resultType="com.xintong.visualinspection.bean.CheckOther" >
        SELECT o.id,o.dept_id,o.user_id,o.check_person_id,o.check_score,o.check_item_id,o.remark,o.check_type,o.file_ids,o.check_num,o.check_time,
		o.check_time_name,o.delete_flag,o.update_time,c.name check_item_name, o.wrecker_check_type,o.special_check_type
        FROM other_check o
        LEFT JOIN check_item c ON o.check_item_id = c.id
        WHERE o.id = #{id}
    </select>
    
    <update id="update" parameterType="com.xintong.visualinspection.bean.CheckOther" >
        UPDATE
        other_check
        SET
        <if test="dept_id != null">dept_id = #{dept_id}</if>
        <if test="user_id != null">,user_id = #{user_id}</if>
        <if test="check_person_id != null">,check_person_id = #{check_person_id}</if>
        <if test="check_type != null">,check_type = #{check_type}</if>
        <if test="check_type != null">,check_type = #{check_type}</if>
        <if test="check_item_id != null">,check_item_id = #{check_item_id}</if>
        <if test="remark != null and remark !='' " >,remark = #{remark}</if>
        <if test="check_score != null">,check_score = #{check_score}</if>
        <if test="file_ids != null">,file_ids = #{file_ids}</if>
        <if test="check_time != null">,check_time = #{check_time}</if>
        <if test="check_time_name !=  null">,check_time_name= #{check_time_name} </if>
        <if test="check_num != null">,check_num = #{check_num}</if>
        <if test="wrecker_check_type != null">,wrecker_check_type = #{wrecker_check_type}</if>
        <if test="special_check_type != null">,special_check_type = #{special_check_type}</if>
        WHERE
        id = #{id}
    </update>

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

    <select id="getcheckitemnames" parameterType="java.lang.Long" resultType="java.lang.String">
        select if(p.score is NULL,p.name,(select name from check_item where id = p.parent_id)) itemname from check_item p where p.id in (select item_id from check_rule_item where rule_id=#{id}) GROUP BY itemname
    </select>

</mapper>