|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+<?php
|
|
|
2
|
+
|
|
|
3
|
+namespace app\common\repositories\user;
|
|
|
4
|
+
|
|
|
5
|
+use app\common\dao\user\UserWithdrawalLimitRecordDao;
|
|
|
6
|
+use app\common\enum\user\UserWithdrawalLimitRecordEnum;
|
|
|
7
|
+use app\common\repositories\BaseRepository;
|
|
|
8
|
+use app\common\repositories\store\order\StoreOrderRepository;
|
|
|
9
|
+use app\entity\data\user\UserEntity;
|
|
|
10
|
+use app\entity\data\user\UserWithdrawalLimitRecordEntity;
|
|
|
11
|
+use think\db\exception\DbException;
|
|
|
12
|
+use think\exception\ValidateException;
|
|
|
13
|
+
|
|
|
14
|
+class UserWithdrawalLimitRecordRepository extends BaseRepository
|
|
|
15
|
+{
|
|
|
16
|
+
|
|
|
17
|
+ protected $dao;
|
|
|
18
|
+
|
|
|
19
|
+ /**
|
|
|
20
|
+ * UserWithdrawalLimitRecordRepository constructor.
|
|
|
21
|
+ * @param UserWithdrawalLimitRecordDao $dao
|
|
|
22
|
+ */
|
|
|
23
|
+ public function __construct(UserWithdrawalLimitRecordDao $dao)
|
|
|
24
|
+ {
|
|
|
25
|
+ $this->dao = $dao;
|
|
|
26
|
+ }
|
|
|
27
|
+
|
|
|
28
|
+ /**
|
|
|
29
|
+ * @param int $id
|
|
|
30
|
+ * @return UserWithdrawalLimitRecordEntity
|
|
|
31
|
+ */
|
|
|
32
|
+ public function getUserWithdrawalLimitRecordEntityById(int $id): UserWithdrawalLimitRecordEntity
|
|
|
33
|
+ {
|
|
|
34
|
+ $entityList = $this->getUserWithdrawalLimitRecordEntityListByIdList([$id]);
|
|
|
35
|
+ $entity = array_shift($entityList);
|
|
|
36
|
+ if ($entity instanceof UserWithdrawalLimitRecordEntity) {
|
|
|
37
|
+ return $entity;
|
|
|
38
|
+ } else {
|
|
|
39
|
+ /** @var UserWithdrawalLimitRecordEntity */
|
|
|
40
|
+ return UserWithdrawalLimitRecordEntity::newInstance();
|
|
|
41
|
+ }
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ /**
|
|
|
45
|
+ * @param array $idList
|
|
|
46
|
+ * @return UserWithdrawalLimitRecordEntity[]
|
|
|
47
|
+ */
|
|
|
48
|
+ public function getUserWithdrawalLimitRecordEntityListByIdList(array $idList): array
|
|
|
49
|
+ {
|
|
|
50
|
+ return $this->dao->getUserWithdrawalLimitRecordEntityListByIdList($idList);
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ public function create($data)
|
|
|
54
|
+ {
|
|
|
55
|
+ return $this->dao->create($data);
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ /**
|
|
|
59
|
+ * @param UserWithdrawalLimitRecordEntity $userWithdrawalLimitRecordEntity
|
|
|
60
|
+ * @return UserWithdrawalLimitRecordEntity
|
|
|
61
|
+ */
|
|
|
62
|
+ public function createByEntity(UserWithdrawalLimitRecordEntity $userWithdrawalLimitRecordEntity): UserWithdrawalLimitRecordEntity
|
|
|
63
|
+ {
|
|
|
64
|
+ $result = $this->create($userWithdrawalLimitRecordEntity->toUnderlineArray())->toArray();
|
|
|
65
|
+ /** @var UserWithdrawalLimitRecordEntity $entity */
|
|
|
66
|
+ $entity = UserWithdrawalLimitRecordEntity::newInstance($result);
|
|
|
67
|
+ return $entity;
|
|
|
68
|
+ }
|
|
|
69
|
+
|
|
|
70
|
+ /**
|
|
|
71
|
+ * @param $id
|
|
|
72
|
+ * @param $data
|
|
|
73
|
+ * @return int
|
|
|
74
|
+ */
|
|
|
75
|
+ public function update($id, $data): int
|
|
|
76
|
+ {
|
|
|
77
|
+ try {
|
|
|
78
|
+ return $this->dao->update($id, $data);
|
|
|
79
|
+ } catch (DbException $e) {
|
|
|
80
|
+ return 0;
|
|
|
81
|
+ }
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ /**
|
|
|
85
|
+ * @param UserWithdrawalLimitRecordEntity $userWithdrawalLimitRecordEntity
|
|
|
86
|
+ * @return UserWithdrawalLimitRecordEntity
|
|
|
87
|
+ */
|
|
|
88
|
+ public function updateByEntity(UserWithdrawalLimitRecordEntity $userWithdrawalLimitRecordEntity): UserWithdrawalLimitRecordEntity
|
|
|
89
|
+ {
|
|
|
90
|
+ if (empty($userWithdrawalLimitRecordEntity->getId())) {
|
|
|
91
|
+ return UserWithdrawalLimitRecordEntity::newInstance();
|
|
|
92
|
+ }
|
|
|
93
|
+ $result = $this->update($userWithdrawalLimitRecordEntity->getId(), $userWithdrawalLimitRecordEntity->toUnderlineArray());
|
|
|
94
|
+ if ($result > 0) {
|
|
|
95
|
+ return $this->getUserWithdrawalLimitRecordEntityById($userWithdrawalLimitRecordEntity->getId());
|
|
|
96
|
+ }
|
|
|
97
|
+ return UserWithdrawalLimitRecordEntity::newInstance();
|
|
|
98
|
+ }
|
|
|
99
|
+
|
|
|
100
|
+ /**
|
|
|
101
|
+ * 添加 提现额度
|
|
|
102
|
+ * @param int $uid
|
|
|
103
|
+ * @param int $pm
|
|
|
104
|
+ * @param float $number
|
|
|
105
|
+ * @param int $typeShop
|
|
|
106
|
+ * @param string $orderSn
|
|
|
107
|
+ * @param string $remark
|
|
|
108
|
+ * @return UserWithdrawalLimitRecordEntity
|
|
|
109
|
+ */
|
|
|
110
|
+ public function addUserWithdrawalLimitRecord(int $uid, int $pm, float $number, int $typeShop = 0, string $orderSn = '', string $remark = ''): UserWithdrawalLimitRecordEntity
|
|
|
111
|
+ {
|
|
|
112
|
+ if ($typeShop == UserWithdrawalLimitRecordEnum::TYPE_SHOP['Platform']['code']) {
|
|
|
113
|
+ /** @var StoreOrderRepository $storeOrderRepository */
|
|
|
114
|
+ $storeOrderRepository = app()->make(StoreOrderRepository::class);
|
|
|
115
|
+ $storeOrderEntity = $storeOrderRepository->getStoreOrderEntityByOrderSn($orderSn);
|
|
|
116
|
+ if (empty($storeOrderEntity->getOrderId())) {
|
|
|
117
|
+ throw new ValidateException('未查询到订单信息');
|
|
|
118
|
+ }
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
|
121
|
+ /** @var UserWithdrawalLimitRecordEntity $userWithdrawalLimitRecordEntity */
|
|
|
122
|
+ $userWithdrawalLimitRecordEntity = UserWithdrawalLimitRecordEntity::newInstance();
|
|
|
123
|
+ $userWithdrawalLimitRecordEntity->setUserId($uid)
|
|
|
124
|
+ ->setPm($pm)
|
|
|
125
|
+ ->setNumber($number)
|
|
|
126
|
+ ->setStatus(UserWithdrawalLimitRecordEnum::STATUS['PENDING']['code'])
|
|
|
127
|
+ ->setTypeShop($typeShop)
|
|
|
128
|
+ ->setOrderSn($orderSn)
|
|
|
129
|
+ ->setRemark($remark);
|
|
|
130
|
+
|
|
|
131
|
+ return $this->createByEntity($userWithdrawalLimitRecordEntity);
|
|
|
132
|
+ }
|
|
|
133
|
+
|
|
|
134
|
+ /**
|
|
|
135
|
+ * @param string $typeShop
|
|
|
136
|
+ * @param array $orderSnList
|
|
|
137
|
+ * @return UserWithdrawalLimitRecordEntity[]
|
|
|
138
|
+ */
|
|
|
139
|
+ public function getUserWithdrawalLimitRecordEntityListByOrderSnListAndPending(string $typeShop, array $orderSnList): array
|
|
|
140
|
+ {
|
|
|
141
|
+ return $this->dao->getUserWithdrawalLimitRecordEntityListByOrderSnListAndPending($typeShop, $orderSnList);
|
|
|
142
|
+ }
|
|
|
143
|
+
|
|
|
144
|
+ /**
|
|
|
145
|
+ * @param $dataList
|
|
|
146
|
+ * @return array
|
|
|
147
|
+ */
|
|
|
148
|
+ public function batchUpdateUserWithdrawalLimitRecordDataByDataList($dataList): array
|
|
|
149
|
+ {
|
|
|
150
|
+ return $this->dao->saveAll($dataList);
|
|
|
151
|
+ }
|
|
|
152
|
+
|
|
|
153
|
+ /**
|
|
|
154
|
+ * 结算 提现额度
|
|
|
155
|
+ * @param array $idList
|
|
|
156
|
+ * @return array
|
|
|
157
|
+ */
|
|
|
158
|
+ public function executeByIdList(array $idList): array
|
|
|
159
|
+ {
|
|
|
160
|
+ $userWithdrawalLimitRecordEntityList = $this->getUserWithdrawalLimitRecordEntityListByIdList($idList);
|
|
|
161
|
+ // 忽略 已经生效的 记录
|
|
|
162
|
+ $userWithdrawalLimitRecordEntityList = array_filter($userWithdrawalLimitRecordEntityList, function ($entity) {
|
|
|
163
|
+ return $entity->getStatus() != UserWithdrawalLimitRecordEnum::STATUS['VALID']['code'];
|
|
|
164
|
+ });
|
|
|
165
|
+
|
|
|
166
|
+ if (!empty($userWithdrawalLimitRecordEntityList)) {
|
|
|
167
|
+ // 获取所有记录的 归属人
|
|
|
168
|
+ $userIdList = array_map(function ($userWithdrawalLimitRecordEntity) {
|
|
|
169
|
+ return $userWithdrawalLimitRecordEntity->getUserId();
|
|
|
170
|
+ }, $userWithdrawalLimitRecordEntityList);
|
|
|
171
|
+
|
|
|
172
|
+ // 获取用户 映射信息
|
|
|
173
|
+ /** @var UserRepository $userRepository */
|
|
|
174
|
+ $userRepository = app()->make(UserRepository::class);
|
|
|
175
|
+ /** @var UserEntity[] $userEntityMapByUserId */
|
|
|
176
|
+ $userEntityMapByUserId = [];
|
|
|
177
|
+ if (!empty($userIdList)) {
|
|
|
178
|
+ $userEntityMapByUserId = $userRepository->getUserEntityMapByUidList($userIdList);
|
|
|
179
|
+ }
|
|
|
180
|
+
|
|
|
181
|
+ $updateUserEntityList = [];
|
|
|
182
|
+ $updateUserWithdrawalLimitRecordEntityList = [];
|
|
|
183
|
+ foreach ($userWithdrawalLimitRecordEntityList as $userWithdrawalLimitRecordEntity) {
|
|
|
184
|
+ if (empty($userWithdrawalLimitRecordEntity->getId()) || empty($userWithdrawalLimitRecordEntity->getUserId())) {
|
|
|
185
|
+ // throw new ValidateException('无效的账单记录');
|
|
|
186
|
+ continue;
|
|
|
187
|
+ }
|
|
|
188
|
+ if (!empty($userWithdrawalLimitRecordEntity->getTakeTime())) {
|
|
|
189
|
+ throw new ValidateException("该奖励记录已更新,请勿重复操作({$userWithdrawalLimitRecordEntity->getId()})");
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+ $userEntity = $userEntityMapByUserId[$userWithdrawalLimitRecordEntity->getUserId()];
|
|
|
193
|
+ if (empty($userEntity) || empty($userEntity->getUid())) {
|
|
|
194
|
+ throw new ValidateException("未查询到用户信息({$userWithdrawalLimitRecordEntity->getId()})");
|
|
|
195
|
+ }
|
|
|
196
|
+
|
|
|
197
|
+ // 更新 用户信息
|
|
|
198
|
+ /** @var UserEntity $updateUserEntity */
|
|
|
199
|
+ $updateUserEntity = $updateUserEntityList[$userEntity->getUid()] ?? UserEntity::newInstance();
|
|
|
200
|
+ $updateUserEntity->setUid($userEntity->getUid());
|
|
|
201
|
+ // 更新 账单 记录
|
|
|
202
|
+ /** @var UserWithdrawalLimitRecordEntity $updateUserWithdrawalLimitRecord */
|
|
|
203
|
+ $updateUserWithdrawalLimitRecord = $updateUserWithdrawalLimitRecordEntityList[$userWithdrawalLimitRecordEntity->getId()] ?? UserWithdrawalLimitRecordEntity::newInstance();
|
|
|
204
|
+
|
|
|
205
|
+ // 提现额度
|
|
|
206
|
+ if (is_null($updateUserEntity->getWithdrawalLimit())) {
|
|
|
207
|
+ $updateUserEntity->setWithdrawalLimit($userEntity->getWithdrawalLimit());
|
|
|
208
|
+ }
|
|
|
209
|
+ // 更新用户 佣金 信息
|
|
|
210
|
+ if ($userWithdrawalLimitRecordEntity->getPm() == UserWithdrawalLimitRecordEnum::PM['EXPEND']['code']) {
|
|
|
211
|
+ if ($updateUserEntity->getWithdrawalLimit() < $userWithdrawalLimitRecordEntity->getNumber()) {
|
|
|
212
|
+ throw new ValidateException('用户剩余提现额度不足');
|
|
|
213
|
+ }
|
|
|
214
|
+ $updateUserEntity->setWithdrawalLimit(bcsub($updateUserEntity->getWithdrawalLimit(), $userWithdrawalLimitRecordEntity->getNumber(), 2));
|
|
|
215
|
+ } else {
|
|
|
216
|
+ $updateUserEntity->setWithdrawalLimit(bcadd($updateUserEntity->getWithdrawalLimit(), $userWithdrawalLimitRecordEntity->getNumber(), 2));
|
|
|
217
|
+ }
|
|
|
218
|
+
|
|
|
219
|
+ // 更新 账单记录 状态
|
|
|
220
|
+ $updateUserWithdrawalLimitRecord
|
|
|
221
|
+ ->setId($userWithdrawalLimitRecordEntity->getId())
|
|
|
222
|
+ ->setStatus(UserWithdrawalLimitRecordEnum::STATUS['VALID']['code'])
|
|
|
223
|
+ ->setTakeTime(date('Y-m-d H:i:s'))
|
|
|
224
|
+ ->setBalance($updateUserEntity->getWithdrawalLimit());
|
|
|
225
|
+
|
|
|
226
|
+ $updateUserEntityList[$userEntity->getUid()] = $updateUserEntity;
|
|
|
227
|
+ $updateUserWithdrawalLimitRecordEntityList[$userWithdrawalLimitRecordEntity->getId()] = $updateUserWithdrawalLimitRecord;
|
|
|
228
|
+ }
|
|
|
229
|
+ if (!empty($updateUserEntityList)) {
|
|
|
230
|
+ $userRepository->batchUpdateUserDataByDataList(
|
|
|
231
|
+ array_map(function (UserEntity $updateUserEntity) {
|
|
|
232
|
+ return $updateUserEntity->toUnderlineArray();
|
|
|
233
|
+ }, $updateUserEntityList)
|
|
|
234
|
+ );
|
|
|
235
|
+ }
|
|
|
236
|
+
|
|
|
237
|
+ if (!empty($updateUserWithdrawalLimitRecordEntityList)) {
|
|
|
238
|
+ $this->batchUpdateUserWithdrawalLimitRecordDataByDataList(
|
|
|
239
|
+ array_map(function (UserWithdrawalLimitRecordEntity $updateUserWithdrawalLimitRecordEntity) {
|
|
|
240
|
+ return $updateUserWithdrawalLimitRecordEntity->toUnderlineArray();
|
|
|
241
|
+ }, $updateUserWithdrawalLimitRecordEntityList)
|
|
|
242
|
+ );
|
|
|
243
|
+ }
|
|
|
244
|
+ }
|
|
|
245
|
+
|
|
|
246
|
+ return $idList;
|
|
|
247
|
+ }
|
|
|
248
|
+}
|