<?php

namespace app\api\controller\cloudapi;

use app\common\library\cloudapi\ErrorCode;
use think\Db;
use think\Loader;

/**
 * 用户相关
 * Class User
 * @package app\api\controller\cloudapi
 */
class User extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $deviceTypeList = [
        '1' => '水位计',
        '2' => '渗流计',
        '3' => '渗压计',
        '4' => 'GNSS',
        '5' => '雨量计',
        '6' => '位移检测',
        '7' => '其他',
    ];

    //定义验证器类
    protected $validateClass = 'app\api\validate\cloudapi\User';
    /**
     * 获取用户拥有访问权限的水库列表
     */
    public function reservoirs(){
        //获取当前用户的水库权限
        $where = [
            'rl.id' => ['in', $this->apiUserInfo['reservoir_ids']]
        ];

        $order = [
            'rl.createtime' => 'ASC'
        ];
        $field = 'rl.id,rl.name';
        $tableName = 'reservoir_list';
        $list = Db::name($tableName)
            ->alias('rl')
            ->field($field)
            ->where($where)
            ->order($order)
            ->select();

        $result = [
            'list' => $list,
        ];
        $this->success('操作成功', $result);
    }

    /**
     * 获取用户拥有访问权限的设备列表
     */
    public function devices(){
        $params = $this->request->param();
        //参数验证
        $validate = Loader::validate($this->validateClass);
        if(!$validate->scene('device')->check($params)){
            $this->error($validate->getError());
        }
        //获取当前用户的水库权限
        $where = [
            're.reservoir_id' => $params['reservoir_id']
        ];

        $order = [
            're.createtime' => 'ASC'
        ];
        $field = 're.deviceId as device_code,re.type';
        $tableName = 'reservoir_equipment';
        $list = Db::name($tableName)
            ->alias('re')
            ->field($field)
            ->where($where)
            ->order($order)
            ->select();
        foreach ($list as &$item){
            $item['type_text'] = $this->deviceTypeList[$item['type']];
        }
        unset($item);
        $result = [
            'list' => $list,
        ];
        $this->success('操作成功', $result);
    }

    /**
     * 获取token并刷新token过期时间
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function token(){
        $params = $this->request->param();
        //参数验证
        $validate = Loader::validate($this->validateClass);
        if(!$validate->scene('token')->check($params)){
            $this->error($validate->getError());
        }

        $where = [
            'app_id' => $params['app_id'],
            'app_secret' => $params['app_secret'],
        ];
        $find = (new \app\admin\model\cloudapi\User())
            ->where($where)
            ->find();
        if (empty($find)){
            $this->error(ErrorCode::getMessage(ErrorCode::ID_OR_SECRET_INCORRECT),null,ErrorCode::ID_OR_SECRET_INCORRECT);
        }
        //不管是否过期,重新生成token并刷新过期时间
        $token = md5($params['app_id'].$params['app_secret'].time());
        $expire_time = time() + 24*3600;
        $res = $find->save(['token' => $token, 'expire_time' => $expire_time]);
        if ($res){
            $this->success('操作成功', ['token' => $token, 'expire_time' => $expire_time]);
        }
        $this->error('操作失败');
    }
}