<?php
# place 身份入口
class Place
{
    public static $info;
    public static $user;
    public static $resource;
    public static $uid;
    public static $code;
    public static $system;
    public static $sector;
    public static $module;
    public static $id;

    public static function entry($sector_id, $module_id, $user_id, $place_id)
    {
        $code = \Dever\Helper\Secure::encode($sector_id . '-' . $module_id . '-' . $user_id . '-' . $place_id);
        return $code;
    }

    # 初始化
    public static function init()
    {
        self::$code = Dever::input('p');
        $code = Dever\Helper\Secure::decode(self::$code);
        if (!$code) {
            Dever::error('身份码错误');
        }
        list($sector_id, $module_id, $user_id, $place_id) = explode('-', $code);
        self::$system = Dever::db('system', 'manage')->find(array('key' => 'sector'));
        self::$sector = Dever::db(self::$system['info_table'])->find($sector_id);
        if (!self::$sector) {
            Dever::error('领域信息错误');
        }
        # 切换数据库
        Dever::load('common', 'manage')->setAuth(self::$system['partition'], self::$system['id'], self::$sector['id'], $module_id, $user_id);

        # 初始化身份
        self::info($user_id, $place_id);

        # 切换数据库
        self::$module = Dever::db('system_module', 'manage')->find(array('key' => 'sector_place'));
        Dever::load('common', 'manage')->setAuth(self::$system['partition'], self::$system['id'], self::$sector['id'], self::$module['id'], $place_id);

        # 初始化资源
        self::resource();

    }

    # 身份信息
    public static function info($user_id, $place_id)
    {
        $user = Dever::db('user', 'sector')->find($user_id);
        self::$info = Dever::db('info', 'place')->find($place_id, array('col' => 'name,logo,content,score as score_name,score_per,bg,type,vip_name,agent_name,money_id,uid,pay_account'));
        if (!self::$info) {
            Dever::error('信息有误');
        }
        if (self::$info['uid'] != $user['id']) {
            Dever::error('信息有误');
        }
        if (!self::$info['vip_name']) {
            self::$info['vip_name'] = '会员';
        }
        if (!self::$info['agent_name']) {
            self::$info['agent_name'] = '代理';
        }
        self::$id = $place_id;
        self::$info['channel'] = $user['channel'];
        self::$info['content'] = htmlspecialchars_decode(self::$info['content']);
        $money = Dever::db('money', 'sector')->find(self::$info['money_id']);
        self::$info['money_name'] = $money['sign'];
    }

    # 资源信息
    public static function resource()
    {
        self::$resource = Dever::db('resource', 'place')->select(['status' => 1], array('col' => 'name,icon,type'));
    }

    # 用户信息
    public static function user($uid)
    {
        self::$user = Dever::db('member', 'place')->find($uid, array('col' => 'id,name,avatar,cash,score_cash,vip_id,agent_id,client_id'));
        if (self::$user) {
            self::$uid = $uid;
            self::$user['score_cash_name'] = self::$user['score_cash'] . self::$info['score_name'];
            self::$user['money_cash_name'] = self::$info['money_name'] . self::$user['cash'];
        }
    }

    public static function result($data)
    {
        $data['place'] = self::$info;
        $data['resource'] = self::$resource;
        $data['user'] = self::$user;
        return $data;
    }
}