<?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.zt.plat.module.contractorder.dal.mysql.purchaseorder.PurchaseOrderMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectOrderByIds" resultMap="PurchaseOrderWithDetailsResultMap">
        SELECT
        po.ID as po_id,
        po.ORD_SAP_NUM,
        po.SYS_ORD_NUM,
        po.CPN_NAME,
        po.CPN_NUM,
        po.SPLR_NUM,
        po.SPLR_NAME,
        po.TP,
        po.MTRG_TP,
        po.VCHR_DT,
        po.PRCH_ORGZ_CD,
        po.RCV_FACT_NAME,
        po.RCV_FACT_NUM,
        po.RCV_WRH_NAME,
        po.RCV_WRH_NUM,
        po.PRCH_GRP,
        po.CUR_NUM,
        po.EXCH_RTE,
        po.PPR_CTRT_NUM,
        po.AGR_NUM,
        po.RMK,
        po.AGT_NUM,
        po.AGT_NAME,
        po.CTRT_NUM,
        po.MTRL_NUM,
        po.MTRL_NAME,
        po.CTRT_NAME,
        po.TNT_NUM,
        po.ERP_PRCH_CPN_NUM,
        po.ERP_PRCH_CPN_NAME,
        po.ERP_SALE_CPN_NUM,
        po.ERP_SALE_CPN_NAME,
        po.PRCH_ORGZ_NAME,
        po.ERP_STS,
        po.CAUS,
        po.STS,
        po.MTRL_TP,
        po.SPLY_BSN_TP,
        po.PRCH_GRP_NAME,
        po.PRCS_INSC_ID,
        po.RVW_ONN,
        po.TSK_NDE_ID,
        po.IS_PUSH,
        po.CREATE_TIME,
        po.UPDATE_TIME,
        po.CREATOR,
        po.UPDATER,
        po.UNT,
        po.DELETED,
        po.PDT_GRP_CDG,
        po.PDT_GRP_NAME,
        po.SALE_ACS_CDG,
        po.SALE_ACS_NAME,
        pod.ID as pod_id,
        pod.ORD_ID,
        pod.LINE_NUM,
        pod.MTRL_NUM,
        pod.MTRL_NAME,
        pod.RCV_FACT_NAME,
        pod.RCV_FACT_NUM,
        pod.RCV_WRH_NAME,
        pod.RCV_WRH_NUM,
        pod.QTY,
        pod.UNT,
        pod.IN_TAX_UPRC,
        pod.PRC_UNT,
        pod.TAX_NUM,
        pod.IS_GR_INV,
        pod.IS_UNL_RCV,
        pod.BAT,
        pod.PRJ_CTGR,
        pod.ACTS_CTGR,
        pod.MTRL_CPNT_NUM,
        pod.MTRL_CPNT_DSP,
        pod.SHRT_TXT,
        pod.IS_RLBK_CGO,
        pod.IS_FREE_RCV,
        pod.OUT_LINE_NUM,
        pod.RMK_UNT,
        pod.RMK_MTRL,
        pod.BGN_DT,
        pod.DDL_DT,
        pod.LST_QTY,
        pod.TRF_QTY,
        pod.AGR_NUM as pod_agr_num,
        pod.TRF_FACT_NAME,
        pod.TRF_FACT_NUM,
        pod.TRF_WRH_NAME,
        pod.TRF_WRH_NUM,
        pod.RMK as pod_rmk,
        pod.ORIG_WET,
        pod.SALE_MTRL_NUM,
        pod.IN_ORD,
        pod.PRCH_CTGR,
        pod.IS_ENB,
        pod.ACTS_CTGR_DTL,
        pod.ENTT_DTL,
        pod.ELEM_ABBR,
        pod.ELEM_NAME,
        pod.ELEM_CDG
        FROM BSE_PRCH_ORD po
        LEFT JOIN BSE_PRCH_ORD_DTL pod ON po.ID = pod.ORD_ID
        WHERE po.Id IN
        <foreach collection="ids" item="orderNumber" open="(" separator="," close=")">
            #{orderNumber}
        </foreach>
        AND po.deleted=0
        ORDER BY po.SYS_ORD_NUM, pod.LINE_NUM
    </select>

    <select id="selectOrderByOrderNos" resultMap="PurchaseOrderWithDetailsResultMap">
        SELECT
        po.ID as po_id,
        po.ORD_SAP_NUM,
        po.SYS_ORD_NUM,
        po.CPN_NAME,
        po.CPN_NUM,
        po.SPLR_NUM,
        po.SPLR_NAME,
        po.TP,
        po.MTRL_TP,
        po.VCHR_DT,
        po.PRCH_ORGZ_CD,
        po.RCV_FACT_NAME,
        po.RCV_FACT_NUM,
        po.RCV_WRH_NAME,
        po.RCV_WRH_NUM,
        po.PRCH_GRP,
        po.CUR_NUM,
        po.EXCH_RTE,
        po.PPR_CTRT_NUM,
        po.AGR_NUM,
        po.RMK,
        po.MTRG_TP,
        po.UNT,
        po.AGT_NUM,
        po.AGT_NAME,
        po.CTRT_NUM,
        po.MTRL_NUM,
        po.MTRL_NAME,
        po.CTRT_NAME,
        po.TNT_NUM,
        po.ERP_PRCH_CPN_NUM,
        po.ERP_PRCH_CPN_NAME,
        po.ERP_SALE_CPN_NUM,
        po.ERP_SALE_CPN_NAME,
        po.PRCH_ORGZ_NAME,
        po.ERP_STS,
        po.CAUS,
        po.STS,
        po.PRCH_GRP_NAME,
        po.PRCS_INSC_ID,
        po.RVW_ONN,
        po.TSK_NDE_ID,
        po.IS_PUSH,
        po.SPLY_BSN_TP,
        po.CREATE_TIME,
        po.UPDATE_TIME,
        po.CREATOR,
        po.UPDATER,
        po.DELETED,
        po.PDT_GRP_CDG,
        po.PDT_GRP_NAME,
        po.SALE_ACS_CDG,
        po.SALE_ACS_NAME,
        pod.ID as pod_id,
        pod.ORD_ID,
        pod.LINE_NUM,
        pod.MTRL_NUM,
        pod.MTRL_NAME,
        pod.RCV_FACT_NAME,
        pod.RCV_FACT_NUM,
        pod.RCV_WRH_NAME,
        pod.RCV_WRH_NUM,
        pod.QTY,
        pod.UNT,
        pod.IN_TAX_UPRC,
        pod.PRC_UNT,
        pod.TAX_NUM,
        pod.IS_GR_INV,
        pod.IS_UNL_RCV,
        pod.BAT,
        pod.PRJ_CTGR,
        pod.ACTS_CTGR,
        pod.MTRL_CPNT_NUM,
        pod.MTRL_CPNT_DSP,
        pod.SHRT_TXT,
        pod.IS_RLBK_CGO,
        pod.IS_FREE_RCV,
        pod.OUT_LINE_NUM,
        pod.RMK_UNT,
        pod.RMK_MTRL,
        pod.BGN_DT,
        pod.DDL_DT,
        pod.LST_QTY,
        pod.TRF_QTY,
        pod.AGR_NUM as pod_agr_num,
        pod.TRF_FACT_NAME,
        pod.TRF_FACT_NUM,
        pod.TRF_WRH_NAME,
        pod.TRF_WRH_NUM,
        pod.RMK as pod_rmk,
        pod.ORIG_WET,
        pod.SALE_MTRL_NUM,
        pod.IN_ORD,
        pod.PRCH_CTGR,
        pod.IS_ENB,
        pod.ACTS_CTGR_DTL,
        pod.ENTT_DTL,
        pod.ELEM_ABBR,
        pod.ELEM_NAME,
        pod.ELEM_CDG
        FROM BSE_PRCH_ORD po
        LEFT JOIN BSE_PRCH_ORD_DTL pod ON po.ID = pod.ORD_ID
        WHERE po.sys_ord_num IN
        <foreach collection="orderNos" item="orderNumber" open="(" separator="," close=")">
            #{orderNumber}
        </foreach>
        AND po.deleted=0
        ORDER BY po.SYS_ORD_NUM, pod.LINE_NUM
    </select>

    <select id="selectByOrderId"
            resultType="com.zt.plat.module.contractorder.dal.dataobject.purchaseorder.PurchaseOrderDO">
        select * from bse_prch_ord where sys_ord_num =#{orderId};
    </select>


    <resultMap id="PurchaseOrderWithDetailsResultMap" type="com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.PurchaseOrderWithDetailsVO">
        <result column="SYS_ORD_NUM" property="systemOrderNumber"/>
        <association property="purchaseOrder" resultMap="PurchaseOrderResultMap"/>
        <collection property="orderDetails" ofType="com.zt.plat.module.contractorder.dal.dataobject.purchaseorder.PrchOrdDtlDO" resultMap="PrchOrdDtlResultMap"/>
    </resultMap>

    <resultMap id="PurchaseOrderResultMap" type="com.zt.plat.module.contractorder.dal.dataobject.purchaseorder.PurchaseOrderDO">
        <id column="po_id" property="id"/>
        <result column="ORD_SAP_NUM" property="orderSAPNumber"/>
        <result column="SYS_ORD_NUM" property="systemOrderNumber"/>
        <result column="CPN_NAME" property="cpName"/>
        <result column="CPN_NUM" property="cpNum"/>
        <result column="SPLR_NUM" property="supplierNumber"/>
        <result column="SPLR_NAME" property="supplierName"/>
        <result column="TP" property="type"/>
        <result column="VCHR_DT" property="voucherDate"/>
        <result column="PRCH_ORGZ_CD" property="purchaseOrganizationCustomsDeclaration"/>
        <result column="RCV_FACT_NAME" property="receiveFactoryName"/>
        <result column="RCV_FACT_NUM" property="receiveFactoryNumber"/>
        <result column="RCV_WRH_NAME" property="receiveWarehouseName"/>
        <result column="RCV_WRH_NUM" property="receiveWarehouseNumber"/>
        <result column="PRCH_GRP" property="purchaseGroup"/>
        <result column="CUR_NUM" property="currencyNumber"/>
        <result column="EXCH_RTE" property="exchangeRate"/>
        <result column="PPR_CTRT_NUM" property="paperContractNumber"/>
        <result column="AGR_NUM" property="agreementNumber"/>
        <result column="RMK" property="remark"/>
        <result column="MTRG_TP" property="meteringType"/>
        <result column="AGT_NUM" property="agentNumber"/>
        <result column="AGT_NAME" property="agentName"/>
        <result column="CTRT_NUM" property="contractNumber"/>
        <result column="MTRL_NUM" property="materialNumber"/>
        <result column="MTRL_NAME" property="materialName"/>
        <result column="CTRT_NAME" property="contractName"/>
        <result column="TNT_NUM" property="tenantNumber"/>
        <result column="ERP_PRCH_CPN_NUM" property="erpPurchaseCompanyNumber"/>
        <result column="ERP_PRCH_CPN_NAME" property="erpPurchaseCompanyName"/>
        <result column="ERP_SALE_CPN_NUM" property="erpSalesCompanyNumber"/>
        <result column="ERP_SALE_CPN_NAME" property="erpSalesCompanyName"/>
        <result column="PRCH_ORGZ_NAME" property="purchaseOrganizationName"/>
        <result column="ERP_STS" property="erpStatus"/>
        <result column="CAUS" property="cause"/>
        <result column="STS" property="status"/>
        <result column="PRCH_GRP_NAME" property="purchaseGroupName"/>
        <result column="PRCS_INSC_ID" property="processInstanceId"/>
        <result column="RVW_ONN" property="reviewOpinion"/>
        <result column="TSK_NDE_ID" property="taskId"/>
        <result column="IS_PUSH" property="isPush"/>
        <result column="UNT" property="unt"/>
        <result column="MTRL_TP" property="mtrlTp"/>
        <result column="SPLY_BSN_TP" property="splyBsnTp"/>
        <result column="PDT_GRP_CDG" property="pdtGrpCdg"/>
        <result column="PDT_GRP_NAME" property="pdtGrpName"/>
        <result column="SALE_ACS_CDG" property="saleAcsCdg"/>
        <result column="SALE_ACS_NAME" property="saleAcsName"/>
    </resultMap>

    <resultMap id="PrchOrdDtlResultMap" type="com.zt.plat.module.contractorder.dal.dataobject.purchaseorder.PrchOrdDtlDO">
        <id column="pod_id" property="id"/>
        <result column="ORD_ID" property="ordId"/>
        <result column="LINE_NUM" property="lineNum"/>
        <result column="MTRL_NUM" property="mtrlNum"/>
        <result column="MTRL_NAME" property="mtrlName"/>
        <result column="RCV_FACT_NAME" property="rcvFactName"/>
        <result column="RCV_FACT_NUM" property="rcvFactNum"/>
        <result column="RCV_WRH_NAME" property="rcvWrhName"/>
        <result column="RCV_WRH_NUM" property="rcvWrhNum"/>
        <result column="QTY" property="qty"/>
        <result column="UNT" property="unt"/>
        <result column="IN_TAX_UPRC" property="inTaxUprc"/>
        <result column="PRC_UNT" property="prcUnt"/>
        <result column="TAX_NUM" property="taxNum"/>
        <result column="IS_GR_INV" property="isGrInv"/>
        <result column="IS_UNL_RCV" property="isUnlRcv"/>
        <result column="BAT" property="bat"/>
        <result column="PRJ_CTGR" property="prjCtgr"/>
        <result column="ACTS_CTGR" property="actsCtgr"/>
        <result column="MTRL_CPNT_NUM" property="mtrlCpntNum"/>
        <result column="MTRL_CPNT_DSP" property="mtrlCpntDsp"/>
        <result column="SHRT_TXT" property="shrtTxt"/>
        <result column="IS_RLBK_CGO" property="isRlbkCgo"/>
        <result column="IS_FREE_RCV" property="isFreeRcv"/>
        <result column="OUT_LINE_NUM" property="outLineNum"/>
        <result column="RMK_UNT" property="rmkUnt"/>
        <result column="RMK_MTRL" property="rmkMtrl"/>
        <result column="BGN_DT" property="bgnDt"/>
        <result column="DDL_DT" property="ddlDt"/>
        <result column="LST_QTY" property="lstQty"/>
        <result column="TRF_QTY" property="trfQty"/>
        <result column="pod_agr_num" property="agrNum"/>
        <result column="TRF_FACT_NAME" property="trfFactName"/>
        <result column="TRF_FACT_NUM" property="trfFactNum"/>
        <result column="TRF_WRH_NAME" property="trfWrhName"/>
        <result column="TRF_WRH_NUM" property="trfWrhNum"/>
        <result column="pod_rmk" property="rmk"/>
        <result column="ORIG_WET" property="origWet"/>
        <result column="SALE_MTRL_NUM" property="saleMtrlNum"/>
        <result column="IN_ORD" property="inOrd"/>
        <result column="PRCH_CTGR" property="prchCtgr"/>
        <result column="IS_ENB" property="isEnb"/>
        <result column="ACTS_CTGR_DTL" property="actsCtgrDtl"/>
        <result column="ENTT_DTL" property="enttDtl"/>
        <result column="ELEM_ABBR" property="elemAbbr"/>
        <result column="ELEM_NAME" property="elemName"/>
    </resultMap>
</mapper>
