|
|
@@ -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
|
}
|