<?php

namespace Modules\Task\Facades;


use Carbon\Carbon;
use Modules\Task\Models\Task;
use Modules\Task\Models\User;

class TaskFacade
{
    /**
     * Notes: 执行任务
     *
     * @Author: 玄尘
     * @Date: 2022/10/20 13:15
     * @param $key
     * @param $user_id
     * @param  array  $source
     */
    public static function do($key, $user_id, array $source = [], $total = 1)
    {
        $task = Task::query()->shown()->where('key', $key)->first();

        if ($task) {
            $timeRange = self::getTimeInterval($task->cycle);

            $task->taskLogs()->create([
                'user_id' => $user_id,
                'source'  => $source,
            ]);

            $taskUser = $task->taskUsers()
                ->when($task->cycle != 'one', function ($q) use ($timeRange) {
                    $q->whereBetween('task_at', $timeRange);
                })
                ->byUserId($user_id)
                ->first();

            if (! $taskUser) {
                $taskUser = $task->taskUsers()->create([
                    'user_id' => $user_id,
                    'task_at' => now(),
                    'total'   => $total,
                ]);
            } else {
                $taskUser->incrementTotal($total);//增加数值
            }

            //存在任务
            if ($taskUser->total >= $task->task_number && $taskUser->status == User::STATUS_INTI) {
                $taskUser->status = User::STATUS_FINISH;
                $taskUser->save();
                $variable = $task->getRuleNumber();
                info('key '.$key.' variable'.$variable);
                
                if ($variable > 0) {
                    //发放水滴
                    $taskUser->user->account->rule($task->rule_id, $variable, false, $source ?? []);
                }
            }
        }

    }

    /**
     * Notes: 获取时间区间
     *
     * @Author: 玄尘
     * @Date: 2022/10/20 10:37
     * @param $type
     * @param  string  $date
     * @return array|void
     */
    public static function getTimeInterval($type, string $date = '')
    {
        switch ($type) {
            case 'day':
                return [
                    'start_at' => Carbon::parse($date)->startOfDay(),
                    'end_at'   => Carbon::parse($date)->endOfDay(),
                ];
            case 'week':
                return [
                    'start_at' => Carbon::parse($date)->startOfWeek(),
                    'end_at'   => Carbon::parse($date)->endOfWeek(),
                ];
            case 'month':
                return [
                    'start_at' => Carbon::parse($date)->startOfMonth(),
                    'end_at'   => Carbon::parse($date)->endOfMonth(),
                ];
            case 'year':
                return [
                    'start_at' => Carbon::parse($date)->startOfYear(),
                    'end_at'   => Carbon::parse($date)->endOfYear(),
                ];
            case 'one':
                return [
                    'start_at' => '',
                    'end_at'   => Carbon::parse($date)->endOfDay(),
                ];
        }
    }
}