<?php


namespace App\Http\Controllers\Api;


use App\Models\DynamicModel;
use App\Models\UserLikeModel;
use App\Models\UserSystemMessageModel;
use App\Services\NoticeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NoticeController extends Controller
{
    protected $noticeService;
    public function __construct()
    {
        $this->noticeService = new NoticeService();
    }

    /**
     * 获取消息列表
     * @param Request $request
     */
    public function get_notice_list(Request $request){
        try {
            $user = auth('api')->user();
            $res['notice_list'] = [];
            //未读喜欢数量
            $res['like_num'] = UserLikeModel::query()->where(["like_id"=>$user->id,'status'=>0])->count();
            //未读点赞数量
            $res['zan_num'] = DynamicModel::query()->with(["user"])->where('status','=',0)
                ->whereHas("user",function($query)use($user){
                    $query->where('id',$user->id)->select();
                })->count();
            //未读系统消息
            $res['system_num'] = UserSystemMessageModel::query()->where(['user_id'=>$user->id,'status'=>0])->count();
        }catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json($res);
    }

    /**
     * 获取喜欢列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function get_like_list(Request $request){
        try {
            $user = auth('api')->user();
            $param['user_id'] = $user->id;
            $param['type'] = $request->type;
            $res = $this->noticeService->get_like_list($param);
        } catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json($res);
    }

    /**
     * 获取点赞列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function get_zan_list(Request $request){
        try {
            $user = auth('api')->user();
            $param['user_id'] = $user->id;
            $param['type'] = $request->type;
            $res = $this->noticeService->get_zan_list($param);
        } catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json($res);
    }

    /**
     * 获取系统消息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function get_system_message(Request $request){
        try {
            $user = auth('api')->user();
            $res =UserSystemMessageModel::query()
                ->where(['user_id'=>$user->id,'is_delete'=>0])
                ->leftJoin('notice','users_system_message.msg_id','=','notice.id')
                ->select(['users_system_message.*','notice.title','notice.content'])
                ->orderByDesc('id')
                ->paginate(request("perPage",20));
            //清除未读状态
            UserSystemMessageModel::query()->where(['user_id'=>$user->id,'status'=>0])->update(['status'=>1]);
        } catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json($res);
    }

    /**
     * 删除系统消息
     */
    public function del_system_message(Request $request){
        try {
            $this->noticeService->del_message($request->id);
        } catch (\Exception $exception){
            return $this->response->errorForbidden($exception->getMessage());
        }
        return response()->json(['message'=>"操作成功"]);
    }
}
