<?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.zfire.mall.manager.commonmapper.manage.TransferMapper">




    <select id="list" resultType="com.zfire.mall.manager.bean.k3.KingTransfer">
        SELECT *
        FROM king_dee_transfer kdt
                 LEFT JOIN king_dee_transfer_item kdti ON kdt.id = kdti.f_id
        where 1 = 1
        <if test="billNo != null and billNo != ''">
            and kdt.bill_no like  CONCAT('%', #{billNo},'%')
        </if>
        <if test="materialName != null and materialName != ''">
            and kdti.material_name like  CONCAT('%', #{materialName},'%')
        </if>
        <if test="startTime != null and startTime != ''">
            and kdt.f_date BETWEEN #{startTime} and #{endTime}
        </if>
        order by kdt.create_time desc
    </select>


    <resultMap id="MapDetail" type="com.zfire.mall.manager.bean.k3.KingTransferBean">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="supply_org_id" jdbcType="VARCHAR" property="supplyOrgId"/>

        <result column="bill_no" jdbcType="VARCHAR" property="billNo"/>
        <result column="f_date" jdbcType="TIMESTAMP" property="forDate"/>
        <result column="stock_org_id" jdbcType="VARCHAR" property="stockOrgId"/>
        <result column="stock_out_org_id" jdbcType="VARCHAR" property="stockOutOrgId"/>
        <result column="transfer_biz_type" jdbcType="VARCHAR" property="transferBizType"/>

        <result column="cancel_status" jdbcType="VARCHAR" property="cancelStatus"/>
        <result column="note" jdbcType="VARCHAR" property="note"/>
        <result column="approver_id" jdbcType="VARCHAR" property="approverId"/>
        <result column="approve_date" jdbcType="TIMESTAMP" property="approveDate"/>
        <result column="canceller_id" jdbcType="VARCHAR" property="cancellerId"/>
        <result column="cancel_date" jdbcType="TIMESTAMP" property="cancelDate"/>
        <result column="bill_type" jdbcType="VARCHAR" property="billType"/>
        <result column="document_status" jdbcType="VARCHAR" property="documentStatus"/>
        <result column="sync_time" jdbcType="TIMESTAMP" property="syncTime"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <collection property="kingDeeTransferItems" ofType="com.zfire.mall.manager.plus.entity.KingDeeTransferItem" select="itemDetail" column="id">
            <id column="id" jdbcType="VARCHAR" property="id"/>
            <result column="supply_org_id" jdbcType="VARCHAR" property="supplyOrgId"/>
            <result column="f_id" jdbcType="VARCHAR" property="fId"/>
            <result column="bill_no" jdbcType="VARCHAR" property="billNo"/>
            <result column="entry_id" jdbcType="VARCHAR" property="entryId"/>
            <result column="material_id" jdbcType="VARCHAR" property="materialId"/>
            <result column="material_number" jdbcType="VARCHAR" property="materialNumber"/>
            <result column="material_name" jdbcType="VARCHAR" property="materialName"/>
            <result column="src_stock_id" jdbcType="VARCHAR" property="srcStockId"/>
            <result column="dest_stock_id" jdbcType="VARCHAR" property="destStockId"/>
            <result column="qty" jdbcType="DECIMAL" property="qty"/>
            <result column="owner_id" jdbcType="VARCHAR" property="ownerId"/>
            <result column="owner_out_id" jdbcType="VARCHAR" property="ownerOutId"/>
            <result column="model" jdbcType="VARCHAR" property="model"/>
            <result column="base_unit_id" jdbcType="VARCHAR" property="baseUnitId"/>
            <result column="consign_amount" jdbcType="DECIMAL" property="consignAmount"/>
            <result column="consign_price" jdbcType="DECIMAL" property="consignPrice"/>
            <result column="sync_time" jdbcType="TIMESTAMP" property="syncTime"/>
            <result column="src_stock_loc_id" jdbcType="VARCHAR" property="srcStockLocId"/>
            <result column="dest_stock_loc_id" jdbcType="VARCHAR" property="destStockLocId"/>
            <result column="note_entry" jdbcType="VARCHAR" property="noteEntry"/>
            <result column="src_stock_name" jdbcType="VARCHAR" property="srcStockName"/>
            <result column="dest_stock_name" jdbcType="VARCHAR" property="destStockName"/>
        </collection>
    </resultMap>

    <select id="detail" resultMap="MapDetail">
        SELECT *
        FROM king_dee_transfer kdt
        where kdt.id=#{id}
    </select>


    <select id="itemDetail" resultType="com.zfire.mall.manager.plus.entity.KingDeeTransferItem">
        SELECT *
        FROM king_dee_transfer_item kdt
        where kdt.f_id=#{id}
    </select>

    <select id="listPurchase" resultType="com.zfire.mall.manager.bean.k3.KingDeePurchaseOrderBean">
        SELECT
            *
        FROM
            king_dee_purchase_order kdpo
                LEFT JOIN king_dee_purchase_order_item kdpoi ON kdpo.id = kdpoi.f_id
        where 1=1
        <if test="businessType != null and businessType != ''">
          and kdpo.business_type =#{businessType}
        </if>
        <if test="assignSupplierId != null and assignSupplierId != ''">
          and kdpo.assign_supplier_id =#{assignSupplierId}
        </if>
        <if test="startTime != null and startTime != ''">
          and kdpo.f_date BETWEEN #{startTime} and #{endTime}
        </if>
        order by kdpo.f_date desc
    </select>

    <resultMap id="BaseResultMap" type="com.zfire.mall.manager.bean.k3.KingDeePurchaseOrderData">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="bill_no" jdbcType="VARCHAR" property="billNo"/>
        <result column="business_type" jdbcType="VARCHAR" property="businessType"/>
        <result column="f_date" jdbcType="TIMESTAMP" property="forDate"/>
        <result column="bill_type" jdbcType="VARCHAR" property="billType"/>
        <result column="assign_supplier_id" jdbcType="VARCHAR" property="assignSupplierId"/>
        <result column="purchase_dept_id" jdbcType="VARCHAR" property="purchaseDeptId"/>
        <result column="purchaser_id" jdbcType="VARCHAR" property="purchaserId"/>
        <result column="pay_condition_id" jdbcType="VARCHAR" property="payConditionId"/>
        <result column="abcd_remarks" jdbcType="VARCHAR" property="abcdRemarks"/>
        <result column="approver_name" jdbcType="VARCHAR" property="approverName"/>
        <result column="creator_name" jdbcType="VARCHAR" property="creatorName"/>
        <result column="changer_name" jdbcType="VARCHAR" property="changerName"/>
        <result column="bill_all_amount" jdbcType="DECIMAL" property="billAllAmount"/>
        <result column="bill_amount" jdbcType="DECIMAL" property="billAmount"/>
        <result column="bill_tax_amount" jdbcType="DECIMAL" property="billTaxAmount"/>
        <result column="settle_curr" jdbcType="VARCHAR" property="settleCurr"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="sync_time" jdbcType="TIMESTAMP" property="syncTime"/>
        <collection property="kingDeePurchaseOrderItems" ofType="com.zfire.mall.manager.plus.entity.KingDeePurchaseOrderItem" select="purchaseItemDetail" column="id">
            <id column="id" jdbcType="VARCHAR" property="id"/>
            <result column="f_id" jdbcType="VARCHAR" property="fId"/>
            <result column="material_id" jdbcType="VARCHAR" property="materialId"/>
            <result column="material_name" jdbcType="VARCHAR" property="materialName"/>
            <result column="material_number" jdbcType="VARCHAR" property="materialNumber"/>
            <result column="unit" jdbcType="VARCHAR" property="unit"/>
            <result column="qty" jdbcType="DECIMAL" property="qty"/>
            <result column="price" jdbcType="DECIMAL" property="price"/>
            <result column="entry_amount" jdbcType="DECIMAL" property="entryAmount"/>
            <result column="entry_note" jdbcType="VARCHAR" property="entryNote"/>
            <result column="all_amount" jdbcType="DECIMAL" property="allAmount"/>
            <result column="invoice_qty" jdbcType="DECIMAL" property="invoiceQty"/>
            <result column="invoice_amount" jdbcType="DECIMAL" property="invoiceAmount"/>
            <result column="model" jdbcType="VARCHAR" property="model"/>
            <result column="entry_tax_amount" jdbcType="DECIMAL" property="entryTaxAmount"/>
            <result column="tax_price" jdbcType="DECIMAL" property="taxPrice"/>
            <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
            <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
            <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
            <result column="sync_time" jdbcType="TIMESTAMP" property="syncTime"/>
        </collection>
    </resultMap>

    <select id="detailPurchase" resultMap="BaseResultMap">
        SELECT *
        FROM king_dee_purchase_order kdpo
        where kdpo.id=#{id}
    </select>
    <select id="purchaseItemDetail" resultType="com.zfire.mall.manager.plus.entity.KingDeePurchaseOrderItem">
        SELECT *
        FROM king_dee_purchase_order_item kdt
        where kdt.f_id=#{id}
    </select>

    <select id="listStockIn" resultType="com.zfire.mall.manager.bean.k3.KingDeePurchaseStockInBean">
        SELECT
        kdpsi.*,
        kdpsii.*,
               kdm.old_number as  material_old_number
        FROM
            king_dee_purchase_stock_in kdpsi
                 JOIN king_dee_purchase_stock_in_item kdpsii ON kdpsi.id = kdpsii.f_id
         join  king_dee_material kdm on kdpsii.material_id = kdm.id
        where 1=1
        <if test="billNo != null and billNo != ''">
            and kdpsi.bill_no =#{billNo}
        </if>
        <if test="materialName != null and materialName != ''">
            and kdpsii.material_name =#{materialName}
        </if>
        <if test="materialNumber != null and materialNumber != ''">
            and kdpsii.material_number =#{materialNumber}
        </if>
        <if test="materialCode != null and materialCode != ''">
            and kdm.old_number =#{materialCode}
        </if>
        <if test="startTime != null and startTime != ''">
            and kdpsi.f_date BETWEEN #{startTime} and #{endTime}
        </if>
          <if test="materialOldNumber != null and materialOldNumber != ''">
              and kdm.old_number =#{materialOldNumber}
          </if>
        order by kdpsi.f_date desc
    </select>

    <resultMap id="StockInDataResultMap" type="com.zfire.mall.manager.bean.k3.KingDeePurchaseStockInData">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="supply_org_id" jdbcType="VARCHAR" property="supplyOrgId"/>
        <result column="f_id" jdbcType="VARCHAR" property="fId"/>
        <result column="bill_no" jdbcType="VARCHAR" property="billNo"/>
        <result column="f_date" jdbcType="TIMESTAMP" property="forDate"/>
        <result column="stock_org_id" jdbcType="VARCHAR" property="stockOrgId"/>
        <result column="demand_org_id" jdbcType="VARCHAR" property="demandOrgId"/>
        <result column="purchase_org_id" jdbcType="VARCHAR" property="purchaseOrgId"/>
        <result column="supplier_id" jdbcType="VARCHAR" property="supplierId"/>
        <result column="stock_dept_id" jdbcType="VARCHAR" property="stockDeptId"/>
        <result column="cancel_status" jdbcType="VARCHAR" property="cancelStatus"/>
        <result column="approver_id" jdbcType="VARCHAR" property="approverId"/>
        <result column="approve_date" jdbcType="TIMESTAMP" property="approveDate"/>
        <result column="canceller_id" jdbcType="VARCHAR" property="cancellerId"/>
        <result column="cancel_date" jdbcType="TIMESTAMP" property="cancelDate"/>
        <result column="supply_name" jdbcType="VARCHAR" property="supplyName"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="sync_time" jdbcType="TIMESTAMP" property="syncTime"/>
        <collection property="kingDeePurchaseStockInItems" ofType="com.zfire.mall.manager.bean.k3.KingDeePurchaseStockInBean" select="StockInDataItemDetail" column="id">
            <id column="id" jdbcType="VARCHAR" property="id"/>
            <result column="supply_org_id" jdbcType="VARCHAR" property="supplyOrgId"/>
            <result column="f_id" jdbcType="VARCHAR" property="fId"/>
            <result column="bill_no" jdbcType="VARCHAR" property="billNo"/>
            <result column="entry_id" jdbcType="VARCHAR" property="entryId"/>
            <result column="material_id" jdbcType="VARCHAR" property="materialId"/>
            <result column="material_number" jdbcType="VARCHAR" property="materialNumber"/>
            <result column="material_name" jdbcType="VARCHAR" property="materialName"/>
            <result column="stock_id" jdbcType="VARCHAR" property="stockId"/>
            <result column="note" jdbcType="VARCHAR" property="note"/>
            <result column="must_qty" jdbcType="DECIMAL" property="mustQty"/>
            <result column="real_qty" jdbcType="DECIMAL" property="realQty"/>
            <result column="unit" jdbcType="VARCHAR" property="unit"/>
            <result column="aux_unit_qty" jdbcType="DECIMAL" property="auxUnitQty"/>
            <result column="price" jdbcType="DECIMAL" property="price"/>
            <result column="entry_tax_rate" jdbcType="DECIMAL" property="entryTaxRate"/>
            <result column="amount" jdbcType="DECIMAL" property="amount"/>
            <result column="tax_price" jdbcType="DECIMAL" property="taxPrice"/>
            <result column="entry_tax_amount" jdbcType="DECIMAL" property="entryTaxAmount"/>
            <result column="all_amount" jdbcType="DECIMAL" property="allAmount"/>
            <result column="uom" jdbcType="VARCHAR" property="uom"/>
            <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
            <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
            <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
            <result column="sync_time" jdbcType="TIMESTAMP" property="syncTime"/>
            <result column="stock_name" jdbcType="VARCHAR" property="stockName"/>
        </collection>
    </resultMap>



    <select id="detailStockIn" resultMap="StockInDataResultMap">
        SELECT
            *
        FROM
            king_dee_purchase_stock_in kdpsi
        where kdpsi.id=#{id}
    </select>

    <select id="StockInDataItemDetail" resultType="com.zfire.mall.manager.bean.k3.KingDeePurchaseStockInBean">
        SELECT kdt.*,kdm.old_number as  material_old_number
        FROM king_dee_purchase_stock_in_item kdt
                 left join  king_dee_material kdm on kdt.material_id = kdm.id
        where kdt.f_id=#{id}
    </select>
    <select id="v2ListPage" resultType="com.zfire.mall.manager.bean.k3.v2.KingDeePurchaseOrderVO">
        SELECT
            a.*,
            b.*
        FROM
            king_dee_purchase_order a
            LEFT JOIN king_dee_purchase_order_item b ON a.id = b.f_id
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.for_date DESC
        </if>
        ${ex.orderBy}
    </select>
    <select id="v2StockInListPage" resultType="com.zfire.mall.manager.bean.k3.v2.KingDeePurchaseStockInVO">
        SELECT
            a.*,
            b.*,
            c.old_number as material_old_number
        FROM
            king_dee_purchase_stock_in a
            JOIN king_dee_purchase_stock_in_item b ON a.id = b.bill_no
            JOIN king_dee_material c ON b.material_id = c.id
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.for_date DESC
        </if>
        ${ex.orderBy}
    </select>
    <select id="v2TransferListPage" resultType="com.zfire.mall.manager.bean.k3.v2.KingTransferVO">
        SELECT
            a.*,
            b.*
        FROM
            king_dee_transfer a
         JOIN king_dee_transfer_item b ON a.id = b.bill_no
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>
</mapper>