<?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.aucma.base.mapper.BaseQualityInspectionItemMapper">

    <resultMap type="BaseQualityInspectionItem" id="BaseQualityInspectionItemResult">
        <result property="objId" column="obj_id"/>
        <result property="qualityDefectCode" column="quality_defect_code"/>
        <result property="qualityDefectName" column="quality_defect_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="materialCategories" column="material_categories"/>
        <result property="materialSubclass" column="material_subclass"/>
        <result property="stationCode" column="station_code"/>
        <result property="remark" column="remark"/>
        <result property="isFlag" column="is_flag"/>
        <result property="createdBy" column="created_by"/>
        <result property="createdTime" column="created_time"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updatedTime" column="updated_time"/>
    </resultMap>

    <sql id="selectBaseQualityInspectionItemVo">
        select obj_id,
               quality_defect_code,
               quality_defect_name,
               parent_id,
               material_categories,
               material_subclass,
               station_code,
               remark,
               is_flag,
               created_by,
               created_time,
               updated_by,
               updated_time
        from base_quality_inspection_item
    </sql>

    <select id="selectBaseQualityInspectionItemList" parameterType="BaseQualityInspectionItem"
            resultMap="BaseQualityInspectionItemResult">
        <include refid="selectBaseQualityInspectionItemVo"/>
        <where>
            <if test="qualityDefectCode != null  and qualityDefectCode != ''">and quality_defect_code =
                #{qualityDefectCode}
            </if>
            <if test="qualityDefectName != null  and qualityDefectName != ''">and quality_defect_name like
                concat(concat('%', #{qualityDefectName}), '%')
            </if>
            <if test="parentId != null  and parentId != ''">and parent_id = #{parentId}</if>
            <if test="materialCategories != null  and materialCategories != ''">and material_categories =
                #{materialCategories}
            </if>
            <if test="materialSubclass != null  and materialSubclass != ''">and material_subclass =
                #{materialSubclass}
            </if>
            <if test="stationCode != null  and stationCode != ''">and station_code = #{stationCode}</if>
            <if test="isFlag != null ">and is_flag = #{isFlag}</if>
            <if test="createdBy != null  and createdBy != ''">and created_by = #{createdBy}</if>
            <if test="createdTime != null ">and created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''">and updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and updated_time = #{updatedTime}</if>
        </where>
        order by quality_defect_code
    </select>

    <select id="selectBaseQualityInspectionItemByObjId" parameterType="Long"
            resultMap="BaseQualityInspectionItemResult">
        <include refid="selectBaseQualityInspectionItemVo"/>
        where obj_id = #{objId}
    </select>
    <select id="selectBaseQualityInspectionItemParent" resultMap="BaseQualityInspectionItemResult">
        <include refid="selectBaseQualityInspectionItemVo"/>
        where PARENT_ID = 0
        order by quality_defect_code
    </select>
    <select id="getQualityDefects" parameterType="String" resultMap="BaseQualityInspectionItemResult">
        select bq2.obj_id,
               bq2.quality_defect_code,
               bq2.quality_defect_name,
               bq1.station_code
        from base_quality_inspection_item bq1
                 left join base_quality_inspection_item bq2 on bq2.PARENT_ID = bq1.quality_defect_code
        where bq1.station_code = '1003'
    </select>

    <insert id="insertBaseQualityInspectionItem" parameterType="BaseQualityInspectionItem">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_base_quality_inspection.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into base_quality_inspection_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="qualityDefectCode != null">quality_defect_code,</if>
            <if test="qualityDefectName != null">quality_defect_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="materialCategories != null">material_categories,</if>
            <if test="materialSubclass != null">material_subclass,</if>
            <if test="stationCode != null">station_code,</if>
            <if test="remark != null">remark,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="createdBy != null">created_by,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="updatedBy != null">updated_by,</if>
            <if test="updatedTime != null">updated_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="qualityDefectCode != null">#{qualityDefectCode},</if>
            <if test="qualityDefectName != null">#{qualityDefectName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="materialCategories != null">#{materialCategories},</if>
            <if test="materialSubclass != null">#{materialSubclass},</if>
            <if test="stationCode != null">#{stationCode},</if>
            <if test="remark != null">#{remark},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="createdBy != null">#{createdBy},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedBy != null">#{updatedBy},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
        </trim>
    </insert>

    <update id="updateBaseQualityInspectionItem" parameterType="BaseQualityInspectionItem">
        update base_quality_inspection_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="qualityDefectCode != null">quality_defect_code = #{qualityDefectCode},</if>
            <if test="qualityDefectName != null">quality_defect_name = #{qualityDefectName},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="materialCategories != null">material_categories = #{materialCategories},</if>
            <if test="materialSubclass != null">material_subclass = #{materialSubclass},</if>
            <if test="stationCode != null">station_code = #{stationCode},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="createdBy != null">created_by = #{createdBy},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="updatedBy != null">updated_by = #{updatedBy},</if>
            <if test="updatedTime != null">updated_time = #{updatedTime},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteBaseQualityInspectionItemByObjId" parameterType="Long">
        delete
        from base_quality_inspection_item
        where obj_id = #{objId}
    </delete>

    <delete id="deleteBaseQualityInspectionItemByObjIds" parameterType="String">
        delete from base_quality_inspection_item where obj_id in
        <foreach item="objId" collection="array" open="(" separator="," close=")">
            #{objId}
        </foreach>
    </delete>
</mapper>
