<?php
namespace app\model\system;

use library\traits\ModelTrait;
use library\basic\BaseModel;

/**
 * 附件目录
 * Class AttachmentCategory
 * @package app\model\system
 */
class AttachmentCategory extends BaseModel
{

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'attachment_category';

    use ModelTrait;

    /**
     * 获取分类图
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAll($name, $mer_id = ''){
        $model = new self;
        if($name) $model = $model->where('name','LIKE',"%$name%");
        if($mer_id) $model = $model->where('mer_id', 'in', [0, $mer_id]);
        $list=self::tidyMenuTier($model->select(),0);
        return compact('list');
    }
    public static function tidyMenuTier($menusList,$pid = 0,$navList = [])
    {
        foreach ($menusList as $k=>$menu){
            $menu = $menu->getData();
            $menu['title']=$menu['name'];
            if($menu['pid'] == $pid){
                unset($menusList[$k]);
                $menu['children'] = self::tidyMenuTier($menusList,$menu['id']);
                if ($menu['children']) $menu['expand']=true;
                $navList[] = $menu;
            }
        }
        return $navList;
    }

    /**
     * 获取分类下拉列表
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getCateList($id = 10000, $mer_id = ''){
        $model = new self();
        if($id == 0) $model = $model->where('pid',$id);
        $model = $model->where('mer_id', 'in', [0, $mer_id]);
        return sort_list_tier($model->select()->toArray());
    }

    /**
     * 获取单条信息
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getinfo($id){
        $model = new self;
        return $model->where('id',$id)->find()->toArray();
    }

}
