| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\admin\model\system;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- /**
- * 附件目录
- * Class SystemAttachmentCategory
- * @package app\admin\model\system
- */
- class SystemAttachmentCategory extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'system_attachment_category';
- use ModelTrait;
- /**
- * 添加分类
- * @param $name
- * @param $att_size
- * @param $att_type
- * @param $att_dir
- * @param string $satt_dir
- * @param int $pid
- * @return SystemAttachmentCategory|\think\Model
- */
- public static function Add($name, $att_size, $att_type, $att_dir, $satt_dir = '', $pid = 0)
- {
- $data['name'] = $name;
- $data['att_dir'] = $att_dir;
- $data['satt_dir'] = $satt_dir;
- $data['att_size'] = $att_size;
- $data['att_type'] = $att_type;
- $data['time'] = time();
- $data['pid'] = $pid;
- return self::create($data);
- }
- /**
- * 获取分类图
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function getAll()
- {
- $model = new self;
- $res = $model->select()->toArray();
- // print_r($res->toArray());
- return self::tidyMenuTier($res);
- }
- public static function searchBy(string $name): array
- {
- $model = new self;
- if ($name) {
- $model = $model->where('name', 'LIKE', "%$name%");
- }
- $res = $model->select()->toArray();
- // print_r($res->toArray());
- return self::tidyMenuTier2($res);
- }
- /**
- * 本函数支持图片框二级目录搜索
- *
- * 1. 分类目录最多支持两级
- * 2. 根据 like 的检索结果,如果为根级别,则自动检索出所有子集;如果为子级别,则自动检索出上级
- * 3. 为保证性能,like 的结果只取 3 条记录
- *
- * TODO: 注意这里有一个循环 SQL
- */
- public static function tidyMenuTier2($menusList)
- {
- $navList = [];
- $pids = [];
- $children = [];
- foreach ($menusList as $k => $menu) {
- $navList[] = $menu;
- if ($menu['pid'] == 0) {
- $pids[] = $menu['id'];
- } else {
- $children[] = $menu['pid'];
- }
- }
- // retrieve all children layer
- if ($pids || $children) {
- if (!$pids) {
- $pids = [-1]; // some skills.
- } else if (!$children) {
- $children = [-1];
- }
- $attachmentCates = SystemAttachmentCategory::where('pid', 'in', implode(',', $pids))
- ->whereOr('id', 'in', implode(',', $children))
- ->select()->toArray();
- // echo SystemAttachmentCategory::getLastSql();
- foreach ($attachmentCates as $attchmentCate) {
- $navList[] = $attchmentCate;
- }
- }
- // print_r($navList);
- return self::tidyMenuTier($navList);
- }
- public static function tidyMenuTier($menusList, $pid = 0, $navList = [])
- {
- foreach ($menusList as $k => $menu) {
- if ($menu['pid'] == $pid) {
- unset($menusList[$k]);
- $menu['child'] = self::tidyMenuTier($menusList, $menu['id']);
- $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, $type = 0)
- {
- $model = new self();
- if ($id == 0)
- $model->where('pid', $id);
- if ($type == 1)
- return sort_list_tier($model->where('pid', 0)->select()->toArray());
- return sort_list_tier($model->select()->toArray());
- }
- /**
- * 获取单条信息
- * @param $att_id
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function getinfo($att_id)
- {
- $model = new self;
- $where['att_id'] = $att_id;
- return $model->where($where)->select()->toArray()[0];
- }
- }
|