<?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.CheckItemDao" >
    <resultMap id="BaseResultMap" type="com.xintong.visualinspection.bean.CheckItem" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="parent_id" property="parent_id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="content" property="content" jdbcType="VARCHAR" />
        <result column="score" property="score" jdbcType="INTEGER" />
        <result column="score_type" property="score_type" jdbcType="INTEGER" />
        <result column="type" property="type" jdbcType="INTEGER" />
    </resultMap>

    <select id="getAll" resultMap="BaseResultMap"  >
        SELECT *
        FROM check_item ORDER BY parent_id ASC , id ASC
    </select>

    <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT *
        FROM check_item
        WHERE id = #{id}
    </select>
    
	<select id="getByParentId" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT *
        FROM check_item
        WHERE parent_id = #{parent_id}
    </select>
    
    <insert id="insert" parameterType="com.xintong.visualinspection.bean.CheckItem" >
        INSERT INTO
        check_item
        (name,parent_id,content,score,score_type,type)
        VALUES
        (#{name}, #{parent_id},#{content},#{score},#{score_type},#{type})
    </insert>

    <update id="update" parameterType="com.xintong.visualinspection.bean.CheckItem" >
        UPDATE
        check_item
        SET
        <if test="name != null">name = #{name},</if>
        <if test="parent_id != null">parent_id = #{parent_id},</if>
        <if test="content != null">content = #{content},</if>
        <if test="score_type != null">score_type = #{score_type},</if>
        <if test="type != null">type = #{type},</if>
        id =id ,score = #{score}
        WHERE
        id = #{id}
    </update>

    <delete id="delete" parameterType="java.lang.Long" >
        DELETE FROM
        check_item
        WHERE
        id =#{id}
    </delete>
    
    <select id="getCheckItemByRuleType" parameterType="map" resultMap="BaseResultMap"  >
        SELECT *
        FROM check_item
        WHERE id in (
        			select item_id
        			from check_rule_item
        			where rule_id = (
        								select id
        								from check_rule
        								where rule_type=#{rule_type}
        							)
        			)
        ORDER BY parent_id ASC , id ASC
    </select>
</mapper>