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


    <sql id = "BaseSql">
    PCOR.REL_ID,
    PCOR.PRODUCT_ORDER_NO,
    PCOR.BASE_ORDER_NO,
    PCOR.IP_ADDRESS,
    PCOR.TENANT_ID,
    PCOR.ATTRIBUTE1,
    PCOR.ATTRIBUTE2,
    PCOR.ATTRIBUTE3,
    PCOR.ATTRIBUTE4,
    PCOR.ATTRIBUTE5,
    PCOR.creation_date,    PCOR.created_by,    PCOR.last_updated_by,    PCOR.last_update_date,    PCOR.object_version_number
    </sql>

    <select id = "selectList" resultType = "com.xisc.pm.domain.entity.PmCompositeOrderRel">
        select
        <include refid = "BaseSql"/>
        from PM_COMPOSITE_ORDER_REL PCOR
        <where>
            <if test="relId !=null">
                and PCOR.REL_ID = #{relId,jdbcType = INTEGER}
            </if>
            <if test="productOrderNo !=null">
                and PCOR.PRODUCT_ORDER_NO = #{productOrderNo,jdbcType = VARCHAR}
            </if>
            <if test="baseOrderNo !=null">
                and PCOR.BASE_ORDER_NO = #{baseOrderNo,jdbcType = VARCHAR}
            </if>
            <if test="ipAddress !=null">
                and PCOR.IP_ADDRESS = #{ipAddress,jdbcType = VARCHAR}
            </if>
            <if test="tenantId !=null">
                and PCOR.TENANT_ID = #{tenantId,jdbcType = INTEGER}
            </if>
            <if test="attribute1 !=null">
                and PCOR.ATTRIBUTE1 = #{attribute1,jdbcType = VARCHAR}
            </if>
            <if test="attribute2 !=null">
                and PCOR.ATTRIBUTE2 = #{attribute2,jdbcType = VARCHAR}
            </if>
            <if test="attribute3 !=null">
                and PCOR.ATTRIBUTE3 = #{attribute3,jdbcType = VARCHAR}
            </if>
            <if test="attribute4 !=null">
                and PCOR.ATTRIBUTE4 = #{attribute4,jdbcType = VARCHAR}
            </if>
            <if test="attribute5 !=null">
                and PCOR.ATTRIBUTE5 = #{attribute5,jdbcType = VARCHAR}
            </if>
        </where>
    </select>

    <select id="listQdOrder" resultType="com.xisc.pm.api.dto.QdToPoOrderDTO">
        SELECT
        qtpo.ORDER_NO,
        qtpo.ORDER_TYPE_CODE,
        qtpo.SG_SIGN,
        qtpo.ORDER_THICK,
        qtpo.ORDER_WIDTH,
        qtpo.ORDER_LEN,
        qtpo.ORDER_NUM,
        qtpo.CONSIGN_CUST_CNAME
        FROM
        QD_TO_PO_ORDER qtpo
        JOIN CT_MAIN cm ON qtpo.ORDER_NO = cm.ORDER_NO
        <where>
        cm.ORDER_STATUS >= '23'
        AND cm.ORDER_STATUS <![CDATA[ < ]]> '67'
        AND qtpo.ORDER_TYPE_CODE != 'ZFH'
            <if test="orderNoList != null and orderNoList.size() > 0">
            and qtpo.ORDER_NO in
            <foreach collection="orderNoList" item="orderNo" open="(" close=")" separator="," >
                #{orderNo}
            </foreach>
            </if>
            <if test="orderNo !=null">
                <bind name="orderNoLike" value="'%' + orderNo + '%'"/>
                and qtpo.ORDER_NO like #{orderNoLike}
            </if>
            <if test="tenantId !=null">
                and qtpo.TENANT_ID = #{tenantId}
            </if>
        </where>
    </select>

    <select id="selectStNoBySteel" resultType="com.xisc.pm.domain.vo.QdToLineupResultSteelVO">
        SELECT distinct
            qtlrs.ORDER_NO,
            qtlrs.ST_NO
        FROM
            QD_TO_LINEUP_RESULT_STEEL qtlrs
        <where>
            <if test="orderNoList != null and orderNoList.size()>0">
                and  (qtlrs.ORDER_NO in
                <foreach collection="orderNoList" item="orderNo" open="(" close=")" separator="," index="index">
                    <if test="(index % 999) == 998"> NULL ) OR qtlrs.ORDER_NO in (</if> #{orderNo}
                </foreach>)
            </if>
            <if test="tenantId !=null">
                and qtlrs.TENANT_ID = #{tenantId}
             </if>
        </where>
    </select>

    <select id="listQdToLineupResultSteel" resultType="string">
        SELECT
            qtlrs.ORDER_NO
        FROM
            QD_TO_LINEUP_RESULT_STEEL qtlrs
        <where>
            <if test="tenantId !=null">
                and qtlrs.TENANT_ID = #{tenantId}
            </if>
            <if test="stNo !=null">
                and qtlrs.st_no = #{stNo}
            </if>
        </where>
    </select>

    <select id="selectQdAndCtOrder" resultType="com.xisc.pm.api.dto.QdToPoOrderDTO">
        SELECT
        qtpo.ORDER_NO,
        qtpo.ORDER_TYPE_CODE,
        qtpo.SG_SIGN,
        qtpo.ORDER_THICK,
        qtpo.ORDER_WIDTH,
        qtpo.ORDER_LEN,
        qtpo.ORDER_NUM,
        qtpo.CONSIGN_CUST_CNAME
        FROM
          QD_TO_PO_ORDER qtpo
          JOIN CT_MAIN cm ON qtpo.ORDER_NO = cm.ORDER_NO
        <where>
            cm.ORDER_STATUS <![CDATA[ <= ]]> '67'
            AND qtpo.ORDER_TYPE_CODE = 'ZFH'
            <if test="tenantId !=null">
                and qtpo.TENANT_ID = #{tenantId}
            </if>
            <if test="orderNoList != null and orderNoList.size()>0">
                and  ( qtpo.ORDER_NO in
                <foreach collection="orderNoList" item="orderNo" open="(" close=")" separator="," index="index">
                    <if test="(index % 999) == 998"> NULL ) OR qtpo.ORDER_NO in (</if> #{orderNo}
                </foreach>)
            </if>
        </where>
    </select>

    <select id="queryQdOrderByOrder" resultType="com.xisc.pm.api.dto.QdToPoOrderDTO">
        SELECT
        qtpo.ORDER_NO,
        qtpo.ORDER_TYPE_CODE,
        qtpo.SG_SIGN,
        qtpo.ORDER_THICK,
        qtpo.ORDER_WIDTH,
        qtpo.ORDER_LEN,
        qtpo.ORDER_NUM,
        qtpo.CONSIGN_CUST_CNAME
        FROM
        QD_TO_PO_ORDER qtpo
        <where>
            <if test="orderNoList != null and orderNoList.size()>0">
                and  ( qtpo.ORDER_NO in
                <foreach collection="orderNoList" item="orderNo" open="(" close=")" separator="," index="index">
                    <if test="(index % 999) == 998"> NULL ) OR qtpo.ORDER_NO in (</if> #{orderNo}
                </foreach>)
            </if>
            <if test="tenantId !=null">
                and qtpo.TENANT_ID = #{tenantId}
            </if>
        </where>
    </select>
</mapper>

