<?php

namespace addons\unishop\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 分类
 */
class Category extends Api
{

    protected $frequently = ['all','menu','inlist'];
    protected $noNeedLogin = ['all','menu','inlist'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \addons\unishop\model\Category();
    }


    /**
     * @ApiTitle    (全部分类数据)
     * @ApiSummary  (全部分类数据)
     * @ApiMethod   (GET)
     * @ApiHeaders  (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
     * @ApiHeaders  (name=cookie, type=string, required=false, description="用户会话的cookie")
     * @ApiReturn   ({"code":1,"msg":"","data":[]})
     *
     * @ApiReturnParams  (name="id", type="integer", description="分类id")
     * @ApiReturnParams  (name="name", type="string", description="分类名称")
     * @ApiReturnParams  (name="pid", type="integer", description="上级id")
     * @ApiReturnParams  (name="image", type="string", description="图片")
     * @ApiReturnParams  (name="type", type="string", description="类型")
     * @ApiReturnParams  (name="flag", type="integer", description="标签/位置")
     * @ApiReturnParams  (name="weigh", type="integer", description="排序")
     */
    public function all(){
        $all = $this->model
            ->where('type','product')
            ->where('status','normal')
            ->field('id,name,nickname,pid,image,type,flag,weigh')
            ->order('weigh ASC')
            ->cache(20)
            ->select();
        if ($all) {
            $all = collection($all)->toArray();
        }
        $this->success('',$all);
    }

    public function inlist(){
        $list = Db::name('unishop_category')
            ->where('status','normal')
            ->field('id,name,nickname,keywords')
            ->order('weigh ASC')
            ->cache(20)
            ->select();

        $this->success('',$list);
    }

    public function inlist_old(){
        $list = Db::name('unishop_category')
            ->where('status','normal')
            ->field('id,name,nickname,pid')
            ->order('weigh ASC')
            ->cache(20)
            ->select();

        //按级拆分
        $list_1 = [];
        $list_2 = [];

        foreach($list as $key => $value){
            if($value['pid'] == 0){
                $list_1[] = $value;
            }else{
                $list_2[] = $value;
            }
        }

        //市级并到省级
        foreach($list_1 as $k1 => $v1){
            foreach($list_2 as $k2 => $v2){
                if($v1['id'] == $v2['pid']){
                    $list_1[$k1]['child'][] = $v2;
                }
            }
        }

        $this->success('',$list_1);
    }


    /**
     * @ApiTitle    (首页广告下面的分类)
     * @ApiSummary  (首页广告下面的分类)
     * @ApiMethod   (GET)
     * @ApiHeaders  (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
     * @ApiHeaders  (name=cookie, type=string, required=false, description="用户会话的cookie")
     * @ApiReturn   ({"code":1,"msg":"","data":{}})
     *
     * @ApiReturnParams  (name="id", type="integer", description="分类id")
     * @ApiReturnParams  (name="name", type="string", description="分类名称")
     * @ApiReturnParams  (name="pid", type="integer", description="上级id")
     * @ApiReturnParams  (name="image", type="string", description="图片")
     * @ApiReturnParams  (name="type", type="string", description="类型")
     * @ApiReturnParams  (name="flag", type="integer", description="标签/位置")
     * @ApiReturnParams  (name="weigh", type="integer", description="排序")
     */
    public function menu()
    {
        $list = $this->model
            ->where('flag','index')
            ->where('status','normal')
            ->cache(20)
            ->select();
        if ($list) {
            $list = collection($list)->toArray();
        }
        $this->success('菜单',$list);
    }



}
