Files
TimiServerAPI/src/main/resources/mapper/timi-server/gao/GaoSalesOrderMapper.xml
T
2026-09-20 15:40:03 +08:00

177 lines
8.9 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="storeId != null and storeId != ''">AND o.`store_id` = #{storeId}</if>
<if test="customerId != null and customerId != ''">AND o.`customer_id` = #{customerId}</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.* FROM `gao_sales_order` o LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id` WHERE <include refid="queryWhere"/>
ORDER BY o.`purchased_at` DESC, o.`created_at` DESC
LIMIT #{offset}, #{limit}
</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(o.`sale_amount` - o.`paid_amount`), 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"/>
</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>