<?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.CheckRuleDao" >
    <resultMap id="BaseResultMap" type="com.xintong.visualinspection.bean.CheckRule" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="checked_person_type" property="checked_person_type" jdbcType="INTEGER" />
        <result column="checked_item_num" property="checked_item_num" jdbcType="INTEGER"/>
        <result column="checked_position_id" property="checked_position_id" jdbcType="INTEGER" />
        <result column="checked_position_name" property="checked_position_name" jdbcType="VARCHAR" />
        <result column="checked_dept_id" property="checked_dept_id" jdbcType="INTEGER" />
        <result column="rule_type" property="rule_type" jdbcType="INTEGER"/>
        <collection property="check_items" ofType="com.xintong.visualinspection.bean.CheckItem"  
        	javaType="ArrayList">
        	<id property="id"  column="i_id"/>
        	<result property="parent_id" column="parent_id" />
    		<result property="name"  column="i_name"/>
    		<result property="score"  column="score"/>    
        </collection>
    </resultMap>

	<select id="selectItems" resultType="com.xintong.visualinspection.bean.CheckItem" parameterType="Long">  
    	SELECT a.*,b.*
        FROM check_rule a left join check_rule_item b on a.id=b.rule_id
        WHERE id = #{id}
  	</select>  
  	
  	<select id="getAllAndCountItem"  resultMap="BaseResultMap" parameterType="com.xintong.visualinspection.bean.CheckRule" >
		SELECT o.*,(SELECT COUNT(1) FROM check_rule_item t WHERE t.rule_id =o.id) AS checked_item_num ,sj.name AS checked_position_name  FROM check_rule o 
			LEFT JOIN sys_job sj ON o.checked_position_id = sj.id
		<if test="name!=null">
			WHERE o.name like CONCAT('%',#{name},'%') 		
		</if>
  	</select>
  	
    <select id="getAll" resultMap="BaseResultMap"  >
        SELECT *
        FROM check_rule
    </select>

    <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT t.*,c.name AS i_name ,c.score ,c.id AS i_id ,c.parent_id FROM (SELECT a.*,b.*
        FROM check_rule a left join check_rule_item b on a.id=b.rule_id
        WHERE a.id = #{id}) t LEFT JOIN check_item c ON t.item_id = c.id 
    </select>
    
    <select id="getByName"  parameterType="java.lang.String" resultMap="BaseResultMap"  >
        SELECT *
        FROM check_rule
        WHERE name like CONCAT('%',#{name},'%') 
    </select>

    <insert id="insert" parameterType="com.xintong.visualinspection.bean.CheckRule" >
        INSERT INTO
        check_rule
        (name,checked_person_type,checked_position_id,checked_dept_id,rule_type)
        VALUES
        (#{name}, #{checked_person_type},#{checked_position_id},#{checked_dept_id},#{rule_type})
    </insert>
	
	<insert id="insertRuleItem" parameterType="List" >
        INSERT INTO check_rule_item
        	(rule_id,item_id)
        VALUES
         <foreach collection ="list" item="item" index= "index" separator =",">
        	(#{item.rule_id}, #{item.id})
         </foreach >
    </insert>
    
    <update id="update" parameterType="com.xintong.visualinspection.bean.CheckRule" >
        UPDATE
        check_rule
        SET
        <if test="name != null">name = #{name},</if>
        <if test="checked_person_type != null">checked_person_type = #{checked_person_type},</if>
        <if test="checked_position_id != null">checked_position_id = #{checked_position_id},</if>
        <if test="checked_dept_id != null">checked_dept_id = #{checked_dept_id}</if>
        <if test="rule_type != null">rule_type = #{rule_type}</if>
        WHERE
        id = #{id}
    </update>
    
    <update id="updateRuleType" parameterType="com.xintong.visualinspection.bean.CheckRule" >
        UPDATE
        check_rule
        SET rule_type = null
        WHERE
        rule_type = #{rule_type}
    </update>
    
    <delete id="deleteRuleItem" parameterType="java.lang.Long" >
        DELETE FROM
        	check_rule_item
        WHERE
        	rule_id = #{id}
    </delete>
    
    <delete id="delete" parameterType="java.lang.Long" >
        DELETE FROM
        check_rule
        WHERE
        id =#{id}
    </delete>
    
    <select id="getByRuleType"  parameterType="map" resultMap="BaseResultMap"  >
        SELECT *
        FROM check_rule
        WHERE rule_type = #{rule_type}
    </select>
</mapper>