Browse Source

修复:定时任务回退订单积分报错

萤火科技 4 years ago
parent
commit
07a001aee2
3 changed files with 14 additions and 11 deletions
  1. 4 2
      app/common/model/User.php
  2. 9 8
      app/common/model/user/PointsLog.php
  3. 1 1
      app/common/service/Order.php

+ 4 - 2
app/common/model/User.php

@@ -199,16 +199,18 @@ class User extends BaseModel
      * 累积用户的可用积分
      * @param int $userId 用户ID
      * @param int $points 累计的积分
-     * @param string $describe
+     * @param string $describe 积分描述
+     * @param int|null $storeId
      * @return mixed
      */
-    public static function setIncPoints(int $userId, int $points, string $describe)
+    public static function setIncPoints(int $userId, int $points, string $describe, ?int $storeId = null)
     {
         // 新增积分变动明细
         PointsLogModel::add([
             'user_id' => $userId,
             'value' => $points,
             'describe' => $describe,
+            'store_id' => $storeId
         ]);
         // 更新用户可用积分
         return (new static)->setInc($userId, 'points', $points);

+ 9 - 8
app/common/model/user/PointsLog.php

@@ -8,11 +8,12 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\common\model\user;
 
 use cores\BaseModel;
+use think\model\relation\BelongsTo;
 
 /**
  * 用户积分变动明细模型
@@ -31,9 +32,9 @@ class PointsLog extends BaseModel
 
     /**
      * 关联会员记录表
-     * @return \think\model\relation\BelongsTo
+     * @return BelongsTo
      */
-    public function user()
+    public function user(): BelongsTo
     {
         $module = self::getCalledModule();
         return $this->belongsTo("app\\{$module}\\model\\User");
@@ -41,12 +42,13 @@ class PointsLog extends BaseModel
 
     /**
      * 新增记录
-     * @param $data
+     * @param array $data
      */
-    public static function add($data)
+    public static function add(array $data)
     {
         $static = new static;
-        $static->save(array_merge(['store_id' => $static::$storeId], $data));
+        empty($data['store_id']) && $data['store_id'] = $static::$storeId;
+        $static->save($data);
     }
 
     /**
@@ -54,9 +56,8 @@ class PointsLog extends BaseModel
      * @param $saveData
      * @return bool
      */
-    public function onBatchAdd($saveData)
+    public function onBatchAdd($saveData): bool
     {
         return $this->addAll($saveData) !== false;
     }
-
 }

+ 1 - 1
app/common/service/Order.php

@@ -46,7 +46,7 @@ class Order extends BaseService
         // 回退用户积分
         if ($order['points_num'] > 0) {
             $describe = "订单取消:{$order['order_no']}";
-            UserModel::setIncPoints($order['user_id'], $order['points_num'], $describe);
+            UserModel::setIncPoints($order['user_id'], $order['points_num'], $describe, $order['store_id']);
         }
     }
 }