MemberController.php 1.94 KB
<?php

namespace App\Http\Container\AppSection\Controllers\Auth\Group;

use App\Enums\ActivityWorkStatusEnum;
use App\Enums\ActivityWorkTypeEnum;
use App\Helpers\JsonResource;
use App\Models\ActivityUser;
use App\Models\User;
use Auth;
use Hikoon\LaravelApi\Support\ApiController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class MemberController extends ApiController
{
    public function __construct()
    {
        $this->middleware('emptyToNull');
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $pageSize = $request->integer('size', 20);

        $data = User::filter(['nickName' => $request->get('text')])
            ->with(['authTags:id,name', 'userTag:id,name,icon,frame'])
            ->select(['id', 'avatar', 'nick_name', 'chat_mode', 'identity', 'user_tag_id'])
            ->where("business_id", Auth::id())->latest()->paginate($pageSize);

        return JsonResource::success(JsonResource::SUCCESS, ['data' => $data->items(), 'count' => $data->total()]);
    }

    /**
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(int $id): JsonResponse
    {
        $user = User::with(['authTags:id,name', 'styleTags:id,name', 'userTag:id,name,icon,frame'])
            ->select(['id', 'nick_name', 'real_name', 'avatar', 'last_login', 'rate', 'chat_mode', 'company', 'identity', 'user_tag_id'])
            ->findOrFail($id);

        if ($user) {
            $user->setAttribute('attend_count', ActivityUser::where('user_id', $id)->where('type', ActivityWorkTypeEnum::SUBMIT)->count());
            $user->setAttribute('check_count', ActivityUser::where('user_id', $id)->where('type', ActivityWorkTypeEnum::SUBMIT)->where('status', ActivityWorkStatusEnum::SUCCESS)->count());
        }

        return JsonResource::success(JsonResource::SUCCESS, $user);
    }
}