SystemAttachmentCategory.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\admin\model\system;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * 附件目录
  7. * Class SystemAttachmentCategory
  8. * @package app\admin\model\system
  9. */
  10. class SystemAttachmentCategory extends BaseModel
  11. {
  12. /**
  13. * 数据表主键
  14. * @var string
  15. */
  16. protected $pk = 'id';
  17. /**
  18. * 模型名称
  19. * @var string
  20. */
  21. protected $name = 'system_attachment_category';
  22. use ModelTrait;
  23. /**
  24. * 添加分类
  25. * @param $name
  26. * @param $att_size
  27. * @param $att_type
  28. * @param $att_dir
  29. * @param string $satt_dir
  30. * @param int $pid
  31. * @return SystemAttachmentCategory|\think\Model
  32. */
  33. public static function Add($name, $att_size, $att_type, $att_dir, $satt_dir = '', $pid = 0)
  34. {
  35. $data['name'] = $name;
  36. $data['att_dir'] = $att_dir;
  37. $data['satt_dir'] = $satt_dir;
  38. $data['att_size'] = $att_size;
  39. $data['att_type'] = $att_type;
  40. $data['time'] = time();
  41. $data['pid'] = $pid;
  42. return self::create($data);
  43. }
  44. /**
  45. * 获取分类图
  46. * @return array
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. public static function getAll()
  52. {
  53. $model = new self;
  54. $res = $model->select()->toArray();
  55. // print_r($res->toArray());
  56. return self::tidyMenuTier($res);
  57. }
  58. public static function searchBy(string $name): array
  59. {
  60. $model = new self;
  61. if ($name) {
  62. $model = $model->where('name', 'LIKE', "%$name%");
  63. }
  64. $res = $model->select()->toArray();
  65. // print_r($res->toArray());
  66. return self::tidyMenuTier2($res);
  67. }
  68. /**
  69. * 本函数支持图片框二级目录搜索
  70. *
  71. * 1. 分类目录最多支持两级
  72. * 2. 根据 like 的检索结果,如果为根级别,则自动检索出所有子集;如果为子级别,则自动检索出上级
  73. * 3. 为保证性能,like 的结果只取 3 条记录
  74. *
  75. * TODO: 注意这里有一个循环 SQL
  76. */
  77. public static function tidyMenuTier2($menusList)
  78. {
  79. $navList = [];
  80. $pids = [];
  81. $children = [];
  82. foreach ($menusList as $k => $menu) {
  83. $navList[] = $menu;
  84. if ($menu['pid'] == 0) {
  85. $pids[] = $menu['id'];
  86. } else {
  87. $children[] = $menu['pid'];
  88. }
  89. }
  90. // retrieve all children layer
  91. if ($pids || $children) {
  92. if (!$pids) {
  93. $pids = [-1]; // some skills.
  94. } else if (!$children) {
  95. $children = [-1];
  96. }
  97. $attachmentCates = SystemAttachmentCategory::where('pid', 'in', implode(',', $pids))
  98. ->whereOr('id', 'in', implode(',', $children))
  99. ->select()->toArray();
  100. // echo SystemAttachmentCategory::getLastSql();
  101. foreach ($attachmentCates as $attchmentCate) {
  102. $navList[] = $attchmentCate;
  103. }
  104. }
  105. // print_r($navList);
  106. return self::tidyMenuTier($navList);
  107. }
  108. public static function tidyMenuTier($menusList, $pid = 0, $navList = [])
  109. {
  110. foreach ($menusList as $k => $menu) {
  111. if ($menu['pid'] == $pid) {
  112. unset($menusList[$k]);
  113. $menu['child'] = self::tidyMenuTier($menusList, $menu['id']);
  114. $navList[] = $menu;
  115. }
  116. }
  117. return $navList;
  118. }
  119. /**
  120. * 获取分类下拉列表
  121. * @param int $id
  122. * @return array
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. * @throws \think\exception\DbException
  126. */
  127. public static function getCateList($id = 10000, $type = 0)
  128. {
  129. $model = new self();
  130. if ($id == 0)
  131. $model->where('pid', $id);
  132. if ($type == 1)
  133. return sort_list_tier($model->where('pid', 0)->select()->toArray());
  134. return sort_list_tier($model->select()->toArray());
  135. }
  136. /**
  137. * 获取单条信息
  138. * @param $id
  139. * @return mixed
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. * @throws \think\exception\DbException
  143. */
  144. public static function getinfo($id)
  145. {
  146. $model = new self;
  147. $where['id'] = $id;
  148. return $model->where($where)->select()->toArray()[0];
  149. }
  150. }