<?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.dgtly.order.mapper.MetaHanaSalesOrderMapper">

    <resultMap type="MetaHanaSalesOrder" id="MetaHanaSalesResult">
        <result property="docNumber"    column="DOC_NUMBER"    />
        <result property="docItem"    column="DOC_ITEM"    />
        <result property="salesOrg"    column="SALES_ORG"    />
        <result property="salesOrgName"    column="SALES_ORG_NAME"    />
        <result property="distributionChannel"    column="DISTRIBUTION_CHANNEL"    />
        <result property="customerCode"    column="CUSTOMER_CODE"    />
        <result property="customerName"    column="CUSTOMER_NAME"    />
        <result property="route"    column="ROUTE"    />
        <result property="division"    column="DIVISION"    />
        <result property="salesGroup"    column="SALES_GROUP"    />
        <result property="plant"    column="PLANT"    />
        <result property="docType"    column="DOC_TYPE"    />
        <result property="createDate"    column="CREATE_DATE"    />
        <result property="createDateItem"    column="CREATE_DATE_ITEM"    />
        <result property="deliverStatus"    column="DELIVER_STATUS"    />
        <result property="materialCode"    column="MATERIAL_CODE"    />
        <result property="materialName"    column="MATERIAL_NAME"    />
        <result property="sizeDim"    column="SIZE_DIM"    />
        <result property="creditDate"    column="CREDIT_DATE"    />
        <result property="creditStatus"    column="CREDIT_STATUS"    />
        <result property="rejectReason"    column="REJECT_REASON"    />
        <result property="noDeliverQty"    column="NO_DELIVER_QTY"    />
        <result property="noDeliverWt"    column="NO_DELIVER_WT"    />
        <result property="noDeliverAmt"    column="NO_DELIVER_AMT"    />
        <result property="deliverQty"    column="DELIVER_QTY"    />
        <result property="deliverWt"    column="DELIVER_WT"    />
        <result property="deliverAmt"    column="DELIVER_AMT"    />
    </resultMap>
    <sql id="selectMetaHanaSalesOrderVo">
        select DOC_NUMBER, DOC_ITEM, SALES_ORG, SALES_ORG_NAME, DISTRIBUTION_CHANNEL, CUSTOMER_CODE, CUSTOMER_NAME, ROUTE,
        DIVISION, SALES_GROUP, PLANT, DOC_TYPE, CREATE_DATE, CREATE_DATE_ITEM, DELIVER_STATUS, MATERIAL_CODE, MATERIAL_NAME, SIZE_DIM
         , CREDIT_DATE, CREDIT_STATUS, REJECT_REASON, NO_DELIVER_QTY, NO_DELIVER_WT, NO_DELIVER_AMT, DELIVER_QTY, DELIVER_WT, DELIVER_AMT from meta_hana_sales_order
    </sql>
    <select id="selectDistinctMetaHanaSalesOrder" parameterType="MetaHanaSalesOrder" resultMap="MetaHanaSalesResult">
        select distinct mhso.DOC_NUMBER, mhso.CUSTOMER_CODE, mhso.CUSTOMER_NAME,
         mhso.CREATE_DATE, mhso.CREDIT_DATE, mhso.CREDIT_STATUS, mhso.REJECT_REASON
        from meta_hana_sales_order mhso
        <where>
            <if test="docNumber != null  and docNumber != ''"> and mhso.DOC_NUMBER like concat('%', #{docNumber}, '%')</if>
            <if test="salesOrg != null  and salesOrg != ''"> and mhso.SALES_ORG =#{salesOrg}</if>
            <if test="salesOrgName != null  and salesOrgName != ''"> and mhso.SALES_ORG_NAME like concat('%', #{salesOrgName}, '%')</if>
            <if test="customerCode != null  and customerCode != ''"> and mhso.CUSTOMER_CODE like concat('%', #{customerCode}, '%')</if>
            <if test="customerName != null  and customerName != ''"> and mhso.CUSTOMER_NAME like concat('%', #{customerName}, '%')</if>
            <if test="docType != null  and docType != ''"> and mhso.DOC_TYPE = #{docType}</if>
            <if test="creditStatus != null  and creditStatus != ''"> and mhso.CREDIT_STATUS = #{creditStatus}</if>
        </where>
    </select>

    <select id="selectOrderExcepTimeByList" parameterType="String" resultType="Map">
        select order_number ,expected_time
        from sales_order_base
        where order_number in
        <foreach collection="list" item="orderNumber" open="(" separator="," close=")">
            #{orderNumber}
        </foreach>
    </select>

    <select id="selectDistinctMetaHanaSalesOrder1" parameterType="MetaHanaSalesOrder" resultMap="MetaHanaSalesResult">
        select distinct mhso.DOC_NUMBER, mhso.CUSTOMER_CODE, mhso.CUSTOMER_NAME,
        mhso.CREATE_DATE, mhso.CREDIT_DATE, mhso.CREDIT_STATUS, mhso.REJECT_REASON
            ,sob.expected_time as expectedTime

        from meta_hana_sales_order mhso
        LEFT JOIN sales_order_base sob
        on mhso.DOC_NUMBER = sob.order_number
        <where>
            <if test="docNumber != null  and docNumber != ''"> and mhso.DOC_NUMBER like concat('%', #{docNumber}, '%')</if>
            <if test="salesOrg != null  and salesOrg != ''"> and mhso.SALES_ORG =#{salesOrg}</if>
            <if test="salesOrgName != null  and salesOrgName != ''"> and mhso.SALES_ORG_NAME like concat('%', #{salesOrgName}, '%')</if>
            <if test="customerCode != null  and customerCode != ''"> and mhso.CUSTOMER_CODE like concat('%', #{customerCode}, '%')</if>
            <if test="customerName != null  and customerName != ''"> and mhso.CUSTOMER_NAME like concat('%', #{customerName}, '%')</if>
            <if test="docType != null  and docType != ''"> and mhso.DOC_TYPE = #{docType}</if>
            <if test="creditStatus != null  and creditStatus != ''"> and mhso.CREDIT_STATUS = #{creditStatus}</if>
            <if test="params.start_expectedTime != null and params.start_expectedTime != ''"><!-- 开始时间检索 -->
                and date_format(expected_time,'%y%m%d') &gt;= date_format(#{params.start_expectedTime},'%y%m%d')
            </if>
            <if test="params.end_expectedTime != null and params.end_expectedTime != ''"><!-- 结束时间检索 -->
                and date_format(expected_time,'%y%m%d') &lt;= date_format(#{params.end_expectedTime},'%y%m%d')
            </if>
        </where>
    </select>

</mapper>
