Explorar el Código

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

sunxbiao hace 11 meses
padre
commit
537a9c20f8

+ 62 - 44
app/common/repositories/user/UserSignScoreRepository.php

@@ -6,6 +6,7 @@ use app\common\dao\user\UserSignScoreDao;
6 6
 use app\common\enum\user\UserSignScoreEnum;
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\UserSignScoreEntity;
10 11
 use think\db\exception\DbException;
11 12
 use think\exception\ValidateException;
@@ -163,64 +164,81 @@ class UserSignScoreRepository extends BaseRepository
163 164
 
164 165
     /**
165 166
      * 将 积分 记录设置为生效状态 并对用户的 积分 字段进行增减
166
-     * @param int $id
167
+     * @param array $idList
167 168
      * @return UserSignScoreEntity
168 169
      */
169 170
     public function executeByIdList(array $idList): UserSignScoreEntity
170 171
     {
171 172
         $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()));
173
+        $userIdList = [];
174
+
175
+        // 忽略 已经生效的 记录
176
+        $userSignScoreEntityList = array_filter($userSignScoreEntityList, function ($entity) use (&$userIdList) {
177
+            // 积分数据不为空 用户不为空 状态属于待结算 并且结算字段为空
178
+            if ((!empty($entity->getId()) && !empty($entity->getUid()) && ($entity->getStatus() == UserSignScoreEnum::STATUS['Pending']['code']) && empty($entity->getSettlementTime()))) {
179
+                $userIdList[] = $entity->getUid();
180
+                return true;
181
+            }
182
+            return false;
177 183
         });
178 184
 
179 185
         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
-
186
+            // 获取用户 映射信息
194 187
             /** @var UserRepository $userRepository */
195 188
             $userRepository = app()->make(UserRepository::class);
196
-            $userEntity = $userRepository->getUserEntityByUid($userSignScoreEntity->getUid());
197
-            if (empty($userEntity->getUid())) {
198
-                throw new ValidateException('未查询到用户信息');
189
+            /** @var UserEntity[] $userEntityMapByUid */
190
+            $userEntityMapByUid = [];
191
+            if (!empty($userIdList)) {
192
+                $userEntityMapByUid = $userRepository->getUserEntityMapByUidList($userIdList);
199 193
             }
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('用户积分不足');
194
+
195
+            $updateUserEntityList = [];
196
+            $updateUserScoreEntityList = [];
197
+            foreach ($userSignScoreEntityList as $userSignScoreEntity) {
198
+                // if (empty($userSignScoreEntity->getId()) || empty($userSignScoreEntity->getUid())) {
199
+                //     throw new ValidateException('无效的积分记录');
200
+                // }
201
+                // if ($userSignScoreEntity->getStatus() == UserSignScoreEnum::STATUS['Invalid']['code']) {
202
+                //     throw new ValidateException('该积分记录已失效');
203
+                // }
204
+                // if (!empty($userSignScoreEntity->getSettlementTime()) || $userSignScoreEntity->getStatus() == UserSignScoreEnum::STATUS['Valid']['code']) {
205
+                //     throw new ValidateException('该积分记录已更新,请勿重复操作');
206
+                // }
207
+
208
+                $userEntity = $userEntityMapByUid[$userSignScoreEntity->getUid()];
209
+                if (empty($userEntity) || empty($userEntity->getUid())) {
210
+                    throw new ValidateException("未查询到用户信息(scoreId:{$userSignScoreEntity->getId()})");
204 211
                 }
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) {
212
+                // 更新 用户信息
213
+                /** @var UserEntity $updateUserEntity */
214
+                $updateUserEntity = $updateUserEntityList[$userEntity->getUid()] ?? UserEntity::newInstance();
215
+                $updateUserEntity->setUid($userEntity->getUid());
216
+
217
+
218
+                if ($userSignScoreEntity->getPm() == UserSignScoreEnum::PM['Expend']['code']) {
219
+                    $surplus = bcsub($userEntity->getScore(), $userSignScoreEntity->getRewardScore(), 2);
220
+                    if ($surplus < 0.00) {
221
+                        throw new ValidateException('用户积分不足');
222
+                    }
223
+                } else {
224
+                    $surplus = bcadd($userEntity->getScore(), $userSignScoreEntity->getRewardScore(), 2);
225
+                }
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) {
220 238
 
239
+                }
240
+                return $userSignScoreEntity;
221 241
             }
222
-            return $userSignScoreEntity;
223 242
         }
224
-
225 243
     }
226 244
 }

+ 91 - 91
app/entity/data/user/UserSignScoreEntity.php

@@ -6,289 +6,289 @@ use app\entity\data\DataEntity;
6 6
 
7 7
 class UserSignScoreEntity extends DataEntity
8 8
 {
9
-    public $id = 0;  // 自增id
10
-    public $uid = 0;  // 会员id
11
-    public $continuityDay = 0;  // 连续签到几天
12
-    public $rewardScore = 0.00;  // 赠送积分数量
13
-    public $signinTime = 0;  // 签到时间
14
-    public $sourceUid = 0;  // 赠送积分来源用户id
15
-    public $status = 0;  // 积分状态:1-有效,0-失效,-1待确认
16
-    public $mark = '';  // 备注
17
-    public $orderId = '';  // 订单id
18
-    public $pm = 1;  // 1-收入,2-支出
19
-    public $surplus = 0.00;  // 剩余
20
-    public $orderType = '';  // 订单类型,关联订单ID
21
-    public $addtime = 0;  // 添加时间
22
-    public $sourceType = 1;  // 积分来源类型:1签到,2分享好友注册赠送,3自己下单赠送,4好友下单赠送,5购买产品消耗
23
-    public $settlementTime = 0;  // 结算时间
9
+    public $id = null;  // 自增id
10
+    public $uid = null;  // 会员id
11
+    public $continuity_day = null;  // 连续签到几天
12
+    public $reward_score = null;  // 赠送积分数量
13
+    public $signin_time = null;  // 签到时间
14
+    public $source_uid = null;  // 赠送积分来源用户id
15
+    public $status = null;  // 积分状态1-有效 0-失效 -1待确认
16
+    public $mark = null;  // 备注
17
+    public $order_id = null;  // 订单id
18
+    public $pm = null;  // 1-收入。2-支出
19
+    public $surplus = null;  // 剩余
20
+    public $order_type = null;  // 订单类型。关联订单ID
21
+    public $addtime = null;  // 添加时间
22
+    public $source_type = null;  // 积分类型:1签到,2分享好友注册赠送,3自己下单赠送,4好友下单赠送,5.购买产品消耗
23
+    public $settlement_time = null;  // 结算时间
24 24
 
25 25
     /**
26
-     * @return int
26
+     * @return mixed
27 27
      */
28
-    public function getId(): int
28
+    public function getId()
29 29
     {
30 30
         return $this->id;
31 31
     }
32 32
 
33 33
     /**
34
-     * @param int $id
34
+     * @param mixed $id
35 35
      * @return UserSignScoreEntity
36 36
      */
37
-    public function setId(int $id): UserSignScoreEntity
37
+    public function setId($id): UserSignScoreEntity
38 38
     {
39 39
         $this->id = $id;
40 40
         return $this;
41 41
     }
42 42
 
43 43
     /**
44
-     * @return int
44
+     * @return mixed
45 45
      */
46
-    public function getUid(): int
46
+    public function getUid()
47 47
     {
48 48
         return $this->uid;
49 49
     }
50 50
 
51 51
     /**
52
-     * @param int $uid
52
+     * @param mixed $uid
53 53
      * @return UserSignScoreEntity
54 54
      */
55
-    public function setUid(int $uid): UserSignScoreEntity
55
+    public function setUid($uid): UserSignScoreEntity
56 56
     {
57 57
         $this->uid = $uid;
58 58
         return $this;
59 59
     }
60 60
 
61 61
     /**
62
-     * @return int
62
+     * @return mixed
63 63
      */
64
-    public function getContinuityDay(): int
64
+    public function getContinuityDay()
65 65
     {
66
-        return $this->continuityDay;
66
+        return $this->continuity_day;
67 67
     }
68 68
 
69 69
     /**
70
-     * @param int $continuityDay
70
+     * @param mixed $continuity_day
71 71
      * @return UserSignScoreEntity
72 72
      */
73
-    public function setContinuityDay(int $continuityDay): UserSignScoreEntity
73
+    public function setContinuityDay($continuity_day): UserSignScoreEntity
74 74
     {
75
-        $this->continuityDay = $continuityDay;
75
+        $this->continuity_day = $continuity_day;
76 76
         return $this;
77 77
     }
78 78
 
79 79
     /**
80
-     * @return float
80
+     * @return mixed
81 81
      */
82
-    public function getRewardScore(): float
82
+    public function getRewardScore()
83 83
     {
84
-        return $this->rewardScore;
84
+        return $this->reward_score;
85 85
     }
86 86
 
87 87
     /**
88
-     * @param float $rewardScore
88
+     * @param mixed $reward_score
89 89
      * @return UserSignScoreEntity
90 90
      */
91
-    public function setRewardScore(float $rewardScore): UserSignScoreEntity
91
+    public function setRewardScore($reward_score): UserSignScoreEntity
92 92
     {
93
-        $this->rewardScore = $rewardScore;
93
+        $this->reward_score = $reward_score;
94 94
         return $this;
95 95
     }
96 96
 
97 97
     /**
98
-     * @return int
98
+     * @return mixed
99 99
      */
100
-    public function getSigninTime(): int
100
+    public function getSigninTime()
101 101
     {
102
-        return $this->signinTime;
102
+        return $this->signin_time;
103 103
     }
104 104
 
105 105
     /**
106
-     * @param int $signinTime
106
+     * @param mixed $signin_time
107 107
      * @return UserSignScoreEntity
108 108
      */
109
-    public function setSigninTime(int $signinTime): UserSignScoreEntity
109
+    public function setSigninTime($signin_time): UserSignScoreEntity
110 110
     {
111
-        $this->signinTime = $signinTime;
111
+        $this->signin_time = $signin_time;
112 112
         return $this;
113 113
     }
114 114
 
115 115
     /**
116
-     * @return int
116
+     * @return mixed
117 117
      */
118
-    public function getSourceUid(): int
118
+    public function getSourceUid()
119 119
     {
120
-        return $this->sourceUid;
120
+        return $this->source_uid;
121 121
     }
122 122
 
123 123
     /**
124
-     * @param int $sourceUid
124
+     * @param mixed $source_uid
125 125
      * @return UserSignScoreEntity
126 126
      */
127
-    public function setSourceUid(int $sourceUid): UserSignScoreEntity
127
+    public function setSourceUid($source_uid): UserSignScoreEntity
128 128
     {
129
-        $this->sourceUid = $sourceUid;
129
+        $this->source_uid = $source_uid;
130 130
         return $this;
131 131
     }
132 132
 
133 133
     /**
134
-     * @return int
134
+     * @return mixed
135 135
      */
136
-    public function getStatus(): int
136
+    public function getStatus()
137 137
     {
138 138
         return $this->status;
139 139
     }
140 140
 
141 141
     /**
142
-     * @param int $status
142
+     * @param mixed $status
143 143
      * @return UserSignScoreEntity
144 144
      */
145
-    public function setStatus(int $status): UserSignScoreEntity
145
+    public function setStatus($status): UserSignScoreEntity
146 146
     {
147 147
         $this->status = $status;
148 148
         return $this;
149 149
     }
150 150
 
151 151
     /**
152
-     * @return string
152
+     * @return mixed
153 153
      */
154
-    public function getMark(): string
154
+    public function getMark()
155 155
     {
156 156
         return $this->mark;
157 157
     }
158 158
 
159 159
     /**
160
-     * @param string $mark
160
+     * @param mixed $mark
161 161
      * @return UserSignScoreEntity
162 162
      */
163
-    public function setMark(string $mark): UserSignScoreEntity
163
+    public function setMark($mark): UserSignScoreEntity
164 164
     {
165 165
         $this->mark = $mark;
166 166
         return $this;
167 167
     }
168 168
 
169 169
     /**
170
-     * @return string
170
+     * @return mixed
171 171
      */
172
-    public function getOrderId(): string
172
+    public function getOrderId()
173 173
     {
174
-        return $this->orderId;
174
+        return $this->order_id;
175 175
     }
176 176
 
177 177
     /**
178
-     * @param string $orderId
178
+     * @param mixed $order_id
179 179
      * @return UserSignScoreEntity
180 180
      */
181
-    public function setOrderId(string $orderId): UserSignScoreEntity
181
+    public function setOrderId($order_id): UserSignScoreEntity
182 182
     {
183
-        $this->orderId = $orderId;
183
+        $this->order_id = $order_id;
184 184
         return $this;
185 185
     }
186 186
 
187 187
     /**
188
-     * @return int
188
+     * @return mixed
189 189
      */
190
-    public function getPm(): int
190
+    public function getPm()
191 191
     {
192 192
         return $this->pm;
193 193
     }
194 194
 
195 195
     /**
196
-     * @param int $pm
196
+     * @param mixed $pm
197 197
      * @return UserSignScoreEntity
198 198
      */
199
-    public function setPm(int $pm): UserSignScoreEntity
199
+    public function setPm($pm): UserSignScoreEntity
200 200
     {
201 201
         $this->pm = $pm;
202 202
         return $this;
203 203
     }
204 204
 
205 205
     /**
206
-     * @return float
206
+     * @return mixed
207 207
      */
208
-    public function getSurplus(): float
208
+    public function getSurplus()
209 209
     {
210 210
         return $this->surplus;
211 211
     }
212 212
 
213 213
     /**
214
-     * @param float $surplus
214
+     * @param mixed $surplus
215 215
      * @return UserSignScoreEntity
216 216
      */
217
-    public function setSurplus(float $surplus): UserSignScoreEntity
217
+    public function setSurplus($surplus): UserSignScoreEntity
218 218
     {
219 219
         $this->surplus = $surplus;
220 220
         return $this;
221 221
     }
222 222
 
223 223
     /**
224
-     * @return string
224
+     * @return mixed
225 225
      */
226
-    public function getOrderType(): string
226
+    public function getOrderType()
227 227
     {
228
-        return $this->orderType;
228
+        return $this->order_type;
229 229
     }
230 230
 
231 231
     /**
232
-     * @param string $orderType
232
+     * @param mixed $order_type
233 233
      * @return UserSignScoreEntity
234 234
      */
235
-    public function setOrderType(string $orderType): UserSignScoreEntity
235
+    public function setOrderType($order_type): UserSignScoreEntity
236 236
     {
237
-        $this->orderType = $orderType;
237
+        $this->order_type = $order_type;
238 238
         return $this;
239 239
     }
240 240
 
241 241
     /**
242
-     * @return int
242
+     * @return mixed
243 243
      */
244
-    public function getAddtime(): int
244
+    public function getAddtime()
245 245
     {
246 246
         return $this->addtime;
247 247
     }
248 248
 
249 249
     /**
250
-     * @param int $addtime
250
+     * @param mixed $addtime
251 251
      * @return UserSignScoreEntity
252 252
      */
253
-    public function setAddtime(int $addtime): UserSignScoreEntity
253
+    public function setAddtime($addtime): UserSignScoreEntity
254 254
     {
255 255
         $this->addtime = $addtime;
256 256
         return $this;
257 257
     }
258 258
 
259 259
     /**
260
-     * @return int
260
+     * @return mixed
261 261
      */
262
-    public function getSourceType(): int
262
+    public function getSourceType()
263 263
     {
264
-        return $this->sourceType;
264
+        return $this->source_type;
265 265
     }
266 266
 
267 267
     /**
268
-     * @param int $sourceType
268
+     * @param mixed $source_type
269 269
      * @return UserSignScoreEntity
270 270
      */
271
-    public function setSourceType(int $sourceType): UserSignScoreEntity
271
+    public function setSourceType($source_type): UserSignScoreEntity
272 272
     {
273
-        $this->sourceType = $sourceType;
273
+        $this->source_type = $source_type;
274 274
         return $this;
275 275
     }
276 276
 
277 277
     /**
278
-     * @return int
278
+     * @return mixed
279 279
      */
280
-    public function getSettlementTime(): int
280
+    public function getSettlementTime()
281 281
     {
282
-        return $this->settlementTime;
282
+        return $this->settlement_time;
283 283
     }
284 284
 
285 285
     /**
286
-     * @param int $settlementTime
286
+     * @param mixed $settlement_time
287 287
      * @return UserSignScoreEntity
288 288
      */
289
-    public function setSettlementTime(int $settlementTime): UserSignScoreEntity
289
+    public function setSettlementTime($settlement_time): UserSignScoreEntity
290 290
     {
291
-        $this->settlementTime = $settlementTime;
291
+        $this->settlement_time = $settlement_time;
292 292
         return $this;
293 293
     }
294 294
 }