<?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.ivt.InboundMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        into_id ,into_no, into_type, from_id, from_no, org_id, staff_id, cus_id, sup_id,
        into_status, intoing_qty, intoing_amt, into_qty, into_amt, return_qty,
        return_amt, into_date, remarks, annex_paths, make_staff, make_time,
        receivable_id, 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,wh_id,
        into_reason,flg_auto_handle,flg_handle_setting,flg_neg_into
    </sql>


    <!-- response通用查询列 -->
    <sql id="Base_Column_List_Response">
        tpi.into_id, tpi.into_no, tpi.into_type, tpi.from_id, tpi.from_no, tpi.org_id, tpi.staff_id,
        tpi.cus_id, tpi.sup_id, tpi.into_status, tpi.intoing_qty, tpi.intoing_amt, tpi.into_qty,
        tpi.into_amt, tpi.return_qty, tpi.return_amt, tpi.into_date, tpi.remarks, tpi.annex_paths,
        tpi.make_staff, tpi.make_time, tpi.receivable_id, tpi.flg_valid, tpi.cp_id,
        tpi.wh_id,tpi.into_reason,tpi.flg_auto_handle,tpi.flg_handle_setting,tpi.flg_neg_into
    </sql>


    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.Inbound">
        <id column="into_id" property="intoId" typeHandler="UuidTypeHandler"/>
        <result column="into_no" property="intoNo"/>
        <result column="into_type" property="intoType"/>
        <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
        <result column="from_no" property="fromNo"/>
        <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="cus_id" property="cusId" typeHandler="UuidTypeHandler"/>
        <result column="sup_id" property="supId" typeHandler="UuidTypeHandler"/>
        <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="into_date" property="intoDate" typeHandler="TimestampTypeHandler"/>
        <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="receivable_id" property="receivableId" typeHandler="UuidTypeHandler"/>
        <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="into_reason" property="intoReason" typeHandler="UuidTypeHandler"/>
        <result column="wh_id" property="whId"/>
        <result column="flg_auto_handle" property="flgAutoHandle"/>
        <result column="flg_handle_setting" property="flgHandleSetting"/>
        <result column="flg_neg_into" property="flgNegInto"/>
    </resultMap>

    <!-- response查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.InboundResponse">
        <id column="into_id" property="intoId" typeHandler="UuidTypeHandler"/>
        <result column="into_no" property="intoNo"/>
        <result column="into_type" property="intoType"/>
        <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
        <result column="from_no" property="fromNo"/>
        <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="cus_id" property="cusId" typeHandler="UuidTypeHandler"/>
        <result column="sup_id" property="supId" typeHandler="UuidTypeHandler"/>
        <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="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
        <result column="return_qty" property="returnQty"/>
        <result column="return_amt" property="returnAmt"/>
        <result column="into_date" property="intoDate" typeHandler="TimestampTypeHandler"/>
        <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="receivable_id" property="receivableId" typeHandler="UuidTypeHandler"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="into_reason" property="intoReason" typeHandler="UuidTypeHandler"/>
        <result column="into_reason_name" property="intoReasonName" />
<!--        <result column="orgName" property="orgName" />-->
<!--        <result column="staff_name" property="staffName" />-->
        <result column="cus_code" property="cusCode" />
<!--        <result column="cus_name" property="cusName" />-->
<!--        <result column="cus_phone" property="cusPhone" />-->
        <result column="contact_phone" property="contactPhone" />
        <result column="contact_name" property="contactName" />
        <result column="address_area" property="addressArea" typeHandler="JsonTypeHandler"/>
        <result column="address_name" property="addressName"/>
        <result column="address_no" property="addressNo"/>
        <result column="address_gcj02" property="addressGcj02" typeHandler="JsonTypeHandler"/>
        <result column="address_full" property="addressFull"/>
        <result column="channel_name" property="channelName" />
        <result column="cus_from_name" property="cusFromName" />
        <result column="return_address" property="returnAddress" />
        <result column="flg_auto_handle" property="flgAutoHandle"/>
        <result column="flg_handle_setting" property="flgHandleSetting"/>
        <result column="flg_neg_into" property="flgNegInto"/>
        <result column="pickup_date" property="pickupDate" typeHandler="TimestampTypeHandler"/>
    </resultMap>


    <!-- response明细通用查询列 -->
    <sql id="Base_Column_List_Item_Response">
        tpi.into_id
        , tpi.into_no, tpi.into_type, tpi.from_id, tpi.from_no, tpi.org_id, tpi.staff_id, tpi.cus_id, tpi.sup_id, tpi.into_status, tpi.intoing_qty, tpi.intoing_amt,
        tpi.into_qty, tpi.into_amt, tpi.return_qty, tpi.return_amt, tpi.into_date, tpi.remarks, tpi.annex_paths, tpi.make_staff, tpi.make_time,
        tpi.receivable_id, tpi.flg_valid, tpi.cp_id
    </sql>
    <!-- response明细关联明细映射-->
    <resultMap id="BaseResultMapItemResponse" type="com.dk.mdm.model.response.ivt.InboundResponse">
        <id column="into_id" property="intoId" typeHandler="UuidTypeHandler"/>
        <result column="into_no" property="intoNo"/>
        <result column="into_type" property="intoType"/>
        <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
        <result column="from_no" property="fromNo"/>
        <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="cus_id" property="cusId" typeHandler="UuidTypeHandler"/>
        <result column="sup_id" property="supId" typeHandler="UuidTypeHandler"/>
        <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="into_date" property="intoDate" typeHandler="TimestampTypeHandler"/>
        <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="receivable_id" property="receivableId" typeHandler="UuidTypeHandler"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <collection property="inboundItemList" resultMap="inboundItemListMap" columnPrefix="list_"/>
    </resultMap>

    <!--    关联映射-->
    <resultMap id="inboundItemListMap" type="java.util.Map">
        <result column="item_id" property="itemId" typeHandler="UuidTypeHandler"/>
        <result column="into_id" property="intoId" typeHandler="UuidTypeHandler"/>
        <result column="into_no" property="intoNo" typeHandler="UuidTypeHandler"/>
        <result column="into_type" property="intoType"/>
        <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
        <result column="from_item_id" property="fromItemId" typeHandler="UuidTypeHandler"/>
        <result column="item_index" property="itemIndex"/>
        <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
        <result column="item_qty" property="itemQty"/>
        <result column="price_into" property="priceInto"/>
        <result column="item_amt" property="itemAmt"/>
        <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="inv_id" property="invId" typeHandler="UuidTypeHandler"/>
        <result column="cost_price" property="costPrice"/>
        <result column="cost_amt" property="costAmt"/>
        <result column="out_qty" property="outQty"/>
        <result column="inv_qty" property="invQty"/>
        <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="sku_model" property="skuModel"/>
        <result column="short_name" property="shortName"/>
        <result column="price_purchase" property="pricePurchase"/>
        <result column="reject_qty" property="rejectQty"/>
        <result column="sku_images" property="skuImages"  typeHandler="JsonTypeHandler"/>
        <result column="usable_qty" property="usableQty"/>
        <result column="inv_qty" property="invQty"/>
        <result column="outing_qty" property="outingQty"/>
        <result column="price_std" property="priceStd"/>
        <result column="into_item_id" property="intoItemId"/>
        <result column="inv_id" property="invId"/>
        <result column="wh_id" property="whId"/>
        <result column="inv_qty" property="invQty"/>
        <result column="usable_qty" property="usableQty"/>
        <result column="wh_name" property="whName"/>

    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="intoNo != null and intoNo != ''">
                AND tpi.into_no LIKE concat('%',my_ex.likequery(#{intoNo}),'%')
            </if>
            <if test="intoType != null and intoType != ''">
                AND tpi.into_type LIKE concat('%',my_ex.likequery(#{intoType}),'%')
            </if>
            <if test="intoTypes != null and intoTypes.size()>0">
                AND tpi.into_type =any(#{intoTypes,typeHandler=StringListTypeHandler})
            </if>
            <if test="intoReasonList != null and intoReasonList.size()>0">
                AND tpi.into_reason =any(#{intoReasonList,typeHandler=UuidListTypeHandler})
            </if>
            <if test="searchText !=null and searchText != ''">
                AND (
                    tpi.into_no   LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                    or tpi.from_no LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                    or tmc.cus_phone LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                    or tmc.cus_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                    or tmo.org_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                    or tmp.sup_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                    or tms.staff_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                )
            </if>
            <if test="fromId != null and fromId != ''">
                AND tpi.from_id = #{fromId}::uuid
            </if>
            <if test="fromNo != null and fromNo != ''">
                AND tpi.from_no LIKE concat('%',my_ex.likequery(#{fromNo}),'%')
            </if>
            <if test="orgId != null and orgId != ''">
                AND tpi.org_id = #{orgId}::uuid
            </if>
            <if test="orgName != null and orgName != ''">
                AND tmo.org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
            </if>
            <if test="supName != null and supName != ''">
                AND tmp.sup_name LIKE concat('%',my_ex.likequery(#{supName}),'%')
            </if>
            <if test="staffName != null and staffName != ''">
                AND tms.staff_name LIKE concat('%',my_ex.likequery(#{staffName}),'%')
            </if>
            <if test="whName != null and whName != ''">
                AND tmw.wh_name LIKE concat('%',my_ex.likequery(#{whName}),'%')
            </if>
            <if test="staffId != null and staffId != ''">
                AND tpi.staff_id = #{staffId}::uuid
            </if>
            <if test="cusId != null and cusId != ''">
                AND tpi.cus_id = #{cusId}::uuid
            </if>
            <if test="supId != null and supId != ''">
                AND tpi.sup_id = #{supId}::uuid
            </if>
            <if test="makeStaff != null and makeStaff != ''">
                AND tpi.make_staff = #{makeStaff}
            </if>

            <if test="makeTime != null">
                AND tpi.make_time = #{makeTime}
            </if>
            <if test="receivableId != null and receivableId != ''">
                AND tpi.receivable_id = #{receivableId}
            </if>
            <if test="flgValid != null">
                AND tpi.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tpi.cp_id = #{cpId}
            </if>
            <if test="cusPhone != null and cusPhone != ''">
                AND tmc.cus_phone LIKE concat('%',my_ex.likequery(#{cusPhone}),'%')
            </if>
            <if test="cusName != null and cusName != ''">
                AND tmc.cus_name LIKE concat('%',my_ex.likequery(#{cusName}),'%')
            </if>
            <if test="orgIdList != null and orgIdList.size() > 0">
                AND tmo.org_id  =any(#{orgIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="staffIdList != null and staffIdList.size() > 0">
                AND tms.staff_id  =any(#{staffIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="intoDateStart != null and intoDateEnd != null">
                AND tpi.into_date &gt;= #{intoDateStart}::timestamp with time zone
                AND tpi.into_date &lt; #{intoDateEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="makeTimeStart != null and makeTimeEnd != null">
                AND tpi.make_time &gt;= #{makeTimeStart}::timestamp with time zone
                AND tpi.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="intoStatusList != null and intoStatusList.size()>0">
                AND tpi.into_status =
                any(#{intoStatusList, typeHandler= StringListTypeHandler})
            </if>
            <if test="intoStatus != null and intoStatus != ''">
                 AND tpi.into_status LIKE concat('%',my_ex.likequery(#{intoStatus}),'%')
            </if>
            <if test="skuModel != null and skuModel!='' ">
                and exists (select 1 from dkic_b.t_psi_inbound_item tpii
                left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpii.sku_id
                where
                tpi.into_id = tpii.into_id
                and tpii.flg_valid
                AND position(#{skuModel} in tmgs.sku_model) > 0
                )
            </if>
            <if test="skuCode != null and skuCode!='' ">
                and exists (select 1 from dkic_b.t_psi_inbound_item tpii
                left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpii.sku_id
                where
                tpi.into_id = tpii.into_id
                and tpii.flg_valid
                AND position(#{skuCode} in tmgs.sku_code) > 0
                )
            </if>
            <if test="skuName != null and skuName!='' ">
                and exists (select 1 from dkic_b.t_psi_inbound_item tpii
                left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpii.sku_id
                where
                tpi.into_id = tpii.into_id
                and tpii.flg_valid
                AND position(#{skuName} in tmgs.sku_name) > 0
                )
            </if>
        </where>
    </sql>
    <sql id="ConditionIsSup">
        <where>
            <if test="intoNo != null and intoNo != ''">
                AND tpi.into_no LIKE concat('%',my_ex.likequery(#{intoNo}),'%')
            </if>
            <if test="intoType != null and intoType != ''">
                AND tpi.into_type LIKE concat('%',my_ex.likequery(#{intoType}),'%')
            </if>
            <if test="intoTypes != null and intoTypes.size()>0">
                AND tpi.into_type =any(#{intoTypes,typeHandler=StringListTypeHandler})
            </if>
            <if test="intoReasonList != null and intoReasonList.size()>0">
                AND tpi.into_reason =any(#{intoReasonList,typeHandler=UuidListTypeHandler})
            </if>

            <if test="fromId != null and fromId != ''">
                AND tpi.from_id = #{fromId}::uuid
            </if>
            <if test="fromNo != null and fromNo != ''">
                AND tpi.from_no LIKE concat('%',my_ex.likequery(#{fromNo}),'%')
            </if>
            <if test="orgId != null and orgId != ''">
                AND tpi.org_id = #{orgId}::uuid
            </if>
            <if test="orgName != null and orgName != ''">
                AND tmo.org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
            </if>
            <if test="supName != null and supName != ''">
                AND tmp.sup_name LIKE concat('%',my_ex.likequery(#{supName}),'%')
            </if>
            <if test="staffName != null and staffName != ''">
                AND tms.staff_name LIKE concat('%',my_ex.likequery(#{staffName}),'%')
            </if>
            <if test="whName != null and whName != ''">
                AND tmw.wh_name LIKE concat('%',my_ex.likequery(#{whName}),'%')
            </if>
            <if test="staffId != null and staffId != ''">
                AND tpi.staff_id = #{staffId}::uuid
            </if>
            <if test="cusId != null and cusId != ''">
                AND tpi.cus_id = #{cusId}::uuid
            </if>
            <if test="supId != null and supId != ''">
                AND tpi.sup_id = #{supId}::uuid
            </if>
            <if test="makeStaff != null and makeStaff != ''">
                AND tpi.make_staff = #{makeStaff}
            </if>

            <if test="makeTime != null">
                AND tpi.make_time = #{makeTime}
            </if>
            <if test="receivableId != null and receivableId != ''">
                AND tpi.receivable_id = #{receivableId}
            </if>
            <if test="flgValid != null">
                AND tpi.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tpi.cp_id = #{cpId}
            </if>
            <if test="searchText !=null and searchText != ''">
                AND ( tpi.into_no   LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                or  (exists (select 1 from dkic_b.t_psi_inbound_item tpii
                left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id = tpii.sku_id
                where
                tpi.into_id = tpii.into_id
                and tpii.flg_valid
                AND position(#{searchText} in tmgs.sku_model) > 0
                ))
                )
            </if>
        </where>
    </sql>
    <sql id="idsForeach">
        <!-- 根据主键intoId批量操作 -->
        WHERE into_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表t_psi_inbound，（条件查询+分页）列表   tpi.sum_quantity, tpi.sum_amount,-->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,tmo.org_name AS "orgName",
        tmp.sup_name AS "supplierName",
        tms.staff_name AS "staffName",
        tms.staff_name AS "makeStaffName",
        tmw.wh_name as "whName",
        tmc.cus_name                                      as "cusName",
        tmc.cus_phone                                     as "cusPhone",
        tmc.cus_from                                      as "cusFrom",
        makestaff.staff_name                              as "makeStaffName",
        sys.f_get_name_i18n(tdktype.kind_name_i18n,  #{i18n}) AS  "intoTypeName",
        sys.f_get_name_i18n(tdk.kind_name_i18n,  #{i18n}) AS  "intoStatusName",
        tmd.data_value                                   as into_reason_name,
        tmp.return_address,
        case when tpi.receivable_id is null then false
        when tmai.amt_handle = 0 then false
        else true end as "paymentFlag"
        FROM dkic_b.t_psi_inbound tpi
        <include refid="Join_Table"/>
        <include refid="Condition"/>
        order by tpi.make_time desc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>

    </select>

    <!-- 查询表t_psi_inbound，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_inbound tpi
        <include refid="Join_Table"/>
        <include refid="Condition"/>
    </select>

    <sql id="Join_Table">
        Left join dkic_b.t_mst_org tmo on tmo.org_id = tpi.org_id
        Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = tpi.sup_id
        Left join dkic_b.t_mst_staff tms on tms.staff_id = tpi.staff_id
        Left join dkic_b.t_mst_staff makestaff on makestaff.staff_id = tpi.make_staff
        LEFT JOIN sys.t_data_kind tdk   ON tdk.kind_code = tpi.into_status
        LEFT JOIN sys.t_data_kind tdktype   ON tdktype.kind_code = tpi.into_type
        Left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpi.wh_id
        left join dkic_b.t_mst_customer tmc on tpi.cus_id = tmc.cus_id
        left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = tpi.into_reason
        left join dkic_b.t_mac_account_item tmai on tmai.item_id = tpi.receivable_id
    </sql>


    <!-- 根据主键查询表t_psi_inbound的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,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"
        ,tmd.data_value  as into_reason_name
        ,case when tpi.receivable_id is null then false
        when tmai.amt_handle = 0 then false
        else true end as "paymentFlag"
        ,tpp.pickup_date
        FROM dkic_b.t_psi_inbound tpi
        Left join dkic_b.t_psi_purchase tpp on tpp.pur_id = tpi.from_id
        Left join dkic_b.t_mst_org tmo on tmo.org_id = tpi.org_id
        Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = tpi.sup_id
        Left join dkic_b.t_mst_staff tms on tms.staff_id = tpi.staff_id
        Left join dkic_b.t_mst_staff tms1 on tms1.staff_id = tpi.make_staff
        Left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpi.wh_id
        left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = tpi.into_reason
        left join dkic_b.t_mac_account_item tmai on tmai.item_id = tpi.receivable_id
        WHERE tpi.into_id = #{id}::uuid
        and tpi.flg_valid
    </select>

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

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

    <insert id="insertBatch">
        insert into dkic_b.t_psi_inbound
        (
        <trim suffixOverrides=",">
            into_no,
            into_type,
            from_id,
            from_no,
            org_id,
            staff_id,
            cus_id,
            sup_id,
            into_status,
            intoing_qty,
            intoing_amt,
            into_qty,
            into_amt,
            return_qty,
            return_amt,
            into_date,
            remarks,
            annex_paths,
            make_staff,
            make_time,
            receivable_id,
            cp_id,
            op_app_code,
            wh_id,
            entry_reason,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.intoNo},
                #{item.intoType},
                #{item.fromId}::uuid,
                #{item.fromNo},
                #{item.orgId}::uuid,
                #{item.staffId}::uuid,
                #{item.cusId}::uuid,
                #{item.supId}::uuid,
                #{item.intoStatus},
                #{item.intoingQty},
                #{item.intoingAmt},
                #{item.intoQty},
                #{item.intoAmt},
                #{item.returnQty},
                #{item.returnAmt},
                #{item.intoDate},
                #{item.remarks},
                #{item.annexPaths},
                #{item.makeStaff}::uuid,
                #{item.makeTime},
                #{item.receivableId}::uuid,
                #{item.cpId},
                #{item.opAppCode},
                #{item.whId},
                #{item.intoReason}::uuid,
            </trim>
            )
        </foreach>
    </insert>


    <sql id="Base_Column_List_Item_Response_Join">
        tpii
        .
        item_id
        AS list_item_id,
        tpii.item_id  AS list_into_item_Id,
        tpii.into_id AS list_into_id,
        tpi.into_no AS list_into_no,
        tpii.into_type AS list_into_type,
        tpii.from_id AS list_from_id,
        tpii.from_item_id AS list_from_item_id,
        tpii.item_index AS list_item_index,
        tpii.sku_id AS list_sku_id,
        tpii.price_into AS list_price_into,
        tpii.non_std_code AS list_non_std_code,
        tpii.into_status AS list_into_status,
        tpii.intoing_qty AS list_intoing_qty,
        tpii.intoing_amt AS list_intoing_amt,
        tpii.into_qty AS list_into_qty,
        tpii.into_amt AS list_into_amt,
        tpii.return_qty AS list_return_qty,
        tpii.return_amt AS list_return_amt,
        tpii.remarks AS list_remarks,
        tpii.inv_id AS list_inv_id,
        tpii.cost_price AS list_cost_price,
        tpii.cost_amt AS list_cost_amt,
        tpii.out_qty AS list_out_qty,
        tpii.inv_qty AS list_inv_qty,
        tpii.flg_valid AS list_flg_valids,
        tpii.cp_id AS list_cp_id,
        tmgs.sku_code AS  list_sku_code,
        tmgs.sku_name AS  list_sku_name,
        tmgs.sku_model AS  list_sku_model,
        tmgb.short_name AS  list_short_name,
        tmgs.price_purchase AS  list_price_purchase,
        tmgs.sku_images AS  list_sku_images,

        ( tpii.into_qty - tpii.return_qty)  AS list_reject_qty,
--         tpit.usable_qty AS list_usable_qty,
--         tpit.inv_qty AS list_inv_qty,
--         tpit.outing_qty AS list_outing_qty
--         ,
        tmgs.price_standard AS list_price_std,

        i.inv_id  as list_inv_id,
        i.wh_id as list_wh_id,
        i.inv_qty    as list_inv_qty,
        i.usable_qty  as list_usable_qty,
        i.wh_name     as list_wh_name
    </sql>


    <!-- 查询表t_psi_inbound，（条件查询+分页）列表 -->
    <select id="selectInboundAndItem" resultMap="BaseResultMapItemResponse">
        SELECT
        <include refid="Base_Column_List_Item_Response"/>,
        <include refid="Base_Column_List_Item_Response_Join"/>
        FROM dkic_b.t_psi_inbound  tpi
        left  join dkic_b.t_psi_inbound_item tpii  on tpi.into_id = tpii.into_id and  ( tpii.into_qty - tpii.return_qty) > 0
        left  join  dkic_b.t_mst_warehouse tmw on  tmw.wh_id =   tpii.wh_id
        inner join  dkic_b.t_mst_goods_sku  tmgs on tmgs.sku_id = tpii.sku_id
        left join  dkic_b.t_mst_goods_brand  tmgb on tmgb.brand_id = tmgs.brand_id
        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,
        tmw.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 tmw on tmw.wh_id = tpi.wh_id) T
        WHERE T.RK = 1) as i
        on i.sku_id = tpii.sku_id and i.non_std_code = tpii.non_std_code and i.cp_id = tpii.cp_id


        <include refid="ConditionIsSup"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_psi_inbound，（条件查询）个数 -->
    <select id="selectInboundAndItemCountByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_inbound tpi
        left join dkic_b.t_psi_inbound_item tpii  on tpi.into_id = tpii.into_id
        left  join  dkic_b.t_mst_warehouse tmw on  tmw.wh_id =   tpii.wh_id
        inner join  dkic_b.t_mst_goods_sku  tmgs on tmgs.sku_id = tpii.sku_id
        <include refid="ConditionIsSup"/>
    </select>


    <update id="updateAmount" parameterType="com.dk.mdm.model.pojo.ivt.InboundItem">
        update dkic_b.t_psi_inbound
        <set>
            <if test="returnQty!= null">
                return_qty= #{returnQty},
            </if>
            <if test="returnAmt!= null">
                return_amt = #{returnAmt},
            </if>
        </set>
        where into_id = #{intoId}::uuid
    </update>


    <!-- 根据id查询带出销售退货单数据（采购退货入库办理用）-->
    <select id="selectMessageByOtherQuery" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,tmo.org_name  as "orgName"
        ,tms.staff_name  as "staffName"
        ,tmc.cus_code
        ,tmc.cus_name                                      as "cusName"
        ,tmc.cus_phone                                     as "cusPhone"
        ,tmc.contact_phone
        ,tmc.contact_name
        ,tmc.address_area
        ,tmc.address_name
        ,tmc.address_no
        ,tmc.address_gcj02
        ,tmc.address_full
        ,tmsc.channel_name
        ,tmsc.channel_name
        ,tmsc.channel_name
        ,tmd.data_value  as cus_from_name
        FROM dkic_b.t_psi_inbound tpi
        Left join dkic_b.t_mst_org tmo on tmo.org_id = tpi.org_id
        Left join dkic_b.t_mst_staff tms on tms.staff_id = tpi.staff_id
        left join dkic_b.t_mst_customer tmc on tpi.cus_id = tmc.cus_id
        left join dkic_b.t_mst_sale_channel tmsc on tmc.channel_id = tmsc.channel_id
        left join dkic_b.t_mst_dictionary_data tmd on tmc.cus_from = tmd.data_id
        <where>
            <if test="intoId != null and intoId != ''">
                AND tpi.into_id = #{intoId}::uuid
            </if>
            <if test="intoStatus != null and intoStatus != ''">
                AND tpi.into_status = #{intoStatus}
            </if>
            <if test="fromId != null and fromId != ''">
                AND tpi.from_id = #{fromId}::uuid
            </if>
        </where>
    </select>



    <!-- 入库办理显示字段 -->
    <sql id="Base_Column_List_Response_Into">
        tpi.into_id,
        tpi.into_no,
        tpi.into_type,
        tpi.from_id,
        tpi.from_no,
        tpi.org_id,
        tpi.staff_id,
        tpi.cus_id,
        tpi.sup_id,
        tpi.into_status,
        tpi.intoing_qty,
        tpi.intoing_amt,
        tpi.into_qty,
        tpi.into_amt,
        tpi.return_qty,
        tpi.return_amt,
        tpi.into_date,
        tpi.remarks,
        tpi.annex_paths,
        tpi.make_staff,
        tpi.make_time,
        tpi.wh_id,
        tpi.into_reason,
        tpi.flg_valid,
        tpi.flg_auto_handle,
        tpi.flg_handle_setting
    </sql>
    <sql id="Base_Column_List_Response_Out">
        tpo.out_id  as into_id,
        tpo.out_no  as into_no,
        tpo.out_type as into_type,
        tpo.from_id,
        tpo.from_no,
        tpo.org_id,
        tpo.staff_id,
        tpo.cus_id,
        tpo.sup_id,
        tpo.out_status,
        tpo.outing_qty as intoing_qty,
        tpo.outing_amt as intoing_amt,
        tpo.out_qty as into_qty,
        tpo.out_amt as into_amt,
        tpo.return_qty,
        tpo.return_amt,
        tpo.out_date as into_date,
        tpo.remarks,
        tpo.annex_paths,
        tpo.make_staff,
        tpo.make_time,
        null as wh_id,
        tpo.out_reason as into_reason,
        tpo.flg_valid,
        tpo.flg_auto_handle,
        tpo.flg_handle_setting
    </sql>
    <!-- 条件查询 web端入库办理用,-->
    <select id="selectInbound" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response_Into"/>,
        tmo.org_name AS "orgName",
        tmp.sup_name AS "supplierName",
        tms.staff_name AS "staffName",
        tms.staff_name AS "makeStaffName",
        tmw.wh_name as "whName",
        tmc.cus_name                                      as "cusName",
        tmc.cus_phone                                     as "cusPhone",
        tmc.cus_from                                      as "cusFrom",
        tmc.address_full,
        makestaff.staff_name                              as "makeStaffName",
        sys.f_get_name_i18n(tdktype.kind_name_i18n, #{i18n}) AS  "intoTypeName",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "intoStatusName",
        tmd.data_value                                   as into_reason_name
        FROM dkic_b.t_psi_inbound tpi
        <include refid="Select_Inbound_Join_Table_Into"/>
        <where>
            AND tpi.cp_id = #{cpId}
            <if test="intoTypes != null and intoTypes.size()>0">
                AND tpi.into_type =any(#{intoTypes,typeHandler=StringListTypeHandler})
            </if>
            <if test="intoTypes == null || intoTypes.size()==0">
                AND (tpi.into_type = '入库类型-其他入库'
                or tpi.into_type =  '入库类型-采购入库')
            </if>
            <if test=" intoStatus != null and intoStatus != '' ">
                AND  tpi.into_status LIKE concat('%',my_ex.likequery(#{intoStatus}),'%')
            </if>
            <if test="makeTimeStart != null and makeTimeEnd != null">
                AND tpi.make_time &gt;= #{makeTimeStart}::timestamp with time zone
                AND tpi.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="intoNo != null and intoNo != ''">
                AND tpi.into_no LIKE concat('%',my_ex.likequery(#{intoNo}),'%')
            </if>
            <if test="cusPhone != null and cusPhone != ''">
                AND tmc.cus_phone LIKE concat('%',my_ex.likequery(#{cusPhone}),'%')
            </if>
            <if test="cusName != null and cusName != ''">
                AND tmc.cus_name LIKE concat('%',my_ex.likequery(#{cusName}),'%')
            </if>
            <if test="supId != null and supId != ''">
                AND tpi.sup_id = #{supId}::uuid
            </if>
            <if test="orgIdList != null and orgIdList.size() > 0">
                AND tmo.org_id  =any(#{orgIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="staffIdList != null and staffIdList.size() > 0">
                AND tms.staff_id  =any(#{staffIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="flgValidList != null and flgValidList.size()>0">
                AND tpi.flg_valid =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
        </where>
        union all
        select
          <include refid="Base_Column_List_Response_Out"/>,
        tmo.org_name AS "orgName",
        tmp.sup_name AS "supplierName",
        tms.staff_name AS "staffName",
        tms.staff_name AS "makeStaffName",
        null as "whName",
        tmc.cus_name                                      as "cusName",
        tmc.cus_phone                                     as "cusPhone",
        tmc.cus_from                                      as "cusFrom",
        tmc.address_full,
        makestaff.staff_name                              as "makeStaffName",
        sys.f_get_name_i18n(tdktype.kind_name_i18n, #{i18n}) AS  "intoTypeName",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "intoStatusName",
        tmd.data_value                                   as into_reason_name
        from dkic_b.t_psi_outbound tpo
        <include refid="Select_Inbound_Join_Table_Out"/>
        <where>
            AND tpo.cp_id = #{cpId}
            <if test="intoTypes != null and intoTypes.size()>0">
                AND tpo.out_type =any(#{intoTypes,typeHandler=StringListTypeHandler})
            </if>
            <if test="intoTypes == null || intoTypes.size()==0">
                AND tpo.out_type = '出库类型-销退回库'
            </if>
            <if test="outStatus != null and outStatus != '' ">
                AND  tpo.out_status LIKE concat('%',my_ex.likequery(#{outStatus}),'%')
            </if>
            <if test="makeTimeStart != null and makeTimeEnd != null">
                AND tpo.make_time &gt;= #{makeTimeStart}::timestamp with time zone
                AND tpo.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="intoNo != null and intoNo != ''">
                AND tpo.out_no LIKE concat('%',my_ex.likequery(#{intoNo}),'%')
            </if>
            <if test="cusPhone != null and cusPhone != ''">
                AND tmc.cus_phone LIKE concat('%',my_ex.likequery(#{cusPhone}),'%')
            </if>
            <if test="cusName != null and cusName != ''">
                AND tmc.cus_name LIKE concat('%',my_ex.likequery(#{cusName}),'%')
            </if>
            <if test="supId != null and supId != ''">
                AND tpo.sup_id = #{supId}::uuid
            </if>
            <if test="orgIdList != null and orgIdList.size() > 0">
                AND tmo.org_id  =any(#{orgIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="staffIdList != null and staffIdList.size() > 0">
                AND tms.staff_id  =any(#{staffIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="flgValidList != null and flgValidList.size()>0">
                AND tpo.flg_valid =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
        </where>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>
    <!--  条件查询 web端入库办理用个数 -->
    <select id="selectInboundCond" resultType="Long">
        SELECT
        count(1) from (
        SELECT
        <include refid="Base_Column_List_Response_Into"/>,
        tmo.org_name AS "orgName",
        tmp.sup_name AS "supplierName",
        tms.staff_name AS "staffName",
        tms.staff_name AS "makeStaffName",
        tmw.wh_name as "whName",
        tmc.cus_name                                      as "cusName",
        tmc.cus_phone                                     as "cusPhone",
        tmc.cus_from                                      as "cusFrom",
        makestaff.staff_name                              as "makeStaffName",
        sys.f_get_name_i18n(tdktype.kind_name_i18n, #{i18n}) AS  "intoTypeName",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "intoStatusName",
        tmd.data_value                                   as into_reason_name
        FROM dkic_b.t_psi_inbound tpi
        <include refid="Select_Inbound_Join_Table_Into"/>
        <where>
            AND tpi.cp_id = #{cpId}
            <if test="intoTypes != null and intoTypes.size()>0">
                AND tpi.into_type =any(#{intoTypes,typeHandler=StringListTypeHandler})
            </if>
            <if test="intoTypes == null || intoTypes.size()==0">
                AND (tpi.into_type = '入库类型-其他入库'
                or tpi.into_type =  '入库类型-采购入库')
            </if>
            <if test=" intoStatus != null and intoStatus != '' ">
                AND  tpi.into_status LIKE concat('%',my_ex.likequery(#{intoStatus}),'%')
            </if>
            <if test="makeTimeStart != null and makeTimeEnd != null">
                AND tpi.make_time &gt;= #{makeTimeStart}::timestamp with time zone
                AND tpi.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="intoNo != null and intoNo != ''">
                AND tpi.into_no LIKE concat('%',my_ex.likequery(#{intoNo}),'%')
            </if>
            <if test="cusPhone != null and cusPhone != ''">
                AND tmc.cus_phone LIKE concat('%',my_ex.likequery(#{cusPhone}),'%')
            </if>
            <if test="cusName != null and cusName != ''">
                AND tmc.cus_name LIKE concat('%',my_ex.likequery(#{cusName}),'%')
            </if>
            <if test="supId != null and supId != ''">
                AND tpi.sup_id = #{supId}::uuid
            </if>
            <if test="orgIdList != null and orgIdList.size() > 0">
                AND tmo.org_id  =any(#{orgIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="staffIdList != null and staffIdList.size() > 0">
                AND tms.staff_id  =any(#{staffIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="flgValidList != null and flgValidList.size()>0">
                AND tpi.flg_valid =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
        </where>
        union all
        select
        <include refid="Base_Column_List_Response_Out"/>,
        tmo.org_name AS "orgName",
        tmp.sup_name AS "supplierName",
        tms.staff_name AS "staffName",
        tms.staff_name AS "makeStaffName",
        null as "whName",
        tmc.cus_name                                      as "cusName",
        tmc.cus_phone                                     as "cusPhone",
        tmc.cus_from                                      as "cusFrom",
        makestaff.staff_name                              as "makeStaffName",
        sys.f_get_name_i18n(tdktype.kind_name_i18n, #{i18n}) AS  "intoTypeName",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "intoStatusName",
        tmd.data_value                                   as into_reason_name
        from dkic_b.t_psi_outbound tpo
        <include refid="Select_Inbound_Join_Table_Out"/>
        <where>
            AND tpo.cp_id = #{cpId}
            <if test="intoTypes != null and intoTypes.size()>0">
                AND tpo.out_type =any(#{intoTypes,typeHandler=StringListTypeHandler})
            </if>
            <if test="intoTypes == null || intoTypes.size()==0">
                AND tpo.out_type = '出库类型-销退回库'
            </if>
            <if test="outStatus != null and outStatus != '' ">
                AND  tpo.out_status LIKE concat('%',my_ex.likequery(#{outStatus}),'%')
            </if>
            <if test="makeTimeStart != null and makeTimeEnd != null">
                AND tpo.make_time &gt;= #{makeTimeStart}::timestamp with time zone
                AND tpo.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="intoNo != null and intoNo != ''">
                AND tpo.out_no LIKE concat('%',my_ex.likequery(#{intoNo}),'%')
            </if>
            <if test="cusPhone != null and cusPhone != ''">
                AND tmc.cus_phone LIKE concat('%',my_ex.likequery(#{cusPhone}),'%')
            </if>
            <if test="cusName != null and cusName != ''">
                AND tmc.cus_name LIKE concat('%',my_ex.likequery(#{cusName}),'%')
            </if>
            <if test="supId != null and supId != ''">
                AND tpo.sup_id = #{supId}::uuid
            </if>
            <if test="orgIdList != null and orgIdList.size() > 0">
                AND tmo.org_id  =any(#{orgIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="staffIdList != null and staffIdList.size() > 0">
                AND tms.staff_id  =any(#{staffIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="flgValidList != null and flgValidList.size()>0">
                AND tpo.flg_valid =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
        </where>
        ) tt
    </select>
    <!--入库办理显示入库关联表-->
    <sql id="Select_Inbound_Join_Table_Into">
        Left join dkic_b.t_mst_org tmo on tmo.org_id = tpi.org_id
        Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = tpi.sup_id
        Left join dkic_b.t_mst_staff tms on tms.staff_id = tpi.staff_id
        Left join dkic_b.t_mst_staff makestaff on makestaff.staff_id = tpi.make_staff
        LEFT JOIN sys.t_data_kind tdk   ON tdk.kind_code = tpi.into_status
        LEFT JOIN sys.t_data_kind tdktype   ON tdktype.kind_code = tpi.into_type
        Left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpi.wh_id
        left join dkic_b.t_mst_customer tmc on tpi.cus_id = tmc.cus_id
        left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = tpi.into_reason
    </sql>
    <!--入库办理显示出库关联表-->
    <sql id="Select_Inbound_Join_Table_Out">
        Left join dkic_b.t_mst_org tmo on tmo.org_id = tpo.org_id
        Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = tpo.sup_id
        Left join dkic_b.t_mst_staff tms on tms.staff_id = tpo.staff_id
        Left join dkic_b.t_mst_staff makestaff on makestaff.staff_id = tpo.make_staff
        LEFT JOIN sys.t_data_kind tdk   ON tdk.kind_code = tpo.out_status
        LEFT JOIN sys.t_data_kind tdktype   ON tdktype.kind_code = tpo.out_type
        left join dkic_b.t_mst_customer tmc on tpo.cus_id = tmc.cus_id
        left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = tpo.out_reason
    </sql>

    <update id="updateIntoStatus" parameterType="com.dk.mdm.model.pojo.ivt.InboundItem">
        update dkic_b.t_psi_inbound
        <set>
            <if test="intoStatus!= null">
                into_status= #{intoStatus},
            </if>

        </set>
        where into_id = #{intoId}::uuid
    </update>

<!--    采购退货用-->
    <resultMap id="BaseResultMapInboundReturnResponse" type="com.dk.mdm.model.response.ivt.InboundResponse">
        <id column="into_id" property="intoId" typeHandler="UuidTypeHandler"/>
        <result column="into_no" property="intoNo"/>
        <result column="into_type" property="intoType"/>
        <result column="from_id" property="fromId" typeHandler="UuidTypeHandler"/>
        <result column="from_no" property="fromNo"/>
        <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="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="wh_id" property="whId" typeHandler="UuidTypeHandler"/>
        <result column="return_qty" property="returnQty"/>
        <result column="return_amt" property="returnAmt"/>
        <result column="into_date" property="intoDate" typeHandler="TimestampTypeHandler"/>
        <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="receivable_id" property="receivableId" typeHandler="UuidTypeHandler"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="sup_name" property="supName"/>
        <result column="into_reason" property="intoReason" typeHandler="UuidTypeHandler"/>
        <result column="into_reason_name" property="intoReasonName" />
        <result column="org_name" property="orgName" />
        <result column="staff_name" property="staffName" />
        <result column="contact_phone" property="contactPhone" />
        <result column="contact_name" property="contactName" />
        <result column="channel_name" property="channelName" />
        <result column="cus_from_name" property="cusFromName" />
        <result column="return_address" property="returnAddress" />
        <result column="purSumAmount" property="purSumAmount" />
        <result column="purReturnAmt" property="purReturnAmt" />


    </resultMap>

    <sql id="Join_Table_Return">
        Left join dkic_b.t_mst_org tmo on tmo.org_id = tpi.org_id
        Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = tpi.sup_id
        Left join dkic_b.t_mst_staff tms on tms.staff_id = tpi.staff_id
        Left join dkic_b.t_mst_staff tms1 on tms1.staff_id = tpi.make_staff
        LEFT JOIN sys.t_data_kind tdk   ON tdk.kind_code = tpi.into_status
        LEFT JOIN sys.t_data_kind tdktype   ON tdktype.kind_code = tpi.into_type
        Left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpi.wh_id
        left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = tpi.into_reason
        left join dkic_b.t_mac_account_item tmai on tmai.item_id = tpi.receivable_id
    </sql>
<!--    采购退货用-->
    <select id="selectByInboundReturnCond" resultMap="BaseResultMapInboundReturnResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,tmo.org_name
        ,tmp.sup_name
        ,tms.staff_name
        ,tmw.wh_name
        ,tms1.staff_name as make_staff_name
        ,sys.f_get_name_i18n(tdktype.kind_name_i18n,  #{i18n}) AS  into_type_name
        ,sys.f_get_name_i18n(tdk.kind_name_i18n,  #{i18n}) AS into_status_name
        ,tmd.data_value as into_reason_name
        ,tmp.return_address
        ,tmp.contact_name
        ,tmp.contact_phone
        ,case when tpi.receivable_id is null then false
        when tmai.amt_handle = 0 then false
        else true end as "paymentFlag"
        FROM dkic_b.t_psi_inbound tpi
        <include refid="Join_Table_Return"/>
        <include refid="Condition"/>
        ORDER BY tpi.op_create_time DESC
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>
    <!--采购退货用 查询表t_psi_inbound，（条件查询）个数 -->
    <select id="countByInboundReturnCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_inbound tpi
        <include refid="Join_Table_Return"/>
        <include refid="Condition"/>
    </select>
    <!--    查看来源单据,总单加明细  采购退货用-->
    <select id="selectInboundReturnById" resultMap="BaseResultMapInboundReturnResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,tmo.org_name
        ,tms.staff_name as staff_name
        ,tms1.staff_name as make_staff_name
        ,tmp.sup_name
        ,tmw.wh_name
        ,tmd.data_value  as into_reason_name
        ,tmp.return_address
        ,tmp.contact_name
        ,tmp.contact_phone
        ,case when tpi.receivable_id is null then false
        when tmai.amt_handle = 0 then false
        else true end as "paymentFlag"
        ,tpp.sum_amount AS "purSumAmount"
        ,tpp.return_amt AS "purReturnAmt"
        FROM dkic_b.t_psi_inbound tpi
        Left join dkic_b.t_mst_org tmo on tmo.org_id = tpi.org_id
        Left join dkic_b.t_psi_purchase tpp on tpp.pur_id = tpi.from_id
        Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = tpi.sup_id
        Left join dkic_b.t_mst_staff tms on tms.staff_id = tpi.staff_id
        Left join dkic_b.t_mst_staff tms1 on tms1.staff_id = tpi.make_staff
        Left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = tpi.wh_id
        left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = tpi.into_reason
        left join dkic_b.t_mac_account_item tmai on tmai.item_id = tpi.receivable_id
        WHERE tpi.into_id = #{id}::uuid AND tpi.flg_valid = true
    </select>


    <!-- 根据来源id查询 -->
    <select id="selectByFromId" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_inbound tpi
        WHERE tpi.from_id = #{id}::uuid
        and tpi.flg_valid
    </select>
</mapper>
