Просмотр исходного кода

优化用户下单方法-进行中

sunxbiao 1 год назад
Родитель
Сommit
5e79df78a2

+ 30 - 0
app/common/dao/user/UserSignScoreDao.php

@@ -3,9 +3,13 @@
3 3
 namespace app\common\dao\user;
4 4
 
5 5
 use app\common\dao\BaseDao;
6
+use app\common\enum\user\UserSignScoreEnum;
6 7
 use app\common\model\user\UserSignScore;
7 8
 use app\entity\CommonEntity;
8 9
 use app\entity\data\user\UserSignScoreEntity;
10
+use think\db\exception\DataNotFoundException;
11
+use think\db\exception\DbException;
12
+use think\db\exception\ModelNotFoundException;
9 13
 
10 14
 class UserSignScoreDao extends BaseDao
11 15
 {
@@ -38,4 +42,30 @@ class UserSignScoreDao extends BaseDao
38 42
         }
39 43
         return $entityList;
40 44
     }
45
+
46
+    /**
47
+     * @param string $orderType
48
+     * @param array $orderIdList
49
+     * @return UserSignScoreEntity[]
50
+     */
51
+    public function getUserBillEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
52
+    {
53
+        $entityList = [];
54
+        try {
55
+            $dataList = UserSignScore::getDB()
56
+                ->where('order_type', '=', $orderType)
57
+                ->whereIn('order_id', $orderIdList)
58
+                ->where('status', '=', UserSignScoreEnum::STATUS['Pending']['code'])
59
+                ->select()
60
+                ->toArray();
61
+            if (!empty($dataList)) {
62
+                $entityList = array_map(function ($data) {
63
+                    return UserSignScoreEntity::newInstance($data);
64
+                }, $dataList);
65
+            }
66
+        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
67
+
68
+        }
69
+        return $entityList;
70
+    }
41 71
 }

+ 15 - 5
app/common/repositories/store/order/StoreOrderRepository.php

@@ -12068,29 +12068,39 @@ class StoreOrderRepository extends BaseRepository
12068 12068
             /** @var UserRepository $userRepository */
12069 12069
             $userRepository = app()->make(UserRepository::class);
12070 12070
 
12071
-            // 筛选
12072
-            $existOrderIdList = [];
12071
+            // 筛选 未结算的 订单ID
12072
+            $unsettledOrderIdList = [];
12073 12073
             foreach ($storeOrderEntityList as $storeOrderEntity) {
12074 12074
                 if ($storeOrderEntity->getIsSettlement() == StoreOrderEnum::IS_SETTLEMENT['NO']['code']) {
12075
-                    $existOrderIdList[] = $storeOrderEntity->getOrderId();
12075
+                    $unsettledOrderIdList[] = $storeOrderEntity->getOrderId();
12076 12076
                 }
12077 12077
             }
12078
-            if (empty($existOrderIdList)) {
12078
+            if (empty($unsettledOrderIdList)) {
12079 12079
                 return;
12080 12080
             }
12081 12081
 
12082 12082
             /** 养老金 佣金 */
12083 12083
             /** @var UserBillRepository $userBillRepository */
12084 12084
             $userBillRepository = app()->make(UserBillRepository::class);
12085
-            $userBillEntityList = $userBillRepository->getUserBillEntityListByOrderIdListAndPending(UserBillEnum::TYPE_SHOP['Platform']['code'], $existOrderIdList);
12085
+            $userBillEntityList = $userBillRepository->getUserBillEntityListByOrderIdListAndPending(UserBillEnum::TYPE_SHOP['Platform']['code'], $unsettledOrderIdList);
12086 12086
             if (!empty($userBillEntityList)) {
12087 12087
                 $billIdList = array_map(function ($userBillEntity) {
12088 12088
                     return $userBillEntity->getBillId();
12089 12089
                 }, $userBillEntityList);
12090
+                // 去结算 养老金 和佣金
12090 12091
                 $userBillRepository->executeByIdList($billIdList);
12091 12092
             }
12092 12093
 
12094
+            /** 积分 */
12095
+            /** @var UserSignScoreRepository $userSignScoreRepository */
12096
+            $userSignScoreRepository = app()->make(UserSignScoreRepository::class);
12097
+            $userSignScoreEntityList = $userSignScoreRepository->getStoreSignScoreEntityListByOrderIdListAndPending(UserSignScoreEnum::ORDER_TYPE['Platform']['code'], $unsettledOrderIdList);
12098
+            if (!empty($userSignScoreEntityList)) {
12099
+                $userSignScoreIdList = array_map(function ($userSignScoreEntity) {
12100
+                    return $userSignScoreEntity->getId();
12101
+                }, $userSignScoreEntityList);
12093 12102
 
12103
+            }
12094 12104
 
12095 12105
             foreach ($storeOrderEntityList as $storeOrderEntity) {
12096 12106
                 // 结算养老金

+ 73 - 0
app/common/repositories/user/UserSignScoreRepository.php

@@ -48,6 +48,16 @@ class UserSignScoreRepository extends BaseRepository
48 48
     }
49 49
 
50 50
     /**
51
+     * @param string $orderType
52
+     * @param array $orderIdList
53
+     * @return UserSignScoreEntity[]
54
+     */
55
+    public function getStoreSignScoreEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
56
+    {
57
+        return $this->dao->getUserBillEntityListByOrderIdListAndPending($orderType, $orderIdList);
58
+    }
59
+
60
+    /**
51 61
      * 写入积分记录
52 62
      * @param UserSignScoreEntity $userSignScoreEntity
53 63
      * @return UserSignScoreEntity
@@ -150,4 +160,67 @@ class UserSignScoreRepository extends BaseRepository
150 160
         }
151 161
         return $userSignScoreEntity;
152 162
     }
163
+
164
+    /**
165
+     * 将 积分 记录设置为生效状态 并对用户的 积分 字段进行增减
166
+     * @param int $id
167
+     * @return UserSignScoreEntity
168
+     */
169
+    public function executeByIdList(array $idList): UserSignScoreEntity
170
+    {
171
+        $userSignScoreEntityList = $this->getStoreSignScoreEntityListByIdList($idList);
172
+
173
+        // 忽略 已经生效的 记录 todo 把要获取的 用户列表等信息 从这里获取 use ()
174
+        $userSignScoreEntityList = array_filter($userSignScoreEntityList, function($entity) {
175
+            // 用户不为空 状态属于待结算 并且结算字段为空
176
+            return (!empty($entity->getId()) &&  !empty($entity->getUid()) && ($entity->getStatus() == UserSignScoreEnum::STATUS['Pending']['code']) && empty($entity->getSettlementTime()));
177
+        });
178
+
179
+        if (!empty($userSignScoreEntityList)) {
180
+
181
+        }
182
+
183
+        foreach ($userSignScoreEntityList as $userSignScoreEntity) {
184
+            // if (empty($userSignScoreEntity->getId()) || empty($userSignScoreEntity->getUid())) {
185
+            //     throw new ValidateException('无效的积分记录');
186
+            // }
187
+            // if ($userSignScoreEntity->getStatus() == UserSignScoreEnum::STATUS['Invalid']['code']) {
188
+            //     throw new ValidateException('该积分记录已失效');
189
+            // }
190
+            // if (!empty($userSignScoreEntity->getSettlementTime()) || $userSignScoreEntity->getStatus() == UserSignScoreEnum::STATUS['Valid']['code']) {
191
+            //     throw new ValidateException('该积分记录已更新,请勿重复操作');
192
+            // }
193
+
194
+            /** @var UserRepository $userRepository */
195
+            $userRepository = app()->make(UserRepository::class);
196
+            $userEntity = $userRepository->getUserEntityByUid($userSignScoreEntity->getUid());
197
+            if (empty($userEntity->getUid())) {
198
+                throw new ValidateException('未查询到用户信息');
199
+            }
200
+            if ($userSignScoreEntity->getPm() == UserSignScoreEnum::PM['Expend']['code']) {
201
+                $surplus = bcsub($userEntity->getScore(), $userSignScoreEntity->getRewardScore(), 2);
202
+                if ($surplus < 0.00) {
203
+                    throw new ValidateException('用户积分不足');
204
+                }
205
+            } else {
206
+                $surplus = bcadd($userEntity->getScore(), $userSignScoreEntity->getRewardScore(), 2);
207
+            }
208
+            try {
209
+                $userSignScoreEntity
210
+                    ->setSurplus($surplus)
211
+                    ->setSettlementTime(date('Y-m-d H:i:s'))
212
+                    ->setStatus(UserSignScoreEnum::STATUS['Valid']['code']);
213
+                $userRepository->update($userEntity->getUid(), ['score' => $surplus]);
214
+                $this->dao->update($userSignScoreEntity->getId(), [
215
+                    'surplus' => $userSignScoreEntity->getSurplus(),
216
+                    'settlement_time' => $userSignScoreEntity->getSettlementTime(),
217
+                    'status' => $userSignScoreEntity->getStatus()
218
+                ]);
219
+            } catch (DbException $e) {
220
+
221
+            }
222
+            return $userSignScoreEntity;
223
+        }
224
+
225
+    }
153 226
 }