<?php

namespace Modules\Task\Traits;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Modules\Task\Models\Task;
use Modules\Task\Models\User;

trait HasTasks
{

    /**
     * Notes: 做任务列表
     *
     * @Author: 玄尘
     * @Date: 2022/10/21 9:53
     * @return HasMany
     */
    public function tasks(): HasMany
    {
        return $this->hasMany(User::class);
    }

    /**
     * Notes: 获取任务数据
     *
     * @Author: 玄尘
     * @Date: 2022/10/21 9:54
     */
    public function getTaskCount($task_id): array
    {
        $task = Task::find($task_id);

        return $task->getUserTask($this);
    }

    /**
     * Notes: 所有任务数据
     *
     * @Author: 玄尘
     * @Date: 2022/10/21 9:59
     * @param $task_id
     * @return array
     */
    public function getTasksCount(): array
    {
        return $this->tasks()->map(function ($taskUser) {
            return $taskUser->task->getUserTask($this);
        });
    }

}