<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 21/11/23
 * Time: 19:05
 */

namespace App\Services\Api;


use App\Models\Course;
use App\Models\CourseUser;
use App\Models\GameUser;
use Illuminate\Support\Facades\DB;

class CourseService
{
    public function __construct()
    {
        $this->user = auth('api')->user();
    }

    //附近的球场
    public function getNearByCourses($lng, $lat)
    {
        $list = Course::query()
            ->whereNull('deleted_at')
            ->select('id', 'name', 'img', 'address', 'position', 'lng', 'lat', DB::raw('(st_distance(point(lng, lat), point(' . $lng . ',' . $lat . ')) * 111195) as distance'))
            ->orderBy('distance', 'asc')
            ->limit(15)
            ->get()
            ->toArray();

        array_walk($list, function (&$v) {
            $v['distance'] = round($v['distance'] / 1000, 2) . 'km';
        });

        //获取我参加过的球场和次数
        $idCounts = (new GameUser())->setTable('gu')->from('game_users as gu')
            ->join('games as g', 'g.id', '=', 'gu.game_id')
            ->join('courses as c', 'c.id', '=', 'g.course_id')
            ->where('user_id', $this->user->id)
            ->whereNull('gu.deleted_at')
            ->whereNotNull('g.end_time')
            ->select('c.id', DB::raw('COUNT(c.id) as count'))
            ->groupBy('c.id')
            ->get()
            ->toArray();

        foreach ($list as $key => $val) {
            $val['count'] = 0;
            foreach ($idCounts as $key2 => $val2) {
                if($val['id'] == $val2['id']){
                    $val['count'] = $val2['count'];
                }
            }
            $list[$key] = $val;
        }

        return $list;
    }

    //获取用户办过会员的球场
    public function getUserCourseList($status, $userId)
    {
        $list = (new CourseUser())->setTable('cu')->from('course_users as cu')
            ->join('courses as c', 'cu.course_id', '=', 'c.id')
            ->where('user_id', $userId)
            ->where('cu.status', $status)
            ->whereNull('c.deleted_at')
            ->whereNull('cu.deleted_at')
            ->select('c.id as course_id', 'c.name as course_name', 'c.address', 'c.img', 'cu.type', 'cu.status')
            ->get()
            ->toArray();

        foreach ($list as $key => $val) {
            $val['status_name'] = trans('api.WAITING_AUDIT');
            if ($val['status'] == 1) {
                $val['status_name'] = trans('api.APPROVED');
            }
            if ($val['status'] == 2) {
                $val['status_name'] = trans('api.AUDIT_FAILED');
            }
            $val['type_name'] = ($val['type'] == 1) ? trans('api.HOME_FIELD') : trans('api.VICE_FIELD');

            $list[$key] = $val;
        }

        return $list;
    }

    //获取可供选择的球场
    public function getCourseList()
    {
        $list = Course::query()->whereNull('deleted_at')->select('id as course_id', 'name', 'img')->get()->toArray();

        return SortService::getResultList($list);
    }

}