<?php namespace Place\Lib;
use Dever;
use Manage\Lib\Auth;
class Manage extends Auth
{
    # 获取身份的树形列表
    public function getTree()
    {
        $extend = Dever::load('common', 'manage')->extend();
        $data = Dever::db('info', 'place')->find(array('id' => $extend['data_id']));
        $result = array();
        $result[] = array
        (
            'id' => 'root',
            'name' => '身份',
            'children' => array($data),
        );
        return $result;
    }

    # 检测身份码
    public function checkCode($db, $data)
    {
        if (isset($data['code']) && $data['code']) {
            $code = Dever::db('code', 'place')->find(['code' => $data['code']]);
            if ($code) {
                if (isset($data['id']) && $data['id'] == $code['id']) {
                    return $data;
                }
                Dever::error('身份码已存在');
            }
        }
        return $data;
    }

    # 更新身份码
    public function updateCode($db, $data)
    {
        if (isset($data['code']) && $data['code']) {
            $code = Dever::db('code', 'place')->find(['code' => $data['code']]);
            if (!$code) {
                $extend = Dever::load('common', 'manage')->extend();
                $module = Dever::db('system_module', 'manage')->find($extend['module_id']);
                if ($module['key'] == 'sector_place') {
                    $module = Dever::db('system_module', 'manage')->find(array('key' => 'sector_user'));
                }
                $update['code'] = $data['code'];
                $update['sector_id'] = $extend['info_id'];
                $update['module_id'] = $module['id'];
                $update['user_id'] = $extend['data_id'];
                $update['place_id'] = $data['id'];
                Dever::db('code', 'place')->insert($update);
            }
        }
    }

    # 获取支付账户列表
    public function getAccountList($uid = false)
    {
        $data = array
        (
            0 => array
            (
                'id' => -1,
                'name' => '平台默认账户',
            ),
        );
        if ($uid) {
            $where['uid'] = $uid;
        } else {
            $extend = Dever::load('common', 'manage')->extend();
            $where['uid'] = $extend['data_id'];
        }
        $data = array_merge($data, Dever::db('account', 'place')->select($where));
        return $data;
    }
}