<?php


namespace App\Services\Api;


use App\Models\Game;
use App\Models\SystemConfig;
use App\Models\Team;
use App\Models\User;
use App\Models\UserMessage;
use App\Services\JPushService;

class NoticeService
{
    public static $instance;

    public static function getInstance()
    {
        if(empty(self::$instance)){
            self::$instance = new self();
        }

        return self::$instance;
    }

    //发送消息
    public function send_msg($user_id, $sub_id, $type, $do_user, $content, $matter_id)
    {
        $user = User::query()->find($user_id);
        $lang = $user->lang ?: 'en';
        UserMessage::query()->create([
            'user_id' => $user_id,
            'type' => $type,
            'content' => trans($content, [], $lang),
            'sub_id' => $sub_id,
            'do_user' => $do_user,
        ]);

        //添加极光推送
        $jpush_status = SystemConfig::get('system-config', 'jpush_status', "");
        if ($jpush_status == 1) {
            $jump_type = 1; //跳转类型
            $do_user = User::query()->where('id', $do_user)->first();
            $j_content = "";
            if ($type == 5 || $type == 6) {
                //球队消息
                $title = trans('api.TEAM_NEWS', [], $lang);
                $team = Team::query()->where('id', $matter_id)->first();
                $jump_type = 1;
                $j_content = $do_user->name . ' ' . trans($content, [], $lang) . ' ' . $team->name;
            } elseif ($type == 7 || $type == 8) {
                //比赛消息
                $title = trans('api.GAME_NEWS', [], $lang);
                $team = Game::query()->where('id', $matter_id)->first();
                $jump_type = 1;
                $j_content = $do_user->name . ' ' . trans($content, [], $lang) . ' ' . $team->name;
            } elseif ($type == 2 || $type == 3 ) {
                //点赞消息 评论消息 被关注
                $title = trans('api.FEEDS_NEWS', [], $lang);
                $jump_type = $type;
                $j_content = $do_user->name . ' ' . trans($content, [], $lang);
            } elseif ($type == 9) {
                //关注的人发布动态 通知
                $title = trans('api.FEEDS_NEWS', [], $lang);
                $jump_type = 9;
                $j_content = $do_user->name . ' ' . trans($content, [], $lang);
            } elseif ($type == 10) {
                //被关注
                $title = trans('api.FOLLOW', [], $lang);
                $jump_type = $type;
                $j_content = $do_user->name . ' ' . trans($content, [], $lang);
            }

            $extras = ['type' => $jump_type];
            //通过别名发送：
            JPushService::pushNotify([
                'title' => $title,
                'content' => $j_content,
                'alias' => 'user_id_' . $user->id,
                'extras' => $extras,
                'type' => JPushService::PUSH_TYPE_ALIAS,
                'sound' => $user->jpush_voice ?: '',
            ]);
        }
    }
}
