|
|
@@ -3,12 +3,14 @@
|
|
3
|
3
|
namespace app\common\repositories\shared;
|
|
4
|
4
|
|
|
5
|
5
|
use app\common\dao\shared\SharedProsperityBalanceLogDao;
|
|
|
6
|
+use app\common\dao\shared\SharedProsperityPointLogDao;
|
|
6
|
7
|
use app\common\dao\shared\SharedProsperityRecommendConfigDao;
|
|
7
|
8
|
use app\common\dao\shared\SharedProsperityUserDao;
|
|
8
|
|
-use app\common\dao\store\order\StoreOrderDao;
|
|
9
|
9
|
use app\common\enum\CommonEnum;
|
|
10
|
10
|
use app\common\repositories\BaseRepository;
|
|
|
11
|
+use think\Collection;
|
|
11
|
12
|
use think\facade\Db;
|
|
|
13
|
+use think\facade\Log;
|
|
12
|
14
|
|
|
13
|
15
|
class SharedProsperityTaskRepository extends BaseRepository
|
|
14
|
16
|
{
|
|
|
@@ -123,4 +125,91 @@ class SharedProsperityTaskRepository extends BaseRepository
|
|
123
|
125
|
throw new \Exception($e->getMessage());
|
|
124
|
126
|
}
|
|
125
|
127
|
}
|
|
|
128
|
+
|
|
|
129
|
+ /**
|
|
|
130
|
+ * @param $data
|
|
|
131
|
+ * @return true
|
|
|
132
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
133
|
+ * @throws \think\db\exception\DbException
|
|
|
134
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
135
|
+ * @author 史晨
|
|
|
136
|
+ * @date 2026/2/10 15:22
|
|
|
137
|
+ * 感恩奖
|
|
|
138
|
+ */
|
|
|
139
|
+ public function thank($data)
|
|
|
140
|
+ {
|
|
|
141
|
+ // 获取时间范围内订单
|
|
|
142
|
+ $start = $data['startTime'];
|
|
|
143
|
+ $end = $data['endTime'];
|
|
|
144
|
+
|
|
|
145
|
+ // 获取pv
|
|
|
146
|
+ /** @var SharedProsperityRewardRecordRepository $rewardRecordRepository */
|
|
|
147
|
+ $rewardRecordRepository = app()->make(SharedProsperityRewardRecordRepository::class);
|
|
|
148
|
+ $rewardList = $rewardRecordRepository->getListByTime($start, $end);
|
|
|
149
|
+ if (empty($rewardList)) {
|
|
|
150
|
+ throw new \Exception('没有订单');
|
|
|
151
|
+ }
|
|
|
152
|
+
|
|
|
153
|
+ // user_id分区求和 [id]=>[number]
|
|
|
154
|
+ $groupedResult = array_reduce($rewardList, function ($carry, $item) {
|
|
|
155
|
+ $userId = $item['user_id'];
|
|
|
156
|
+ $number = floatval($item['number']);
|
|
|
157
|
+
|
|
|
158
|
+ if (!isset($carry[$userId])) {
|
|
|
159
|
+ $carry[$userId] = 0;
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
|
162
|
+ $carry[$userId] += $number;
|
|
|
163
|
+
|
|
|
164
|
+ return $carry;
|
|
|
165
|
+ }, []);
|
|
|
166
|
+
|
|
|
167
|
+ if (empty($groupedResult)) {
|
|
|
168
|
+ throw new \Exception('分组求和错误');
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ /** @var SharedProsperityUserRepository $userRepository */
|
|
|
172
|
+ $userRepository = app()->make(SharedProsperityUserRepository::class);
|
|
|
173
|
+
|
|
|
174
|
+ /** @var SharedProsperityPointLogDao $balanceDao */
|
|
|
175
|
+ $logDao = app()->make(SharedProsperityPointLogDao::class);
|
|
|
176
|
+
|
|
|
177
|
+ DB::startTrans();
|
|
|
178
|
+ try {
|
|
|
179
|
+ foreach ($groupedResult as $key => $value) {
|
|
|
180
|
+ // 获取感恩奖比例
|
|
|
181
|
+ $radio = CommonEnum::ORDER_EARNING_ALLOCATION['SharedProsperity']['Thank']['code'];
|
|
|
182
|
+ // 计算积分
|
|
|
183
|
+ $points = floor($value * $radio * 100) / 100;
|
|
|
184
|
+ // 获取上级
|
|
|
185
|
+ $userData = $userRepository->getSharedProsperityUserEntityByUserId($key);
|
|
|
186
|
+ $parentId = $userData->getParentId();
|
|
|
187
|
+ if ($parentId == 0) {
|
|
|
188
|
+ Log::info('共富感恩奖发放:' . $key . '的上级是0,不发放感恩奖');
|
|
|
189
|
+ }
|
|
|
190
|
+ // 给上级发积分,记录日志
|
|
|
191
|
+ if ($parentId > 0) {
|
|
|
192
|
+ $parentData = $userRepository->getSharedProsperityUserEntityByUserId($parentId);
|
|
|
193
|
+ // 记录日志
|
|
|
194
|
+ $logDao->create([
|
|
|
195
|
+ 'user_id' => $parentId,
|
|
|
196
|
+ 'total' => $value,
|
|
|
197
|
+ 'radio' => $radio,
|
|
|
198
|
+ 'old' => $parentData->getPoints(),
|
|
|
199
|
+ 'number' => $points,
|
|
|
200
|
+ 'after' => $parentData->getPoints() + $points
|
|
|
201
|
+ ]);
|
|
|
202
|
+
|
|
|
203
|
+ // 发放积分
|
|
|
204
|
+ $userRepository->updateByUserId($parentId, ['points' => ['inc', $points]]);
|
|
|
205
|
+ }
|
|
|
206
|
+ }
|
|
|
207
|
+
|
|
|
208
|
+ Db::commit();
|
|
|
209
|
+ return true;
|
|
|
210
|
+ } catch (\Exception $e) {
|
|
|
211
|
+ DB::rollback();
|
|
|
212
|
+ throw new \Exception($e->getMessage());
|
|
|
213
|
+ }
|
|
|
214
|
+ }
|
|
126
|
215
|
}
|