ActivityWorkAuditSuccess.php 5.19 KB
<?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;
    }
}