<?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="stationName" column="station_name"/>
        <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 qi.obj_id,
               qi.quality_defect_code,
               qi.quality_defect_name,
               qi.parent_id,
               qi.material_categories,
               qi.material_subclass,
               qi.station_code,
               pl.PRODUCT_LINE_NAME station_name,
               qi.remark,
               qi.is_flag,
               qi.created_by,
               qi.created_time,
               qi.updated_by,
               qi.updated_time
        from base_quality_inspection_item qi
                 left join BASE_PRODUCTLINE pl on pl.PRODUCT_LINE_CODE = qi.STATION_CODE
    </sql>

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

    <select id="selectBaseQualityInspectionItemByObjId" parameterType="Long"
            resultMap="BaseQualityInspectionItemResult">
        <include refid="selectBaseQualityInspectionItemVo"/>
        where qi.obj_id = #{objId}
    </select>
    <select id="selectBaseQualityInspectionItemParent" resultMap="BaseQualityInspectionItemResult">
        <include refid="selectBaseQualityInspectionItemVo"/>
        where qi.PARENT_ID = 0
        order by qi.quality_defect_code
    </select>
    <select id="getQualityDefects" parameterType="String" resultMap="BaseQualityInspectionItemResult">
        select sdr.obj_id,
        bq1.quality_defect_code quality_defect_code,
        bq1.quality_defect_name quality_defect_name,
        sdr.station_code        station_code,
        pl.PRODUCT_LINE_NAME    station_name,
        dl.DICT_LABEL           MATERIAL_CATEGORIES,
        xl.DICT_LABEL           MATERIAL_SUBCLASS
        from STATION_DEFECT_RELATION sdr
        left join BASE_QUALITY_INSPECTION_ITEM bq1 on sdr.defect_code = bq1.QUALITY_DEFECT_CODE
        left join BASE_PRODUCTLINE pl on sdr.station_code = pl.PRODUCT_LINE_CODE
        left join Z_SYS_DICT_DATA dl on dl.DICT_TYPE = 'defect_categories' and bq1.MATERIAL_CATEGORIES = dl.DICT_VALUE
        left join Z_SYS_DICT_DATA xl on xl.DICT_TYPE = 'defect_subclass' and bq1.MATERIAL_SUBCLASS = xl.DICT_VALUE
        <where>
            <if test="stationCode != null ">and sdr.station_code = #{stationCode}</if>
        </where>
    </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>
