<?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.PmCtCollectPoolMapper">

    <select id="selectOrderAndBackLog" resultType = "com.xisc.pm.api.dto.PmCtCollectPoolResDTO">
        SELECT DISTINCT
            cm.ORDER_NO,
            qtplc.WHOLE_BACKLOG,
            QTPLC.ST_NO
        FROM
            CT_MAIN cm
                JOIN QD_TO_PO_ORDER qtpo ON cm.ORDER_NO = qtpo.ORDER_NO
                JOIN QD_TO_PO_LINE_COMB qtplc ON qtpo.ORDER_NO = qtplc.ORDER_NO
        <where>
            cm.ORDER_STATUS <![CDATA[ >= ]]> '38' AND cm.ORDER_STATUS <![CDATA[ <= ]]> '47'
            AND qtpo.PROD_CLASS_CODE = 'A'
                  <if test="dto.orderNoFrom !=null">
                      and cm.ORDER_NO <![CDATA[ >= ]]> #{dto.orderNoFrom}
                  </if>
                  <if test="dto.orderNoTo !=null">
                      and cm.ORDER_NO <![CDATA[ <= ]]> #{dto.orderNoTo}
                  </if>
                  <if test="tenantId !=null">
                      and cm.TENANT_ID = #{tenantId}
                  </if>
                  <if test="dto.orderTypeCode !=null">
                      AND qtpo.ORDER_TYPE_CODE = #{dto.orderTypeCode}
                  </if>
                  <if test="dto.delivyDateFrom !=null">
                      AND qtpo.DELIVY_DATE <![CDATA[ >= ]]> #{dto.delivyDateFrom}
                  </if>
                  <if test="dto.delivyDateTo !=null">
                      AND qtpo.DELIVY_DATE <![CDATA[ <= ]]> #{dto.delivyDateTo}
                  </if>
                 <if test="dto.mscLineNo !=null">
                      <bind name="mscLineNoLike" value="dto.mscLineNo + '%'"/>
                      AND qtplc.MSC_LINE_NO like #{mscLineNoLike}
                 </if>
                <if test="dto.stNo !=null">
                    <bind name="stNoLike" value="'%' + dto.stNo + '%'"/>
                    AND qtplc.st_no like #{stNoLike}
                </if>
              </where>
          </select>

          <select id="selectList" resultType = "com.xisc.pm.domain.vo.CdAndQtCollectPoolVO">
              SELECT
                  cd.ORDER_NO,
                  cm.ORD_REM_WGT,
                  cm.ORD_REM_NUM,
                  cd.PLAN_WGT,
                  cd.WHOLE_BACKLOG_CODE,
                  cd.DELAY_FLAG,
                  cpppt.PLAN_END_DATE,
                  cpppt.LATEST_END_DATE,
                  qtpo.URG_ORDER_FLAG,
                  qtpo.ORDER_WT,
                  qtpo.DELIVY_WT_TOL_PLUS,
                  qtpo.DELIVY_WT_TOL_MINUS,
                  qtpo.DELIVY_DATE,
                  qtpo.ORDER_TYPE_CODE,
                  qtpo.SG_SIGN,
                  qtpo.CUST_SG_CODE AS sgCode
              FROM
                  CT_DET cd
                      join CT_MAIN cm on cd.ORDER_NO = cm.ORDER_NO
                      left join CT_PRC_PRED_PLAN_TIME cpppt ON (cd.ORDER_NO =cpppt.ORDER_NO  AND cpppt.WHOLE_BACKLOG_CODE = '14')
                      JOIN QD_TO_PO_ORDER qtpo ON cd.ORDER_NO = qtpo.ORDER_NO
              <where>
                  cd.WHOLE_BACKLOG_CODE = '14'
                  AND cm.ORD_REM_WGT >= 0
                  <if test="tenantId !=null">
                      and cd.TENANT_ID = #{tenantId}
                  </if>
                  <if test="dto.planDateFrom !=null">
                      AND cpppt.PLAN_END_DATE <![CDATA[ >= ]]> #{dto.planDateFrom}
                  </if>
                  <if  test="dto.planDateTo !=null">
                      AND cpppt.PLAN_END_DATE <![CDATA[ <= ]]> #{dto.planDateTo}
                  </if>
                  <if test="dto.urgOrderFlag !=null">
                      AND qtpo.URG_ORDER_FLAG = #{dto.urgOrderFlag}
                  </if>
                  <if test="orderNoList != null and orderNoList.size() != 0">
                      AND  cd.ORDER_NO IN
                      <foreach collection="orderNoList" index="index" item="item" open="(" separator="," close=")">
                          #{item}
                      </foreach>
                  </if>
              </where>
          </select>

    <select id="selectOverDevoByOrderNo" resultType="com.xisc.pm.domain.vo.CtMainVO">
        SELECT
            cd.ORDER_NO,
            cm.ORD_REM_WGT,
            cm.ORD_REM_NUM,
            cm.OVER_DEVO_WGT_TOTAL,
            cm.OVER_DEVO_NUM_TOTAL,
            cd.PLAN_WGT,
            cd.STOCK_WGT,
            cd.STOCK_NUM
        FROM
           CT_MAIN cm
                join CT_DET cd on cd.ORDER_NO = cm.ORDER_NO
        <where>
            cd.WHOLE_BACKLOG_CODE = '13'
            <if test="tenantId !=null">
                and cd.TENANT_ID = #{tenantId}
            </if>
            <if test="orderNoList != null and orderNoList.size() != 0">
                AND  cd.ORDER_NO IN
                <foreach collection="orderNoList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

          <select id="selectListByQd" resultType = "com.xisc.pm.api.dto.PmCtCollectPoolResDTO">
              select
                  qtlrs.ORDER_NO,
                  qtplc.WHOLE_BACKLOG,
                  qtlrs.MSC_LINE_NO,
                  qtlrs.ST_NO,
                  QTPO.ORDER_THICK  NOM_SLAB_THICK,
                  QTPO.ORDER_LEN  NOM_SLAB_LEN,
                  QTPO.ORDER_MAX_LEN  SLAB_MAX_NOM_LEN,
                  QTPO.ORDER_MIN_LEN SLAB_MIN_NOM_LEN,
                  QTPO.ORDER_WIDTH NOM_SLAB_WIDTH,
                  QTPO.ORDER_WIDTH SLAB_MAX_NOM_WIDTH,
                  QTPO.ORDER_WIDTH SLAB_MIN_NOM_WIDTH,
                  QTPO.ORDER_UNIT_AIM_WT NOM_SLAB_WT,
                  QTPO.ORDER_UNIT_MAX_WT SLAB_MAX_NOM_WT,
                  QTPO.ORDER_UNIT_MIN_WT SLAB_MIN_NOM_WT,
                  QTPO.THICK_TOL_MIN,
                  QTPO.THICK_TOL_MAX,
                  QTPO.WIDTH_MIN,
                  QTPO.WIDTH_MAX,
                  QTPO.WIDTH_TOL_MIN,
                  QTPO.WIDTH_TOL_MAX,
                  QTPO.ORDER_MIN_LEN,
                  QTPO.ORDER_MAX_LEN,
                  QTPO.LEN_TOL_MIN,
                  QTPO.LEN_TOL_MAX,
                  qtlrs.SLAB_PLACE_CODE,
                  qtlrs.SLAB_DEST,
                  qtlrs.REFINE_ROUTE_CODE
              from QD_TO_LINEUP_RESULT_STEEL qtlrs
              inner join QD_TO_PO_ORDER qtpo on (qtlrs.order_no = qtpo.order_no
              and qtlrs.tenant_id = qtpo.tenant_id)
              inner join QD_TO_PO_LINE_COMB qtplc
              on (qtplc.order_no = qtpo.order_no and qtplc.tenant_id = qtpo.tenant_id and
              qtlrs.msc_line_no = qtplc.msc_line_no)
              <where>
                  <if test="tenantId !=null">
                      and qtlrs.TENANT_ID = #{tenantId}
                  </if>
                  <if test="orderNoList != null and orderNoList.size() != 0">
                      AND  qtlrs.ORDER_NO IN
                      <foreach collection="orderNoList" index="index" item="item" open="(" separator="," close=")">
                          #{item}
                      </foreach>
                  </if>
                  <if test="wholeBacklogList != null and wholeBacklogList.size() != 0">
                      AND  qtplc.WHOLE_BACKLOG IN
                      <foreach collection="wholeBacklogList" index="index" item="item" open="(" separator="," close=")">
                          #{item}
                      </foreach>
                  </if>
                  <if test="dto.mscLineNo !=null">
                      <bind name="mscLineNoLike" value="dto.mscLineNo + '%'"/>
                      AND qtlrs.MSC_LINE_NO like #{mscLineNoLike}
                  </if>
                  <if test="dto.slabThick !=null">
                      and qtpo.ORDER_THICK = #{dto.slabThick}
                  </if>
                  <if test="dto.slabWidthFrom !=null">
                      and QTPO.ORDER_WIDTH <![CDATA[ >= ]]> #{dto.slabWidthFrom}
                  </if>
                  <if test="dto.slabWidthTo !=null">
                      and QTPO.ORDER_WIDTH <![CDATA[ <= ]]> #{dto.slabWidthTo}
                  </if>
                  <if test="dto.slabLenFrom !=null">
                      and qtpo.ORDER_LEN <![CDATA[ >= ]]> #{dto.slabLenFrom}
                  </if>
                  <if test="dto.slabLenTo !=null">
                      and qtpo.ORDER_LEN <![CDATA[ <= ]]> #{dto.slabLenTo}
                  </if>
              </where>
          </select>

    <select id="selectQdToLineupResultSteelList" resultType="com.xisc.pm.domain.vo.QdToLineupResultSteelVO">
        SELECT
            qtlrs.EXPAND_QUOT,
            qtlrs.ORDER_NO,
            qtlrs.MSC_LINE_NO
        FROM
            QD_TO_LINEUP_RESULT_STEEL qtlrs
        <where>
            <if test="tenantId !=null">
                and qtlrs.TENANT_ID = #{tenantId}
            </if>
            <if test="orderNoList != null and orderNoList.size() != 0">
                AND  qtlrs.ORDER_NO IN
                <foreach collection="orderNoList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="mscLineNoList != null and mscLineNoList.size() != 0">
                AND  qtlrs.MSC_LINE_NO IN
                <foreach collection="mscLineNoList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
