ActivityWorkAuditSuccess.php
5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace App\Actions;
use App\Enums\ActivityStatusEnum;
use App\Enums\ActivityWorkStatusEnum;
use App\Enums\ActivityWorkTypeEnum;
use App\Enums\BrokerPushMatchRecordStatusEnum;
use App\Jobs\BrokerPushMatchRecordJob;
use App\Models\Activity;
use App\Models\ActivityUser;
use App\Models\BrokerPushConfig;
use App\Models\BrokerPushMatchRecord;
use App\Models\BrokerUserConfig;
use App\Models\BrokerUserConfigItem;
use App\Models\SystemConfig;
use App\Models\Views\ActivityWork;
use App\Notifications\ActivitySingerSubmitWorkPassNotification;
use App\Notifications\ActivitySubmitWorkPassNotification;
use App\Support\Model;
use DB;
use Lorisleiva\Actions\Concerns\AsAction;
class ActivityWorkAuditSuccess
{
use AsAction;
private int $brokerId;
private BrokerUserConfig|Model $brokerUserConfig;
private string $brokerUserLevel;
/**
* @param \App\Models\Views\ActivityWork $work
* @param string $remark
* @return void
*/
public function handle(ActivityWork $work): void
{
$this->brokerId = (int)$work->getAttribute('business_id');
DB::transaction(function () use ($work) {
Activity::query()->whereKey($work->getAttribute('activity_id'))
->update(['status' => ActivityStatusEnum::MATCH, 'match_at' => now()->toDateTimeString()]);
ActivityUser::query()->whereKey($work->getKey())
->update(['status' => ActivityWorkStatusEnum::SUCCESS, 'broker_id' => $this->brokerId, 'broker_level' => '']);
ActivityWork::query()->where('activity_id', $work->getAttribute('activity_id'))
->where('type', ActivityWorkTypeEnum::SUBMIT)->where('status', ActivityWorkStatusEnum::WAIT)
->eachById(fn(ActivityWork $activityWork) => ActivityWorkAuditFail::dispatch($activityWork));
});
$this->pushUserNotification($work);
$this->pushBusinessNotification($work);
}
/**
* @param \App\Models\Views\ActivityWork $work
* @param string $remark
* @return void
*/
private function pushUserNotification(ActivityWork $work): void
{
$work->user?->notify(new ActivitySubmitWorkPassNotification($work));
}
/**
* @param \App\Models\Views\ActivityWork $work
* @return void
*/
private function pushBusinessNotification(ActivityWork $work): void
{
$work->business?->notify(new ActivitySingerSubmitWorkPassNotification($work));
if ($config = $this->getUserConfig()) {
$level = $this->getUserLevel($config);
ActivityUser::query()->whereKey($work->getKey())->update(['broker_id' => $this->brokerId, 'broker_level' => $level]);
if ($pushConfig = BrokerPushConfig::query()->where('identifier', $level)->where('match_title', '!=', '')->first()) {
BrokerPushMatchRecord::created(static fn($record) => BrokerPushMatchRecordJob::dispatch($record));
BrokerPushMatchRecord::query()->create([
'broker_id' => $this->brokerId,
'broker_level' => str_replace('对应分类:', '', $level),
'user_id' => $work->getAttribute('user_id'),
'user_tag' => $work->user->authTags()->pluck('name')?->join('|') ?? '',
'activity_id' => $work->getAttribute('activity_id'),
'project_id' => $work->getAttribute('project_id'),
'title' => $pushConfig->getAttribute('match_title'),
'content' => str_replace(
['{border}', '{singer}', '{start}', '{end}', '{type}', '{brand}', '{song}'],
[
$work->business->getAttribute('nick_name'),
$work->user->getAttribute('nick_name'),
$config->getAttribute('begin_at'),
$config->getAttribute('end_at'),
str_replace('对应分类:', '', $level),
$work->project()->value('name'),
$work->getAttribute('activity_name')
],
$pushConfig->getAttribute('match_intro')
),
'send_at' => now()->toDateTimeString(),
'status' => BrokerPushMatchRecordStatusEnum::PROCESSING
]);
}
}
}
/**
* @return \App\Support\Model|null
*/
private function getUserConfig(): ?Model
{
return BrokerUserConfig::query()->where('begin_at', '<=', now()->toDateTimeString())->where('end_at', '>=', now()->toDateTimeString())->first();
}
/**
* @param \App\Support\Model $config
* @return string
*/
private function getUserLevel(Model $config): string
{
if (!$level = BrokerUserConfigItem::query()->where('config_id', $config->getKey())->where('user_id', $this->brokerId)->value('identifier')) {
$level = SystemConfig::query()->where('identifier', 'g65owmrgKSUhxV2afndUg')->value('content') ?? '';
$level = (empty($level) ? '' : '对应分类:' . $level);
}
return $level;
}
}