<?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.dk.mdm.mapper.pur.PurchaseMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        pur_id, pur_no, pur_type, org_id, staff_id, sup_id, contact_name, contact_phone,
        pickup_date, sys.f_remove_zero(sum_quantity) as sum_quantity, sum_standard, sum_amount, pur_discount, pur_status,
        into_status, sys.f_remove_zero(intoing_qty) as intoing_qty, intoing_amt, sys.f_remove_zero(into_qty) as into_qty, into_amt,
        sys.f_remove_zero(return_qty) as return_qty, return_amt,
        amt_payable, amt_handle, amt_residue, remarks, annex_paths, make_staff, make_time, flg_valid, cp_id,
        op_create_time, op_create_user_id, op_update_time, op_update_user_id, op_app_code, op_timestamp, op_db_user,
        flg_auto_handle,inbound_processing_flag
    </sql>

    <!-- 通用查询列 -->
    <sql id="Base_Column_List_Left_join">
        tpp.pur_id,
        tpp.pur_no,
        tpp.pur_type,
        tpp.org_id,
        tpp.staff_id,
        tpp.sup_id,
        tpp.contact_name,
        tpp.contact_phone,
        tpp.pickup_date,
        sys.f_remove_zero(tpp.sum_quantity) as sum_quantity,
        tpp.sum_standard,
        tpp.sum_amount,
        tpp.pur_discount,
        tpp.pur_status,
        tpp.into_status,
        sys.f_remove_zero(tpp.intoing_qty) as intoing_qty,
        tpp.intoing_amt,
        sys.f_remove_zero(tpp.into_qty) as into_qty,
        tpp.into_amt,
        sys.f_remove_zero(tpp.return_qty) as return_qty,
        tpp.return_amt,
        tpp.amt_payable,
        tpp.amt_handle,
        tpp.amt_residue,
        tpp.remarks,
        tpp.annex_paths,
        tpp.make_staff,
        tpp.make_time,
        tpp.flg_valid,
        tpp.cp_id,
        tpp.op_create_time,
        tpp.op_create_user_id,
        tpp.op_update_time,
        tpp.op_update_user_id,
        tpp.op_app_code,
        tpp.op_timestamp,
        tpp.op_db_user,
        tpp.wh_id,
        tpp.from_ids,
        tpp.from_nos,
        tpp.flg_auto_handle,
        tpp.inbound_processing_flag
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.pur.Purchase">
        <id column="pur_id" property="purId"/>
                <result column="pur_no" property="purNo"/>
                <result column="pur_type" property="purType"/>
                <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
                <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
                <result column="sup_id" property="supId" typeHandler="UuidTypeHandler"/>
                <result column="contact_name" property="contactName"/>
                <result column="contact_phone" property="contactPhone"/>
            <result column="pickup_date" property="pickupDate" typeHandler="TimestampTypeHandler"/>
                <result column="sum_quantity" property="sumQuantity"/>
                <result column="sum_standard" property="sumStandard"/>
                <result column="sum_amount" property="sumAmount"/>
                <result column="pur_discount" property="purDiscount"/>
                <result column="pur_status" property="purStatus"/>
                <result column="into_status" property="intoStatus"/>
                <result column="intoing_qty" property="intoingQty"/>
                <result column="intoing_amt" property="intoingAmt"/>
                <result column="into_qty" property="intoQty"/>
                <result column="into_amt" property="intoAmt"/>
                <result column="return_qty" property="returnQty"/>
                <result column="return_amt" property="returnAmt"/>
                <result column="amt_payable" property="amtPayable"/>
                <result column="amt_handle" property="amtHandle"/>
                <result column="amt_residue" property="amtResidue"/>
                <result column="remarks" property="remarks"/>
                <result column="annex_paths" property="annexPaths" typeHandler="JsonTypeHandler"/>
                <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
            <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
                <result column="flg_valid" property="flgValid"/>
                <result column="cp_id" property="cpId"/>
            <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
                <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
            <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
                <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
                <result column="op_app_code" property="opAppCode"/>
            <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
                <result column="op_db_user" property="opDbUser"/>
                <result column="wh_id" property="whId"/>
                <result column="flg_auto_handle" property="flgAutoHandle"/>
                <result column="inbound_processing_flag" property="inboundProcessingFlag"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="purchaseItemListMap" type="java.util.Map">
        <id column="item_id" property="itemId"/>
        <result column="pur_id" property="purId" typeHandler="UuidTypeHandler"/>
        <result column="pur_no" property="purNo"/>
        <result column="item_index" property="itemIndex"/>
        <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
        <result column="item_qty" property="itemQty"/>
        <result column="price_std" property="priceStd"/>
        <result column="amt_std" property="amtStd"/>
        <result column="price_pur" property="pricePur"/>
        <result column="item_amt" property="itemAmt"/>
        <result column="price_discount" property="priceDiscount"/>
        <result column="non_std_code" property="nonStdCode"/>
        <result column="into_status" property="intoStatus"/>
        <result column="intoing_qty" property="intoingQty"/>
        <result column="intoing_amt" property="intoingAmt"/>
        <result column="into_qty" property="intoQty"/>
        <result column="into_amt" property="intoAmt"/>
        <result column="return_qty" property="returnQty"/>
        <result column="return_amt" property="returnAmt"/>
        <result column="remarks" property="remarks"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="sku_code" property="skuCode"/>
        <result column="sku_name" property="skuName"/>
        <result column="price_standard" property="priceStandard"/>
        <result column="sku_model" property="skuModel"/>
        <result column="short_name" property="shortName"/>
        <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_app_code" property="opAppCode"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
        <result column="brand_name" property="brandName"/>
        <result column="sku_images" property="skuImages"/>


    </resultMap>
    <!--    关联映射-->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.pur.PurchaseResponse">
        <id column="pur_id" property="purId"/>
        <result column="pur_no" property="purNo"/>
        <result column="pur_type" property="purType"/>
        <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="sup_id" property="supId" typeHandler="UuidTypeHandler"/>
        <result column="contact_name" property="contactName"/>
        <result column="contact_phone" property="contactPhone"/>
        <result column="pickup_date" property="pickupDate" typeHandler="TimestampTypeHandler"/>
        <result column="sum_quantity" property="sumQuantity"/>
        <result column="sum_standard" property="sumStandard"/>
        <result column="sum_amount" property="sumAmount"/>
        <result column="pur_discount" property="purDiscount"/>
        <result column="pur_status" property="purStatus"/>
        <result column="into_status" property="intoStatus"/>
        <result column="intoing_qty" property="intoingQty"/>
        <result column="intoing_amt" property="intoingAmt"/>
        <result column="into_qty" property="intoQty"/>
        <result column="into_amt" property="intoAmt"/>
        <result column="return_qty" property="returnQty"/>
        <result column="return_amt" property="returnAmt"/>
        <result column="amt_payable" property="amtPayable"/>
        <result column="amt_handle" property="amtHandle"/>
        <result column="amt_residue" property="amtResidue"/>
        <result column="remarks" property="remarks"/>
        <result column="annex_paths" property="annexPaths" typeHandler="JsonTypeHandler"/>
        <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
        <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_app_code" property="opAppCode"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
        <result column="wh_id" property="whId"/>
        <result column="orgName" property="orgName"/>
        <result column="staffName" property="staffName"/>
        <result column="makeStaffName" property="makeStaffName"/>
        <result column="supplierName" property="supplierName"/>
        <result column="whName" property="whName"/>
        <result column="displayedStatus" property="displayedStatus"/>
        <result column="intoStatusName" property="intoStatusName"/>
        <result column="purStatusName" property="purStatusName"/>
        <result column="returnAddress" property="returnAddress"/>
        <result column="from_ids" property="fromIds" typeHandler="StringListTypeHandler"/>
        <result column="from_nos" property="fromNos" typeHandler="StringListTypeHandler"/>
        <result column="inboundReturnQty" property="inboundReturnQty"/>
        <result column="flg_auto_handle" property="flgAutoHandle"/>
        <result column="inbound_processing_flag" property="inboundProcessingFlag"/>
        <result column="category_count" property="categoryCount"/>
        <result column="sup_code" property="supCode"/>
        <result column="sup_type" property="supType"/>
        <result column="contact_phone" property="contactPhone" />
        <result column="contact_name" property="contactName" />
        <result column="service_categories" property="serviceCategories" typeHandler="StringListTypeHandler"/>

        <collection property="purchaseItemResponseList" resultMap="purchaseItemListMap" columnPrefix="list_"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="purNo != null and purNo != ''">
                AND pur_no = #{purNo}
            </if>
            <if test="purType != null and purType != ''">
                AND pur_type = #{purType}
            </if>
            <if test="orgId != null and orgId != ''">
                AND org_id = #{orgId}
            </if>
            <if test="staffId != null and staffId != ''">
                AND staff_id = #{staffId}
            </if>
            <if test="supId != null and supId != ''">
                AND sup_id = #{supId}
            </if>
            <if test="contactName != null and contactName != ''">
                AND contact_name = #{contactName}
            </if>
            <if test="contactPhone != null and contactPhone != ''">
                AND contact_phone = #{contactPhone}
            </if>
            <if test="pickupDate != null">
                AND pickup_date = #{pickupDate}
            </if>
            <if test="sumQuantity != null">
                AND sum_quantity = #{sumQuantity}
            </if>
            <if test="sumStandard != null">
                AND sum_standard = #{sumStandard}
            </if>
            <if test="sumAmount != null">
                AND sum_amount = #{sumAmount}
            </if>
            <if test="purDiscount != null">
                AND pur_discount = #{purDiscount}
            </if>
            <if test="purStatus != null and purStatus != ''">
                AND pur_status = #{purStatus}
            </if>
            <if test="intoStatus != null and intoStatus != ''">
                AND into_status = #{intoStatus}
            </if>
            <if test="intoingQty != null">
                AND intoing_qty = #{intoingQty}
            </if>
            <if test="intoingAmt != null">
                AND intoing_amt = #{intoingAmt}
            </if>
            <if test="intoQty != null">
                AND into_qty = #{intoQty}
            </if>
            <if test="intoAmt != null">
                AND into_amt = #{intoAmt}
            </if>
            <if test="returnQty != null">
                AND return_qty = #{returnQty}
            </if>
            <if test="returnAmt != null">
                AND return_amt = #{returnAmt}
            </if>
            <if test="amtPayable != null">
                AND amt_payable = #{amtPayable}
            </if>
            <if test="amtHandle != null">
                AND amt_handle = #{amtHandle}
            </if>
            <if test="amtResidue != null">
                AND amt_residue = #{amtResidue}
            </if>
            <if test="remarks != null and remarks != ''">
                AND remarks = #{remarks}
            </if>
            <if test="annexPaths != null and annexPaths != ''">
                AND annex_paths = #{annexPaths}
            </if>
            <if test="makeStaff != null and makeStaff != ''">
                AND make_staff = #{makeStaff}
            </if>
            <if test="makeTime != null">
                AND make_time = #{makeTime}
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
            <if test="opCreateTime != null">
                AND op_create_time = #{opCreateTime}
            </if>
            <if test="opCreateUserId != null and opCreateUserId != ''">
                AND op_create_user_id = #{opCreateUserId}
            </if>
            <if test="opUpdateTime != null">
                AND op_update_time = #{opUpdateTime}
            </if>
            <if test="opUpdateUserId != null and opUpdateUserId != ''">
                AND op_update_user_id = #{opUpdateUserId}
            </if>
            <if test="opAppCode != null and opAppCode != ''">
                AND op_app_code = #{opAppCode}
            </if>
            <if test="opTimestamp != null">
                AND op_timestamp = #{opTimestamp}
            </if>
            <if test="opDbUser != null and opDbUser != ''">
                AND op_db_user = #{opDbUser}
            </if>
        </where>
    </sql>

    <!-- 通用条件列 -->
    <sql id="Condition_1">
        <where>
            <if test="orgName != null and orgName != ''">
                AND tmo.org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
            </if>
            <if test="staffName != null and staffName != ''">
                AND tms.staff_name LIKE concat('%',my_ex.likequery(#{staffName}),'%')
            </if>
            <if test="purId != null and purId != ''">
                AND tpp.pur_id = #{purId}::uuid
            </if>
            <if test="purNo != null and purNo != ''">
                AND tpp.pur_no like concat('%', my_ex.likequery(#{purNo}) , '%')
            </if>
            <if test="purType != null and purType != ''">
                AND tpp.pur_type = #{purType}
            </if>
            <if test="orgId != null and orgId != ''">
                AND tpp.org_id = #{orgId}
            </if>
            <if test="staffId != null and staffId != ''">
                AND tpp.staff_id = #{staffId}
            </if>
            <if test="supId != null and supId != ''">
                AND tpp.sup_id = #{supId}::uuid
            </if>
            <if test="contactName != null and contactName != ''">
                AND tpp.contact_name like concat('%', my_ex.likequery(#{contactName}) , '%')
            </if>
            <if test="contactPhone != null and contactPhone != ''">
                AND tpp.contact_phone like concat('%', my_ex.likequery(#{contactPhone}) , '%')
            </if>
            <if test="pickupDate != null">
                AND tpp.pickup_date = #{pickupDate}
            </if>
            <if test="sumQuantity != null">
                AND tpp.sum_quantity = #{sumQuantity}
            </if>
            <if test="sumStandard != null">
                AND tpp.sum_standard = #{sumStandard}
            </if>
            <if test="sumAmount != null">
                AND tpp.sum_amount = #{sumAmount}
            </if>
            <if test="purDiscount != null">
                AND tpp.pur_discount = #{purDiscount}
            </if>
            <if test="purStatus != null and purStatus != ''">
                AND tpp.pur_status = #{purStatus}
            </if>
            <if test="intoStatus != null and intoStatus != ''">
                AND tpp.into_status = #{intoStatus}
            </if>
            <if test="intoingQty != null">
                AND tpp.intoing_qty = #{intoingQty}
            </if>
            <if test="intoingAmt != null">
                AND tpp.intoing_amt = #{intoingAmt}
            </if>
            <if test="intoQty != null">
                AND tpp.into_qty = #{intoQty}
            </if>
            <if test="intoAmt != null">
                AND tpp.into_amt = #{intoAmt}
            </if>
            <if test="returnQty != null">
                AND tpp.return_qty = #{returnQty}
            </if>
            <if test="returnAmt != null">
                AND tpp.return_amt = #{returnAmt}
            </if>
            <if test="amtPayable != null">
                AND tpp.amt_payable = #{amtPayable}
            </if>
            <if test="amtHandle != null">
                AND tpp.amt_handle = #{amtHandle}
            </if>
            <if test="amtResidue != null">
                AND tpp.amt_residue = #{amtResidue}
            </if>
            <if test="remarks != null and remarks != ''">
                AND tpp.remarks = #{remarks}
            </if>
            <if test="annexPaths != null and annexPaths != ''">
                AND tpp.annex_paths = #{annexPaths}
            </if>
            <if test="makeStaff != null and makeStaff != ''">
                AND tpp.make_staff = #{makeStaff}
            </if>
            <if test="makeTime != null">
                AND tpp.make_time = #{makeTime}
            </if>
            <if test="makeTimeStart != null and makeTimeEnd != null">
                AND tpp.make_time &gt;= #{makeTimeStart}::timestamp with time zone
                AND tpp.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="opCreateTime != null">
                AND tpp.op_create_time = #{opCreateTime}
            </if>
            <if test="opCreateUserId != null and opCreateUserId != ''">
                AND tpp.op_create_user_id = #{opCreateUserId}
            </if>
            <if test="opUpdateTime != null">
                AND tpp.op_update_time = #{opUpdateTime}
            </if>
            <if test="opUpdateUserId != null and opUpdateUserId != ''">
                AND tpp.op_update_user_id = #{opUpdateUserId}
            </if>
            <if test="opAppCode != null and opAppCode != ''">
                AND tpp.op_app_code = #{opAppCode}
            </if>
            <if test="opTimestamp != null">
                AND tpp.op_timestamp = #{opTimestamp}
            </if>
            <if test="opDbUser != null and opDbUser != ''">
                AND tpp.op_db_user = #{opDbUser}
            </if>
            <if test="intoStatus != null and intoStatus != ''">
                AND tpp.into_status = #{intoStatus}
            </if>
            <if test="intoStatusList != null  and intoStatusList.size()>0">
                AND tpp.into_status =
                any(#{intoStatusList,typeHandler=StringListTypeHandler})
            </if>
            <if test="(skuName != null and skuName!='') or (skuModel != null and skuModel!='')">
                and exists (select 1 from dkic_b.t_psi_purchase_item tppi
                left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tppi.sku_id
                where
                tpp.pur_id = tppi.pur_id
                and tppi.flg_valid
                <if test="skuName != null and skuName!=''">
                    AND position(#{skuName} in tmgs.sku_name) > 0
                </if>
                <if test="skuModel != null and skuModel!=''">
                    AND position(#{skuModel} in tmgs.sku_model) > 0
                </if>
                )
            </if>
            <if test="pickupDateStart != null">
                AND tpp.pickup_date &gt;= #{pickupDateStart}::timestamp with time zone
            </if>
            <if test="pickupDateEnd != null">
                AND tpp.pickup_date &lt; #{pickupDateEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="searchText !=null and searchText!='' " >
                AND  ( tmp.sup_name LIKE concat('%',my_ex.likequery(#{searchText}), '%')or
                tpp.pur_no LIKE concat('%',my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="flgValid != null">
                AND tpp.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tpp.cp_id = #{cpId}
            </if>
            <if test="orgIds != null and orgIds.size()>0">
                AND tpp.org_id = any(#{orgIds, typeHandler=UuidListTypeHandler})
            </if>
            <if test="staffIds != null and staffIds.size()>0">
                AND tpp.staff_id = any(#{staffIds, typeHandler=uuidListTypeHandler})
            </if>
        </where>
    </sql>

    <sql id="idsForeach">
        <!-- 根据主键purId批量操作 -->
        WHERE pur_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表dkic_b.t_psi_purchase，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Left_join"/>
        ,tmo.org_name as "orgName"
        ,tms.staff_name as "staffName"
        ,tms1.staff_name as "makeStaffName"
        ,tmp.sup_name as "supplierName"
        ,tmp.sup_code
        ,tmp.sup_type
        ,tmp.contact_name as "contactName"
        ,tmp.contact_phone as "contactPhone"
        ,tmp.service_categories as "serviceCategories"
        ,tmp.return_address as "returnAddress"
        ,tmw.wh_name as "whName"
        ,sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "intoStatusName"
        ,sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}) as "purStatusName"
        ,CASE
        WHEN tpp.pur_status = '订单状态-暂存' THEN   sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n})
        WHEN tpp.pur_status != '订单状态-暂存' THEN  sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n})
        END AS "displayedStatus"
        FROM dkic_b.t_psi_purchase tpp
        Left join dkic_b.t_mst_org tmo on tpp.org_id = tmo.org_id
        Left join dkic_b.t_mst_supplier tmp on tpp.sup_id = tmp.sup_id
        Left join dkic_b.t_mst_staff tms on tpp.staff_id = tms.staff_id
        Left join dkic_b.t_mst_staff tms1 on tpp.make_staff = tms1.staff_id
        Left join dkic_b.t_mst_warehouse tmw on tpp.wh_id = tmw.wh_id
        left join sys.t_data_kind tdk1 on tdk1.kind_code = tpp.into_status
        left join sys.t_data_kind tdk2 on tdk2.kind_code = tpp.pur_status
        <include refid="Condition_1"/>
            order by  tpp.make_time desc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>
    <sql id="Base_Column_List_Item_Response_Join">
        ,tppi.item_id  as "List_item_id",
        tppi.pur_id  as "List_pur_id",
        tpp.pur_no as "List_pur_no",
        tppi.item_index as "List_item_index",
        tppi.sku_id as "List_sku_id",
        sys.f_remove_zero(tppi.item_qty) as  "List_item_qty",
        tppi.price_std as "pricePurchase",
        tppi.amt_std as "List_amt_std",
        tppi.price_pur as "List_price_pur",
        tppi.item_amt as "List_item_amt",
        tppi.price_discount as "List_price_discount",
        tppi.non_std_code as "List_non_std_code",
        tppi.into_status as "List_into_status",
        sys.f_remove_zero(tppi.intoing_qty) as "List_intoing_qty",
        tppi.intoing_amt as "List_intoing_amt",
        sys.f_remove_zero(tppi.into_qty) as "List_into_qty",
        tppi.into_amt as "List_into_amt",
        sys.f_remove_zero(tppi.return_qty) as "List_return_qty",
        tppi.return_amt as "List_return_amt",
        tppi.remarks as "List_remarks",
        tppi.flg_valid as "List_flg_valid",
        tppi.cp_id as "List_cp_id",
        tppi.op_create_time as "List_op_create_time",
        tppi.op_create_user_id as "List_op_create_user_id",
        tppi.op_update_time as "List_op_update_time",
        tppi.op_update_user_id as "List_op_update_user_id",
        tppi.op_app_code as "List_op_app_code",
        tppi.op_timestamp as "List_op_timestamp",
        tppi.op_db_user as "List_op_db_user",
        tmgs.sku_code AS  list_sku_code,
        tmgs.sku_name AS  list_sku_name,
        tmgb.short_name AS  list_short_name,
        tmgs.sku_model AS  list_sku_model,
        tmgs.price_purchase AS  list_price_purchase,
        tmgs.price_standard AS  list_price_standard,
        tmgs.sku_images AS  list_sku_images,
        sys.f_remove_zero(tpit.usable_qty) AS list_usable_qty,
        sys.f_remove_zero(tpit.inv_qty) AS list_inv_qty,
        tpit.outing_qty AS list_outing_qty,
        sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "intoStatusName",
        sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}) as "purStatusName"
    </sql>

    <!-- 查询表dkic_b.t_psi_purchase，（条件查询+分页）列表 -->
    <select id="selectByCondItem" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Left_join"/>
        <include refid="Base_Column_List_Item_Response_Join"/>
        ,tmo.org_name as "orgName"
        ,tms.staff_name as "staffName"
        ,tms1.staff_name as "makeStaffName"
        ,tmp.sup_name as "supplierName"
        ,tmw.wh_name as "whName"
        ,tmw.wh_name as "whName"
        ,tmw.wh_name as "whName"
        FROM dkic_b.t_psi_purchase tpp
        left join dkic_b.t_psi_purchase_item tppi  on tppi.pur_id = tpp.pur_id
        left join  dkic_b.t_mst_goods_sku  tmgs on tmgs.sku_id = tppi.sku_id
        left join  dkic_b.t_mst_goods_brand  tmgb on tmgb.brand_id = tmgs.brand_id
        left join  dkic_b.t_psi_inventory  tpit on tpit.sku_id = tppi.sku_id
        Left join dkic_b.t_mst_org tmo on tpp.org_id = tmo.org_id
        Left join dkic_b.t_mst_supplier tmp on tpp.sup_id = tmp.sup_id
        Left join dkic_b.t_mst_staff tms on tpp.staff_id = tms.staff_id
        Left join dkic_b.t_mst_staff tms1 on tpp.make_staff = tms1.staff_id
        Left join dkic_b.t_mst_warehouse tmw on tpp.wh_id = tmw.wh_id


        <include refid="Condition_1"/>
        ORDER BY  tpp.make_time
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>


    <!-- 根据主键查询表dkic_b.t_psi_purchase的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Left_join"/>
        ,tmo.org_name as "orgName"
        ,tms.staff_name as "staffName"
        ,tms1.staff_name as "makeStaffName"
        ,tmp.sup_name as "supplierName"
        ,tmw.wh_name as "whName"
        ,(SELECT sum(tpid.return_qty) FROM dkic_b.t_psi_inbound tpid where tpid.from_id = tpp.pur_id ) as "inboundReturnQty"
        FROM dkic_b.t_psi_purchase tpp
        Left join dkic_b.t_mst_org tmo on tpp.org_id = tmo.org_id
        Left join dkic_b.t_mst_supplier tmp on tpp.sup_id = tmp.sup_id
        Left join dkic_b.t_mst_staff tms on tpp.staff_id = tms.staff_id
        Left join dkic_b.t_mst_staff tms1 on tpp.make_staff = tms1.staff_id
        Left join dkic_b.t_mst_warehouse tmw on tpp.wh_id = tmw.wh_id
        WHERE pur_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表dkic_b.t_psi_purchase的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_purchase
        WHERE pur_id = #{id}::uuid
        for update
    </select>

    <!-- 根据主键锁定表dkic_b.t_psi_purchase的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_purchase
        <include refid="idsForeach"/>
        for update
    </select>

    <!-- 查询表dkic_b.t_psi_purchase，（条件查询）个数 -->
        <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_purchase tpp
        Left join dkic_b.t_mst_org tmo on tpp.org_id = tmo.org_id
        Left join dkic_b.t_mst_supplier tmp on tpp.sup_id = tmp.sup_id
        Left join dkic_b.t_mst_staff tms on tpp.staff_id = tms.staff_id
        Left join dkic_b.t_mst_staff tms1 on tpp.make_staff = tms1.staff_id
        Left join dkic_b.t_mst_warehouse tmw on tpp.wh_id = tmw.wh_id
        left join sys.t_data_kind tdk1 on tdk1.kind_code = tpp.into_status
        left join sys.t_data_kind tdk2 on tdk2.kind_code = tpp.pur_status
        <include refid="Condition_1"/>
    </select>


    <!-- 查询表dkic_b.t_psi_purchase，（条件查询+分页）列表 -->
    <select id="noStoredSelectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Left_join"/>
        ,tmo.org_name as "orgName"
        ,tms.staff_name as "staffName"
        ,tms1.staff_name as "makeStaffName"
        ,tmp.sup_name as "supplierName"
        ,tmw.wh_name as "whName",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "intoStatusName"
        FROM dkic_b.t_psi_purchase tpp
        Left join dkic_b.t_mst_org tmo on tpp.org_id = tmo.org_id
        Left join dkic_b.t_mst_supplier tmp on tpp.sup_id = tmp.sup_id
        Left join dkic_b.t_mst_staff tms on tpp.staff_id = tms.staff_id
        Left join dkic_b.t_mst_staff tms1 on tpp.make_staff = tms1.staff_id
        Left join dkic_b.t_mst_warehouse tmw on tpp.wh_id = tmw.wh_id
        LEFT JOIN sys.t_data_kind tdk   ON tdk.kind_code = tpp.into_status
        <include refid="Condition_1"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表dkic_b.t_psi_purchase，（条件查询）个数 -->
    <select id="noStoredCountByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_purchase tpp
        <include refid="Condition_1"/>
    </select>

    <update id="updateAmount" parameterType="com.dk.mdm.model.pojo.pur.Purchase">
        update dkic_b.t_psi_purchase
        <set>
            <if test="returnQty!= null">
                return_qty= #{returnQty},
            </if>
            <if test="returnAmt!= null">
                return_amt = #{returnAmt},
            </if>
        </set>
        where  pur_id = #{purId}::uuid
    </update>

    <!-- 查询原始数据 冲正用-->
    <select id="selectPurchaseByRighting" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List_Left_join"/>
        FROM dkic_b.t_psi_purchase tpp
        <where>
            <if test="orderId!=null">
                tpp.pur_id = #{purId}::uuid
            </if>

        </where>
    </select>

    <!-- 根据id修改-->
    <update id="updateById" parameterType="com.dk.mdm.model.pojo.pur.Purchase">
        update dkic_b.t_psi_purchase
        <set>
            <if test=" et!=null and et.purNo != null">
                pur_no = #{et.purNo,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.purType != null">
                pur_type = #{et.purType,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.orgId != null">
                org_id = #{et.orgId,typeHandler=UuidTypeHandler},
            </if>
            <if test=" et!=null and et.staffId != null">
                staff_id = #{et.staffId,typeHandler=UuidTypeHandler},
            </if>
            <if test=" et!=null and et.supId != null">
                sup_id = #{et.supId,typeHandler=UuidTypeHandler},
            </if>
            <if test=" et!=null and et.contactName != null">
                contact_name = #{et.contactName,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.contactPhone != null">
                contact_phone = #{et.contactPhone,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.pickupDate != null">
                pickup_date = #{et.pickupDate,jdbcType=DATE},
            </if>
            <if test=" et!=null and et.sumQuantity != null">
                sum_quantity = #{et.sumQuantity,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.sumStandard != null">
                sum_standard = #{et.sumStandard,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.sumAmount != null">
                sum_amount = #{et.sumAmount,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.purDiscount != null">
                pur_discount = #{et.purDiscount,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.purStatus != null">
                pur_status = #{et.purStatus,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.intoStatus != null">
                into_status = #{et.intoStatus,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.intoingQty != null">
                intoing_qty = intoing_qty +  #{et.intoingQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.intoingAmt != null">
                intoing_amt = intoing_amt + #{et.intoingAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.intoQty != null">
                into_qty = into_qty +  #{et.intoQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.intoAmt != null">
                into_amt = into_amt +  #{et.intoAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.returnQty != null">
                return_qty = #{et.returnQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.returnAmt != null">
                return_amt = #{et.returnAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.amtPayable != null">
                amt_payable = #{et.amtPayable,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.amtHandle != null">
                amt_handle = #{et.amtHandle,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.amtResidue != null">
                amt_residue = #{et.amtResidue,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.remarks != null">
                remarks = #{et.remarks,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.annexPaths != null">
                annex_paths = #{et.annexPaths,typeHandler=JsonTypeHandler},
            </if>
            <if test=" et!=null and et.makeStaff != null">
                make_staff = #{et.makeStaff,jdbcType=OTHER},
            </if>
            <if test=" et!=null and et.makeTime != null">
                make_time = #{et.makeTime,jdbcType=TIMESTAMP},
            </if>
            <if test=" et!=null and et.flgValid != null">
                flg_valid = #{et.flgValid,jdbcType=BOOLEAN},
            </if>
            <if test=" et!=null and et.cpId != null">
                cp_id = #{et.cpId,jdbcType=INTEGER},
            </if>
            <if test=" et!=null and et.whId != null">
                wh_id = #{et.whId,typeHandler=UuidTypeHandler},
            </if>
            <if test=" et!=null and et.flgAutoHandle != null">
                flg_auto_handle = #{et.flgAutoHandle,jdbcType=BOOLEAN},
            </if>
            <if test=" et!=null and et.inboundProcessingFlag != null">
                inbound_processing_flag = #{et.inboundProcessingFlag,jdbcType=BOOLEAN},
            </if>
        </set>
        where pur_id = #{et.purId,typeHandler=UuidTypeHandler}
        and sum_quantity >= (
        into_qty
        <if test=" et!=null and et.updateIntoMessageFlag != null and et.updateIntoMessageFlag ">
            + #{et.intoQty,jdbcType=NUMERIC}
        </if>
        + intoing_qty
        <if test=" et!=null and et.updateIntoingMessageFlag != null and et.updateIntoingMessageFlag ">
            + #{et.intoingQty,jdbcType=NUMERIC}
        </if>
        )
    </update>

    <!-- 采购跟踪-->
    <select id="selectTrackReport" resultType="java.util.Map">
        SELECT *
        FROM dkic_b.f_query_pur_for_track(#{purId}::uuid,#{cpId},#{i18n})
    </select>
    <select id="selectPurOrderAndDeatil" resultType="java.util.Map">
        SELECT
        tpp.pur_id AS "purId"
        ,tpp.pur_no AS "purNo"
        ,tpp.pur_type AS "purType"
        ,tpp.org_id AS "orgId"
        ,tpp.staff_id AS "staffId"
        ,tpp.sup_id AS "supId"
        ,tpp.contact_name AS "contactName"
        ,tpp.contact_phone AS "contactPhone"
        ,tpp.pickup_date AS "pickupDate"
        ,sys.f_remove_zero(tpp.sum_quantity) AS "sumQuantity"
        ,tpp.sum_standard AS "sumStandard"
        ,tpp.sum_amount AS "sumAmount"
        ,tpp.pur_discount AS "purDiscount"
        ,tpp.pur_status AS "purStatus"
        ,tpp.into_status AS "intoStatus"
        ,sys.f_remove_zero(tpp.intoing_qty) AS "intoingQty"
        ,tpp.intoing_amt AS "intoingAmt"
        ,sys.f_remove_zero(tpp.into_qty) AS "intoQty"
        ,tpp.into_amt AS "intoAmt"
        ,sys.f_remove_zero(tpp.return_qty) AS "returnQty"
        ,tpp.return_amt AS "returnAmt"
        ,tpp.amt_payable AS "amtPayable"
        ,tpp.amt_handle AS "amtHandle"
        ,tpp.amt_residue AS "amtResidue"
        ,tpp.remarks AS "remarks"
        ,tpp.annex_paths AS "annexPaths"
        ,tpp.make_staff AS "makeStaff"
        ,tpp.make_time AS "makeTime"
        ,tpp.flg_valid AS "flgValid"
        ,tpp.cp_id AS "cpId"
        ,tpp.op_create_time AS "opCreateTime"
        ,tpp.op_create_user_id AS "opCreateUserId"
        ,tpp.op_update_time AS "opUpdateTime"
        ,tpp.op_update_user_id AS "opUpdateUserId"
        ,tpp.op_app_code AS "opAppCode"
        ,tpp.op_timestamp AS "opTimestamp"
        ,tpp.op_db_user AS "opDbUser"
        ,tpp.wh_id AS "whId"
        ,tpp.from_ids AS "fromIds"
        ,tpp.from_nos AS "fromNos"
        ,tmo.org_name as "orgName"
        ,tms.staff_name as "staffName"
        ,tms1.staff_name as "makeStaffName"
        ,tmp.sup_name as "supplierName"
        ,tmp.return_address as "returnAddress"
        ,tmw.wh_name as "whName"
        ,sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "intoStatusName"
        ,sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n}) as "purStatusName"
        ,CASE
        WHEN tpp.pur_status = '订单状态-暂存' THEN   sys.f_get_name_i18n(tdk2.kind_name_i18n, #{i18n})
        WHEN tpp.pur_status != '订单状态-暂存' THEN  sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n})
        END AS "displayedStatus"
       , (
        SELECT
        json_agg ( row_to_json ( item ) )
        FROM
        ( SELECT
            tppi.item_id AS "itemId"
            ,tppi.pur_id AS "purId"
            ,tppi.item_index AS "itemIndex"
            ,tppi.sku_id AS "skuId"
            ,tppi.item_qty AS "itemQty"
            ,tppi.price_std as "priceStd"
            ,tppi.amt_std AS "amtStd"
            ,tppi.price_pur AS "pricePur"
            ,tppi.item_amt AS "itemAmt"
            ,tppi.price_discount AS "priceDiscount"
            ,tppi.non_std_code AS "nonStdCode"
            ,tppi.into_status AS "intoStatus"
            ,tppi.intoing_qty AS "intoingQty"
            ,tppi.intoing_amt AS "intoingAmt"
            ,tppi.into_qty AS "intoQty"
            ,tppi.into_amt AS "intoAmt"
            ,tppi.return_qty AS "returnQty"
            ,tppi.return_amt AS "returnAmt"
            ,tppi.remarks AS "remarks"
            ,tppi.flg_valid AS "flagValid"
            ,tppi.cp_id AS "cpId"
            ,tppi.op_create_time AS "opCreateTime"
            ,tppi.op_create_user_id AS "opCreateUserId"
            ,tppi.op_update_time AS "opUpdateTime"
            ,tppi.op_update_user_id AS "opUpdateUserId"
            ,tppi.op_app_code AS "opAppCode"
            ,tppi.op_timestamp AS "opTimestamp"
            ,tppi.op_db_user AS "opDbUser"
            ,tppi.from_id AS "fromId"
            ,tppi.from_item_id AS "fromItemId"
            ,tpoitem.order_no  as "orderNo"
            ,tmgsitem.sku_code  as "skuCode"
            ,tmgsitem.sku_model as "skuModel"
            ,tmgsitem.sku_name  as "skuName"
            ,tmgsitem.sku_images  as "skuImages"
            ,tmgbitem.brand_name  as "brandName"
            ,tmgbitem.short_name  as "shortName"
            ,tmgsitem.sku_spec  as "skuSpec"
            ,tmgsitem.price_purchase  as "pricePurchase"
            ,tmgsitem.price_standard  as "priceStandard"
            ,sys.f_get_name_i18n(tdk1item.kind_name_i18n, #{i18n}) as "intoStatusName"
            ,i.inv_id AS "invId"
            ,i.inv_qty AS "invQty"
            ,i.usable_qty AS "usableQty"
        ,tppitem.pur_no as "purNo"
        ,tppi.unit_id AS "unitId"
        ,tppi.sub_unit_id AS "subUnitId"
        ,tppi.pack_box AS "packBox"
        ,tppi.box AS "box"
        ,tppi.piece AS "piece"
        ,tmguitems.unit_name         as "subUnitName"
        ,tmguitem.unit_name         as "unitName"
        ,tmgsitem.flg_sub_unit as "flgSubUnit"
        FROM dkic_b.t_psi_purchase_item tppi
        left join(SELECT T.*
        FROM (select tpi.inv_id,
        tpi.wh_id,
        tpi.inv_qty,
        tpi.usable_qty,
        tpi.sku_id,
        tpi.non_std_code,
        tpi.cp_id,
        tmwitem.wh_name,
        rank()
        over (partition by tpi.cp_id,tpi.sku_id,tpi.non_std_code order by tpi.inv_id) as RK
        from dkic_b.t_psi_inventory as tpi
        left join dkic_b.t_mst_warehouse as tmwitem on tmwitem.wh_id = tpi.wh_id) T
        WHERE T.RK = 1) as i on i.sku_id = tppi.sku_id and i.non_std_code = tppi.non_std_code and i.cp_id = tppi.cp_id
        left join dkic_b.t_psi_order tpoitem on tpoitem.order_id = tppi.from_id
        left join dkic_b.t_mst_goods_sku tmgsitem on tppi.sku_id = tmgsitem.sku_id
        left join dkic_b.t_mst_goods_brand tmgbitem on tmgbitem.brand_id = tmgsitem.brand_id
        left join sys.t_data_kind tdk1item on tdk1item.kind_code = tppi.into_status
        left join dkic_b.t_psi_purchase tppitem  on tppitem.pur_id = tppi.pur_id
        left join dkic_b.t_mst_unit as tmguitem on tppi.unit_id = tmguitem.unit_id
        left join dkic_b.t_mst_unit as tmguitems on tppi.sub_unit_id = tmguitems.unit_id
        WHERE
        tppi.pur_id = tpp.pur_id
        AND  tppi.into_status != '入库状态-已入库'
        <if test="cpId != null">
            AND tppi.cp_id = #{cpId}
        </if>
        <if test="flgValid != null">
            AND tppi.flg_valid = #{flgValid}
        </if>
        ) item
        )  AS  "purchaseItemResponseList"
        FROM dkic_b.t_psi_purchase tpp
        Left join dkic_b.t_mst_org tmo on tpp.org_id = tmo.org_id
        Left join dkic_b.t_mst_supplier tmp on tpp.sup_id = tmp.sup_id
        Left join dkic_b.t_mst_staff tms on tpp.staff_id = tms.staff_id
        Left join dkic_b.t_mst_staff tms1 on tpp.make_staff = tms1.staff_id
        Left join dkic_b.t_mst_warehouse tmw on tpp.wh_id = tmw.wh_id
        left join sys.t_data_kind tdk1 on tdk1.kind_code = tpp.into_status
        left join sys.t_data_kind tdk2 on tdk2.kind_code = tpp.pur_status
        where tpp.into_status != '入库状态-已入库'
        <if test="searchText !=null and searchText!='' " >
            AND  ( tmp.sup_name LIKE concat('%',my_ex.likequery(#{searchText}), '%')or
            tpp.pur_no LIKE concat('%',my_ex.likequery(#{searchText}), '%'))
        </if>
        <if test="flgValid != null">
            AND tpp.flg_valid = #{flgValid}
        </if>
        <if test="cpId != null">
            AND tpp.cp_id = #{cpId}
        </if>
        order by  tpp.make_time desc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>



    <!-- 查询表dkic_b.t_psi_purchase，（条件查询+分页）列表 -->
    <select id="selectTotalDetail" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Left_join"/>
        <include refid="Base_Column_List_Item_Response_Join"/>
        ,tmgb.brand_name as "list_brand_name"
        ,(select count(DISTINCT tmgss.category_id) from dkic_b.t_psi_purchase_item ti
        LEFT  JOIN dkic_b.t_mst_goods_sku tmgss ON tmgss.sku_id = ti.sku_id
        where ti.pur_id = tpp.pur_id and ti.flg_valid) as "category_count"
        ,tmo.org_name as "orgName"
        ,tms.staff_name as "staffName"
        ,tms1.staff_name as "makeStaffName"
        ,tmp.sup_name as "supplierName"
        ,tmw.wh_name as "whName"


        FROM dkic_b.t_psi_purchase tpp
        left join dkic_b.t_psi_purchase_item tppi  on tppi.pur_id = tpp.pur_id
        left join  dkic_b.t_mst_goods_sku  tmgs on tmgs.sku_id = tppi.sku_id
        left join  dkic_b.t_mst_goods_brand  tmgb on tmgb.brand_id = tmgs.brand_id
        left join  dkic_b.t_psi_inventory  tpit on tpit.sku_id = tppi.sku_id
        Left join dkic_b.t_mst_org tmo on tpp.org_id = tmo.org_id
        Left join dkic_b.t_mst_supplier tmp on tpp.sup_id = tmp.sup_id
        Left join dkic_b.t_mst_staff tms on tpp.staff_id = tms.staff_id
        Left join dkic_b.t_mst_staff tms1 on tpp.make_staff = tms1.staff_id
        Left join dkic_b.t_mst_warehouse tmw on tpp.wh_id = tmw.wh_id
        left join sys.t_data_kind tdk1 on tdk1.kind_code = tpp.into_status
        left join sys.t_data_kind tdk2 on tdk2.kind_code = tpp.pur_status
        <include refid="Condition_1"/>
        ORDER BY  tpp.make_time
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <select id="selectTotalDetailCount" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_purchase tpp
        Left join dkic_b.t_mst_org tmo on tpp.org_id = tmo.org_id
        Left join dkic_b.t_mst_supplier tmp on tpp.sup_id = tmp.sup_id
        Left join dkic_b.t_mst_staff tms on tpp.staff_id = tms.staff_id
        Left join dkic_b.t_mst_staff tms1 on tpp.make_staff = tms1.staff_id
        Left join dkic_b.t_mst_warehouse tmw on tpp.wh_id = tmw.wh_id
        left join sys.t_data_kind tdk1 on tdk1.kind_code = tpp.into_status
        left join sys.t_data_kind tdk2 on tdk2.kind_code = tpp.pur_status
        <include refid="Condition_1"/>
    </select>
</mapper>
