<?php
namespace app\admin\controller\index;

use app\admin\controller\BaseController;
use app\admin\model\SystemAdmin;
use app\admin\server\index\IndexServer;


class Admin extends BaseController
{

    /**
     * Notes:获取菜单列表
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/16
     */
    public function getAdminList()
    {
        $keywords = $this->request->get('keywords');
        $status = $this->request->get('status');
        [$page, $limit] = $this->getPage();
        $result         = IndexServer::getAdminList($keywords,$page,$limit,$status);
        $result['keywords'] = $keywords;
        $result['status'] = $status;
        return json_success($result, '成功');
    }

    /**
     * Notes:修改管理员
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/16
     */
    public function updateAdmin()
    {
        $admin_id        = $this->request->post('admin_id');
        $admin_real_name = $this->request->post('admin_real_name','');
        $admin_pwd       = $this->request->post('admin_pwd','');
        $agin_admin_pwd  = $this->request->post('agin_admin_pwd','');
        $admin_roles     = $this->request->post('admin_roles');
        $admin_status    = $this->request->post('admin_status');
        if (!empty($admin_pwd) && !empty($agin_admin_pwd)){
            if ($admin_pwd != $agin_admin_pwd){
                throw new \Exception('两次密码输入不一致！');
            }
        }
        $this->validateCheck('index\IndexValidate', ['admin_id' => $admin_id], 'update');
        $result          = IndexServer::updateAdmin($admin_id, $admin_real_name, $admin_pwd, $admin_roles, $admin_status);
        return json_success($result, '修改成功');

    }

    /**
     * Notes:添加管理员
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/16
     */
    public function insertAdmin()
    {
        $admin_real_name = $this->request->post('admin_real_name','');
        $admin_pwd       = $this->request->post('admin_pwd','');
        $admin_account   = $this->request->post('admin_account');
        $agin_admin_pwd  = $this->request->post('agin_admin_pwd','');
        $admin_roles     = $this->request->post('admin_roles');
        $admin_status    = $this->request->post('admin_status');
        if (!empty($admin_pwd) && !empty($agin_admin_pwd)){
            if ($admin_pwd != $agin_admin_pwd){
                throw new \Exception('两次密码输入不一致！');
            }
        }
        $this->validateCheck('index\IndexValidate', ['admin_real_name' => $admin_real_name, 'admin_pwd' => $admin_pwd, 'admin_account' => $admin_account, 'agin_admin_pwd' => $agin_admin_pwd, 'admin_roles' => $admin_roles, 'admin_status' => $admin_status], 'create');
        //查询账号是否重复
        $status = IndexServer::adminCheck($admin_account);
        if (!empty($status)){
            throw new \Exception('账号已存在，请更换其他账号！');
        }
        $result          = IndexServer::insertAdmin($admin_account, $admin_real_name, $admin_pwd, $admin_roles, $admin_status);
        return json_success($result, '修改成功');

    }

    /**
     * Notes:删除管理员
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/16
     */
    public function delAdmin()
    {
        $admin_id = $this->request->get('admin_id');
        $this->validateCheck('index\IndexValidate', ['admin_id' => $admin_id], 'info');
        $result         = IndexServer::delAdmin($admin_id);
        if ($result){
            return json_success($result, '删除成功');
        }else{
            throw new \Exception('删除失败！');
        }

    }

    /**
     * Notes:查询管理员详情
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/13
     */
    public function adminInfo()
    {
        $admin_id = $this->request->get('admin_id');
        $this->validateCheck('index\IndexValidate', ['admin_id' => $admin_id], 'info');
        $result         = IndexServer::adminInfo($admin_id);
        return json_success($result, '成功');
    }

    /**
     * Notes:管理员启用禁用
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/13
     */
    public function updateStatus()
    {
        $admin_id = $this->request->get('admin_id');
        $admin_status = $this->request->get('admin_status');
        $this->validateCheck('index\IndexValidate', ['admin_id' => $admin_id], 'info');
        $result       = IndexServer::updateStatus($admin_id, $admin_status);
        return json_success($result, '修改成功');
    }

    /**
     * Notes:存储设备推送信息(健康驿站)
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/20
     */
    public function equipmentInsert()
    {
        $mation = $this->request->post('mation');
        $info = json_decode($mation,true);
        $log_equipment_id = $info['deviceId'];
        $log_content = $info['topic'];
        $log_type = $this->request->post('log_type');
        $log_msg_type = $this->request->post('log_msg_type');
        $result = IndexServer::equipmentLog($log_equipment_id, $log_content, $log_type, $log_msg_type);
        return json_success($result, '成功');

    }

    /**
     * Notes:存储设备推送信息（正式版）
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/20
     */
    public function logInsert()
    {
        $mation = $this->request->post('mation');
        $info = json_decode($mation,true);
        $log_equipment_id = $info['deviceId'];
        $log_content = $mation;
        $log_type = $this->request->post('log_type');
        $log_msg_type = $this->request->post('log_msg_type');
        $result = IndexServer::equipmentLog($log_equipment_id, $log_content, $log_type, $log_msg_type);
        return json_success($result, '成功');

    }

    /**
     * Notes:接收设备信息并发送
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/20
     */
    public function receiveMation()
    {
        $mation = file_get_contents("php://input");
        $url = 'http://192.168.0.139:90/v1/equipment/log';
        $urls = 'http://192.168.0.139:84/v1/equipment/log';
        $method = 'POST';
        $headers = [];
        $body = [];
        $body['mation'] = $mation;
        $body['log_type'] = 0;
        $body['log_msg_type'] = '设备数据变化';

        if (!empty($mation)){
            //正式版后台
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HEADER, true);
            if (1 == strpos("$".$url, "https://")){
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            }
            curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
            $result = curl_exec($curl);
            $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
            $rheader = substr($result, 0, $header_size);
            $rbody = substr($result, $header_size);

            $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);

            //健康驿站
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $urls);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HEADER, true);
            if (1 == strpos("$".$url, "https://")){
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            }
            curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
            $result = curl_exec($curl);
            $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
            $rheader = substr($result, 0, $header_size);
            $rbody = substr($result, $header_size);

            $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
        }
    }

    //APP设备消息查询
    public function logQuery()
    {
        $log_equipment_id = $this->request->get('log_equipment_id');
        $result = IndexServer::logCheck($log_equipment_id);
        if (!empty($result)){
            $data = true;
        }else{
            $data = false;
        }
        return json_success($data, '成功');
    }

    /**
     * Notes:emqx数据测试
     * Type: StayTooLong 久滞事件；FallDown 跌倒事件；OptInOut 进出事件；BreathHeartRate 呼吸心率信息
     *       PeopleEvent 活动信息；RoomEnv 环境信息；PeopleGesture 手势信息；PeopleCount 人数信息
     *       HeartBeat 心跳信息
     * @return \support\Response
     * User: ZQ
     * Date: 2022/10/19
     */
    public function emqxLog()
    {
        $mation = file_get_contents("php://input");
        file_put_contents('MQTTFileGet.txt',$mation);
        $info = json_decode($mation,true);
        $type = ['StayTooLong'=>'久滞事件','FallDown'=>'跌倒事件','OptInOut'=>'进出事件','BreathHeartRate'=>'呼吸心率信息',
            'PeopleEvent'=>'活动信息','RoomEnv'=>'环境信息','PeopleGesture'=>'手势信息','PeopleCount'=>'人数信息','HeartBeat'=>'心跳信息',];
        if (!empty($info)){
            $mdid = $info['mdid'];
            $user = IndexServer::logCheck($mdid);//根据设备编号查询会员绑定信息
            $data = [];
            $data['log_user_id'] = $user['equipment_user_id'];
            $data['log_equipment_id'] = $user['equipment_id'];
            $data['log_type'] = 0;
            $data['log_msg_type'] = $type[$info['type']];
            $data['log_content'] = $mation;
            $data['log_create_time'] = time();
            $rusult = IndexServer::logInsert($data);
        }
    }

    /**
     * Notes:获取业务控制器列表
     * @return \support\Response
     * User: ZQ
     * Date: 2022/11/17
     */
    public function businessList()
    {
        $keywords = $this->request->get('keywords');
        [$page, $limit] = $this->getPage();
        $result         = IndexServer::getBusinessList($keywords,$page,$limit);
        $result['keywords'] = $keywords;
        return json_success($result, '成功');
    }

    /**
     * Notes:更新业务控制器
     * @return \support\Response
     * User: ZQ
     * Date: 2022/11/17
     */
    public function businessUpdate()
    {
        $business = $this->request->post('business');
        $business_message = $this->request->post('business_message');
        $result         = IndexServer::updateBusiness($business,$business_message);
        if ($result){
            return json_success($result, '更新成功');
        }else{
            throw new \Exception('更新失败~');
        }
    }

    /**
     * Notes:添加业务控制器
     * @return \support\Response
     * User: ZQ
     * Date: 2022/11/17
     */
    public function businessInsert()
    {
        $business_name = $this->request->post('business_name');
        $business_status = $this->request->post('business_status');
        $business_message = $this->request->post('business_message','');
        $result         = IndexServer::insertBusiness($business_name,$business_status,$business_message);
        if ($result){
            return json_success($result, '添加成功');
        }else{
            throw new \Exception('添加失败~');
        }
    }

}
