<?php


namespace App\Repositories;

use App\Exceptions\TencentImException;
use App\Models\ChatTeam;
use App\Services\TencentImGroupService;
use App\Services\TencentImMessage;
use Dcat\Admin\Grid;
use Dcat\Admin\Repositories\Repository;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Pagination\LengthAwarePaginator;

class ChatTeamUser extends Repository
{
    public $group_id;
    public $to_user_id;
    public function __construct($group_id)
    {
        $this->group_id = $group_id;
    }

    /**
     * 查询表格数据
     *
     * @param Grid\Model $model
     * @return LengthAwarePaginator
     */
    public function get(Grid\Model $model)
    {
        $currentPage = $model->getCurrentPage();
        $perPage = $model->getPerPage();

        $data =new TencentImGroupService();

        try {
            $res = $data->getGroupMemberInfo($this->group_id);

            if (!empty($res)) {

                ChatTeam::query()->where(['GroupId'=>$this->group_id])->update(['MemberCount'=>$res['MemberNum']]);
                return $model->makePaginator($res['MemberNum'],$res['MemberList']);
            }
        } catch (TencentImException | GuzzleException $e) {
            return $e->getMessage();
        }
    }
}
