<?php namespace Place_source\Lib;
use Dever;
use Manage\Lib\Auth;
class Manage extends Auth
{
    # 获取频道
    public function getChannel()
    {
        $data = Dever::db('channel', 'place_source')->select(array('status' => 1));
        $result = array();
        $result[] = array
        (
            'id' => 'root',
            'name' => '频道',
            'children' => $data,
        );
        return $result;
    }

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

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

    # 获取分类信息
    public function getCateInfo($cate = '')
    {
        $result = '';
        if ($cate) {
            $cate = explode(',', $cate);
            $channel = Dever::db('channel', 'place_source')->find($cate[0]);
            $result = $channel['name'];
            $info = Dever::db('cate', 'place_source')->find($cate[1]);
            $result .= '.' . $info['name'];
            if (isset($cate[2])) {
                $info = Dever::db('cate', 'place_source')->find($cate[2]);
                $result .= '.' . $info['name'];
            }
        }
        return $result;
    }

    # 获取分类图标
    public function getCateIcon($type, $icon_id, $icon)
    {
        if ($type == 1) {
            return Dever::load('icon', 'cate')->show($icon_id);
        } else {
            return '<img src="' . $icon . '" width="30"></img>';
        }
    }

    # 获取平台分类
    public function getPlatformCate($table, $parent_id)
    {
        if ($parent_id > 0) {
            # 获取三级分类
            $data = Dever::db($table)->find($parent_id);
            if ($data && $data['cate_id']) {
                return Dever::db('level_3', 'cate')->select(array('parent_id' => $data['cate_id']));
            }
        } else {
            # 获取二级分类
            $extend = Dever::load('common', 'manage')->extend();
            $data = Dever::db('info', 'place')->find(array('id' => $extend['data_id']));
            if ($data['cate_id']) {
                return Dever::db('level_2', 'cate')->select(array('parent_id' => $data['cate_id']));
            }
        }
        return array();
    }

    # 获取源主信息
    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;
    }

    # 获取sku
    public function getSku($info_id)
    {
        $data = array(0 => array('id' => -1, 'name' => '免费'));
        $data = array_merge($data, Dever::db('sku', 'place_source')->select(array('info_id' => $info_id)));
        if ($data) {
            foreach ($data as &$v) {
                if (isset($v['key'])) {
                    $v['name'] = Dever::load('sku', 'api')->getName($v['key'], 'place_source');
                    if (!$v['name']) {
                        $v['name'] = '无规格';
                    }
                }
            }
        }
        return $data;
    }

    # 获取skuname
    public function getSkuName($sku_id)
    {
        if ($sku_id && $sku_id > 0) {
            $info = Dever::db('sku', 'place_source')->find($sku_id);
            return Dever::load("sku", "api")->getName($info['key'], "place_source");
        } else {
            return '免费';
        }
    }

    # 获取内容分类名称
    public function getContentCateName($id)
    {
        if ($id && $id > 0) {
            $info = Dever::db('content_cate', 'place_source')->find($id);
            return $info['name'];
        } else {
            return '无';
        }
    }

    # 获取内容分类列表
    public function getContentCate($info_id)
    {
        $data = array(0 => array('id' => -1, 'name' => '无分类'));
        $data = array_merge($data, Dever::db('content_cate', 'place_source')->select(array('info_id' => $info_id)));
        return $data;
    }
}