Files
TimiServerAPI/src/main/resources/mapper/timi-server/gao/GaoSalesOrderMapper.xml
T

241 lines
13 KiB
XML

<?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.imyeyu.api.modules.gao.mapper.GaoSalesOrderMapper">
<select id="selectByRequestNo" resultType="com.imyeyu.api.modules.gao.entity.GaoSalesOrder">
SELECT * FROM `gao_sales_order`
WHERE `store_id` = #{storeId} AND `request_no` = #{requestNo} AND `deleted_at` IS NULL
LIMIT 1
</select>
<select id="selectForUpdate" resultType="com.imyeyu.api.modules.gao.entity.GaoSalesOrder">
SELECT * FROM `gao_sales_order`
WHERE `id` = #{id} AND `deleted_at` IS NULL
FOR UPDATE
</select>
<select id="selectByIdList" resultType="com.imyeyu.api.modules.gao.entity.GaoSalesOrder">
SELECT * FROM `gao_sales_order`
WHERE `id` IN
<foreach collection="idList" item="id" separator="," open="(" close=")">
#{id}
</foreach>
AND `deleted_at` IS NULL
</select>
<sql id="queryWhere">
o.`deleted_at` IS NULL
<if test="orderAllowed != null and !orderAllowed">AND o.`order_type` = 'RECYCLE'</if>
<if test="recycleAllowed != null and !recycleAllowed">AND o.`order_type` &lt;&gt; 'RECYCLE'</if>
<if test="storeId != null and storeId != ''">AND o.`store_id` = #{storeId}</if>
<if test="customerId != null and customerId != ''">AND o.`customer_id` = #{customerId}</if>
<if test="fulfillableSkuId != null and fulfillableSkuId != ''">
AND o.`status` &lt;&gt; 'CANCELLED'
AND EXISTS (
SELECT 1 FROM `gao_sales_order_item` i
WHERE i.`sales_order_id` = o.`id` AND i.`deleted_at` IS NULL
AND i.`sku_id` = #{fulfillableSkuId}
AND COALESCE(i.`fulfilled_base_quantity`, 0) + COALESCE(i.`reserved_base_quantity`, 0) &lt; COALESCE(i.`base_quantity`, 0)
)
</if>
<if test="packageConfigId != null and packageConfigId != ''">
AND EXISTS (
SELECT 1 FROM `gao_customer_package` cp
WHERE cp.`sales_order_id` = o.`id`
AND cp.`package_config_id` = #{packageConfigId}
AND cp.`deleted_at` IS NULL
)
</if>
<choose>
<when test="orderTypeList != null and 0 &lt; orderTypeList.size()">
AND o.`order_type` IN
<foreach collection="orderTypeList" item="orderTypeItem" open="(" separator="," close=")">
#{orderTypeItem}
</foreach>
</when>
<when test="orderType != null">AND o.`order_type` = #{orderType}</when>
</choose>
<choose>
<when test="statusList != null and 0 &lt; statusList.size()">
AND o.`status` IN
<foreach collection="statusList" item="statusItem" open="(" separator="," close=")">
#{statusItem}
</foreach>
</when>
<when test="status != null">AND o.`status` = #{status}</when>
</choose>
<choose>
<when test="paymentStatusList != null and 0 &lt; paymentStatusList.size()">
AND o.`payment_status` IN
<foreach collection="paymentStatusList" item="paymentStatusItem" open="(" separator="," close=")">
#{paymentStatusItem}
</foreach>
</when>
<when test="paymentStatus != null">AND o.`payment_status` = #{paymentStatus}</when>
</choose>
<choose>
<when test="refundStatusList != null and 0 &lt; refundStatusList.size()">
AND o.`refund_status` IN
<foreach collection="refundStatusList" item="refundStatusItem" open="(" separator="," close=")">
#{refundStatusItem}
</foreach>
</when>
<when test="refundStatus != null">AND o.`refund_status` = #{refundStatus}</when>
</choose>
<if test="pendingReceive != null and pendingReceive">AND o.`status` IN ('ACTIVE', 'FULFILLED') AND o.`payment_status` IN ('UNPAID', 'PARTIALLY_PAID')</if>
<if test="fulfilledButUnpaid != null and fulfilledButUnpaid">AND o.`status` = 'FULFILLED' AND o.`paid_amount` &lt; o.`sale_amount`</if>
<if test="keyword != null and keyword.trim() != ''">AND (o.`order_no` LIKE CONCAT('%', #{keyword}, '%') OR c.`name` LIKE CONCAT('%', #{keyword}, '%') OR c.`code` LIKE CONCAT('%', #{keyword}, '%'))</if>
<if test="beginAt != null">AND o.`purchased_at` &gt;= #{beginAt}</if>
<if test="endAt != null">AND o.`purchased_at` &lt;= #{endAt}</if>
</sql>
<select id="countByQuery" resultType="long">
SELECT COUNT(1) FROM `gao_sales_order` o LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id` WHERE <include refid="queryWhere"/>
</select>
<select id="selectByQuery" resultType="com.imyeyu.api.modules.gao.entity.GaoSalesOrder">
SELECT o.*,
(SELECT SUM(p.`quantity`) FROM `gao_recycle_payment` p
WHERE p.`order_id` = o.`id` AND p.`status` = 'PENDING') AS `recyclePendingQuantity`,
<!-- 回收物名称与单位取兑换单创建时的快照,回收物改名后历史订单仍显示当时的名称 -->
e.`id` AS `recycleExchangeId`,
e.`recycle_name_snapshot` AS `recycleItemName`,
e.`recycle_unit_snapshot` AS `recycleUnit`
FROM `gao_sales_order` o
LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id`
LEFT JOIN `gao_recycle_exchange` e ON e.`sales_order_id` = o.`id`
WHERE <include refid="queryWhere"/>
ORDER BY o.`purchased_at` DESC, o.`created_at` DESC
LIMIT #{offset}, #{limit}
</select>
<select id="selectQuickFulfillmentSourceRows" resultType="com.imyeyu.api.modules.gao.vo.GaoQuickFulfillmentSourceRow">
SELECT o.`id` AS `salesOrderId`, o.`order_no` AS `orderNo`, o.`order_type` AS `orderType`,
o.`payment_status` AS `paymentStatus`,
o.`recycle_required_quantity` AS `recycleRequiredQuantity`,
(SELECT COALESCE(SUM(rp.`quantity`), 0) FROM `gao_recycle_payment` rp WHERE rp.`order_id` = o.`id` AND rp.`status` = 'VERIFIED' AND rp.`source_payment_id` IS NULL) AS `recycleVerifiedQuantity`,
(SELECT e.`recycle_name_snapshot` FROM `gao_recycle_exchange` e WHERE e.`sales_order_id` = o.`id` LIMIT 1) AS `recycleItemName`,
(SELECT e.`recycle_unit_snapshot` FROM `gao_recycle_exchange` e WHERE e.`sales_order_id` = o.`id` LIMIT 1) AS `recycleUnit`,
o.`created_at` AS `orderCreatedAt`,
(SELECT cp.`package_name_snapshot` FROM `gao_customer_package` cp
WHERE cp.`sales_order_id` = o.`id` AND cp.`deleted_at` IS NULL LIMIT 1) AS `packageName`,
COALESCE(o.`sale_amount`, 0) AS `saleAmount`,
COALESCE(o.`paid_amount`, 0) AS `paidAmount`,
GREATEST(COALESCE(o.`sale_amount`, 0) - COALESCE(o.`paid_amount`, 0), 0) AS `unpaidAmount`,
i.`id` AS `sourceOrderItemId`, i.`sku_id` AS `skuId`,
p.`name` AS `productName`,
i.`product_name_snapshot` AS `productNameSnapshot`, i.`sku_name_snapshot` AS `skuNameSnapshot`,
COALESCE(i.`base_quantity`, 0) - COALESCE(i.`reserved_base_quantity`, 0) - COALESCE(i.`fulfilled_base_quantity`, 0) AS `availableBaseQuantity`
FROM `gao_sales_order_item` i
INNER JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id` AND o.`deleted_at` IS NULL
INNER JOIN `gao_product_sku` s ON s.`id` = i.`sku_id` AND s.`deleted_at` IS NULL
INNER JOIN `gao_product` p ON p.`id` = s.`product_id` AND p.`deleted_at` IS NULL
WHERE i.`deleted_at` IS NULL AND o.`store_id` = #{storeId} AND o.`customer_id` = #{customerId}
AND o.`status` &lt;&gt; 'CANCELLED'
AND ((o.`order_type` = 'RECYCLE' AND i.`item_type` = 'RECYCLE_ENTITLEMENT'
AND (#{allowUnsettledRecycle} OR (o.`recycle_required_quantity` IS NOT NULL AND
(SELECT COALESCE(SUM(rp.`quantity`), 0) FROM `gao_recycle_payment` rp WHERE rp.`order_id` = o.`id` AND rp.`status` = 'VERIFIED' AND rp.`source_payment_id` IS NULL) &gt;= o.`recycle_required_quantity`)))
OR ((#{allowUnpaid} OR o.`sale_amount` = 0 OR o.`paid_amount` = o.`sale_amount`)
AND ((o.`order_type` = 'PACKAGE' AND i.`item_type` = 'PACKAGE_ENTITLEMENT')
OR (o.`order_type` = 'DIRECT_PRODUCT' AND i.`item_type` = 'DIRECT_PRODUCT'))))
AND COALESCE(i.`base_quantity`, 0) - COALESCE(i.`reserved_base_quantity`, 0) - COALESCE(i.`fulfilled_base_quantity`, 0) &gt; 0
<if test="skuIdList != null and skuIdList.size() &gt; 0">
AND i.`sku_id` IN
<foreach collection="skuIdList" item="skuId" open="(" separator="," close=")">#{skuId}</foreach>
</if>
<if test="salesOrderIdList != null and salesOrderIdList.size() &gt; 0">
AND o.`id` IN
<foreach collection="salesOrderIdList" item="salesOrderId" open="(" separator="," close=")">#{salesOrderId}</foreach>
</if>
ORDER BY o.`purchased_at` ASC, o.`created_at` ASC, o.`id` ASC, i.`sort` ASC, i.`created_at` ASC, i.`id` ASC
</select>
<select id="selectSummaryByQuery" resultType="com.imyeyu.api.modules.gao.vo.GaoSalesOrderReceivableStatView">
SELECT
COUNT(1) AS `orderCount`,
COALESCE(SUM(o.`sale_amount`), 0) AS `saleAmount`,
COALESCE(SUM(o.`paid_amount`), 0) AS `paidAmount`,
COALESCE(SUM(o.`pending_paid_amount`), 0) AS `pendingPaidAmount`,
COALESCE(SUM(GREATEST(o.`sale_amount` - o.`paid_amount`, 0)), 0) AS `unpaidAmount`,
COALESCE(SUM(o.`refunded_amount`), 0) AS `refundedAmount`,
COALESCE(SUM(o.`paid_amount` - o.`refunded_amount`), 0) AS `netPaidAmount`
FROM `gao_sales_order` o LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id`
WHERE <include refid="queryWhere"/>
<if test="(statusList == null or statusList.size() == 0) and status == null">
AND o.`status` &lt;&gt; 'CANCELLED'
</if>
</select>
<sql id="customerProductWhere">
i.`deleted_at` IS NULL
AND o.`deleted_at` IS NULL
AND o.`store_id` = #{storeId}
AND o.`status` &lt;&gt; 'CANCELLED'
<if test="customerId != null and customerId != ''">AND o.`customer_id` = #{customerId}</if>
<if test="customerIdList != null and customerIdList.size() &gt; 0">
AND o.`customer_id` IN
<foreach collection="customerIdList" item="customerIdItem" open="(" separator="," close=")">
#{customerIdItem}
</foreach>
</if>
<if test="itemType != null">AND i.`item_type` = #{itemType}</if>
<if test="keyword != null and keyword.trim() != ''">
AND (
c.`name` LIKE CONCAT('%', #{keyword}, '%')
OR c.`code` LIKE CONCAT('%', #{keyword}, '%')
OR o.`order_no` LIKE CONCAT('%', #{keyword}, '%')
OR i.`product_name_snapshot` LIKE CONCAT('%', #{keyword}, '%')
OR i.`sku_name_snapshot` LIKE CONCAT('%', #{keyword}, '%')
OR i.`sku_code_snapshot` LIKE CONCAT('%', #{keyword}, '%')
OR i.`spec_summary_snapshot` LIKE CONCAT('%', #{keyword}, '%')
)
</if>
</sql>
<select id="countCustomerProductCustomerByQuery" resultType="long">
SELECT COUNT(DISTINCT o.`customer_id`)
FROM `gao_sales_order_item` i
JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id`
LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id`
WHERE <include refid="customerProductWhere"/>
</select>
<select id="selectCustomerProductCustomerByQuery" resultType="com.imyeyu.api.modules.gao.vo.GaoCustomerProductCustomerRow">
SELECT o.`customer_id`, c.`code` AS `customer_code`, c.`name` AS `customer_name`, COUNT(DISTINCT o.`id`) AS `order_count`
FROM `gao_sales_order_item` i
JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id`
LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id`
WHERE <include refid="customerProductWhere"/>
GROUP BY o.`customer_id`, c.`code`, c.`name`
ORDER BY c.`name` ASC, c.`code` ASC, o.`customer_id` ASC
LIMIT #{offset}, #{limit}
</select>
<select id="selectCustomerProductByQuery" resultType="com.imyeyu.api.modules.gao.vo.GaoCustomerProductView">
SELECT i.`id`, i.`sales_order_id`, o.`customer_id`, c.`code` AS `customer_code`, c.`name` AS `customer_name`,
o.`order_no`, o.`order_type`, o.`status` AS `order_status`, o.`purchased_at`,
i.`item_type`, i.`gifted`, i.`sku_id`, i.`sku_unit_id`, i.`product_name_snapshot`, i.`product_id_snapshot`,
i.`sku_code_snapshot`, i.`sku_name_snapshot`, i.`spec_summary_snapshot`, i.`unit_snapshot`, base_unit.`name` AS `base_unit_name`,
i.`base_quantity_per_unit_snapshot`, i.`quantity`, i.`base_quantity`, i.`reserved_base_quantity`,
i.`fulfilled_base_quantity`,
COALESCE(i.`base_quantity`, 0) - COALESCE(i.`fulfilled_base_quantity`, 0) AS `remaining_base_quantity`,
COALESCE(i.`base_quantity`, 0) - COALESCE(i.`reserved_base_quantity`, 0) - COALESCE(i.`fulfilled_base_quantity`, 0) AS `available_base_quantity`
FROM `gao_sales_order_item` i
JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id`
LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id`
LEFT JOIN `gao_product_sku_unit` base_unit ON base_unit.`sku_id` = i.`sku_id` AND base_unit.`is_base` = 1 AND base_unit.`deleted_at` IS NULL
WHERE <include refid="customerProductWhere"/>
ORDER BY o.`purchased_at` DESC, o.`created_at` DESC, i.`sort` ASC, i.`created_at` ASC
</select>
<update id="updatePayment">
UPDATE `gao_sales_order`
SET `pending_paid_amount` = #{pendingPaidAmount}, `paid_amount` = #{paidAmount}, `paid_at` = #{paidAt}, `refunded_amount` = #{refundedAmount},
`payment_status` = #{paymentStatus}, `refund_status` = #{refundStatus}, `updated_at` = FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000)
WHERE `id` = #{id} AND `deleted_at` IS NULL
</update>
<update id="cancelIf">
UPDATE `gao_sales_order`
SET `status` = #{toStatus}, `cancelled_at` = #{cancelledAt}, `cancel_operator_user_id` = #{cancelOperatorUserId},
`cancel_reason` = #{cancelReason}, `refund_status` = #{refundStatus}, `updated_at` = FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000)
WHERE `id` = #{id} AND `status` = #{fromStatus} AND `deleted_at` IS NULL
</update>
<update id="updateStatus">
UPDATE `gao_sales_order`
SET `status` = #{status}, `updated_at` = FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000)
WHERE `id` = #{id} AND `deleted_at` IS NULL
</update>
<update id="updateRemark">
UPDATE `gao_sales_order`
SET `remark` = #{remark}, `updated_at` = FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000)
WHERE `id` = #{id} AND `deleted_at` IS NULL
</update>
</mapper>