<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/4
 * Time: 上午11:49
 */

namespace App\Modules\Book\Services;


use App\Modules\Book\Models\BookCategory;
use App\Modules\Book\Models\Book;
class BookCategoryService
{

    public static function getSecondCategories($id_arr=[]){
        return BookCategory::getSecondCategories($id_arr);
    }

    /**
     * 获取分类
     * @param bool $all true获取所有分类，false获取存在图书的分类
     * @return array  [ ['id'=>'1','name'=>'男频','children'=>[['id'=>'','name'=>''],...]] ,[] ]
     */
    public  static function getCategory($all=false){
        if($all){
            $res = BookCategory::getSecondCategories();
        }else{
            $res = BookCategory::getSecondCategories(Book::getCategoryId());
        }

        $male = [];
        $female = [];
        foreach ($res as $v){
            if($v->channel_name == '男频'){
                $male[] = ['id'=>$v->id,'name'=>$v->category_name];

            }
            if($v->channel_name == '女频'){
                $female[] = ['id'=>$v->id,'name'=>$v->category_name];
            }
        }

        $data = [
            ['id'=>1,'name'=>'男频','children'=>$male],
            ['id'=>2,'name'=>'女频','children'=>$female],
        ];
        return $data;
    }
}