<?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.xisc.pm.infra.mapper.PmPdRecMapper">
    <select id="selectPdOperationRecord" resultType="com.xisc.pm.api.dto.PmPdOperationReResponseDTO">
        SELECT
            ppr.CREATED_BY as createBy,
            ppr.EVENT_MAKER as eventMaker,
            ppr.MAT_NO as matNo,
            ppr.EVENT_CODE as eventCode,
            ppr.ORDER_NO as orderNo,
            ppr.CREATION_DATE as createDate,
            ppcpa.DELIVY_DATE as delivyDate,
            ppcpa.ORDER_WT as orderWt,
            ppcpa.ORDER_THICK as orderThick,
            ppcpa.ORDER_WIDTH as orderWidth,
            ppcpa.ORDER_LEN as orderLen
        FROM
            PM_PD_REC ppr
                LEFT JOIN PM_PRO_CONT_PROD_ATTR ppcpa ON ppr.ORDER_NO = ppcpa.ORDER_NO
        <where>
            ppr.EVENT_CODE IN ( 'HP16', 'HP14', 'HP10' )
            AND (
            EXISTS ( SELECT 1 FROM PM_PD_CC_SLAB ppcs WHERE ppr.MAT_NO = ppcs.VIRTUAL_SLAB_NO AND (ppcs.TWO_ROLL_INTERIM_FLAG = 0  or ppcs.TWO_ROLL_INTERIM_FLAG is null))
            OR EXISTS ( SELECT 1 FROM PM_PD_PLATE ppp WHERE ppr.MAT_NO = ppp.PLATE_NO AND (ppp.TWO_ROLL_INTERIM_FLAG = '0'  or ppp.TWO_ROLL_INTERIM_FLAG is null))
            OR EXISTS ( SELECT 1 FROM PM_PD_SLAB pps WHERE ppr.MAT_NO = pps.VIRTUAL_SLAB_NO AND (pps.TWO_ROLL_INTERIM_FLAG = 0 or pps.TWO_ROLL_INTERIM_FLAG is null))
            )
            <if test="tenantId != null">
                AND ppr.TENANT_ID = #{tenantId}
            </if>
            <if test="dto.eventMaker != null">
                AND ppr.EVENT_MAKER = #{dto.eventMaker}
            </if>
            <if test="dto.createBy != null">
                AND ppr.CREATED_BY = #{dto.createBy}
            </if>
            <if test="dto.createDateFrom != null">
                AND ppr.CREATION_DATE <![CDATA[ >= ]]> #{dto.createDateFrom}
            </if>
            <if test="dto.createDateTo != null">
                AND ppr.CREATION_DATE <![CDATA[ <= ]]> #{dto.createDateTo}
            </if>
            <if test="dto.delivyDateFrom != null">
                AND ppcpa.DELIVY_DATE <![CDATA[ >= ]]> #{dto.delivyDateFrom}
            </if>
            <if test="dto.delivyDateTo != null">
                AND ppcpa.DELIVY_DATE <![CDATA[ <= ]]> #{dto.delivyDateTo}
            </if>
        </where>
        ORDER BY ppr.CREATION_DATE DESC
    </select>
</mapper>