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

use app\admin\model\SystemAdmin;
use app\admin\model\SystemBusiness;
use app\admin\model\SystemRole;
use app\admin\model\IntelligenceLog;
use app\admin\model\IntelligenceUser;
use app\admin\model\SystemPage;
use think\contract\Jsonable;

class IndexServer
{

    /**
     * Notes:管理员列表
     * @param string $keywords
     * @return bool
     * @throws \Exception
     * User: ZQ
     * Date: 2022/9/16
     */
    public static function getAdminList(string $keywords, int $page, int $limit, $status)
    {
        [$list, $count] = SystemAdmin::adminList($keywords, $page, $limit, $status);
        if (!empty($list)){
            foreach ($list as $k => $v){
                if ($v['admin_status'] == 1){
                    $list[$k]['admin_status'] = true;
                }else{
                    $list[$k]['admin_status'] = false;
                }
                if (!empty($v['admin_roles'])){
                    $admin_roles = explode(',',$v['admin_roles']);
                    $mation = SystemRole::roleMation($admin_roles);
                    $name = [];
                    for ($i=0;$i<count($mation);$i++){
                        $name[$i] = $mation[$i]['role_name'];
                    }
                    $list[$k]['admin_roles'] = implode(',',$name);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:修改管理员
     * @param string $role_name
     * @param int $role_id
     * @param int $role_rules
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updateAdmin($admin_id, $admin_real_name, $admin_pwd, $admin_roles, $admin_status)
    {
        SystemAdmin::affairBegin();
        try {
            $data = [];
            $data['admin_real_name'] = $admin_real_name;
            if (!empty($admin_roles)){
                $data['admin_roles'] = implode(',',$admin_roles);
            }
            if (!empty($admin_status)){
                $data['admin_status'] = $admin_status;
            }
            if (!empty($admin_pwd)){
                $data['admin_pwd'] = md5(sha1($admin_pwd));
            }
            $result = SystemAdmin::updateInfo($admin_id, $data);
            if ($result !== false){
                SystemAdmin::affairCommit();
                return true;
            }
            throw new \Exception('修改管理员失败！');
        }catch (\Exception $exception){
            SystemAdmin::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询管理员账号
     * @param string $role_name
     * @param int $role_id
     * @param int $role_rules
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function adminCheck($admin_account){
        $where = [];
        $where['admin_account'] = $admin_account;
        $result = SystemAdmin::where($where)->first();
        return $result;
    }

    /**
     * Notes:添加管理员
     * @param string $admin_account
     * @param string $admin_real_name
     * @param string $admin_pwd
     * @param string $admin_roles
     * @param string $admin_status
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function insertAdmin($admin_account, $admin_real_name, $admin_pwd, $admin_roles, $admin_status)
    {
        SystemAdmin::affairBegin();
        try {
            $data = [];
            $data['admin_account'] = $admin_account;
            $data['admin_real_name'] = $admin_real_name;
            $data['admin_roles'] = implode(',',$admin_roles);
            $data['admin_status'] = $admin_status;
            $data['admin_pwd'] = md5(sha1($admin_pwd));
            $result = SystemAdmin::insertGetId($data);
            if (!empty($result)){
                SystemAdmin::affairCommit();
                return $result;
            }
            throw new \Exception('添加管理员失败！');
        }catch (\Exception $exception){
            SystemAdmin::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除管理员
     * @param int $admin_id
     * @return boolean
     * User: ZQ
     * Date: 2022/9/16
     */
    public static function delAdmin(int $admin_id)
    {
        SystemAdmin::affairBegin();
        try {
            $where = [];
            $where['admin_id'] = $admin_id;
            $data['admin_is_del'] = 1;
            $result = SystemAdmin::where($where)->update($data);
            if (!empty($result)){
                SystemAdmin::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemAdmin::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询管理员
     * @param int $admin_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function adminInfo($admin_id)
    {
        $where = [];
        $where['admin_id'] = $admin_id;
        $result = SystemAdmin::where($where)->first();
        if (!empty($result)){
            $result['admin_roles'] = explode(',',$result['admin_roles']);
        }
        return $result;
    }


    /**
     * Notes:更新管理员状态
     * @param int $admin_id
     * @return boolean
     * User: ZQ
     * Date: 2022/9/16
     */
    public static function updateStatus(int $admin_id, $admin_status)
    {
        SystemAdmin::affairBegin();
        try {
            $data['admin_status'] = $admin_status;
            $result = SystemAdmin::updateInfo($admin_id,$data);
            if (!empty($result)){
                SystemAdmin::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemAdmin::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:会员设备消息推送信息插入
     * @param int $log_equipment_id
     * @param Jsonable $log_content
     * @param int $log_type
     * @param string  $log_msg_type
     * @return boolean
     * User: ZQ
     * Date: 2022/9/20
     */
    public static function equipmentLog($equipment_only, $log_content, $log_type, $log_msg_type)
    {
        //查询设备是否已存在，存在则获取id，不存在插入数据库
        $where = [];
        $where['equipment_only'] = $equipment_only;
        $info = IntelligenceUser::equipmentCheck($where);
        if (!empty($info)){
            $log_equipment_id = $info['equipment_id'];
        }else{
            return false;
//            $mation = [];
//            $json = json_decode($log_content,true);
//            $type = $json['payload']['serviceData']['devicetype'];
//            if ($type == 'HW'){
//                $mation['equipment_name'] = 'NB-IoT智能红外探测报警器';
//            }elseif ($type == 'YG'){
//                $mation['equipment_name'] = 'NB-IoT烟雾报警器';
//            }elseif ($type == 'SO'){
//                $mation['equipment_name'] = 'NB-IoT智能紧急求救报警器';
//            }elseif ($type == 'LS'){
//                $mation['equipment_name'] = 'NB-IoT智能水浸报警器';
//            }elseif ($type == 'MC'){
//                $mation['equipment_name'] = 'NB-IoT磁开关入侵探测器';
//            }else{
//                $mation['equipment_name'] = '手环';
//            }
//            $mation['equipment_only'] = $equipment_only;
//            $mation['equipment_type'] = 0;
//            $log_equipment_id = IntelligenceUser::insertGetId($mation);
        }
        $data = [];
        $data['log_equipment_id'] = $log_equipment_id;
        $data['log_content'] = $log_content;
        $data['log_type'] = $log_type;
        $data['log_msg_type'] = $log_msg_type;
        $data['log_create_time'] = time();
        return IntelligenceLog::logInsert($data);
    }

    /**
     * Notes:会员设备消息推送信息查询
     * @param int $log_equipment_id
     * @param Jsonable $log_content
     * @param int $log_type
     * @param string  $log_msg_type
     * @return boolean
     * User: ZQ
     * Date: 2022/9/20
     */
    public static function logCheck($log_equipment_id)
    {
        $where = [];
        $where['log_equipment_id'] = $log_equipment_id;
        return IntelligenceLog::where($where)->first();

    }

    /**
     * Notes:管理员密码更新
     * @param int $admin_id
     * @param string $old_pwd
     * @param string $new_pwd
     * @param string $confirm_pwd
     * @return bool
     * @throws \Exception
     * User: yym
     * Date: 2022/8/3
     */
    public static function updatePwd(int $admin_id, string $old_pwd, string $new_pwd, string $confirm_pwd)
    {
        try {
            if($new_pwd !== $confirm_pwd)
            {
                throw new \Exception('新密码和确认密码不一致~');
            }
            $info = SystemAdmin::getAdminInfo($admin_id);
            if(empty($info))
            {
                throw new \Exception('管理员信息不存在');
            }
            if($info['admin_pwd'] != md5(sha1($old_pwd)))
            {
                throw new \Exception('旧密码不正确~');
            }
            if($info['admin_pwd'] == md5(sha1($new_pwd)))
            {
                throw new \Exception('新密码与旧密码一致！');
            }

            $result = SystemAdmin::updateInfo($admin_id, array('admin_pwd' => md5(sha1($new_pwd))));
            if($result !== false)
            {
                return true;
            }
            throw new \Exception('更新失败~');
        }catch (\Exception $exception){
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:获取小程序页面地址
     * @return array
     * User: yym
     * Date: 2022/10/25
     */
    public static function getPageList()
    {
        return SystemPage::getPageList();
    }

    /**
     * Notes:业务控制器列表
     * @param string $keywords
     * @return bool
     * @throws \Exception
     * User: ZQ
     * Date: 2022/11/14
     */
    public static function getBusinessList(string $keywords, int $page, int $limit)
    {
        [$list, $count] = SystemBusiness::businessList($keywords, $page, $limit);
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:更新业务控制器
     * @return bool
     * @throws \Exception
     * User: ZQ
     * Date: 2022/11/14
     */
    public static function updateBusiness($business,$business_message)
    {
        SystemBusiness::affairBegin();
        try {
            foreach ($business as $k => $v){
                if (!empty($v['business_id'])){
                    $business[$k]['business_message'] = $business_message;
                    $result[] = SystemBusiness::where(['business_id'=>$v['business_id']])->update($business[$k]);
                }else{
                    SystemBusiness::affairRollback();
                    throw new \Exception('更新失败~');
                }
            }
            if (!empty($result)){
                SystemBusiness::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemBusiness::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:添加业务控制器
     * @param string $keywords
     * @return bool
     * @throws \Exception
     * User: ZQ
     * Date: 2022/11/14
     */
    public static function insertBusiness($business_name,$business_status,$business_message)
    {
        SystemBusiness::affairBegin();
        try {
            $data = [];
            $data['business_name'] = $business_name;
            $data['business_status'] = $business_status;
            $data['business_message'] = $business_message;
            $result = SystemBusiness::insertGetId($data);
            if (!empty($result)){
                SystemBusiness::affairCommit();
                return $result;
            }else{
                return $result;
            }
        }catch (\Exception $exception){
            SystemBusiness::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

}
