<?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.report.mapper.ReportQualityInspectionMapper">

    <resultMap type="ReportQualityInspection" id="ReportQualityInspectionResult">
        <result property="objId" column="obj_id"/>
        <result property="barCode" column="bar_code"/>
        <result property="materialName" column="material_name"/>
        <result property="processCode" column="process_code"/>
        <result property="testItemCode" column="test_item_code"/>
        <result property="qualityDefectCode" column="quality_defect_code"/>
        <result property="qualityDefectName" column="quality_defect_name"/>
        <result property="treatmentMeasure" column="treatment_measure"/>
        <result property="processResult" column="process_result"/>
        <result property="isLowerLine" column="is_lower_line"/>
        <result property="groupCode" column="group_code"/>
        <result property="inspectorCode" column="inspector_code"/>
        <result property="inspectorTime" column="inspector_time"/>
        <result property="reworkNumber" column="rework_number"/>
        <result property="finishTime" column="finish_time"/>
        <result property="isFlag" column="is_flag"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updatedTime" column="updated_time"/>
        <result property="stationCode" column="STATION_CODE"/>
        <result property="submitQualtyId" column="SUBMINT_QUALTY_ID"/>
        <result property="productLineName" column="PRODUCT_LINE_NAME"/>
        <result property="processName" column="processName"/>
        <result property="groupName" column="groupName"/>
        <result property="inspectorName" column="inspectorName"/>
        <result property="orderCode" column="ORDER_CODE"/>
    </resultMap>

    <sql id="selectReportQualityInspectionVo">
        select rqi.obj_id,
               rqi.bar_code,
               rqi.material_name,
               rqi.process_code,
               bps.PROCESS_NAME     processName,
               rqi.test_item_code,
               pl.PRODUCT_LINE_NAME PRODUCT_LINE_NAME,
               rqi.quality_defect_code,
               rqi.quality_defect_name,
               rqi.treatment_measure,
               rqi.process_result,
               rqi.is_lower_line,
               rqi.group_code,
               btm.TEAM_NAME        groupName,
               rqi.inspector_code,
               su.NICK_NAME         inspectorName,
               rqi.inspector_time,
               rqi.rework_number,
               rqi.finish_time,
               rqi.is_flag,
               rqi.updated_by,
               rqi.updated_time
        from report_quality_inspection rqi
                 left join BASE_PROCESS_STATION bps on bps.PROCESS_CODE = rqi.PROCESS_CODE
                 left join BASE_TEAMMEMBERS btm on btm.TEAM_CODE = rqi.group_code
                 left join Z_SYS_USER su on su.USER_NAME = rqi.inspector_code
                 left join BASE_PRODUCTLINE pl on pl.PRODUCT_LINE_CODE = rqi.STATION_CODE
    </sql>

    <select id="selectReportQualityInspectionList" parameterType="ReportQualityInspection"
            resultMap="ReportQualityInspectionResult">
        <include refid="selectReportQualityInspectionVo"/>
        <where>
            <if test="barCode != null  and barCode != ''">and rqi.bar_code = #{barCode}</if>
            <if test="materialName != null  and materialName != ''">and rqi.material_name like concat(concat('%',
                #{materialName}), '%')
            </if>
            <if test="processCode != null  and processCode != ''">and rqi.process_code = #{processCode}</if>
            <if test="testItemCode != null  and testItemCode != ''">and rqi.test_item_code = #{testItemCode}</if>
            <if test="qualityDefectCode != null  and qualityDefectCode != ''">and rqi.quality_defect_code =
                #{qualityDefectCode}
            </if>
            <if test="qualityDefectName != null  and qualityDefectName != ''">and rqi.quality_defect_name like
                concat(concat('%', #{qualityDefectName}), '%')
            </if>
            <if test="treatmentMeasure != null  and treatmentMeasure != ''">and rqi.treatment_measure =
                #{treatmentMeasure}
            </if>
            <if test="processResult != null  and processResult != ''">and rqi.process_result = #{processResult}</if>
            <if test="isLowerLine != null  and isLowerLine != ''">and rqi.is_lower_line = #{isLowerLine}</if>
            <if test="groupCode != null  and groupCode != ''">and rqi.group_code = #{groupCode}</if>
            <if test="inspectorCode != null  and inspectorCode != ''">and rqi.inspector_code = #{inspectorCode}</if>
            <if test="stationCode != null and stationCode != ''">and rqi.station_code = #{stationCode}</if>
            <if test="reworkNumber != null ">and rqi.rework_number = #{reworkNumber}</if>
            <if test="finishTime != null ">and rqi.finish_time = #{finishTime}</if>
            <if test="isFlag != null ">and rqi.is_flag = #{isFlag}</if>
            <if test="updatedBy != null  and updatedBy != ''">and rqi.updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and rqi.updated_time = #{updatedTime}</if>
            <if test="beginBeginTime != null  and beginBeginTime != '' and endBeginTime != null  and endBeginTime != ''">
                AND rqi.inspector_time BETWEEN TO_DATE(#{beginBeginTime}, 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE(#{endBeginTime}, 'YYYY-MM-DD HH24:MI:SS')
            </if>
        </where>
        order by rqi.inspector_time desc
    </select>

    <select id="selectReportQualityInspectionByObjId" parameterType="Long" resultMap="ReportQualityInspectionResult">
        <include refid="selectReportQualityInspectionVo"/>
        where rqi.obj_id = #{objId}
    </select>
    <select id="pdaQueryQuantityNumber" resultType="java.util.HashMap" parameterType="String">
        SELECT COUNT(*) QUANTITY_SUM, NVL(SUM(REPAIR_SUM),0) REPAIR_SUM
        FROM (
        WITH CTT AS (
        SELECT MIN(START_TIME) START_TIME, MAX(END_TIME) END_TIME
        FROM VIEW_CURRENT_TEAM_TIME
        )
        SELECT STATION_CODE, BAR_CODE, MAX(CASE WHEN TREATMENT_MEASURE = '1' THEN 1 ELSE 0 END) REPAIR_SUM
        FROM REPORT_QUALITY_INSPECTION VPO
        CROSS JOIN CTT
        WHERE VPO.INSPECTOR_TIME >= CTT.START_TIME
        AND VPO.INSPECTOR_TIME &lt; CTT.END_TIME
        AND STATION_CODE = #{stationCode}
        GROUP BY STATION_CODE, BAR_CODE
        )
    </select>

    <insert id="insertReportQualityInspection" parameterType="ReportQualityInspection">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_report_quality_inspection.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into report_quality_inspection
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="barCode != null">bar_code,</if>
            <if test="materialName != null">material_name,</if>
            <if test="processCode != null">process_code,</if>
            <if test="testItemCode != null">test_item_code,</if>
            <if test="qualityDefectCode != null">quality_defect_code,</if>
            <if test="qualityDefectName != null">quality_defect_name,</if>
            <if test="treatmentMeasure != null">treatment_measure,</if>
            <if test="processResult != null">process_result,</if>
            <if test="isLowerLine != null">is_lower_line,</if>
            <if test="groupCode != null">group_code,</if>
            <if test="inspectorCode != null">inspector_code,</if>
            <if test="inspectorTime != null">inspector_time,</if>
            <if test="reworkNumber != null">rework_number,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="updatedBy != null">updated_by,</if>
            <if test="updatedTime != null">updated_time,</if>
            <if test="stationCode != null">STATION_CODE,</if>
            <if test="submitQualtyId != null">SUBMINT_QUALTY_ID,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="barCode != null">#{barCode},</if>
            <if test="materialName != null">#{materialName,jdbcType=NVARCHAR},</if>
            <if test="processCode != null">#{processCode},</if>
            <if test="testItemCode != null">#{testItemCode},</if>
            <if test="qualityDefectCode != null">#{qualityDefectCode},</if>
            <if test="qualityDefectName != null">#{qualityDefectName},</if>
            <if test="treatmentMeasure != null">#{treatmentMeasure},</if>
            <if test="processResult != null">#{processResult},</if>
            <if test="isLowerLine != null">#{isLowerLine},</if>
            <if test="groupCode != null">#{groupCode},</if>
            <if test="inspectorCode != null">#{inspectorCode},</if>
            <if test="inspectorTime != null">#{inspectorTime},</if>
            <if test="reworkNumber != null">#{reworkNumber},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="updatedBy != null">#{updatedBy},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
            <if test="stationCode != null">#{stationCode},</if>
            <if test="submitQualtyId != null">#{submitQualtyId},</if>
        </trim>
    </insert>

    <update id="updateReportQualityInspection" parameterType="ReportQualityInspection">
        update report_quality_inspection
        <trim prefix="SET" suffixOverrides=",">
            <if test="barCode != null">bar_code = #{barCode},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="processCode != null">process_code = #{processCode},</if>
            <if test="testItemCode != null">test_item_code = #{testItemCode},</if>
            <if test="qualityDefectCode != null">quality_defect_code = #{qualityDefectCode},</if>
            <if test="qualityDefectName != null">quality_defect_name = #{qualityDefectName},</if>
            <if test="treatmentMeasure != null">treatment_measure = #{treatmentMeasure},</if>
            <if test="processResult != null">process_result = #{processResult},</if>
            <if test="isLowerLine != null">is_lower_line = #{isLowerLine},</if>
            <if test="groupCode != null">group_code = #{groupCode},</if>
            <if test="inspectorCode != null">inspector_code = #{inspectorCode},</if>
            <if test="inspectorTime != null">inspector_time = #{inspectorTime},</if>
            <if test="reworkNumber != null">rework_number = #{reworkNumber},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</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="deleteReportQualityInspectionByObjId" parameterType="Long">
        delete
        from report_quality_inspection
        where obj_id = #{objId}
    </delete>

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

    <select id="selectReportQualityInspectionByBarcode" parameterType="String"
            resultMap="ReportQualityInspectionResult">
        SELECT PB.MATERIAL_BARCODE BAR_CODE,
               PB.ORDER_CODE,
               ML.MATERIAL_NAME,
               PS.PROCESS_CODE     PROCESS_CODE,
               'ZLJY-01'           GROUP_CODE,
               'djcj'              INSPECTOR_CODE,
               '2005'              STATION_CODE,
               SYSDATE             INSPECTOR_TIME,
               0                   REWORK_NUMBER,
               1                   IS_FLAG
        FROM PRINT_BARCODE PB
                 JOIN BASE_MATERIALINFO ML ON PB.MATERIAL_CODE = ML.MATERIAL_CODE
                 JOIN BASE_PROCESS_STATION PS ON PROCESS_TYPE = 2 AND PS.PARENT_ID LIKE '%2005%'
        WHERE PB.MATERIAL_BARCODE = #{barcode}
    </select>

    <select id="selectOneReportQualityInspectionByBarcode" parameterType="String"
            resultMap="ReportQualityInspectionResult">
        SELECT PB.MATERIAL_BARCODE BAR_CODE,
               PB.ORDER_CODE,
               ML.MATERIAL_NAME,
               PS.PROCESS_CODE     PROCESS_CODE,
               'ZLJY-11'           GROUP_CODE,
               'djcj'              INSPECTOR_CODE,
               '2105'              STATION_CODE,
               SYSDATE             INSPECTOR_TIME,
               0                   REWORK_NUMBER,
               1                   IS_FLAG
        FROM PRINT_BARCODE PB
                 JOIN BASE_MATERIALINFO ML ON PB.MATERIAL_CODE = ML.MATERIAL_CODE
                 JOIN BASE_PROCESS_STATION PS ON PROCESS_TYPE = 2 AND PS.PARENT_ID LIKE '%2105%'
        WHERE PB.MATERIAL_BARCODE = #{barcode}
    </select>

</mapper>