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


    <sql id = "BaseSql">
    CM.MAIN_ID,
    CM.CONTRACT_NUM,
    CM.ORDER_NO,
    CM.ORDER_STATUS,
    CM.LINEUP_STATUS,
    CM.MSC_LINE_NO,
    CM.ORDER_WGT,
    CM.ORDER_NUM,
    CM.REQR_PROD_WGT,
    CM.REQR_PROD_NUM,
    CM.CONFM_WGT,
    CM.CONFM_NUM,
    CM.DELIVY_WGT,
    CM.DELIVY_NUM,
    CM.ORD_THK,
    CM.ORD_WID,
    CM.ORD_LEN,
    CM.ORD_WGT,
    CM.DELIVY_NUM_TOL_PLUS,
    CM.DELIVY_NUM_TOL_MINUS,
    CM.DELIVY_WGT_TOL_PLUS,
    CM.DELIVY_WGT_TOL_MINUS,
    CM.PROD_END_FLAG,
    CM.DELIVY_END_FLAG,
    CM.ORDER_DELAY_FLAG,
    CM.TAIL_FLAG,
    CM.ORDER_CREATE_TIME,
    CM.ORDER_PROC_TIME,
    CM.ORDER_MODI_TIME,
    CM.PROD_DEVO_TIME,
    CM.PROD_END_TIME,
    CM.DELIVY_END_TIME,
    CM.FINA_END_TIME,
    CM.SUPPLEMENTARY_ROLLS_NUM_TOTAL,
    CM.OVER_DEVO_WGT_TOTAL,
    CM.OVER_DEVO_NUM_TOTAL,
    CM.OFF_ORDER_WGT_TOTAL,
    CM.OFF_ORDER_NUM_TOTAL,
    CM.ON_ORDER_WGT_TOTAL,
    CM.ON_ORDER_NUM_TOTAL,
    CM.XS_OVER_NUM,
    CM.SC_OVER_NUM,
    CM.DES_DUEDATE,
    CM.SMS_DUEDATE,
    CM.MILL_DUEDATE,
    CM.CUT_DUEDATE,
    CM.HTM_DUEDATE,
    CM.SHAPE_DUEDATE,
    CM.QUALITY_DUEDATE,
    CM.SHP_DUEDATE,
    CM.ORDER_DUEDATE,
    CM.PLAN_DUEDATE,
    CM.DEL_DELAY_DAY,
    CM.LEVEL_CODE,
    CM.REMARK,
    CM.ORD_REM_WGT,
    CM.ORD_REM_NUM,
    CM.PROD_WGT,
    CM.PROD_NUM,
    CM.PROD_END_WGT,
    CM.PROD_END_NUM,
    CM.SHIP_END_WGT,
    CM.SHIP_END_NUM,
    CM.TENANT_ID,
    CM.creation_date,    CM.created_by,    CM.last_updated_by,    CM.last_update_date,    CM.object_version_number
    </sql>

    <select id = "selectList" resultType = "com.xisc.pm.domain.vo.CtMainVO1">
        select
        <include refid = "BaseSql"/>
        from CT_MAIN CM
        <where>
            <if test="mainId !=null">
                and CM.MAIN_ID = #{mainId,jdbcType = INTEGER}
            </if>
            <if test="contractNum !=null">
                and CM.CONTRACT_NUM = #{contractNum,jdbcType = VARCHAR}
            </if>
            <if test="orderNo !=null">
                and CM.ORDER_NO = #{orderNo,jdbcType = VARCHAR}
            </if>
            <if test="orderStatus !=null">
                and CM.ORDER_STATUS = #{orderStatus,jdbcType = VARCHAR}
            </if>
            <if test="lineupStatus !=null">
                and CM.LINEUP_STATUS = #{lineupStatus,jdbcType = VARCHAR}
            </if>
            <if test="mscLineNo !=null">
                and CM.MSC_LINE_NO = #{mscLineNo,jdbcType = VARCHAR}
            </if>
            <if test="orderWgt !=null">
                and CM.ORDER_WGT = #{orderWgt,jdbcType = INTEGER}
            </if>
            <if test="orderNum !=null">
                and CM.ORDER_NUM = #{orderNum,jdbcType = INTEGER}
            </if>
            <if test="reqrProdWgt !=null">
                and CM.REQR_PROD_WGT = #{reqrProdWgt,jdbcType = INTEGER}
            </if>
            <if test="reqrProdNum !=null">
                and CM.REQR_PROD_NUM = #{reqrProdNum,jdbcType = INTEGER}
            </if>
            <if test="confmWgt !=null">
                and CM.CONFM_WGT = #{confmWgt,jdbcType = INTEGER}
            </if>
            <if test="confmNum !=null">
                and CM.CONFM_NUM = #{confmNum,jdbcType = INTEGER}
            </if>
            <if test="delivyWgt !=null">
                and CM.DELIVY_WGT = #{delivyWgt,jdbcType = INTEGER}
            </if>
            <if test="delivyNum !=null">
                and CM.DELIVY_NUM = #{delivyNum,jdbcType = INTEGER}
            </if>
            <if test="ordThk !=null">
                and CM.ORD_THK = #{ordThk,jdbcType = INTEGER}
            </if>
            <if test="ordWid !=null">
                and CM.ORD_WID = #{ordWid,jdbcType = INTEGER}
            </if>
            <if test="ordLen !=null">
                and CM.ORD_LEN = #{ordLen,jdbcType = INTEGER}
            </if>
            <if test="ordWgt !=null">
                and CM.ORD_WGT = #{ordWgt,jdbcType = INTEGER}
            </if>
            <if test="delivyNumTolPlus !=null">
                and CM.DELIVY_NUM_TOL_PLUS = #{delivyNumTolPlus,jdbcType = INTEGER}
            </if>
            <if test="delivyNumTolMinus !=null">
                and CM.DELIVY_NUM_TOL_MINUS = #{delivyNumTolMinus,jdbcType = INTEGER}
            </if>
            <if test="delivyWgtTolPlus !=null">
                and CM.DELIVY_WGT_TOL_PLUS = #{delivyWgtTolPlus,jdbcType = INTEGER}
            </if>
            <if test="delivyWgtTolMinus !=null">
                and CM.DELIVY_WGT_TOL_MINUS = #{delivyWgtTolMinus,jdbcType = INTEGER}
            </if>
            <if test="prodEndFlag !=null">
                and CM.PROD_END_FLAG = #{prodEndFlag,jdbcType = INTEGER}
            </if>
            <if test="delivyEndFlag !=null">
                and CM.DELIVY_END_FLAG = #{delivyEndFlag,jdbcType = INTEGER}
            </if>
            <if test="orderDelayFlag !=null">
                and CM.ORDER_DELAY_FLAG = #{orderDelayFlag,jdbcType = INTEGER}
            </if>
            <if test="tailFlag !=null">
                and CM.TAIL_FLAG = #{tailFlag,jdbcType = INTEGER}
            </if>
            <if test="orderCreateTime !=null">
                and CM.ORDER_CREATE_TIME = #{orderCreateTime,jdbcType = OTHER}
            </if>
            <if test="orderProcTime !=null">
                and CM.ORDER_PROC_TIME = #{orderProcTime,jdbcType = OTHER}
            </if>
            <if test="orderModiTime !=null">
                and CM.ORDER_MODI_TIME = #{orderModiTime,jdbcType = OTHER}
            </if>
            <if test="prodDevoTime !=null">
                and CM.PROD_DEVO_TIME = #{prodDevoTime,jdbcType = OTHER}
            </if>
            <if test="prodEndTime !=null">
                and CM.PROD_END_TIME = #{prodEndTime,jdbcType = OTHER}
            </if>
            <if test="delivyEndTime !=null">
                and CM.DELIVY_END_TIME = #{delivyEndTime,jdbcType = OTHER}
            </if>
            <if test="finaEndTime !=null">
                and CM.FINA_END_TIME = #{finaEndTime,jdbcType = OTHER}
            </if>
            <if test="supplementaryRollsNumTotal !=null">
                and CM.SUPPLEMENTARY_ROLLS_NUM_TOTAL = #{supplementaryRollsNumTotal,jdbcType = INTEGER}
            </if>
            <if test="overDevoWgtTotal !=null">
                and CM.OVER_DEVO_WGT_TOTAL = #{overDevoWgtTotal,jdbcType = INTEGER}
            </if>
            <if test="overDevoNumTotal !=null">
                and CM.OVER_DEVO_NUM_TOTAL = #{overDevoNumTotal,jdbcType = INTEGER}
            </if>
            <if test="offOrderWgtTotal !=null">
                and CM.OFF_ORDER_WGT_TOTAL = #{offOrderWgtTotal,jdbcType = INTEGER}
            </if>
            <if test="offOrderNumTotal !=null">
                and CM.OFF_ORDER_NUM_TOTAL = #{offOrderNumTotal,jdbcType = INTEGER}
            </if>
            <if test="onOrderWgtTotal !=null">
                and CM.ON_ORDER_WGT_TOTAL = #{onOrderWgtTotal,jdbcType = INTEGER}
            </if>
            <if test="onOrderNumTotal !=null">
                and CM.ON_ORDER_NUM_TOTAL = #{onOrderNumTotal,jdbcType = INTEGER}
            </if>
            <if test="xsOverNum !=null">
                and CM.XS_OVER_NUM = #{xsOverNum,jdbcType = INTEGER}
            </if>
            <if test="scOverNum !=null">
                and CM.SC_OVER_NUM = #{scOverNum,jdbcType = INTEGER}
            </if>
            <if test="desDuedate !=null">
                and CM.DES_DUEDATE = #{desDuedate,jdbcType = OTHER}
            </if>
            <if test="smsDuedate !=null">
                and CM.SMS_DUEDATE = #{smsDuedate,jdbcType = OTHER}
            </if>
            <if test="millDuedate !=null">
                and CM.MILL_DUEDATE = #{millDuedate,jdbcType = OTHER}
            </if>
            <if test="cutDuedate !=null">
                and CM.CUT_DUEDATE = #{cutDuedate,jdbcType = OTHER}
            </if>
            <if test="htmDuedate !=null">
                and CM.HTM_DUEDATE = #{htmDuedate,jdbcType = OTHER}
            </if>
            <if test="shapeDuedate !=null">
                and CM.SHAPE_DUEDATE = #{shapeDuedate,jdbcType = OTHER}
            </if>
            <if test="qualityDuedate !=null">
                and CM.QUALITY_DUEDATE = #{qualityDuedate,jdbcType = OTHER}
            </if>
            <if test="shpDuedate !=null">
                and CM.SHP_DUEDATE = #{shpDuedate,jdbcType = OTHER}
            </if>
            <if test="orderDuedate !=null">
                and CM.ORDER_DUEDATE = #{orderDuedate,jdbcType = OTHER}
            </if>
            <if test="planDuedate !=null">
                and CM.PLAN_DUEDATE = #{planDuedate,jdbcType = OTHER}
            </if>
            <if test="delDelayDay !=null">
                and CM.DEL_DELAY_DAY = #{delDelayDay,jdbcType = INTEGER}
            </if>
            <if test="levelCode !=null">
                and CM.LEVEL_CODE = #{levelCode,jdbcType = VARCHAR}
            </if>
            <if test="remark !=null">
                and CM.REMARK = #{remark,jdbcType = VARCHAR}
            </if>
            <if test="ordRemWgt !=null">
                and CM.ORD_REM_WGT = #{ordRemWgt,jdbcType = INTEGER}
            </if>
            <if test="ordRemNum !=null">
                and CM.ORD_REM_NUM = #{ordRemNum,jdbcType = INTEGER}
            </if>
            <if test="prodWgt !=null">
                and CM.PROD_WGT = #{prodWgt,jdbcType = INTEGER}
            </if>
            <if test="prodNum !=null">
                and CM.PROD_NUM = #{prodNum,jdbcType = INTEGER}
            </if>
            <if test="prodEndWgt !=null">
                and CM.PROD_END_WGT = #{prodEndWgt,jdbcType = INTEGER}
            </if>
            <if test="prodEndNum !=null">
                and CM.PROD_END_NUM = #{prodEndNum,jdbcType = INTEGER}
            </if>
            <if test="shipEndWgt !=null">
                and CM.SHIP_END_WGT = #{shipEndWgt,jdbcType = INTEGER}
            </if>
            <if test="shipEndNum !=null">
                and CM.SHIP_END_NUM = #{shipEndNum,jdbcType = INTEGER}
            </if>
            <if test="tenantId !=null">
                and CM.TENANT_ID = #{tenantId,jdbcType = INTEGER}
            </if>
        </where>
    </select>
    
    <select id="selectCtMainByOrderList" resultType="com.xisc.pm.domain.vo.CtMainVO">
		select 
			ORDER_NO , 
			ORD_REM_WGT,
			ORD_REM_NUM,
            ORDER_NUM
		from CT_MAIN
		where TENANT_ID = #{tenantId}
		and ORDER_NO in 
		<foreach collection="orderNoList" item="orderNo" open="(" close=")" separator="," >
			#{orderNo}
		</foreach>
	</select>
	
	<select id="selectByCtMain" resultType="java.lang.String" parameterType="com.xisc.pm.api.dto.PmProContPoolRequestDTO">
		select cm.ORDER_NO
		from CT_MAIN cm
		where cm.TENANT_ID = #{request.tenantId}
		<if test="orderStatus != null and orderStatus != ''">
			and cm.ORDER_STATUS = #{orderStatus}
		</if>
		<if test="orderStatusFrom != null and orderStatusFrom != ''">
			and cm.ORDER_STATUS <![CDATA[ >= ]]> #{orderStatusFrom}
		</if>
		<if test="orderStatusTo != null and orderStatusTo != ''">
			and cm.ORDER_STATUS <![CDATA[ <= ]]> #{orderStatusTo}
		</if>
		<if test="request.orderNo != null and request.orderNo != ''">
			and cm.ORDER_NO = #{request.orderNo}
		</if>
		<!-- <if test="request.delivyDateFrom != null">
			and cm.DELIVY_END_TIME <![CDATA[ >= ]]> #{request.delivyDateFrom}
		</if>
		<if test="request.delivyDateTo != null">
			and cm.DELIVY_END_TIME <![CDATA[ <= ]]> #{request.delivyDateTo}
		</if> -->
		<if test="request.orderThickFrom != null">
			and cm.ORD_THK <![CDATA[ >= ]]> #{request.orderThickFrom}
		</if>
		<if test="request.orderThickTo != null">
			and cm.ORD_THK <![CDATA[ <= ]]> #{request.orderThickTo}
		</if>
		<!-- <if test="request.mscLineNo != null and request.mscLineNo != ''">
			and cm.MSC_LINE_NO = #{request.mscLineNo}
		</if> -->

	</select>
	
	<select id="selectCanPdByOrderStatus" resultType="com.xisc.pm.domain.vo.CtMainVO" parameterType="com.xisc.pm.api.dto.PmProContFilterRequestDTO">
		select
		    cm.ORDER_NO,
		    cm.TAIL_FLAG,
		    cm.ORD_REM_NUM,
		    cm.ORD_REM_WGT,
		    cm.ORDER_STATUS,
		    cd.STOCK_NUM,
		    cd.PLAN_NUM,
            (COALESCE(cd.STOCK_NUM, 0) - COALESCE(cd.PLAN_NUM, 0)) AS userdBoardNum
		from CT_MAIN cm
		left join CT_DET cd on cd.ORDER_NO = cm.ORDER_NO and cd.WHOLE_BACKLOG_CODE = '13'
		where cm.TENANT_ID = #{request.tenantId}
		and cm.ORDER_STATUS <![CDATA[ >= ]]> '40'
		and cm.ORDER_STATUS <![CDATA[ < ]]> '67'
		<!-- <if test="request.delivyDateFrom != null">
			and cm.DELIVY_END_TIME <![CDATA[ >= ]]> #{request.delivyDateFrom}
		</if>
		<if test="request.delivyDateTo != null">
			and cm.DELIVY_END_TIME <![CDATA[ <= ]]> #{request.delivyDateTo}
		</if> -->
		<if test="request.orderThickFrom != null">
			and cm.ORD_THK <![CDATA[ >= ]]> #{request.orderThickFrom}
		</if>
		<if test="request.orderThickTo != null">
			and cm.ORD_THK <![CDATA[ <= ]]> #{request.orderThickTo}
		</if>
		<!-- <if test="request.mscLineNo != null and request.mscLineNo != ''">
			and cm.MSC_LINE_NO = #{request.mscLineNo}
		</if> -->
		<if test="request.orderNoList != null and request.orderNoList.size() > 0">
			and cm.ORDER_NO in 
			<foreach collection="request.orderNoList" item="orderNoItem" open="(" close=")" separator="," >
				#{orderNoItem}
			</foreach>
		</if>

	</select>

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