SystemAttachmentCategoryServices.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\system\attachment;
  13. use app\services\BaseServices;
  14. use app\dao\system\attachment\SystemAttachmentCategoryDao;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\FormBuilder as Form;
  17. use think\facade\Route as Url;
  18. /**
  19. *
  20. * Class SystemAttachmentCategoryServices
  21. * @package app\services\attachment
  22. * @method get($id) 获取一条数据
  23. * @method count($where) 获取条件下数据总数
  24. */
  25. class SystemAttachmentCategoryServices extends BaseServices
  26. {
  27. /**
  28. * SystemAttachmentCategoryServices constructor.
  29. * @param SystemAttachmentCategoryDao $dao
  30. */
  31. public function __construct(SystemAttachmentCategoryDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 获取分类列表
  37. * @param array $where
  38. * @return array
  39. * @throws \ReflectionException
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function getAll(array $where)
  45. {
  46. $list = $this->dao->getList($where);
  47. if ($where['all'] == 1) {
  48. $list = $this->tidyMenuTier($list);
  49. } else {
  50. foreach ($list as &$item) {
  51. $item['title'] = $item['name'];
  52. if ($where['name'] == '' && $this->dao->count(['pid' => $item['id']])) {
  53. $item['loading'] = false;
  54. $item['children'] = [];
  55. }
  56. }
  57. }
  58. return compact('list');
  59. }
  60. /**
  61. * 格式化列表
  62. * @param $menusList
  63. * @param int $pid
  64. * @param array $navList
  65. * @return array
  66. */
  67. public function tidyMenuTier($menusList, $pid = 0, $navList = [])
  68. {
  69. foreach ($menusList as $k => $menu) {
  70. $menu['title'] = $menu['name'];
  71. if ($menu['pid'] == $pid) {
  72. unset($menusList[$k]);
  73. $menu['children'] = $this->tidyMenuTier($menusList, $menu['id']);
  74. if (count($menu['children'])) {
  75. $menu['expand'] = true;
  76. } else {
  77. unset($menu['children']);
  78. }
  79. $navList[] = $menu;
  80. }
  81. }
  82. return $navList;
  83. }
  84. /**
  85. * 创建新增表单
  86. * @return array
  87. * @throws \FormBuilder\Exception\FormBuilderException
  88. */
  89. public function createForm($pid)
  90. {
  91. return create_form('添加分类', $this->form(['pid' => $pid]), Url::buildUrl('/file/category'), 'POST');
  92. }
  93. /**
  94. * 创建编辑表单
  95. * @param $id
  96. * @return array
  97. * @throws \FormBuilder\Exception\FormBuilderException
  98. */
  99. public function editForm(int $id)
  100. {
  101. $info = $this->dao->get($id);
  102. return create_form('编辑分类', $this->form($info), Url::buildUrl('/file/category/' . $id), 'PUT');
  103. }
  104. /**
  105. * 生成表单参数
  106. * @param array $info
  107. * @return array
  108. * @throws \FormBuilder\Exception\FormBuilderException
  109. */
  110. public function form($info = [])
  111. {
  112. [$pidList, $data] = $this->getPidList((int)($info['pid'] ?? 0));
  113. return [
  114. Form::cascader('pid', '上级分类', $data)->options($pidList)->filterable(true)->props(['props' => ['multiple' => false, 'checkStrictly' => true, 'emitPath' => false]])->style(['width' => '100%']),
  115. Form::input('name', '分类名称', $info['name'] ?? '')->maxlength(30),
  116. ];
  117. }
  118. /**
  119. * 获取分类
  120. * @param $value
  121. * @return array
  122. * @throws \ReflectionException
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\DbException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. * @author: 吴汐
  127. * @email: 442384644@qq.com
  128. * @date: 2023/9/12
  129. */
  130. public function getPidList($value)
  131. {
  132. $pidList = $this->dao->selectList([], 'id as value, pid, name as label')->toArray();
  133. if ($value) {
  134. $data = get_tree_value($pidList, $value);
  135. } else {
  136. $data = [0];
  137. }
  138. $pidList = get_tree_children($pidList, 'children', 'value');
  139. array_unshift($pidList, ['value' => 0, 'pid' => 0, 'label' => '顶级分类']);
  140. return [$pidList, array_reverse($data)];
  141. }
  142. /**
  143. * 获取分类列表(添加修改)
  144. * @param array $where
  145. * @return mixed
  146. */
  147. public function getCateList(array $where)
  148. {
  149. $list = $this->dao->getList($where);
  150. $options = [['value' => 0, 'label' => '所有分类']];
  151. foreach ($list as $id => $cateName) {
  152. $options[] = ['label' => $cateName['name'], 'value' => $cateName['id']];
  153. }
  154. return $options;
  155. }
  156. /**
  157. * 保存新建的资源
  158. * @param array $data
  159. */
  160. public function save(array $data)
  161. {
  162. if ($this->dao->getOne(['name' => $data['name']])) {
  163. throw new AdminException(400101);
  164. }
  165. $res = $this->dao->save($data);
  166. if (!$res) throw new AdminException(100022);
  167. return $res;
  168. }
  169. /**
  170. * 保存修改的资源
  171. * @param int $id
  172. * @param array $data
  173. */
  174. public function update(int $id, array $data)
  175. {
  176. $attachment = $this->dao->getOne(['name' => $data['name']]);
  177. if ($attachment && $attachment['id'] != $id) {
  178. throw new AdminException(400101);
  179. }
  180. $res = $this->dao->update($id, $data);
  181. if (!$res) throw new AdminException(100007);
  182. }
  183. /**
  184. * 删除分类
  185. * @param int $id
  186. */
  187. public function del(int $id)
  188. {
  189. $count = $this->dao->getCount(['pid' => $id]);
  190. if ($count) {
  191. throw new AdminException(400102);
  192. } else {
  193. $res = $this->dao->delete($id);
  194. if (!$res) throw new AdminException(400102);
  195. }
  196. }
  197. /**
  198. * 获取一条数据
  199. * @param $where
  200. * @return array
  201. * @throws \think\db\exception\DataNotFoundException
  202. * @throws \think\db\exception\DbException
  203. * @throws \think\db\exception\ModelNotFoundException
  204. */
  205. public function getOne($where)
  206. {
  207. return $this->dao->getOne($where);
  208. }
  209. }