123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- namespace App\Repositories;
- use App\Events\ObjectActionCreate;
- use App\Models\Action;
- use App\Models\Enums\ActionObjectType;
- use App\Models\Enums\ObjectAction;
- use App\Models\History;
- use App\Models\File;
- use App\Models\Project;
- use App\Models\Requirement;
- use App\Models\Task;
- use App\Services\File\ImageUrlService;
- use Carbon\Carbon;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- use Overtrue\CosClient\Client;
- use Overtrue\Flysystem\Cos\CosAdapter;
- class ActionRepository
- {
- public static function create(
- int $objectId,
- ActionObjectType $objectType,
- ObjectAction $action,
- int|null $projectId = null,
- string $comment = null,
- array $extraFields = [],
- array $objectChanges = [],
- ): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
- {
- $action = Action::query()->create([
- "object_id" => $objectId,
- "object_type" => $objectType->value,
- "action" => $action->value,
- "project_id" => $projectId,
- "comment" => $comment,
- "extra_fields" => $extraFields ?: null,
- "created_by" => Auth::id(),
- ]);
- if ($objectChanges) {
- History::query()->insert(array_map(fn($change) => [...$change, 'action_id' => $action->id], $objectChanges));
- }
- ObjectActionCreate::dispatch($action);
- return $action;
- }
- public static function createByProject(
- Project $project,
- ObjectAction $action,
- string $comment = null,
- array $extraFields = [],
- array $objectChanges = [],
- ): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
- {
- return self::create(
- $project->id,
- ActionObjectType::PROJECT,
- $action,
- $project->id,
- $comment,
- $extraFields,
- $objectChanges,
- );
- }
- public static function createRequirement(
- Requirement $requirement,
- ObjectAction $action,
- string $comment = null,
- array $extraFields = [],
- array $objectChanges = [],
- ): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
- {
- return self::create(
- $requirement->id,
- ActionObjectType::REQUIREMENT,
- $action,
- null,
- $comment,
- $extraFields,
- $objectChanges,
- );
- }
- public static function createByTask(
- Task $task,
- ObjectAction $action,
- string $comment = null,
- array $extraFields = [],
- array $objectChanges = [],
- ): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
- {
- return self::create(
- $task->id,
- ActionObjectType::TASK,
- $action,
- $task->project_id,
- $comment,
- $extraFields,
- $objectChanges,
- );
- }
- public static function latestDynamic(Project $project)
- {
- $actions = Action::query()
- ->where("project_id", $project->id)
- ->with(['createdBy'])
- ->orderByDesc("created_at")
- ->limit(20)
- ->get();
- $objectNames = self::objectNamesGroupByType($actions);
- $items = [];
- foreach ($actions as $action) {
- $items[] = self::actionFormat($action->toArray(), $objectNames);
- }
- return $items;
- }
- public static function actionFormat(array $action, array $objectNames): array
- {
- $labelKey = sprintf("action-labels.label.%s", $action['action']);
- $objectLabelKey = sprintf("action-labels.object_type.%s", $action['object_type']);
- $cratedAt = Carbon::parse($action['created_at'])->timezone('Asia/Shanghai');
- $imgService=new ImageUrlService();
- //存在评论时才会执行获取图片路径
- if(isset($action['comment'])){
- $action['comment']=$imgService->getImageUrl($action['comment']);
- }
- return [
- 'id' => $action['id'],
- 'time' => $cratedAt->toTimeString(),
- 'created_at' => $cratedAt->toDateTimeString(),
- 'created_by' => [
- 'id' => $action['created_by']['id'],
- 'name' => $action['created_by']['name'],
- 'username' => $action['created_by']['username'],
- ],
- 'action_label' => app('translator')->has($labelKey) ? __($labelKey) : $action['action'],
- 'object_label' => app('translator')->has($objectLabelKey) ? __($objectLabelKey) : $action['object_type'],
- 'object_id' => $action['object_id'],
- 'object_type' => $action['object_type'],
- 'object_name' => data_get($objectNames, sprintf("%s.%d", $action['object_type'], $action['object_id'])),
- 'comment' => $action['comment'],
- ];
- }
- public static function dynamic(Project $project, array $filter = [])
- {
- $actions = Action::query()->filter($filter)
- ->where("project_id", $project->id)
- ->with(['createdBy'])
- ->selectRaw("*, date_format(created_at, '%Y-%m-%d') as created_date")
- ->orderBy("created_at")
- ->get();
- $objectNames = self::objectNamesGroupByType($actions);
- $items = $actions->groupBy("created_date")->toArray();
- krsort($items);
- $data = [];
- foreach ($items as $day => $actionItems) {
- $dayItems = [
- 'date' => $day,
- 'items' => []
- ];
- foreach ($actionItems as $actionItem) {
- $dayItems['items'][] = self::actionFormat($actionItem, $objectNames);
- }
- $data[] = $dayItems;
- }
- return $data;
- }
- /**
- * @param Collection $actions
- * @return array
- */
- protected static function objectNamesGroupByType(Collection $actions): array
- {
- $groupActions = $actions->groupBy("object_type");
- $objectNames = [];
- foreach ($groupActions as $group => $items) {
- $objectType = ActionObjectType::tryFrom($group);
- $actionObjectBuilder= $objectType?->modelBuilder();
- if (! $actionObjectBuilder) {
- continue;
- }
- $objectNames[$group] = $actionObjectBuilder
- ->whereIn("id", $items->pluck("object_id")->unique()->toArray())
- ->pluck($objectType->nameField(), "id");
- }
- return $objectNames;
- }
- public static function actionWithHistory(ActionObjectType $actionObjectType, string $objectId): array
- {
- $actions = Action::query()
- ->with(['histories', 'createdBy'])
- ->where("object_type", $actionObjectType->value)
- ->where("object_id", $objectId)
- ->orderBy("created_at")
- ->get();
- $objectNames = self::objectNamesGroupByType($actions);
- self::eagerLoadingHistoryConverters($actionObjectType, $actions);
- $items = [];
- foreach ($actions as $action) {
- $item = self::actionFormat($action->toArray(), $objectNames);
- $item['histories'] = self::formatHistories($actionObjectType, $action->histories);
- $items[] = $item;
- }
- return $items;
- }
- public static function eagerLoadingHistoryConverters(ActionObjectType $actionObjectType, Collection $actions)
- {
- $detector = $actionObjectType->detectorClassName();
- $converters = [];
- foreach(call_user_func([$detector, "converters"]) as $key => $converter) {
- if (! $converter->isEagerLoading()) {
- continue;
- }
- $converters[$key] = $converter;
- }
- if (! $converters) {
- return;
- }
- $arrayFields = call_user_func([$detector, "arrayFields"]);
- $converterFields = array_keys($converters);
- $values = [];
- $pushValue = function (string $field, $fieldValue) use (&$values) {
- if (! $fieldValue) {
- return;
- }
- if (is_array($fieldValue)) {
- foreach ($fieldValue as $v) {
- if (in_array($v, $values[$field] ?? [])) {
- continue;
- }
- $values[$field][] = $v;
- }
- } else {
- if (! in_array($fieldValue, $values[$field] ?? [])) {
- $values[$field][] = $fieldValue;
- }
- }
- };
- foreach ($actions as $action) {
- foreach ($action->histories as $history) {
- if (! in_array($history->field, $converterFields)) {
- continue;
- }
- $old = in_array($history->field, $arrayFields) ? json_decode($history->old, true) : $history->old;
- $new = in_array($history->field, $arrayFields) ? json_decode($history->new, true) : $history->new;
- $pushValue($history->field, $old);
- $pushValue($history->field, $new);
- }
- }
- foreach ($values as $key => $value) {
- call_user_func([$converters[$key], "eagerLoad"], $value);
- }
- }
- public static function formatHistories(ActionObjectType $actionObjectType, Collection $histories): array
- {
- $detector = $actionObjectType->detectorClassName();
- $items = [];
- foreach ($histories as $history) {
- $labelKey = sprintf("fields.%s", $history->field);
- $item = [
- 'field' => $history->field,
- 'field_label' => app('translator')->has($labelKey) ? __($labelKey) : $history->field,
- 'new' => self::coverFieldValue($detector, $history->field ?? '', $history->new ?? ''),
- 'old' => self::coverFieldValue($detector, $history->field ?? '', $history->old ?? ''),
- 'diff' => (string)$history->diff,
- ];
- $items[] = $item;
- }
- return $items;
- }
- protected static function coverFieldValue(string $detector, string $field, string $value): mixed
- {
- if (! $detector) {
- return $value;
- }
- $converter = call_user_func([$detector, "converter"], $field);
- return $converter ? $converter->handle($value) : $value;
- }
- }
|