<?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.sale.OrderItemMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        item_id
        , order_id, item_index, sku_id, sub_unit_id, unit_id,pack_box,box, piece,
        sys.f_remove_zero(item_qty) as item_qty, price_std, amt_std, price_sale, item_amt, price_discount, non_std_code, 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, remarks, flg_valid, cp_id, wh_id
    </sql>

    <sql id="Base_Column_List_Response">
        t.item_id, t.order_id, t.item_index, t.sku_id, sys.f_remove_zero(t.item_qty) as item_qty, t.price_std, t.amt_std, t.price_sale, t.item_amt, t.price_discount, t.non_std_code,
        t.out_status, 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.remarks, t.flg_valid, t.cp_id, t.wh_id
        ,t.box,t.piece,t.sub_unit_id,t.unit_id,t.pack_box
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.sale.OrderItem">
        <id column="item_id" property="itemId"/>
                <result column="order_id" property="orderId" typeHandler="UuidTypeHandler"/>
                <result column="item_index" property="itemIndex"/>
                <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
                <result column="unit_id" property="unitId" typeHandler="UuidTypeHandler"/>
                <result column="sub_unit_id" property="subUnitId" typeHandler="UuidTypeHandler"/>
                <result column="pack_box" property="packBox"/>
                <result column="box" property="box"/>
                <result column="piece" property="piece"/>
                <result column="item_qty" property="itemQty"/>
                <result column="price_std" property="priceStd"/>
                <result column="amt_std" property="amtStd"/>
                <result column="price_sale" property="priceSale"/>
                <result column="item_amt" property="itemAmt"/>
                <result column="price_discount" property="priceDiscount"/>
                <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="flg_valid" property="flgValid"/>
                <result column="cp_id" property="cpId"/>
                <result column="wh_id" property="whId"/>
        <result column="decimalPlaces" property="decimalPlaces"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.sale.OrderItemResponse">
        <id column="item_id" property="itemId"/>
                <result column="order_id" property="orderId" typeHandler="UuidTypeHandler"/>
                <result column="item_index" property="itemIndex"/>
                <result column="sku_id" property="skuId" typeHandler="UuidTypeHandler"/>
                <result column="unit_id" property="unitId" typeHandler="UuidTypeHandler"/>
                <result column="sub_unit_id" property="subUnitId" typeHandler="UuidTypeHandler"/>
                <result column="pack_box" property="packBox"/>
                <result column="item_qty" property="itemQty"/>
                <result column="price_std" property="priceStd"/>
                <result column="amt_std" property="amtStd"/>
                <result column="price_sale" property="priceSale"/>
                <result column="item_amt" property="itemAmt"/>
                <result column="price_discount" property="priceDiscount"/>
                <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="flg_valid" property="flgValid"/>
                <result column="cp_id" property="cpId"/>
                <result column="wh_id" property="whId"/>
                <result column="wh_name" property="whName"/>
                <result column="order_no" property="orderNo"/>
        <result column="brand_name" property="brandName"/>
        <result column="short_name" property="shortName"/>
        <result column="sku_images" property="skuImages" typeHandler="JsonTypeHandler"/>
                <result column="transferableQuantity" property="transferableQuantity"/>
                <result column="skuImages" property="skuImages" typeHandler="JsonTypeHandler"/>
        <result column="decimalPlaces" property="decimalPlaces"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="orderId != null and orderId != ''">
                AND t.order_id = #{orderId}::uuid
            </if>
            <if test="itemIndex != null">
                AND t.item_index = #{itemIndex}
            </if>
            <if test="skuId != null and skuId != ''">
                AND t.sku_id = #{skuId}
            </if>
            <if test="itemQty != null">
                AND t.item_qty = #{itemQty}
            </if>
            <if test="priceStd != null">
                AND t.price_std = #{priceStd}
            </if>
            <if test="amtStd != null">
                AND t.amt_std = #{amtStd}
            </if>
            <if test="priceSale != null">
                AND t.price_sale = #{priceSale}
            </if>
            <if test="itemAmt != null">
                AND t.item_amt = #{itemAmt}
            </if>
            <if test="priceDiscount != null">
                AND t.price_discount = #{priceDiscount}
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND t.non_std_code = #{nonStdCode}
            </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="flgValid != null">
                AND t.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND t.cp_id = #{cpId}
            </if>
        </where>
    </sql>

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

    <!-- 查询表t_psi_order_item，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT t.item_id,
               t.order_id,
               t.item_index,
               t.sku_id,
               tmgs.sku_code  as "skuCode",
               tmgs.sku_model as "skuModel",
               tmgs.sku_name  as "skuName",
               tmgs.sku_images,
               tmgb.brand_name,
               tmgb.short_name,
               dkic_b.f_box_piece(u2.unit_name,ul.unit_name,t.box,t.piece) AS "boxPiece",
               t.pack_box,
               t.box,
               t.piece,
        sys.f_remove_zero(t.item_qty) as item_qty,
               t.price_std,
               t.amt_std,
               t.price_sale,
               t.item_amt,
               t.price_discount,
               t.non_std_code,
               t.out_status,
        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.remarks,
               t.wh_id,
                tmw.wh_name
        ,ul.decimal_places    as "decimalPlaces"
        ,t.unit_id
        ,t.sub_unit_id
        ,ul.unit_name as "unitName"
        ,u2.unit_name as "subUnitName"
        FROM dkic_b.t_psi_order_item as t
                 inner join dkic_b.t_mst_goods_sku tmgs on t.sku_id = tmgs.sku_id
                 LEFT JOIN dkic_b.t_mst_unit AS ul ON tmgs.unit_id = ul.unit_id
                 LEFT JOIN dkic_b.t_mst_unit AS u2 ON tmgs.sub_unit_id = u2.unit_id
                 inner join dkic_b.t_mst_goods_brand tmgb on tmgb.brand_id = tmgs.brand_id
                 left join dkic_b.t_mst_warehouse tmw on tmw.wh_id = t.wh_id
        <include refid="Condition"/>
        order by t.item_index
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

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

    <!-- 根据主键查询表t_psi_order_item的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where dkic_b.t_psi_order_item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_order_item t
        WHERE t.item_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表t_psi_order_item的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where dkic_b.t_psi_order_item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_order_item
        WHERE item_id = #{id}::uuid
        for update
    </select>

    <!-- 根据主表主键锁定表t_psi_order_item的一行数据 -->
    <select id="selectByZIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where dkic_b.t_psi_order_item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_order_item
        WHERE order_id = #{id}::uuid
        for update
    </select>

    <!-- 根据主键锁定表t_psi_order_item的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where dkic_b.t_psi_order_item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_order_item
        <include refid="idsForeach"/>
        for update
    </select>

    <insert id="insertBatch">
        insert into dkic_b.t_psi_order_item
        (
        <trim suffixOverrides=",">
            order_id,
            item_index,
            sku_id,
            item_qty,
            price_std,
            amt_std,
            price_sale,
            item_amt,
            price_discount,
            non_std_code,
            out_status,
            outing_qty,
            outing_amt,
            out_qty,
            out_amt,
            return_qty,
            return_amt,
            remarks,
            cp_id,
            wh_id,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.orderId}::uuid,
                #{item.itemIndex},
                #{item.skuId}::uuid,
                #{item.itemQty},
                #{item.priceStd},
                #{item.amtStd},
                #{item.priceSale},
                #{item.itemAmt},
                #{item.priceDiscount},
                #{item.nonStdCode},
                #{item.outStatus},
                #{item.outingQty},
                #{item.outingAmt},
                #{item.outQty},
                #{item.outAmt},
                #{item.returnQty},
                #{item.returnAmt},
                #{item.remarks},
                #{item.cpId},
                #{item.whId},
            </trim>
            )
        </foreach>
    </insert>

    <delete id="deleteById">
        DELETE FROM dkic_b.t_psi_order_item
        WHERE item_id = #{id}::UUID;
    </delete>

    <!-- 根据条件进行查询（出库用） -->
    <select id="selectByCondForOut" resultMap="BaseResultMapResponse">
        SELECT t.item_id                                  as "fromItemId",
               t.order_id                                 as "fromId",
               t.item_index,
               t.sku_id,
               tmgs.sku_code                              as "skuCode",
               tmgs.sku_model                             as "skuModel",
               tmgs.sku_name                              as "skuName",
               tmgs.sku_images AS "skuImages",
               t.pack_box  ,
               sys.f_remove_zero(t.item_qty)              as "orderQty",
               t.box,
               t.piece,
               sys.f_remove_zero(t.item_qty - t.outing_qty - t.out_qty)      as "canOutingQty",
               sys.f_remove_zero(t.item_qty - t.outing_qty - t.out_qty)      as outing_qty,
               (t.item_qty - t.outing_qty - t.out_qty)/t.pack_box as "outingBox",
                (t.item_qty - t.outing_qty - t.out_qty)%t.pack_box as "outingPiece",
               t.price_sale                               as "priceOut",
               t.price_sale * (t.item_qty - t.outing_qty - t.out_qty) as outing_amt,
               t.non_std_code,
               t.remarks,
               t.wh_id,
               t.unit_id,
               t.sub_unit_id,
               tmw.wh_name,
               tpi.inv_id                                 as "invId",
               sys.f_remove_zero(tpi.inv_qty)                                as "invQty",
               sys.f_remove_zero(tpi.usable_qty)                             as "usableQty"
        ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where dkic_b.t_psi_order_item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_order_item as t
                 inner join dkic_b.t_mst_goods_sku tmgs on t.sku_id = tmgs.sku_id
                 left join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = t.wh_id
                 left join dkic_b.t_psi_inventory as tpi
                           on tpi.sku_id = t.sku_id and tpi.non_std_code = t.non_std_code
                               and tpi.wh_id = t.wh_id and tpi.cp_id = t.cp_id
        where t.flg_valid
          and t.item_qty > t.outing_qty
          and t.order_id = #{orderId}::uuid
        order by t.item_index
    </select>

    <!-- 根据条件进行查询（退货用） -->
    <select id="selectByCondForReturn" resultMap="BaseResultMapResponse">
        SELECT tpobi.out_id                                         as "sOutId",
               tpobi.item_id                                        as "sOutItemId",
               t.order_id                                           as "sOrderId",
               t.item_id                                            as "sOrderItemId",
               t.sku_id,
               tmgs.sku_code                                        as "skuCode",
               tmgs.sku_model                                       as "skuModel",
               tmgs.sku_name                                        as "skuName",
               tpobi.out_qty                                        as "outboundOutQty",
               sys.f_remove_zero(tpobi.out_qty - tpobi.return_qty)                     as "canReturnQty",
               - sys.f_remove_zero(tpobi.out_qty - tpobi.return_qty)                 as outing_qty,
               t.price_sale                                         as "priceOut",
               - t.price_sale * (tpobi.out_qty - tpobi.return_qty)  as outing_amt,
               tpobi.inv_id                                         as "invId",
               0                                                    as out_qty,
               0                                                    as out_amt,
               t.non_std_code,
               tpobi.remarks,
               t.wh_id,
               tmw.wh_name
                ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where dkic_b.t_psi_order_item.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_outbound_item as tpobi
                 inner join dkic_b.t_psi_order_item as t on t.item_id = tpobi.from_item_id
                 inner join dkic_b.t_mst_goods_sku tmgs on t.sku_id = tmgs.sku_id
                 left join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = t.wh_id
        where tpobi.flg_valid
          and tpobi.out_type = '出库类型-销售出库'
          and tpobi.out_qty > tpobi.return_qty
          and t.order_id = #{orderId}::uuid
        order by t.item_index
    </select>

    <!-- 根据id修改-->
    <update id="updateById" parameterType="com.dk.mdm.model.pojo.sale.OrderItem">
        update dkic_b.t_psi_order_item
        <set>
            <if test=" et!=null and et.itemIndex != null">
                item_index = #{et.itemIndex,jdbcType=INTEGER},
            </if>
            <if test=" et!=null and et.skuId != null">
                sku_id = #{et.skuId,typeHandler=UuidTypeHandler},
            </if>
            <if test=" et!=null and et.itemQty != null">
                item_qty = #{et.itemQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.priceStd != null">
                price_std = #{et.priceStd,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.amtStd != null">
                amt_std = #{et.amtStd,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.priceSale != null">
                price_sale = #{et.priceSale,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.itemAmt != null">
                item_amt = #{et.itemAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.priceDiscount != null">
                price_discount = #{et.priceDiscount,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.nonStdCode != null">
                non_std_code = #{et.nonStdCode,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.outStatus != null">
                out_status = #{et.outStatus,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.outingQty != null">
                outing_qty = outing_qty + #{et.outingQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.outingAmt != null">
                outing_amt = outing_amt + #{et.outingAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.outQty != null">
                out_qty = out_qty + #{et.outQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.outAmt != null">
                out_amt = out_amt + #{et.outAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.returnQty != null">
                return_qty = #{et.returnQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.returnAmt != null">
                return_amt = #{et.returnAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.remarks != null">
                remarks = #{et.remarks,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.flgValid != null">
                flg_valid = #{et.flgValid,jdbcType=BOOLEAN},
            </if>
            <if test=" et!=null and et.cpId != null">
                cp_id = #{et.cpId,jdbcType=INTEGER},
            </if>
        </set>
        where item_id = #{et.itemId,typeHandler=UuidTypeHandler}
        and item_qty >=(out_qty + outing_qty)
    </update>

    <!--修改退货信息 -->
    <update id="updateReturnMessage" parameterType="com.dk.mdm.model.pojo.sale.OrderItem">
        update dkic_b.t_psi_order_item
        <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 item_id = #{itemId,typeHandler=UuidTypeHandler}
        and  item_qty >= return_qty
    </update>

    <!-- 通用条件列 -->
    <sql id="Condition_pur">
        <where>
            <if test="orderNo != null">
                AND tpo.order_no like concat('%', my_ex.likequery(#{orderNo}) , '%')
            </if>
            <if test="orderId != null and orderId != ''">
                AND t.order_id = #{orderId}::uuid
            </if>
            <if test="itemIndex != null">
                AND t.item_index = #{itemIndex}
            </if>
            <if test="skuId != null and skuId != ''">
                AND t.sku_id = #{skuId}
            </if>
            <if test="itemQty != null">
                AND t.item_qty = #{itemQty}
            </if>
            <if test="priceStd != null">
                AND t.price_std = #{priceStd}
            </if>
            <if test="amtStd != null">
                AND t.amt_std = #{amtStd}
            </if>
            <if test="priceSale != null">
                AND t.price_sale = #{priceSale}
            </if>
            <if test="itemAmt != null">
                AND t.item_amt = #{itemAmt}
            </if>
            <if test="priceDiscount != null">
                AND t.price_discount = #{priceDiscount}
            </if>
            <if test="nonStdCode != null and nonStdCode != ''">
                AND t.non_std_code = #{nonStdCode}
            </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="flgValid != null">
                AND t.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND t.cp_id = #{cpId}
            </if>
        </where>
    </sql>

    <!-- 查询表t_psi_order_item，（条件查询+分页）列表 -->
    <select id="purchaseBasedOnSaleSelectByCond" resultMap="BaseResultMapResponse">
        SELECT t.item_id,
        t.order_id,
        tpo.order_no,
        t.item_index,
        t.sku_id,
        tmgs.sku_code  as "skuCode",
        tmgs.sku_model as "skuModel",
        tmgs.sku_name  as "skuName",
        sys.f_remove_zero(t.item_qty) as item_qty,
        t.price_std,
        t.amt_std,
        t.price_sale,
        t.item_amt,
        t.price_discount,
        t.non_std_code,
        t.out_status,
        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.remarks,
        t.item_qty - coalesce(sumQty,0) as "transferableQuantity"
        ,(select ul.decimal_places from  dkic_b.t_mst_unit ul where t.unit_id = ul.unit_id) as "decimalPlaces"
        FROM dkic_b.t_psi_order_item as t
        left join dkic_b.t_psi_order as tpo on tpo.order_id = t.order_id
        inner join dkic_b.t_mst_goods_sku tmgs on t.sku_id = tmgs.sku_id
        left join(select from_item_id,sum(item_qty) as sumQty from dkic_b.t_psi_purchase_item
            group by from_item_id) as pi on pi.from_item_id = t.item_id
        <where>
            t.item_qty - coalesce(sumQty,0) > 0
            <if test="orderNo != null">
                AND tpo.order_no like concat('%', my_ex.likequery(#{orderNo}) , '%')
            </if>
        </where>
        order by t.item_id
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_psi_order_item，（条件查询+分页）列表 -->
    <select id="purchaseBasedOnSaleContByCond" resultType="Long">
        SELECT count(1)
        FROM dkic_b.t_psi_order_item as t
        left join dkic_b.t_psi_order as tpo on tpo.order_id = t.order_id
        inner join dkic_b.t_mst_goods_sku tmgs on t.sku_id = tmgs.sku_id
        left join(select from_item_id,sum(item_qty) as sumQty from dkic_b.t_psi_purchase_item
        group by from_item_id) as pi on pi.from_item_id = t.item_id
        <where>
            t.item_qty - coalesce(sumQty,0) > 0
            <if test="orderNo != null">
                AND tpo.order_no like concat('%', my_ex.likequery(#{orderNo}) , '%')
            </if>
        </where>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>
</mapper>
