<?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.OutboundMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        out_id, out_no, out_type, from_id, from_no, org_id, staff_id, cus_id, sup_id,
        out_status, sys.f_remove_zero(outing_qty) as outing_qty, outing_amt,
        sys.f_remove_zero(out_qty) as out_qty, out_amt, sys.f_remove_zero(return_qty) as return_qty, return_amt,
        out_date, remarks, annex_paths, make_staff, make_time, receivable_id,
        flg_valid, cp_id,out_reason,flg_auto_handle,flg_handle_setting
    </sql>

    <sql id="Base_Column_List_Response">
        tpo.out_id, tpo.out_no, tpo.out_type, tpo.from_id, tpo.from_no, tpo.org_id, tpo.staff_id,
        tpo.cus_id, tpo.sup_id, tpo.out_status, sys.f_remove_zero(tpo.outing_qty) as outing_qty, tpo.outing_amt,
        sys.f_remove_zero(tpo.out_qty) as out_qty, tpo.out_amt,
        sys.f_remove_zero(tpo.return_qty) as return_qty, tpo.return_amt, tpo.out_date, tpo.remarks, tpo.annex_paths, tpo.make_staff,
        tpo.make_time, tpo.receivable_id, tpo.flg_valid, tpo.cp_id,tpo.out_reason,
        tpo.flg_auto_handle,tpo.flg_handle_setting
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.Outbound">
        <id column="out_id" property="outId"/>
                <result column="out_no" property="outNo"/>
                <result column="out_type" property="outType"/>
                <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="out_status" property="outStatus"/>
                <result column="outing_qty" property="outingQty"/>
                <result column="outing_amt" property="outingAmt"/>
                <result column="out_qty" property="outQty"/>
                <result column="out_amt" property="outAmt"/>
                <result column="return_qty" property="returnQty"/>
                <result column="return_amt" property="returnAmt"/>
            <result column="out_date" property="outDate" 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="out_reason" property="outReason" typeHandler="UuidTypeHandler"/>
        <result column="flg_auto_handle" property="flgAutoHandle"/>
        <result column="flg_handle_setting" property="flgHandleSetting"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.OutboundResponse">
        <id column="out_id" property="outId"/>
        <result column="out_no" property="outNo"/>
        <result column="out_type" property="outType"/>
        <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="out_status" property="outStatus"/>
        <result column="outing_qty" property="outingQty"/>
        <result column="outing_amt" property="outingAmt"/>
        <result column="out_qty" property="outQty"/>
        <result column="out_amt" property="outAmt"/>
        <result column="return_qty" property="returnQty"/>
        <result column="return_amt" property="returnAmt"/>
        <result column="out_date" property="outDate" 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="flg_auto_handle" property="flgAutoHandle"/>
        <result column="flg_handle_setting" property="flgHandleSetting"/>
<!--        <result column="org_name" property="orgName"/>-->
<!--        <result column="staff_name" property="staffName"/>-->
        <result column="cusCode" property="cusCode"/>
        <result column="cusName" property="cusName"/>
        <result column="cusPhone" 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="addressFull" property="addressFull"/>
        <result column="channel_name" property="channelName"/>
        <result column="channel_id" property="channelId"/>
        <result column="cus_from_name" property="cusFromName"/>
        <result column="out_reason" property="outReason" typeHandler="UuidTypeHandler"/>
        <result column="out_reason_name" property="outReasonName" />
        <result column="sumOutAmt" property="sumOutAmt" />
        <result column="sumOutQty" property="sumOutQty" />
        <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
    </resultMap>

    <resultMap id="BaseResultMapDetailResponse" type="com.dk.mdm.model.response.ivt.OutboundResponse">
        <id column="out_id" property="outId"/>
        <result column="out_no" property="outNo"/>
        <result column="out_type" property="outType"/>
        <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="staffName" property="staffName" />
        <result column="orgName" property="orgName" />
        <result column="cus_id" property="cusId" typeHandler="UuidTypeHandler"/>
        <result column="sup_id" property="supId" typeHandler="UuidTypeHandler"/>
        <result column="out_status" property="outStatus"/>
        <result column="outing_qty" property="outingQty"/>
        <result column="outing_amt" property="outingAmt"/>
        <result column="out_qty" property="outQty"/>
        <result column="out_amt" property="outAmt"/>
        <result column="return_qty" property="returnQty"/>
        <result column="return_amt" property="returnAmt"/>
        <result column="out_date" property="outDate" 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="flg_auto_handle" property="flgAutoHandle"/>
        <result column="flg_handle_setting" property="flgHandleSetting"/>
        <result column="cusCode" property="cusCode"/>
        <result column="cusName" property="cusName"/>
        <result column="cusPhone" 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="addressFull" property="addressFull"/>
        <result column="channel_name" property="channelName"/>
        <result column="cus_from_name" property="cusFromName"/>
        <result column="out_reason" property="outReason" typeHandler="UuidTypeHandler"/>
        <result column="out_reason_name" property="outReasonName" />

        <collection property="goodsList" resultMap="itemListMap" columnPrefix="list_"/>
    </resultMap>

    <resultMap id="itemListMap" type="java.util.Map">
        <id column="item_id" property="itemId"/>
        <result column="out_id" property="outId" typeHandler="UuidTypeHandler"/>
        <result column="out_no" property="outNo" />
        <result column="out_type" property="outType"/>
        <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="price_out" property="priceOut"/>
        <result column="non_std_code" property="nonStdCode"/>
        <result column="out_status" property="outStatus"/>
        <result column="outing_qty" property="outingQty"/>
        <result column="outing_amt" property="outingAmt"/>
        <result column="out_qty" property="outQty"/>
        <result column="out_amt" property="outAmt"/>
        <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="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="s_out_id" property="sOutId" typeHandler="UuidTypeHandler"/>
        <result column="s_out_item_id" property="sOutItemId" typeHandler="UuidTypeHandler"/>
        <result column="s_order_id" property="sOrderId" typeHandler="UuidTypeHandler"/>
        <result column="s_order_item_id" property="sOrderItemId" typeHandler="UuidTypeHandler"/>

        <result column="sku_code" property="skuCode"/>
        <result column="sku_model" property="skuModel"/>
        <result column="sku_name" property="skuName"/>
        <result column="brand_name" property="brandName"/>
        <result column="price_purchase" property="pricePurchase"/>
        <result column="price_standard" property="priceStandard"/>
        <result column="sku_images" property="skuImages"/>
        <result column="wh_id" property="whId"/>
        <result column="wh_name" property="whName"/>
        <result column="invQty" property="invQty"/>
        <result column="usableQty" property="usableQty"/>
        <result column="pack_box" property="packBox"/>
        <result column="flgSubUnit" property="flgSubUnit"/>
        <result column="subUnitName" property="subUnitName"/>
        <result column="unitName" property="unitName"/>
        <result column="outing_box" property="outingBox"/>
        <result column="outing_piece" property="outingPiece"/>
        <result column="out_box" property="box"/>
        <result column="out_piece" property="piece"/>
        <result column="decimal_places" property="decimalPlaces"/>
    </resultMap>
    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>

            <if test="outNo != null and outNo != ''">
                AND t.out_no LIKE concat('%',my_ex.likequery(#{outNo}),'%')
            </if>
            <if test="outType != null and outType != ''">
                AND t.out_type = #{outType}
            </if>
            <if test="outTypeList != null and outTypeList.size()>0">
                AND t.out_type =any(#{outTypeList,typeHandler=StringListTypeHandler})
            </if>
            <if test="outReasonList != null and outReasonList.size()>0">
                AND t.out_reason =any(#{outReasonList,typeHandler=UuidListTypeHandler})
            </if>
            <if test="searchText !=null and searchText != ''">
                AND (
                t.out_no   LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                or t.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="orgName != null and orgName != ''">
                AND tmo.org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
            </if>
            <if test="addressFull != null and addressFull != ''">
                AND  tmc.address_full  LIKE concat('%',my_ex.likequery(#{addressFull}),'%')
            </if>
            <if test="staffIds != null and staffIds.size()>0">
                AND t.staff_id = any(#{staffIds, typeHandler=uuidListTypeHandler})
            </if>
            <if test="orgIds != null and orgIds.size()>0">
                AND t.org_id = any(#{orgIds, typeHandler=uuidListTypeHandler})
            </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="fromId != null and fromId != ''">
                AND t.from_id = #{fromId}
            </if>
            <if test="fromNo != null and fromNo != ''">
                AND t.from_no = #{fromNo}
            </if>
            <if test="orgId != null and orgId != ''">
                AND t.org_id = #{orgId}
            </if>
            <if test="staffId != null and staffId != ''">
                AND t.staff_id = #{staffId}
            </if>
            <if test="cusId != null and cusId != ''">
                AND t.cus_id = #{cusId}::uuid
            </if>
            <if test="supId != null and supId != ''">
                AND t.sup_id = #{supId}
            </if>
            <if test="outStatus != null and outStatus != ''">
                AND t.out_status = #{outStatus}
            </if>
            <if test="outingQty != null">
                AND t.outing_qty = #{outingQty}
            </if>
            <if test="outingAmt != null">
                AND t.outing_amt = #{outingAmt}
            </if>
            <if test="outQty != null">
                AND t.out_qty = #{outQty}
            </if>
            <if test="outAmt != null">
                AND t.out_amt = #{outAmt}
            </if>
            <if test="returnQty != null">
                AND t.return_qty = #{returnQty}
            </if>
            <if test="returnAmt != null">
                AND t.return_amt = #{returnAmt}
            </if>
            <if test="remarks != null and remarks != ''">
                AND t.remarks = #{remarks}
            </if>
            <if test="annexPaths != null and annexPaths != ''">
                AND t.annex_paths = #{annexPaths}
            </if>
            <if test="makeStaff != null and makeStaff != ''">
                AND t.make_staff = #{makeStaff}
            </if>
            <if test="receivableId != null and receivableId != ''">
                AND t.receivable_id = #{receivableId}
            </if>
            <if test="cpId != null">
                AND t.cp_id = #{cpId}
            </if>
            <if test="orderNo != null and orderNo != ''">
                AND t.from_no LIKE concat('%',my_ex.likequery(#{orderNo}),'%')
            </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 t.org_id  =any(#{orgIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="staffIdList != null and staffIdList.size() > 0">
                AND t.staff_id  =any(#{staffIdList, typeHandler=uuidListTypeHandler})
            </if>
            <if test="outStatusList != null and outStatusList.size() > 0">
                AND t.out_status  =any(#{outStatusList, typeHandler=StringListTypeHandler})
            </if>
            <if test="outDateStart != null and outDateEnd != null">
                AND t.out_date &gt;= #{outDateStart}::timestamp with time zone
                AND t.out_date &lt; #{outDateEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="makeTimeStart != null and makeTimeEnd != null">
                AND t.make_time &gt;= #{makeTimeStart}::timestamp with time zone
                AND t.make_time &lt; #{makeTimeEnd}::timestamp with time zone + interval '1 day'
            </if>
            <if test="flgValidList != null and flgValidList.size()>0">
                AND t.flg_valid =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
            <if test="viewVoidedDocuments !=null and viewVoidedDocuments == false">
                AND  t.flg_valid = true
            </if>
            <if test="flgValid != null">
                AND t.flg_valid = #{flgValid}
            </if>
        </where>
    </sql>

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

    <!-- 查询表dkic_b.t_psi_outbound，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT t.out_id,
               t.out_no,
               t.out_type,
               t.from_id,
               t.from_no,
               t.from_no                                         as "biznisNo",
               t.org_id,
               tmo.org_name                                      as "orgName",
               t.staff_id,
               tms.staff_name                                    as "staffName",
               t.cus_id,
               tmc.cus_code                                      as "cusCode",
               tmc.cus_name                                      as "cusName",
               tmc.cus_phone                                     as "cusPhone",
               tmc.cus_from                                      as "cusFrom",
               tmc.contact_phone                                 as "contactPhone",
               tmc.address_full                                  as "addressFull",
               tmdd.data_value                                   as cus_from_name,
               t.sup_id,
               tmp.sup_name                                      AS "supplierName",
               t.out_status,
               sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "outStatusName",
               sys.f_get_name_i18n(tdktype.kind_name_i18n, #{i18n}) AS  "outTypeName",
                sys.f_remove_zero(t.outing_qty) as outing_qty,
               t.outing_amt,
               CAST(t.outing_amt AS DECIMAL(10,2)) + CAST(t.out_amt AS DECIMAL(10,2))  AS "sumOutAmt",
                sys.f_remove_zero(CAST(t.outing_qty AS DECIMAL(10,6)) + CAST(t.out_qty AS DECIMAL(10,6)))  AS "sumOutQty",
        sys.f_remove_zero(t.out_qty) as out_qty,
               t.out_amt,
        sys.f_remove_zero(t.return_qty) as return_qty,
               t.return_amt,
               t.out_date,
               t.remarks,
               t.annex_paths,
               t.make_staff,
               makestaff.staff_name                              as "makeStaffName",
               t.make_time,
               t.receivable_id,
               t.cp_id,
               t.flg_valid,
              tmd.data_value                                   as out_reason_name
        FROM dkic_b.t_psi_outbound as t
                 left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
                 Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = t.sup_id
                 left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
                 left join dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
                 left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.cus_id
                 left join dkic_b.t_mst_dictionary_data tmdd on tmdd.data_id = tmc.cus_from
                 left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.out_status
                 LEFT JOIN sys.t_data_kind tdktype   ON tdktype.kind_code = t.out_type
                 left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = t.out_reason
        <include refid="Condition"/>
        order by t.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_outbound，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_outbound as t
        left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
        Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = t.sup_id
        left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
        left join dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
        left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.cus_id
        left join dkic_b.t_mst_dictionary_data tmdd on tmdd.data_id = tmc.cus_from
        left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.out_status
        LEFT JOIN sys.t_data_kind tdktype   ON tdktype.kind_code = t.out_type
        left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = t.out_reason
        <include refid="Condition"/>

    </select>

    <!-- 根据主键查询表dkic_b.t_psi_outbound的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT t.out_id,
               t.out_no,
               t.out_type,
               t.from_id,
               t.from_no,
               t.from_no                                         as "orderNo",
               t.org_id,
               tmo.org_name                                      as "orgName",
               t.staff_id,
               tms.staff_name                                    as "staffName",
               t.cus_id,
               tmc.cus_code                                      as "cusCode",
               tmc.cus_name                                      as "cusName",
               tmc.cus_phone                                     as "cusPhone",
               tmc.cus_from                                      as "cusFrom",
               tmc.contact_phone                                 as "contactPhone",
               tmc.address_full                                  as "addressFull",
               tmdd.data_value                                   as cus_from_name,
               tmc.address_area,
               tmc.address_name,
               tmc.address_no,
               tmc.address_gcj02,
               tmc.address_full,
               tmc.contact_name,
               tmc.contact_phone,
               tmc.channel_id                                    as sales_channel,
               tmc.channel_id                                    ,
               tmsc.channel_name                                 as channel_name,
               t.sup_id,
               t.out_status,
               sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "outStatusName",
               sys.f_remove_zero(t.outing_qty) as  outing_qty,
               t.outing_amt,
               sys.f_remove_zero(t.out_qty) as out_qty,
               t.out_amt,
               sys.f_remove_zero(t.return_qty) as  return_qty,
               t.return_amt,
               t.out_date,
               t.remarks,
               t.annex_paths,
               t.make_staff,
               makestaff.staff_name                              as "makeStaffName",
               t.make_time,
               t.receivable_id,
               t.cp_id,
               t.flg_valid,
               tmp.sup_name                                      AS "supplierName",
               t.out_reason,
               t.flg_auto_handle,
               t.flg_handle_setting,
               tmd.data_value                                    as out_reason_name
        FROM dkic_b.t_psi_outbound as t
                 left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.cus_id
                 left join dkic_b.t_mst_sale_channel tmsc on tmsc.channel_id = tmc.channel_id
                 left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
                 left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
                 left join dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
                 left join dkic_b.t_mst_dictionary_data tmdd on tmdd.data_id = tmc.cus_from
                 left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.out_status
                 left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = t.out_reason
                 Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = t.sup_id

        WHERE t.out_id = #{id}::uuid
    </select>

    <!-- 获取出库信息（新建退货用） -->
    <select id="selectByIdForReturn" resultMap="BaseResultMapResponse">
        SELECT t.out_id,
               t.out_no,
               t.out_type,
               t.from_id,
               t.from_no,
               t.from_no                                         as "orderNo",
               t.org_id,
               tmo.org_name                                      as "orgName",
               t.staff_id,
               tms.staff_name                                    as "staffName",
               t.cus_id,
               tmc.cus_code                                      as "cusCode",
               tmc.cus_name                                      as "cusName",
               tmc.cus_phone                                     as "cusPhone",
               tmc.cus_from                                      as "cusFrom",
               tmc.contact_phone                                 as "contactPhone",
               tmc.address_full                                  as "addressFull",
               tmdd.data_value                                   as cus_from_name,
               tmc.address_area,
               tmc.address_name,
               tmc.address_no,
               tmc.address_gcj02,
               tmc.address_full,
               tmc.contact_name,
               tmc.contact_phone,
               tmc.channel_id                                    as sales_channel,
               tmsc.channel_name                                 as channel_name,
               t.sup_id,
               t.out_status,
               sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "outStatusName",
               sys.f_remove_zero(t.outing_qty) as outing_qty,
               t.outing_amt,
               sys.f_remove_zero(t.out_qty) as  out_qty,
               t.out_amt,
               sys.f_remove_zero(t.return_qty) as  return_qty,
               t.return_amt,
               t.out_date,
               t.remarks,
               t.annex_paths,
               t.make_staff,
               makestaff.staff_name                              as "makeStaffName",
               t.make_time,
               t.receivable_id,
               t.cp_id,
               t.flg_valid,
               tmd.data_value                                    as out_reason_name
        FROM dkic_b.t_psi_outbound as t
                 left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.cus_id
                 left join dkic_b.t_mst_sale_channel tmsc on tmsc.channel_id = tmc.channel_id
                 left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
                 left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
                 left join dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
                 left join dkic_b.t_mst_dictionary_data tmdd on tmdd.data_id = tmc.cus_from
                 left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.out_status
                 left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = t.out_reason
        WHERE t.out_id = #{id}::uuid
    </select>

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

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

    <insert id="insertBatch">
        insert into dkic_b.t_psi_outbound
        (
        <trim suffixOverrides=",">
            out_no,
            out_type,
            from_id,
            from_no,
            org_id,
            staff_id,
            order_id,
            order_no,
            into_return_id,
            into_return_no,
            cus_id,
            sup_id,
            out_status,
            outing_qty,
            outing_amt,
            out_qty,
            out_amt,
            return_qty,
            return_amt,
            out_date,
            remarks,
            annex_paths,
            make_staff,
            make_time,
            receivable_id,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.outNo},
                #{item.outType},
                #{item.fromId}::uuid,
                #{item.fromNo},
                #{item.orgId}::uuid,
                #{item.staffId}::uuid,
                #{item.cusId}::uuid,
                #{item.supId}::uuid,
                #{item.outStatus},
                #{item.outingQty},
                #{item.outingAmt},
                #{item.outQty},
                #{item.outAmt},
                #{item.returnQty},
                #{item.returnAmt},
                #{item.outDate},
                #{item.remarks},
                #{item.annexPaths},
                #{item.makeStaff}::uuid,
                #{item.makeTime},
                #{item.receivableId}::uuid,
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>

    <!-- 根据id查询带出销售退货单数据（销售退货出库办理用）-->
    <select id="selectMessageByOtherQuery" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,tmo.org_name
        ,tms.staff_name                                  as "staffName"
        ,tmc.cus_code
        ,tmc.cus_name
        ,tmc.cus_phone
        ,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_outbound tpo
        Left join dkic_b.t_mst_org tmo on tmo.org_id = tpo.org_id
        Left join dkic_b.t_mst_staff tms on tms.staff_id = tpo.staff_id
        left join dkic_b.t_mst_customer tmc on tpo.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="outId != null and outId != ''">
                AND tpo.out_id = #{outId}::uuid
            </if>
            <if test="outStatus != null and outStatus != ''">
                AND tpo.out_status = #{outStatus}
            </if>
        </where>
    </select>


    <!--修改退货信息 -->
    <update id="updateReturnMessage" parameterType="com.dk.mdm.model.pojo.sale.OrderItem">
        update dkic_b.t_psi_outbound
        <set>
            <if test="returnQty != null">
                return_qty = return_qty + #{returnQty,jdbcType=NUMERIC},
            </if>
            <if test="returnAmt != null">
                return_amt = return_amt + #{returnAmt,jdbcType=NUMERIC},
            </if>
        </set>
        where out_id = #{outId,typeHandler=UuidTypeHandler}
        and  out_qty >= return_qty
    </update>


    <!-- 出库办理显示字段 -->
    <sql id="Base_Column_List_Response_Out">
        tpo.out_id  ,
        tpo.out_no  ,
        tpo.out_type ,
        tpo.from_id,
        tpo.from_no,
        tpo.org_id,
        tpo.staff_id,
        tpo.cus_id,
        tpo.sup_id,
        tpo.out_status,
        sys.f_remove_zero(tpo.outing_qty) as  outing_qty ,
        tpo.outing_amt,
        sys.f_remove_zero(tpo.out_qty) as   out_qty ,
        tpo.out_amt ,
        sys.f_remove_zero(tpo.return_qty) as  return_qty,
        tpo.return_amt,
        tpo.out_date ,
        tpo.remarks,
        tpo.annex_paths,
        tpo.make_staff,
        tpo.make_time,
        tpo.out_reason,
        tpo.flg_valid,
        tpo.flg_auto_handle,
        tpo.flg_handle_setting,
        tpo.op_update_time
    </sql>
    <sql id="Base_Column_List_Response_Into">
        tpi.into_id as out_id,
        tpi.into_no as out_no,
        tpi.into_type as out_type,
        tpi.from_id,
        tpi.from_no,
        tpi.org_id,
        tpi.staff_id,
        tpi.cus_id,
        tpi.sup_id,
        tpi.into_status as out_status ,
        sys.f_remove_zero(tpi.intoing_qty)   as outing_qty ,
        tpi.intoing_amt as outing_amt ,
        sys.f_remove_zero(tpi.into_qty)   as out_qty ,
        tpi.into_amt as out_amt,
        sys.f_remove_zero(tpi.return_qty) as  return_qty,
        tpi.return_amt,
        tpi.into_date as out_date,
        tpi.remarks,
        tpi.annex_paths,
        tpi.make_staff,
        tpi.make_time,
        tpi.into_reason as out_reason,
        tpi.flg_valid,
        tpi.flg_auto_handle,
        tpi.flg_handle_setting,
        tpi.op_update_time
    </sql>
    <!-- 条件查询 web端入库办理用,-->
    <select id="selectOutbound" resultMap="BaseResultMapResponse">
        select  * from (
        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",
        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  "outTypeName",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "outStatusName",
        tmd.data_value                                   as out_reason_name
        from dkic_b.t_psi_outbound tpo
        <include refid="Select_Inbound_Join_Table_Out"/>
        <where>
            AND tpo.cp_id = #{cpId}
            <if test="outTypes != null and outTypes.size()>0">
                AND tpo.out_type =any(#{outTypes,typeHandler=StringListTypeHandler})
            </if>
            <if test="outTypes == null || outTypes.size()==0">
                AND (tpo.out_type = '出库类型-其他出库'
                or 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="outNo != null and outNo != ''">
                AND tpo.out_no LIKE concat('%',my_ex.likequery(#{outNo}),'%')
            </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>
        union all
        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",
        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  "outTypeName",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "outStatusName",
        tmd.data_value                                   as out_reason_name
        FROM dkic_b.t_psi_inbound tpi
        <include refid="Select_Inbound_Join_Table_Into"/>
        <where>
            AND tpi.cp_id = #{cpId}
            <if test="outTypes != null and outTypes.size()>0">
                AND tpi.into_type =any(#{outTypes,typeHandler=StringListTypeHandler})
            </if>
            <if test="outTypes == null || outTypes.size()==0">
                AND 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="outNo != null and outNo != ''">
                AND tpi.into_no LIKE concat('%',my_ex.likequery(#{outNo}),'%')
            </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>
        ) tt order by tt.op_update_time desc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>
    <!--  条件查询 web端入库办理用个数 -->
    <select id="selectOutboundCond" resultType="Long">
        SELECT
        count(1) from (
        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",
        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  "outTypeName",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "outStatusName",
        tmd.data_value                                   as out_reason_name
        from dkic_b.t_psi_outbound tpo
        <include refid="Select_Inbound_Join_Table_Out"/>
        <where>
            AND tpo.cp_id = #{cpId}
            <if test="outTypes != null and outTypes.size()>0">
                AND tpo.out_type =any(#{outTypes,typeHandler=StringListTypeHandler})
            </if>
            <if test="outTypes == null || outTypes.size()==0">
                AND (tpo.out_type = '出库类型-其他出库'
                or 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="outNo != null and outNo != ''">
                AND tpo.out_no LIKE concat('%',my_ex.likequery(#{outNo}),'%')
            </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>
        union all
        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",
        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  "outTypeName",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "outStatusName",
        tmd.data_value                                   as out_reason_name
        FROM dkic_b.t_psi_inbound tpi
        <include refid="Select_Inbound_Join_Table_Into"/>
        <where>
            AND tpi.cp_id = #{cpId}
            <if test="outTypes != null and outTypes.size()>0">
                AND tpi.into_type =any(#{outTypes,typeHandler=StringListTypeHandler})
            </if>
            <if test="outTypes == null || outTypes.size()==0">
                AND 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="outNo != null and outNo != ''">
                AND tpi.into_no LIKE concat('%',my_ex.likequery(#{outNo}),'%')
            </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>
        ) tt
    </select>
    <!--出库办理显示出库关联表-->
    <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>
    <!--出库办理显示入库关联表-->
    <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_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>


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

    <!-- 查询总单带明细 -->
    <select id="selectByCondDetail" resultMap="BaseResultMapDetailResponse">
        SELECT t.out_id,
               t.out_no,
               t.out_type,
               t.from_id,
               t.from_no,
               t.from_no as "orderNo",
               t.org_id,
               tmo.org_name as "orgName",
               t.staff_id,
               tms.staff_name  as "staffName",
               t.cus_id,
               tmc.cus_code as "cusCode",
               tmc.cus_name  as "cusName",
               tmc.cus_phone   as "cusPhone",
               tmc.cus_from   as "cusFrom",
               tmc.contact_phone  as "contactPhone",
               tmc.address_full  as "addressFull",
               tmdd.data_value  as cus_from_name,
               tmc.address_area,
               tmc.address_name,
               tmc.address_no,
               tmc.address_gcj02,
               tmc.address_full,
               tmc.contact_name,
               tmc.contact_phone,
               tmc.channel_id  as sales_channel,
               tmsc.channel_name  as channel_name,
               t.sup_id,
               t.out_status,
               sys.f_get_name_i18n(tdk1.kind_name_i18n, #{i18n}) as "outStatusName",
        sys.f_remove_zero(t.outing_qty) as  outing_qty,
               t.outing_amt,
        sys.f_remove_zero(t.out_qty) as  out_qty,
               t.out_amt,
        sys.f_remove_zero(t.return_qty) as  return_qty,
               t.return_amt,
               t.out_date,
               t.remarks,
               t.annex_paths,
               t.make_staff,
               makestaff.staff_name as "makeStaffName",
               t.make_time,
               t.receivable_id,
               t.cp_id,
               t.flg_valid,
               tmp.sup_name AS "supplierName",
               t.out_reason,
               t.flg_auto_handle,
               t.flg_handle_setting,
               tmd.data_value as out_reason_name,
        <include refid="Base_Column_List_Item_Response_Detail_Join"/>
        FROM dkic_b.t_psi_outbound as t
                 left join dkic_b.t_mst_customer tmc on tmc.cus_id = t.cus_id
                 left join dkic_b.t_mst_sale_channel tmsc on tmsc.channel_id = tmc.channel_id
                 left join dkic_b.t_mst_org tmo on tmo.org_id = t.org_id
                 left join dkic_b.t_mst_staff tms on tms.staff_id = t.staff_id
                 left join dkic_b.t_mst_staff as makestaff on makestaff.staff_id = t.make_staff
                 left join dkic_b.t_mst_dictionary_data tmdd on tmdd.data_id = tmc.cus_from
                 left join sys.t_data_kind as tdk1 on tdk1.kind_code = t.out_status
                 left join dkic_b.t_mst_dictionary_data tmd on tmd.data_id = t.out_reason
                 Left join dkic_b.t_mst_supplier tmp on tmp.sup_id = t.sup_id
                left join  dkic_b.t_psi_outbound_item  tpoi  on tpoi.out_id = t.out_id
                left join dkic_b.t_psi_inventory tpi on tpi.inv_id = tpoi.inv_id
                left join dkic_b.t_mst_unit as tmgu on tpoi.unit_id = tmgu.unit_id
                left join dkic_b.t_mst_unit as tmgus on tpoi.sub_unit_id = tmgus.unit_id
        left join dkic_b.t_mst_goods_sku tmgs on tmgs.sku_id  = tpoi.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 tpity on tpity.inv_id  = tpoi.inv_id
        left join dkic_b.t_mst_warehouse as tmw on tpity.wh_id = tmw.wh_id
        <include refid="Condition"/>
        order by t.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_Detail_Join">
        tpoi.item_id  AS "list_item_id",
        tpoi.out_id  AS "list_out_id",
        t.out_no  AS "list_out_no",
                    tpoi.out_type  AS "list_out_type",
                    tpoi.from_id  AS "list_from_id",
                    tpoi.from_item_id  AS "list_from_item_id",
                    tpoi.item_index  AS "list_item_index",
        tpoi.sku_id  AS "list_sku_id",
                    tpoi.price_out  AS "list_price_out",
                    tpoi.non_std_code  AS "list_non_std_code",
                    tpoi.out_status  AS "list_out_status",
                    sys.f_remove_zero(tpoi.outing_qty)  AS "list_outing_qty",
                    tpoi.outing_amt  AS "list_outing_amt",
        sys.f_remove_zero(tpoi.out_qty)   AS "list_out_qty",
                    tpoi.out_amt  AS "list_out_amt",
                    sys.f_remove_zero(tpoi.return_qty)   AS "list_return_qty",
                    tpoi.return_amt  AS "list_return_amt",
                    tpoi.remarks  AS "list_remarks",
                    tpoi.inv_id  AS "list_inv_id",
        tpoi.cost_price  AS "list_cost_price",
                    tpoi.cost_amt  AS "list_cost_amt",
                    tpoi.flg_valid  AS "list_flg_valid",
                    tpoi.cp_id  AS "list_cp_id",
        tpoi.s_out_id  AS "list_s_out_id",
                    tpoi.s_out_item_id  AS "list_s_out_item_id",
                    tpoi.s_order_id  AS "list_s_order_id",
                    tpoi.s_order_item_id  AS "list_s_order_item_id",
                        tmgs.sku_code AS list_sku_code,
        tmgs.sku_model AS list_sku_model,
        tmgs.sku_name AS list_sku_name,
        tmgb.brand_name AS list_brand_name,
        tmgb.short_name AS list_short_name,
        tmgs.price_purchase AS list_price_purchase,
        tmgs.price_standard AS list_price_standard,
        tmgs.sku_images AS list_sku_images,
                      tpity.wh_id AS list_wh_id,
                      tmw.wh_name AS list_wh_name
               ,sys.f_remove_zero(tpi.inv_qty)     as "list_invQty"
               ,sys.f_remove_zero(tpi.usable_qty)    as "list_usableQty"
               ,tmgus.unit_name         as "list_subUnitName"
                ,tmgu.unit_name         as "list_unitName"
                ,tpoi.pack_box  as "list_pack_box"
                ,tmgs.flg_sub_unit       as "list_flgSubUnit"
                ,tpoi.outing_box  as "list_outing_box"
                ,tpoi.outing_piece  as "list_outing_piece"
                ,tpoi.out_box  as "list_out_box"
                ,tpoi.out_piece  as "list_out_piece"
,tmgu.decimal_places as "list_decimal_places"
    </sql>

    <select id="countByCondDetail" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_outbound as t
        <include refid="Condition"/>
    </select>

</mapper>
