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

    # 获取资源默认值
    public function getResourceDefault($field)
    {
        $db = Dever::db('resource', 'place');
        $option = array();
        foreach ($db->value('type') as $k => $v) {
            $info = array('type' => $v['id'], 'status' => 1);
            foreach ($field as $k1 => $v1) {
                if (isset($v[$v1])) {
                    $v1 = $v[$v1];
                }
                $info[$k1] = $v1;
            }
            $option[] = $info;
        }
        return $option;
    }

    # 检测身份码
    public function checkPlaceCode($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 updatePlaceCode($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 getResourceType($type)
    {
        return Dever::db('resource', 'place')->value('type', $type);
    }

    # 更新分类信息
    public function updateCate($db, $data)
    {
        if ($data['cate']) {
            $data['cate'] = explode(',', $data['cate']);
            $update['cate_parent_id'] = $data['cate'][0];
            if (isset($data['cate'][1])) {
                $update['cate_child_id'] = $data['cate'][1];
            } else {
                $update['cate_child_id'] = 0;
            }
            $db->update($data['id'], $update);
        }
    }

    # 获取带有子分类的分类列表
    public function getCateList($type = 'content')
    {
        $where = array('parent_id' => 0, 'status' => 1);
        $parent = Dever::db('cate', $type)->select($where);
        $result = array();
        foreach ($parent as $k => $v) {
            $where['parent_id'] = $v['id'];
            $v['children'] = Dever::db('cate', $type)->select($where);
            $result[] = $v;
        }
        return $result;
    }

    public function getCateInfo($type = 'content', $cate = '')
    {
        $result = '';
        if ($cate) {
            $cate = explode(',', $cate);
            $info = Dever::db('cate', $type)->find($cate[0]);
            $result = $info['name'];
            if (isset($cate[1])) {
                $info = Dever::db('cate', $type)->find($cate[1]);
                $result .= '<br />' . $info['name'];
            }
        }
        return $result;
    }

    public function getClientList()
    {
        $where['status'] = 1;
        $client = Dever::db('client', 'place')->select($where);
        if ($client) {
            foreach ($client as &$v) {
                $v['name'] = $v['day'] . '天内>=' . $v['sell'] . '销量';
            }
        }
        return $client;
    }

    public function getSource($id, $type, $app = 'content')
    {
        $result = array();
        $info = Dever::db('info', $app)->find($id);
        $result[] = Dever::db('info', $app)->value('source', $info['source']);
        if ($info['source'] == 2) {
            $relation = Dever::db('resource_relation', 'place')->find(array('type' => $type, 'type_id' => $id));
            if ($relation) {
                $place = Dever::db('info', 'place')->find($relation['channel_place_id']);
                $result[] = $place['name'];
            }
        }
        return implode('<br />', $result);
    }

    # 获取支付账户列表
    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;
    }
}