<?php
namespace Smi\Model;

use Think\Model;

header("Content-type:text/html;charset=utf-8");
class CategoryModel extends Model
{
    /**
     * 获取商品分类及对应商品 三级列表 json
     * @return array
     */
    public function getConnectList()
    {
        $level1 = $this->where("`sid`=0")->field("`id`,`name`")->select();    // 一级分类
        $array = array();
        foreach ($level1 as $k => $val) {
            $array[$val['id']]['level1'] = $val['name'];     // 分类名称
            $level2 = $this->where("`sid`={$val['id']}")->field("`id`,`name`")->select();    // 该一级分类下的二级分类
            $curArray = array();
            $i = 0;
            foreach ($level2 as $key => $value) {
                $curArray[$value['id']]['level2'] = $value['name'];
                $curArray[$value['id']]['goods'] = M("goods")->field("`id`,`goods_name`")->where("`cate_id`={$value['id']}")->select();    // 该二级分类下的商品
                $i++;
            }
            $array[$val['id']]['children'] = $curArray;    // 整合后的二级分类及商品
        }
        return JSON($array);
    }

    /**
     * 商品一级分类
     * @return array
     */
    public function getLevel1()
    {
        $list = $this->where("`sid`=0")->field("`id`,`name`")->select();
        return $list;
    }
}