<?php
/*
 * 第三方会议
 * */
class meet_sysClassModel extends Model
{

    /*
     * 第三方会议-创建joyeey会议
     * */
    public function createJoyeeyMeet($flowModelRs) {

        $baseUrl = getconfig('meetsys_basurl');

        // 往会议管理平台发送数据
        $url = $baseUrl.'meeting/create';

        // 获取信呼会议信息
        $meetId = $flowModelRs['id'];
        $meetingId = $flowModelRs['meeting_id'];
        $meetingRoom = m('meeting_room')->getone('id='.$meetingId);

        $type = '3-会议创建信息';
        $remark = $url.'————'.json_encode($flowModelRs);
        m('log')->addlog($type, $remark);

        if (!isset($meetingId)) {

            $type = '3-会议创建信息-报错';
            $remark = $url.'————'.json_encode($flowModelRs);
            m('log')->addlog($type, $remark);
            return null;
        }

        // 设置 body 参数的值
        $body['type'] = 1;
        $body['room_id'] = intval($meetingRoom['room_id']);
        $body['subject'] = $flowModelRs['title'];
        $body['start'] = $flowModelRs['startdt'];
        $body['end'] = $flowModelRs['enddt'];

        // 查找是否已有登录信息
        $token = m('meet_sys_login')->getMeetSysToken($this->adminid);
        if($token == -2) {
            // 未找到token处理，用超级管理员的token
            $token = m('meet_sys_login')->getMeetSysToken();
        }

        // 调用封装的发送 POST 请求的方法
        $resp = $this->sendPostRequest($url, $body, $token);
        $code = $resp['code'] ?? -1;
        if ($code != 0) {
            // 打log
            $type = '第三方会议-创建joyeey会议-错误';
            $remark = json_encode($resp);
            m('log')->addlog($type, $remark);
            return -1;
        }
        // 更新当前会议关联id
        m('meet')->update('meet_id='.$resp['data']['id'],'id='.$meetId);

        // 打log
        $type = '第三方会议-创建joyeey会议-成功';
        $remark = json_encode($resp);
        m('log')->addlog($type, $remark);

        return $resp;
    }


    /*
     * 第三方会议-取消joyeey会议
     * */
    public function cancelJoyeeyMeet($flowModelRs) {

        $baseUrl = getconfig('meetsys_basurl');
        $url = $baseUrl.'/meeting/cancel';

        if (!isset($flowModelRs['meet_id'])) {
            // 打log
            $type = '3-会议id不存在';
            $remark = json_encode($flowModelRs);
            m('log')->addlog($type, $remark);
            return;
        }

        // 设置 body 参数的值
        $body['id'] = intval($flowModelRs['meet_id']);
        $body['cancel_reason'] = '来自接口';

        $token = m('meet_sys_login')->getMeetSysToken($this->adminid);

        // 调用封装的发送 POST 请求的方法
        $response = $this->sendPostRequest($url, $body, $token);

        if (isset($response['code'])) {
            // 打log
            $type = '登录第三方会议-提交结果1取消';
            $remark = json_encode($response);
            m('log')->addlog($type, $remark);
        }


//        return $response;
    }


    /*
     * 第三方会议-创建会议室
     * */
    public function createMeetingRoom($meetingRoomId) {

        $baseUrl = getconfig('meetsys_basurl');
        $url = $baseUrl.'room/add';

        $roomInfo = m("meeting_room")->getone("id=$meetingRoomId", 'room_name, capacity');

        $body = [
            "name"=>$roomInfo['room_name'],
            "is_vip"=>(bool)intval(false),
            "capacity"=>intval($roomInfo['capacity']), // 容纳人数
            "desc" => $bodyParams['dsc'] ?? null
        ];

        $token = m('meet_sys_login')->getMeetSysToken($this->adminid);

        // 调用封装的发送 POST 请求的方法
        $response = $this->sendPostRequest($url, $body, $token);
        $code = $response['code'];

        if ($code != 0) {
            $type = '3-创建会议-失败';
            $remark = $response['msg'];
            m('log')->addlog($type, $remark);
        } else {
//            $type = '3-创建会议-code非400';
//            m('log')->addlog($type, $response);
            $data = $response['data'];
            // 请求到的会议室id更新到表里
            $valArr = [
                'room_id'=>$data['id']
            ];
            m('meeting_room')->update($valArr, "id=$meetingRoomId");
        }

//        return $response;
    }

    /*
     * 第三方会议-删除会议室
     * */
    public function deleteMeetingRoom($rs) {

        $baseUrl = getconfig('meetsys_basurl');
        $url = $baseUrl.'room/del';

        if (isset($rs['room_id'])) {
            $body = ["id"=>intval($rs['room_id'])];

            $token = m('meet_sys_login')->getMeetSysToken($this->adminid);

            // 调用封装的发送 POST 请求的方法
            $response = $this->sendPostRequest($url, $body, $token);

            $type = '3-会议室删除成功';
            $remark = json_encode($response);
            m('log')->addlog($type, $remark);

            return $response;
        }

        $type = '3-会议室删除失败';
        $remark = '未找到会议室id';
        m('log')->addlog($type, $remark);
        return false;
    }

}