bianjunhui 6 дней назад
Родитель
Сommit
e5551baa20

+ 47 - 26
packages/Longyi/RewardPoints/src/Config/TransactionType.php

@@ -4,7 +4,7 @@ namespace Longyi\RewardPoints\Config;
 
 
 /**
 /**
  * 积分交易类型配置类
  * 积分交易类型配置类
- * 
+ *
  * 统一管理所有积分交易类型,避免硬编码
  * 统一管理所有积分交易类型,避免硬编码
  * 新增类型只需在此类中添加,无需修改其他代码
  * 新增类型只需在此类中添加,无需修改其他代码
  */
  */
@@ -14,65 +14,70 @@ class TransactionType
      * 签到
      * 签到
      */
      */
     const SIGN_IN = 1;
     const SIGN_IN = 1;
-    
+
     /**
     /**
      * 注册
      * 注册
      */
      */
     const REGISTRATION = 2;
     const REGISTRATION = 2;
-    
+
     /**
     /**
      * 订单
      * 订单
      */
      */
     const ORDER = 3;
     const ORDER = 3;
-    
+
     /**
     /**
      * 评价
      * 评价
      */
      */
     const REVIEW = 4;
     const REVIEW = 4;
-    
+
     /**
     /**
      * 推荐
      * 推荐
      */
      */
     const REFERRAL = 5;
     const REFERRAL = 5;
-    
+
     /**
     /**
      * 生日
      * 生日
      */
      */
     const BIRTHDAY = 6;
     const BIRTHDAY = 6;
-    
+
     /**
     /**
      * 分享
      * 分享
      */
      */
     const SHARE = 7;
     const SHARE = 7;
-    
+
     /**
     /**
      * 订阅
      * 订阅
      */
      */
     const SUBSCRIBE = 8;
     const SUBSCRIBE = 8;
-    
+
     /**
     /**
      * 登录
      * 登录
      */
      */
     const LOGIN = 9;
     const LOGIN = 9;
-    
+
     /**
     /**
      * 过期积分
      * 过期积分
      */
      */
     const EXPIRED = 10;
     const EXPIRED = 10;
-    
+
     /**
     /**
      * 兑换礼品卡
      * 兑换礼品卡
      */
      */
     const GIFT_CARD_REDEEM = 11;
     const GIFT_CARD_REDEEM = 11;
-    
+
     /**
     /**
      * 后台管理员修改
      * 后台管理员修改
      */
      */
     const ADMIN_ACTION = 99;
     const ADMIN_ACTION = 99;
 
 
+    /**
+     * 取消订单退回积分
+     */
+    const ORDER_CANCEL_REFUND = 12;
+
     /**
     /**
      * 获取所有类型配置
      * 获取所有类型配置
-     * 
+     *
      * @return array [type_id => ['code' => string, 'name' => string, 'description' => string]]
      * @return array [type_id => ['code' => string, 'name' => string, 'description' => string]]
      */
      */
     public static function all(): array
     public static function all(): array
@@ -162,27 +167,43 @@ class TransactionType
                 'icon' => 'icon-setting',
                 'icon' => 'icon-setting',
                 'color' => 'gray',
                 'color' => 'gray',
             ],
             ],
+            self::ORDER_CANCEL_REFUND => [
+                'code' => 'order_cancel_refund',
+                'name' => '取消订单退回',
+                'description' => '订单取消后退回积分',
+                'icon' => 'icon-undo',
+                'color' => 'cyan',
+            ],
         ];
         ];
     }
     }
-    
+
     /**
     /**
      * 获取可用于规则配置的类型(排除管理员操作、过期、兑换等特殊类型)
      * 获取可用于规则配置的类型(排除管理员操作、过期、兑换等特殊类型)
-     * 
+     *
      * @return array
      * @return array
      */
      */
     public static function getRuleTypes(): array
     public static function getRuleTypes(): array
     {
     {
         $all = self::all();
         $all = self::all();
-        // 排除特殊类型:管理员操作(99)、过期(10)、兑换礼品卡(11)
-        unset($all[self::ADMIN_ACTION]);
-        unset($all[self::EXPIRED]);
-        unset($all[self::GIFT_CARD_REDEEM]);
+
+        // 排除特殊类型:管理员操作(99)、过期(10)、兑换礼品卡(11)、取消订单退回(12)
+        $excludedTypes = [
+            self::ADMIN_ACTION,
+            self::EXPIRED,
+            self::GIFT_CARD_REDEEM,
+            self::ORDER_CANCEL_REFUND,
+        ];
+
+        foreach ($excludedTypes as $typeId) {
+            unset($all[$typeId]);
+        }
+
         return $all;
         return $all;
     }
     }
 
 
     /**
     /**
      * 获取单个类型信息
      * 获取单个类型信息
-     * 
+     *
      * @param int $typeId
      * @param int $typeId
      * @return array|null
      * @return array|null
      */
      */
@@ -194,7 +215,7 @@ class TransactionType
 
 
     /**
     /**
      * 根据 code 获取类型 ID
      * 根据 code 获取类型 ID
-     * 
+     *
      * @param string $code
      * @param string $code
      * @return int|null
      * @return int|null
      */
      */
@@ -210,7 +231,7 @@ class TransactionType
 
 
     /**
     /**
      * 获取类型名称
      * 获取类型名称
-     * 
+     *
      * @param int $typeId
      * @param int $typeId
      * @return string
      * @return string
      */
      */
@@ -222,7 +243,7 @@ class TransactionType
 
 
     /**
     /**
      * 获取类型代码
      * 获取类型代码
-     * 
+     *
      * @param int $typeId
      * @param int $typeId
      * @return string
      * @return string
      */
      */
@@ -234,7 +255,7 @@ class TransactionType
 
 
     /**
     /**
      * 检查类型是否存在
      * 检查类型是否存在
-     * 
+     *
      * @param int $typeId
      * @param int $typeId
      * @return bool
      * @return bool
      */
      */
@@ -245,7 +266,7 @@ class TransactionType
 
 
     /**
     /**
      * 获取所有类型选项(用于下拉选择)
      * 获取所有类型选项(用于下拉选择)
-     * 
+     *
      * @return array [type_id => name]
      * @return array [type_id => name]
      */
      */
     public static function options(): array
     public static function options(): array
@@ -259,7 +280,7 @@ class TransactionType
 
 
     /**
     /**
      * 获取所有类型代码列表
      * 获取所有类型代码列表
-     * 
+     *
      * @return array
      * @return array
      */
      */
     public static function codes(): array
     public static function codes(): array

+ 3 - 2
packages/Longyi/RewardPoints/src/Listeners/OrderEvents.php

@@ -136,8 +136,9 @@ class OrderEvents
             $this->rewardPointRepository->deductPoints(
             $this->rewardPointRepository->deductPoints(
                 $order->customer_id,
                 $order->customer_id,
                 $history->point_remaining,
                 $history->point_remaining,
-                $order->id,
-                "Points deducted due to order cancellation #{$order->increment_id}"
+                RewardActiveRule::TYPE_ORDER_CANCEL_REFUND,
+                "Points refunded due to order cancellation #{$order->increment_id}",
+                $order->id
             );
             );
         }
         }
 
 

+ 2 - 1
packages/Longyi/RewardPoints/src/Models/RewardActiveRule.php

@@ -21,6 +21,7 @@ class RewardActiveRule extends Model
     const TYPE_EXPIRED = TransactionType::EXPIRED;
     const TYPE_EXPIRED = TransactionType::EXPIRED;
     const TYPE_GIFT_CARD_REDEEM = TransactionType::GIFT_CARD_REDEEM;
     const TYPE_GIFT_CARD_REDEEM = TransactionType::GIFT_CARD_REDEEM;
     const TYPE_ADMIN_ACTION = TransactionType::ADMIN_ACTION;
     const TYPE_ADMIN_ACTION = TransactionType::ADMIN_ACTION;
+    const TYPE_ORDER_CANCEL_REFUND = TransactionType::ORDER_CANCEL_REFUND;
 
 
     protected $table = 'mw_reward_active_rules';
     protected $table = 'mw_reward_active_rules';
     protected $primaryKey = 'rule_id';
     protected $primaryKey = 'rule_id';
@@ -190,7 +191,7 @@ class RewardActiveRule extends Model
     {
     {
         return TransactionType::getName($this->type_of_transaction);
         return TransactionType::getName($this->type_of_transaction);
     }
     }
-    
+
     /**
     /**
      * 获取交易类型代码
      * 获取交易类型代码
      */
      */

+ 2 - 2
packages/Longyi/RewardPoints/src/Repositories/RewardPointRepository.php

@@ -207,7 +207,7 @@ class RewardPointRepository extends Repository
     public function deductPoints($customerId, $amount, $type = 0, $detail = null, $orderId = null)
     public function deductPoints($customerId, $amount, $type = 0, $detail = null, $orderId = null)
     {
     {
         try {
         try {
-            return DB::transaction(function () use ($customerId, $amount, $orderId, $detail) {
+            return DB::transaction(function () use ($customerId, $amount, $orderId, $detail,$type) {
                 $customerPoints = RewardPointCustomer::where('customer_id', $customerId)
                 $customerPoints = RewardPointCustomer::where('customer_id', $customerId)
                     ->lockForUpdate()
                     ->lockForUpdate()
                     ->first();
                     ->first();
@@ -297,7 +297,7 @@ class RewardPointRepository extends Repository
 
 
                     if ($customerPoints && $customerPoints->mw_reward_point >= $history->point_remaining) {
                     if ($customerPoints && $customerPoints->mw_reward_point >= $history->point_remaining) {
                         $expiredAmount = $history->point_remaining;
                         $expiredAmount = $history->point_remaining;
-                        
+
                         // 扣除用户总积分
                         // 扣除用户总积分
                         $customerPoints->mw_reward_point -= $expiredAmount;
                         $customerPoints->mw_reward_point -= $expiredAmount;
                         $customerPoints->save();
                         $customerPoints->save();