Procházet zdrojové kódy

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

sunxbiao před 11 měsíci
rodič
revize
080b87fa35

+ 75 - 0
app/common/dao/user/UserSignGongxianDao.php

@@ -3,12 +3,87 @@
3 3
 namespace app\common\dao\user;
4 4
 
5 5
 use app\common\dao\BaseDao;
6
+use app\common\enum\user\UserSignGongxianEnum;
7
+use app\common\model\BaseModel;
6 8
 use app\common\model\user\UserSignGongxian;
9
+use app\entity\CommonEntity;
10
+use app\entity\data\user\UserSignGongxianEntity;
11
+use think\db\exception\DataNotFoundException;
12
+use think\db\exception\DbException;
13
+use think\db\exception\ModelNotFoundException;
7 14
 
8 15
 class UserSignGongxianDao extends BaseDao
9 16
 {
17
+    /**
18
+     * @return BaseModel
19
+     */
10 20
     protected function getModel(): string
11 21
     {
12 22
         return UserSignGongxian::class;
13 23
     }
24
+
25
+    /**
26
+     * @param array $idList
27
+     * @return CommonEntity[]
28
+     */
29
+    public function getUserSignGongxianEntityListByIdList(array $idList): array
30
+    {
31
+        $entityList = [];
32
+        try {
33
+            /** @var UserSignGongxian $model */
34
+            $model = $this->getModel();
35
+            $dataRes = $model::getDB()
36
+                ->whereIn('id', $idList)
37
+                ->select()
38
+                ->toArray();
39
+            if (!empty($dataRes)) {
40
+                $entityList = array_map(function ($data) {
41
+                    return UserSignGongxianEntity::newInstance($data);
42
+                }, $dataRes);
43
+            }
44
+        } catch (\Exception $e) {
45
+
46
+        }
47
+        return $entityList;
48
+    }
49
+
50
+    /**
51
+     * @param string $orderType
52
+     * @param array $orderIdList
53
+     * @return UserSignGongxianEntity[]
54
+     */
55
+    public function getUserSignGongxianEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
56
+    {
57
+        $entityList = [];
58
+        try {
59
+            $dataList = UserSignGongxian::getDB()
60
+                ->where('order_type', '=', $orderType)
61
+                ->whereIn('order_id', $orderIdList)
62
+                ->where('status', '=', UserSignGongxianEnum::STATUS['PendingConfirmation']['code'])
63
+                ->select()
64
+                ->toArray();
65
+            if (!empty($dataList)) {
66
+                $entityList = array_map(function ($data) {
67
+                    return UserSignGongxianEntity::newInstance($data);
68
+                }, $dataList);
69
+            }
70
+        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
71
+
72
+        }
73
+        return $entityList;
74
+    }
75
+
76
+    /**
77
+     * 批量更新
78
+     * @param $dataList
79
+     * @return array
80
+     */
81
+    public function saveAll($dataList): array
82
+    {
83
+        try {
84
+            return $this->getModel()->saveAll($dataList)->toArray();
85
+        } catch (\Exception $e) {
86
+            return [];
87
+        }
88
+    }
14 89
 }

+ 20 - 2
app/common/dao/user/UserSignScoreDao.php

@@ -4,6 +4,7 @@ namespace app\common\dao\user;
4 4
 
5 5
 use app\common\dao\BaseDao;
6 6
 use app\common\enum\user\UserSignScoreEnum;
7
+use app\common\model\BaseModel;
7 8
 use app\common\model\user\UserSignScore;
8 9
 use app\entity\CommonEntity;
9 10
 use app\entity\data\user\UserSignScoreEntity;
@@ -13,6 +14,9 @@ use think\db\exception\ModelNotFoundException;
13 14
 
14 15
 class UserSignScoreDao extends BaseDao
15 16
 {
17
+    /**
18
+     * @return BaseModel
19
+     */
16 20
     protected function getModel(): string
17 21
     {
18 22
         return UserSignScore::class;
@@ -22,7 +26,7 @@ class UserSignScoreDao extends BaseDao
22 26
      * @param array $idList
23 27
      * @return CommonEntity[]
24 28
      */
25
-    public function getStoreSignScoreEntityListByIdList(array $idList): array
29
+    public function getUserSignScoreEntityListByIdList(array $idList): array
26 30
     {
27 31
         $entityList = [];
28 32
         try {
@@ -48,7 +52,7 @@ class UserSignScoreDao extends BaseDao
48 52
      * @param array $orderIdList
49 53
      * @return UserSignScoreEntity[]
50 54
      */
51
-    public function getUserBillEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
55
+    public function getUserSignScoreEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
52 56
     {
53 57
         $entityList = [];
54 58
         try {
@@ -68,4 +72,18 @@ class UserSignScoreDao extends BaseDao
68 72
         }
69 73
         return $entityList;
70 74
     }
75
+
76
+    /**
77
+     * 批量更新
78
+     * @param $dataList
79
+     * @return array
80
+     */
81
+    public function saveAll($dataList): array
82
+    {
83
+        try {
84
+            return $this->getModel()->saveAll($dataList)->toArray();
85
+        } catch (\Exception $e) {
86
+            return [];
87
+        }
88
+    }
71 89
 }

+ 35 - 10
app/common/repositories/store/order/StoreOrderRepository.php

@@ -11767,10 +11767,15 @@ class StoreOrderRepository extends BaseRepository
11767 11767
             $userBillRepository = app()->make(UserBillRepository::class);
11768 11768
             $userBillRepository->addUserBill(
11769 11769
                 $storeOrderEntity->getUid(),
11770
-                UserBillEnum::TYPE_SHOP['Platform']['code'],
11771
-                $storeOrderEntity->getOrderId(),
11772 11770
                 UserBillEnum::COMMISSION_TYPE['PENSION']['code'],
11771
+                UserBillEnum::PM['INCOME']['code'],
11773 11772
                 $yanglaojin,
11773
+                UserBillEnum::CATEGORY['NowMoney']['code'],
11774
+                UserBillEnum::TYPE['Commission']['code'],
11775
+                UserBillEnum::SOURCE['ONLINE']['code'],
11776
+                UserBillEnum::ORDER_TYPE['SELF_OPERATED']['code'],
11777
+                UserBillEnum::TYPE_SHOP['Platform']['code'],
11778
+                $storeOrderEntity->getOrderId(),
11774 11779
                 '购买复购区赠送养老金'
11775 11780
             );
11776 11781
 
@@ -11783,10 +11788,15 @@ class StoreOrderRepository extends BaseRepository
11783 11788
                     $userBillRepository = app()->make(UserBillRepository::class);
11784 11789
                     $userBillRepository->addUserBill(
11785 11790
                         $userEntityBySpreadUid->getUid(),
11786
-                        UserBillEnum::TYPE_SHOP['Platform']['code'],
11787
-                        $storeOrderEntity->getOrderId(),
11788 11791
                         UserBillEnum::COMMISSION_TYPE['PENSION']['code'],
11792
+                        UserBillEnum::PM['INCOME']['code'],
11789 11793
                         $yanglaojinBySpreadDirectly,
11794
+                        UserBillEnum::CATEGORY['NowMoney']['code'],
11795
+                        UserBillEnum::TYPE['Commission']['code'],
11796
+                        UserBillEnum::SOURCE['ONLINE']['code'],
11797
+                        UserBillEnum::ORDER_TYPE['SELF_OPERATED']['code'],
11798
+                        UserBillEnum::TYPE_SHOP['Platform']['code'],
11799
+                        $storeOrderEntity->getOrderId(),
11790 11800
                         '推荐复购区商品赠送养老金'
11791 11801
                     );
11792 11802
 
@@ -11799,10 +11809,15 @@ class StoreOrderRepository extends BaseRepository
11799 11809
                             $userBillRepository = app()->make(UserBillRepository::class);
11800 11810
                             $userBillRepository->addUserBill(
11801 11811
                                 $userEntityByIndirectlySpreadUid->getUid(),
11802
-                                UserBillEnum::TYPE_SHOP['Platform']['code'],
11803
-                                $storeOrderEntity->getOrderId(),
11804 11812
                                 UserBillEnum::COMMISSION_TYPE['PENSION']['code'],
11813
+                                UserBillEnum::PM['INCOME']['code'],
11805 11814
                                 $yanglaojinBySpreadIndirectly,
11815
+                                UserBillEnum::CATEGORY['NowMoney']['code'],
11816
+                                UserBillEnum::TYPE['Commission']['code'],
11817
+                                UserBillEnum::SOURCE['ONLINE']['code'],
11818
+                                UserBillEnum::ORDER_TYPE['SELF_OPERATED']['code'],
11819
+                                UserBillEnum::TYPE_SHOP['Platform']['code'],
11820
+                                $storeOrderEntity->getOrderId(),
11806 11821
                                 '间推复购区商品赠送养老金'
11807 11822
                             );
11808 11823
                         }
@@ -12079,7 +12094,7 @@ class StoreOrderRepository extends BaseRepository
12079 12094
                 return;
12080 12095
             }
12081 12096
 
12082
-            /** 养老金 佣金 */
12097
+            /** 结算 养老金 佣金 */
12083 12098
             /** @var UserBillRepository $userBillRepository */
12084 12099
             $userBillRepository = app()->make(UserBillRepository::class);
12085 12100
             $userBillEntityList = $userBillRepository->getUserBillEntityListByOrderIdListAndPending(UserBillEnum::TYPE_SHOP['Platform']['code'], $unsettledOrderIdList);
@@ -12091,15 +12106,25 @@ class StoreOrderRepository extends BaseRepository
12091 12106
                 $userBillRepository->executeByIdList($billIdList);
12092 12107
             }
12093 12108
 
12094
-            /** 积分 */
12109
+            /** 结算 积分 */
12095 12110
             /** @var UserSignScoreRepository $userSignScoreRepository */
12096 12111
             $userSignScoreRepository = app()->make(UserSignScoreRepository::class);
12097
-            $userSignScoreEntityList = $userSignScoreRepository->getStoreSignScoreEntityListByOrderIdListAndPending(UserSignScoreEnum::ORDER_TYPE['Platform']['code'], $unsettledOrderIdList);
12112
+            $userSignScoreEntityList = $userSignScoreRepository->getUserSignScoreEntityListByOrderIdListAndPending(UserSignScoreEnum::ORDER_TYPE['Platform']['code'], $unsettledOrderIdList);
12098 12113
             if (!empty($userSignScoreEntityList)) {
12099 12114
                 $userSignScoreIdList = array_map(function ($userSignScoreEntity) {
12100 12115
                     return $userSignScoreEntity->getId();
12101 12116
                 }, $userSignScoreEntityList);
12102
-
12117
+                $userSignScoreRepository->executeByIdList($userSignScoreIdList);
12118
+            }
12119
+            /** 结算 贡献值 */
12120
+            /** @var UserSignGongxianRepository $userSignGongxianRepository */
12121
+            $userSignGongxianRepository = app()->make(UserSignGongxianRepository::class);
12122
+            $userSignGongxianEntityList = $userSignGongxianRepository->getUserSignGongxianEntityListByOrderIdListAndPending(UserSignGongxianEnum::ORDER_TYPE['Platform']['code'], $unsettledOrderIdList);
12123
+            if (!empty($userSignGongxianEntityList)) {
12124
+                $userSignGongxianIdList = array_map(function ($userSignGongxianEntity) {
12125
+                    return $userSignGongxianEntity->getId();
12126
+                }, $userSignGongxianEntityList);
12127
+                $userSignGongxianRepository->executeByIdList($userSignGongxianIdList);
12103 12128
             }
12104 12129
 
12105 12130
             foreach ($storeOrderEntityList as $storeOrderEntity) {

+ 2 - 1
app/common/repositories/user/UserBillRepository.php

@@ -570,7 +570,8 @@ class UserBillRepository extends BaseRepository
570 570
                 // 更新 账单记录 状态
571 571
                 $updateUserBillEntity
572 572
                     ->setBillId($userBillEntity->getBillId())
573
-                    ->setStatus(UserBillEnum::STATUS['VALID']['code']);
573
+                    ->setStatus(UserBillEnum::STATUS['VALID']['code'])
574
+                    ->setTakeTime(time());
574 575
 
575 576
                 if ($userBillEntity->getCommissionType() == UserBillEnum::COMMISSION_TYPE['DIRECT_REFERRAL']['code']) {
576 577
                     // 直推奖

+ 161 - 0
app/common/repositories/user/UserSignGongxianRepository.php

@@ -6,7 +6,9 @@ use app\common\dao\user\UserSignGongxianDao;
6 6
 use app\common\enum\user\UserSignGongxianEnum;
7 7
 use app\common\repositories\BaseRepository;
8 8
 use app\common\repositories\store\order\StoreOrderRepository;
9
+use app\entity\data\user\UserEntity;
9 10
 use app\entity\data\user\UserSignGongxianEntity;
11
+use think\db\exception\DbException;
10 12
 use think\exception\ValidateException;
11 13
 
12 14
 class UserSignGongxianRepository extends BaseRepository
@@ -16,6 +18,41 @@ class UserSignGongxianRepository extends BaseRepository
16 18
         $this->dao = $dao;
17 19
     }
18 20
 
21
+    /**
22
+     * @param int $id
23
+     * @return UserSignGongxianEntity
24
+     */
25
+    public function getUserSignGongxianEntityById(int $id): UserSignGongxianEntity
26
+    {
27
+        $entityList = $this->getUserSignGongxianEntityListByIdList([$id]);
28
+        $entity = array_shift($entityList);
29
+        if ($entity instanceof UserSignGongxianEntity) {
30
+            return $entity;
31
+        } else {
32
+            /** @var UserSignGongxianEntity */
33
+            return UserSignGongxianEntity::newInstance();
34
+        }
35
+    }
36
+
37
+    /**
38
+     * @param array $idList
39
+     * @return UserSignGongxianEntity[]
40
+     */
41
+    public function getUserSignGongxianEntityListByIdList(array $idList): array
42
+    {
43
+        return $this->dao->getUserSignGongxianEntityListByIdList($idList);
44
+    }
45
+
46
+    /**
47
+     * @param string $orderType
48
+     * @param array $orderIdList
49
+     * @return UserSignGongxianEntity[]
50
+     */
51
+    public function getUserSignGongxianEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
52
+    {
53
+        return $this->dao->getUserSignGongxianEntityListByOrderIdListAndPending($orderType, $orderIdList);
54
+    }
55
+    
19 56
     public function create($data)
20 57
     {
21 58
         return $this->dao->create($data);
@@ -76,4 +113,128 @@ class UserSignGongxianRepository extends BaseRepository
76 113
         return $this->createByEntity($userSignGongxianEntity);
77 114
     }
78 115
 
116
+    /**
117
+     * 将 贡献值 直推贡献值 记录设置为生效状态 并对用户的 贡献值 直推贡献值 字段进行增减
118
+     * @param array $idList
119
+     * @return array
120
+     */
121
+    public function executeByIdList(array $idList): array
122
+    {
123
+        $userSignGongxianEntityList = $this->getUserSignGongxianEntityListByIdList($idList);
124
+        $userIdList = [];
125
+
126
+        // 忽略 已经生效的 记录
127
+        $userSignGongxianEntityList = array_filter($userSignGongxianEntityList, function ($entity) use (&$userIdList) {
128
+            // 积分数据不为空 用户不为空 状态属于待结算 并且结算字段为空
129
+            if ((!empty($entity->getId()) && !empty($entity->getUid()) && ($entity->getStatus() == UserSignGongxianEnum::STATUS['PendingConfirmation']['code']) && empty($entity->getSettlementTime()))) {
130
+                $userIdList[] = $entity->getUid();
131
+                return true;
132
+            }
133
+            return false;
134
+        });
135
+
136
+        if (!empty($userSignGongxianEntityList)) {
137
+            // 获取用户 映射信息
138
+            /** @var UserRepository $userRepository */
139
+            $userRepository = app()->make(UserRepository::class);
140
+            /** @var UserEntity[] $userEntityMapByUid */
141
+            $userEntityMapByUid = [];
142
+            if (!empty($userIdList)) {
143
+                $userEntityMapByUid = $userRepository->getUserEntityMapByUidList($userIdList);
144
+            }
145
+
146
+            /** @var UserEntity[] $updateUserEntityList */
147
+            $updateUserEntityList = [];
148
+            /** @var UserSignGongxianEntity[] $updateUserScoreEntityList */
149
+            $updateUserScoreEntityList = [];
150
+            foreach ($userSignGongxianEntityList as $userSignGongxianEntity) {
151
+
152
+                $userEntity = $userEntityMapByUid[$userSignGongxianEntity->getUid()];
153
+                if (empty($userEntity) || empty($userEntity->getUid())) {
154
+                    throw new ValidateException("未查询到用户信息(GongxianId:{$userSignGongxianEntity->getId()})");
155
+                }
156
+
157
+                // 组建 更新 用户信息
158
+                if (isset($updateUserEntityList[$userEntity->getUid()])) {
159
+                    $updateUserEntity = $updateUserEntityList[$userEntity->getUid()];
160
+                } else {
161
+                    /** @var UserEntity $updateUserEntity */
162
+                    $updateUserEntity = UserEntity::newInstance();
163
+                    $updateUserEntity
164
+                        ->setUid($userEntity->getUid())
165
+                        ->setContribute($userEntity->getContribute())
166
+                        ->setPerContribute($userEntity->getPerContribute());
167
+                }
168
+                // 组建 账单 记录
169
+                if (isset($updateUserScoreEntityList[$userSignGongxianEntity->getId()])) {
170
+                    $updateUserSignGongxianEntity = $updateUserScoreEntityList[$userSignGongxianEntity->getId()];
171
+                } else {
172
+                    /** @var UserSignGongxianEntity $updateUserSignGongxianEntity */
173
+                    $updateUserSignGongxianEntity = UserSignGongxianEntity::newInstance();
174
+                    // 更新 账单记录 状态
175
+                    $updateUserSignGongxianEntity
176
+                        ->setId($userSignGongxianEntity->getId())
177
+                        ->setStatus(UserSignGongxianEnum::STATUS['Valid']['code'])
178
+                        ->setSettlementTime(date('Y-m-d H:i:s'));
179
+                }
180
+
181
+                if ($userSignGongxianEntity->getType() == UserSignGongxianEnum::TYPE['Consume']['code']) {
182
+                    // 消耗
183
+                    if (strpos($userSignGongxianEntity->getMark(), '购买') !== false) {
184
+                        $updateUserEntity->setContribute(bcsub($updateUserEntity->getContribute(), $userSignGongxianEntity->getNumber(), 2));
185
+                        if ($updateUserEntity->getContribute() < 0.00) {
186
+                            throw new ValidateException('用户贡献值不足');
187
+                        }
188
+                        $updateUserSignGongxianEntity->setSurplus($updateUserEntity->getContribute());
189
+                    } else if (strpos($userSignGongxianEntity->getMark(), '推广') !== false) {
190
+                        $updateUserEntity->setPerContribute(bcsub($updateUserEntity->getPerContribute(), $userSignGongxianEntity->getNumber(), 2));
191
+                        if ($updateUserEntity->getPerContribute() < 0.00) {
192
+                            throw new ValidateException('用户推广贡献值不足');
193
+                        }
194
+                        $updateUserSignGongxianEntity->setSurplus($updateUserEntity->getPerContribute());
195
+                    }
196
+                } else {
197
+                    if (strpos($userSignGongxianEntity->getMark(), '购买') !== false) {
198
+                        $updateUserEntity->setContribute(bcadd($updateUserEntity->getContribute(), $userSignGongxianEntity->getNumber(), 2));
199
+                        $updateUserSignGongxianEntity->setSurplus($updateUserEntity->getContribute());
200
+                    } else if (strpos($userSignGongxianEntity->getMark(), '推广') !== false) {
201
+                        $updateUserEntity->setPerContribute(bcadd($updateUserEntity->getPerContribute(), $userSignGongxianEntity->getNumber(), 2));
202
+                        $updateUserSignGongxianEntity->setSurplus($updateUserEntity->getPerContribute());
203
+                    }
204
+                }
205
+
206
+                $updateUserEntityList[$userSignGongxianEntity->getUid()] = $updateUserEntity;
207
+                $updateUserScoreEntityList[$userSignGongxianEntity->getId()] = $updateUserSignGongxianEntity;
208
+            }
209
+            try {
210
+                if (!empty($updateUserEntityList)) {
211
+                    $userRepository->batchUpdateUserDataByDataList(
212
+                        array_map(function (UserEntity $updateUserEntity) {
213
+                            return $updateUserEntity->toUnderlineArray();
214
+                        }, $updateUserEntityList)
215
+                    );
216
+                }
217
+
218
+                if (!empty($updateUserBillEntityList)) {
219
+                    $this->batchUpdateUserSignGongxianDataByDataList(
220
+                        array_map(function (UserSignGongxianEntity $updateUserSignGongxianEntity) {
221
+                            return $updateUserSignGongxianEntity->toUnderlineArray();
222
+                        }, $updateUserScoreEntityList)
223
+                    );
224
+                }
225
+            } catch (DbException $e) {
226
+
227
+            }
228
+        }
229
+        return $idList;
230
+    }
231
+
232
+    /**
233
+     * @param $dataList
234
+     * @return array
235
+     */
236
+    public function batchUpdateUserSignGongxianDataByDataList($dataList): array
237
+    {
238
+        return $this->dao->saveAll($dataList);
239
+    }
79 240
 }

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

@@ -27,9 +27,9 @@ class UserSignScoreRepository extends BaseRepository
27 27
      * @param int $id
28 28
      * @return UserSignScoreEntity
29 29
      */
30
-    public function getStoreSignScoreEntityById(int $id): UserSignScoreEntity
30
+    public function getUserSignScoreEntityById(int $id): UserSignScoreEntity
31 31
     {
32
-        $entityList = $this->getStoreSignScoreEntityListByIdList([$id]);
32
+        $entityList = $this->getUserSignScoreEntityListByIdList([$id]);
33 33
         $entity = array_shift($entityList);
34 34
         if ($entity instanceof UserSignScoreEntity) {
35 35
             return $entity;
@@ -43,9 +43,9 @@ class UserSignScoreRepository extends BaseRepository
43 43
      * @param array $idList
44 44
      * @return UserSignScoreEntity[]
45 45
      */
46
-    public function getStoreSignScoreEntityListByIdList(array $idList): array
46
+    public function getUserSignScoreEntityListByIdList(array $idList): array
47 47
     {
48
-        return $this->dao->getStoreSignScoreEntityListByIdList($idList);
48
+        return $this->dao->getUserSignScoreEntityListByIdList($idList);
49 49
     }
50 50
 
51 51
     /**
@@ -53,9 +53,9 @@ class UserSignScoreRepository extends BaseRepository
53 53
      * @param array $orderIdList
54 54
      * @return UserSignScoreEntity[]
55 55
      */
56
-    public function getStoreSignScoreEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
56
+    public function getUserSignScoreEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
57 57
     {
58
-        return $this->dao->getUserBillEntityListByOrderIdListAndPending($orderType, $orderIdList);
58
+        return $this->dao->getUserSignScoreEntityListByOrderIdListAndPending($orderType, $orderIdList);
59 59
     }
60 60
 
61 61
     /**
@@ -116,60 +116,22 @@ class UserSignScoreRepository extends BaseRepository
116 116
     /**
117 117
      * 将 积分 记录设置为生效状态 并对用户的 积分 字段进行增减
118 118
      * @param int $id
119
-     * @return UserSignScoreEntity
119
+     * @return int
120 120
      */
121
-    public function execute(int $id): UserSignScoreEntity
121
+    public function execute(int $id): int
122 122
     {
123
-        $userSignScoreEntity = $this->getStoreSignScoreEntityById($id);
124
-        if (empty($userSignScoreEntity->getId()) || empty($userSignScoreEntity->getUid())) {
125
-            throw new ValidateException('无效的积分记录');
126
-        }
127
-        if ($userSignScoreEntity->getStatus() == UserSignScoreEnum::STATUS['Invalid']['code']) {
128
-            throw new ValidateException('该积分记录已失效');
129
-        }
130
-        if (!empty($userSignScoreEntity->getSettlementTime()) || $userSignScoreEntity->getStatus() == UserSignScoreEnum::STATUS['Valid']['code']) {
131
-            throw new ValidateException('该积分记录已更新,请勿重复操作');
132
-        }
133
-
134
-        /** @var UserRepository $userRepository */
135
-        $userRepository = app()->make(UserRepository::class);
136
-        $userEntity = $userRepository->getUserEntityByUid($userSignScoreEntity->getUid());
137
-        if (empty($userEntity->getUid())) {
138
-            throw new ValidateException('未查询到用户信息');
139
-        }
140
-        if ($userSignScoreEntity->getPm() == UserSignScoreEnum::PM['Expend']['code']) {
141
-            $surplus = bcsub($userEntity->getScore(), $userSignScoreEntity->getRewardScore(), 2);
142
-            if ($surplus < 0.00) {
143
-                throw new ValidateException('用户积分不足');
144
-            }
145
-        } else {
146
-            $surplus = bcadd($userEntity->getScore(), $userSignScoreEntity->getRewardScore(), 2);
147
-        }
148
-        try {
149
-            $userSignScoreEntity
150
-                ->setSurplus($surplus)
151
-                ->setSettlementTime(date('Y-m-d H:i:s'))
152
-                ->setStatus(UserSignScoreEnum::STATUS['Valid']['code']);
153
-            $userRepository->update($userEntity->getUid(), ['score' => $surplus]);
154
-            $this->dao->update($userSignScoreEntity->getId(), [
155
-                'surplus' => $userSignScoreEntity->getSurplus(),
156
-                'settlement_time' => $userSignScoreEntity->getSettlementTime(),
157
-                'status' => $userSignScoreEntity->getStatus()
158
-            ]);
159
-        } catch (DbException $e) {
160
-
161
-        }
162
-        return $userSignScoreEntity;
123
+        $result = $this->executeByIdList([$id]);
124
+        return array_shift($result);
163 125
     }
164 126
 
165 127
     /**
166 128
      * 将 积分 记录设置为生效状态 并对用户的 积分 字段进行增减
167 129
      * @param array $idList
168
-     * @return UserSignScoreEntity
130
+     * @return array
169 131
      */
170
-    public function executeByIdList(array $idList): UserSignScoreEntity
132
+    public function executeByIdList(array $idList): array
171 133
     {
172
-        $userSignScoreEntityList = $this->getStoreSignScoreEntityListByIdList($idList);
134
+        $userSignScoreEntityList = $this->getUserSignScoreEntityListByIdList($idList);
173 135
         $userIdList = [];
174 136
 
175 137
         // 忽略 已经生效的 记录
@@ -192,7 +154,9 @@ class UserSignScoreRepository extends BaseRepository
192 154
                 $userEntityMapByUid = $userRepository->getUserEntityMapByUidList($userIdList);
193 155
             }
194 156
 
157
+            /** @var UserEntity[] $updateUserEntityList */
195 158
             $updateUserEntityList = [];
159
+            /** @var UserSignScoreEntity[] $updateUserScoreEntityList */
196 160
             $updateUserScoreEntityList = [];
197 161
             foreach ($userSignScoreEntityList as $userSignScoreEntity) {
198 162
                 // if (empty($userSignScoreEntity->getId()) || empty($userSignScoreEntity->getUid())) {
@@ -209,36 +173,74 @@ class UserSignScoreRepository extends BaseRepository
209 173
                 if (empty($userEntity) || empty($userEntity->getUid())) {
210 174
                     throw new ValidateException("未查询到用户信息(scoreId:{$userSignScoreEntity->getId()})");
211 175
                 }
212
-                // 更新 用户信息
213
-                /** @var UserEntity $updateUserEntity */
214
-                $updateUserEntity = $updateUserEntityList[$userEntity->getUid()] ?? UserEntity::newInstance();
215
-                $updateUserEntity->setUid($userEntity->getUid());
216 176
 
177
+                // 组建 更新 用户信息
178
+                if (isset($updateUserEntityList[$userEntity->getUid()])) {
179
+                    $updateUserEntity = $updateUserEntityList[$userEntity->getUid()];
180
+                } else {
181
+                    /** @var UserEntity $updateUserEntity */
182
+                    $updateUserEntity = UserEntity::newInstance();
183
+                    $updateUserEntity
184
+                        ->setUid($userEntity->getUid())
185
+                        ->setScore($userEntity->getScore());
186
+
187
+                }
188
+                // 组建 账单 记录
189
+                if (isset($updateUserScoreEntityList[$userSignScoreEntity->getId()])) {
190
+                    $updateUserSignScoreEntity = $updateUserScoreEntityList[$userSignScoreEntity->getId()];
191
+                } else {
192
+                    /** @var UserSignScoreEntity $updateUserSignScoreEntity */
193
+                    $updateUserSignScoreEntity = UserSignScoreEntity::newInstance();
194
+                    // 更新 账单记录 状态
195
+                    $updateUserSignScoreEntity
196
+                        ->setId($userSignScoreEntity->getId())
197
+                        ->setStatus(UserSignScoreEnum::STATUS['Valid']['code'])
198
+                        ->setSettlementTime(time());
199
+                }
217 200
 
218 201
                 if ($userSignScoreEntity->getPm() == UserSignScoreEnum::PM['Expend']['code']) {
219
-                    $surplus = bcsub($userEntity->getScore(), $userSignScoreEntity->getRewardScore(), 2);
220
-                    if ($surplus < 0.00) {
202
+                    // 支出
203
+                    $updateUserEntity->setScore(bcsub($updateUserEntity->getScore(), $userSignScoreEntity->getRewardScore(), 2));
204
+                    if ($updateUserEntity->getScore() < 0.00) {
221 205
                         throw new ValidateException('用户积分不足');
222 206
                     }
223 207
                 } else {
224
-                    $surplus = bcadd($userEntity->getScore(), $userSignScoreEntity->getRewardScore(), 2);
208
+                    $updateUserEntity->setScore(bcadd($updateUserEntity->getScore(), $userSignScoreEntity->getRewardScore(), 2));
209
+                }
210
+                $updateUserSignScoreEntity->setSurplus($updateUserEntity->getScore());
211
+
212
+                $updateUserEntityList[$userSignScoreEntity->getUid()] = $updateUserEntity;
213
+                $updateUserScoreEntityList[$userSignScoreEntity->getId()] = $updateUserSignScoreEntity;
214
+            }
215
+            try {
216
+                if (!empty($updateUserEntityList)) {
217
+                    $userRepository->batchUpdateUserDataByDataList(
218
+                        array_map(function (UserEntity $updateUserEntity) {
219
+                            return $updateUserEntity->toUnderlineArray();
220
+                        }, $updateUserEntityList)
221
+                    );
225 222
                 }
226
-                try {
227
-                    $userSignScoreEntity
228
-                        ->setSurplus($surplus)
229
-                        ->setSettlementTime(date('Y-m-d H:i:s'))
230
-                        ->setStatus(UserSignScoreEnum::STATUS['Valid']['code']);
231
-                    $userRepository->update($userEntity->getUid(), ['score' => $surplus]);
232
-                    $this->dao->update($userSignScoreEntity->getId(), [
233
-                        'surplus' => $userSignScoreEntity->getSurplus(),
234
-                        'settlement_time' => $userSignScoreEntity->getSettlementTime(),
235
-                        'status' => $userSignScoreEntity->getStatus()
236
-                    ]);
237
-                } catch (DbException $e) {
238 223
 
224
+                if (!empty($updateUserBillEntityList)) {
225
+                    $this->batchUpdateUserSignScoreDataByDataList(
226
+                        array_map(function (UserSignScoreEntity $updateUserSignScoreEntity) {
227
+                            return $updateUserSignScoreEntity->toUnderlineArray();
228
+                        }, $updateUserScoreEntityList)
229
+                    );
239 230
                 }
240
-                return $userSignScoreEntity;
231
+            } catch (DbException $e) {
232
+
241 233
             }
242 234
         }
235
+        return $idList;
236
+    }
237
+
238
+    /**
239
+     * @param $dataList
240
+     * @return array
241
+     */
242
+    public function batchUpdateUserSignScoreDataByDataList($dataList): array
243
+    {
244
+        return $this->dao->saveAll($dataList);
243 245
     }
244 246
 }