<?php namespace Goods\Lib;
use Dever;
use Manage\Lib\Auth;
class Manage extends Auth
{
    # 获取规格数据
    public function spec($info_id)
    {
        $result = array();
        if ($info_id) {
            $result = Dever::db('info_spec', 'goods')->select(array('info_id' => $info_id));
            if ($result) {
                foreach ($result as $k => $v) {
                    $result[$k]['type'] = 'show';
                    $result[$k]['width'] = '100';
                    $result[$k]['show'] = true;
                    $result[$k]['fixed'] = true;
                    $result[$k]['key'] = $v['name'];
                    $value = Dever::db('info_spec_value', 'goods')->select(array('info_id' => $info_id, 'spec_id' => $v['id']));
                    if ($value) {
                        foreach ($value as $k1 => $v1) {
                            $value[$k1]['parent'] = $result[$k]['key'];
                            $value[$k1]['name'] = $v1['value'];
                            $value[$k1]['key'] = $v1['value'];
                            if ($v1['is_checked'] == 1) {
                                $value[$k1]['checked'] = true;
                            } else {
                                $value[$k1]['checked'] = false;
                            }
                        }
                    }
                    $result[$k]['value'] = $value;
                }
            }
        }
        return $result;
    }

    # 获取商品
    public function getGoods($cate_key = '')
    {
        $where = array();
        if ($cate_key) {
            $where['key'] = $cate_key;
        }
        $cate = Dever::db('cate', 'goods')->select($where);
        $result = array();
        foreach ($cate as $k => $v) {
            $goods = Dever::db('info', 'goods')->select(array('cate_id' => $v['id'], 'status' => 1));
            if ($goods) {
                $result[] = array
                (
                    'id' => $v['id'],
                    'name' => $v['name'],
                    'children' => $goods,
                );
            }
        }
        return $result;
    }

    # 获取商品规格
    public function getGoodsSku($cate_key = '')
    {
        $where = array();
        if ($cate_key) {
            $where['key'] = $cate_key;
        }
        $cate = Dever::db('cate', 'goods')->select($where);
        $result = array();
        foreach ($cate as $k => $v) {
            $goods = Dever::db('info', 'goods')->select(array('cate_id' => $v['id'], 'status' => 1));
            if ($goods) {
                foreach ($goods as $k1 => $v1) {
                    $sku = Dever::db('info_sku', 'goods')->select(array('info_id' => $v1['id']));
                    $option = array();
                    foreach ($sku as $k2 => $v2) {
                        if ($v['key'] != -1) {
                            $spec = Dever::db('info_spec_value', 'goods')->find(array('id' => array('in', $v2['key'])), array('col' => 'id,group_concat(value) as name', 'group' => 'info_id'));
                            if ($spec) {
                                $spec['id'] = $v2['id'];
                                $option[] = $spec;
                            }
                        } else {
                            $option = array();
                        }
                    }
                    $goods[$k1]['children'] = $option;
                }
                $result[] = array
                (
                    'id' => $v['id'],
                    'name' => $v['name'],
                    'children' => $goods,
                );
            }
        }
        return $result;
    }

    # 将数据保存到redis中
    public function saveRedis()
    {
        
    }

    # 获取商品分类
    public function getCate()
    {
        $cate = array
        (
            array
            (
                'id' => -1,
                'name' => '全部',
            )
        );
        $cate = array_merge($cate, Dever::db('cate', 'goods')->select([]));
        return $cate;
    }

    # 展示商品名称
    public function showGoodsInfo($goods_id, $sku_id = false)
    {
        $goods = Dever::db('info', 'goods')->find($goods_id);
        $result = $goods['name'];
        if ($sku_id) {
            $sku = Dever::db('info_sku', 'goods')->find($sku_id);
            if ($sku['key'] != -1) {
                $spec = Dever::db('info_spec_value', 'goods')->find(array('id' => array('in', $sku['key'])), array('col' => 'id,group_concat(value) as name', 'group' => 'info_id'));
                if ($spec) {
                    $result .= '-' . $spec['name'];
                }
            }
        }
        return $result;
    }
}