SystemAttachmentCategoryServices.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. * @mixin SystemAttachmentCategoryDao
  23. */
  24. class SystemAttachmentCategoryServices extends BaseServices
  25. {
  26. /**
  27. * SystemAttachmentCategoryServices constructor.
  28. * @param SystemAttachmentCategoryDao $dao
  29. */
  30. public function __construct(SystemAttachmentCategoryDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 获取分类列表
  36. * @param array $where
  37. * @return array
  38. */
  39. public function getAll(array $where)
  40. {
  41. $list = $this->dao->getList($where);
  42. foreach ($list as &$item) {
  43. $item['title'] = $item['name'];
  44. $item['children'] = [];
  45. if ($where['name'] == '' && $this->dao->count(['pid' => $item['id'],'file_type'=>$where['file_type']])) $item['loading'] = false;
  46. }
  47. return compact('list');
  48. }
  49. /**
  50. * 格式化列表
  51. * @param $menusList
  52. * @param int $pid
  53. * @param array $navList
  54. * @return array
  55. */
  56. public function tidyMenuTier($menusList, $pid = 0, $navList = [])
  57. {
  58. foreach ($menusList as $k => $menu) {
  59. $menu['title'] = $menu['name'];
  60. if ($menu['pid'] == $pid) {
  61. unset($menusList[$k]);
  62. $menu['children'] = $this->tidyMenuTier($menusList, $menu['id']);
  63. if ($menu['children']) $menu['expand'] = true;
  64. $navList[] = $menu;
  65. }
  66. }
  67. return $navList;
  68. }
  69. /**
  70. * 创建新增表单
  71. * @param $pid
  72. * @param int $type
  73. * @param int $relationId
  74. * @param int $file_type
  75. * @return mixed
  76. */
  77. public function createForm($pid, int $type = 1, int $relationId = 0, int $file_type = 1)
  78. {
  79. return create_form('添加分类', $this->form(['pid' => $pid], $type, $relationId, $file_type), Url::buildUrl('/file/category'), 'POST');
  80. }
  81. /**
  82. * 创建编辑表单
  83. * @param int $id
  84. * @param int $type
  85. * @param int $relationId
  86. * @param int $file_type
  87. * @return mixed
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function editForm(int $id, int $type = 1, int $relationId = 0, int $file_type = 1)
  93. {
  94. $info = $this->dao->get($id);
  95. return create_form('编辑分类', $this->form($info, $type, $relationId, $file_type), Url::buildUrl('/file/category/' . $id), 'PUT');
  96. }
  97. /**
  98. * 生成表单参数
  99. * @param $info
  100. * @param int $type
  101. * @param int $relationId
  102. * @param int $file_type
  103. * @return array
  104. */
  105. public function form($info = [], int $type = 1, int $relationId = 0, int $file_type = 1)
  106. {
  107. return [
  108. Form::hidden('file_type', $file_type),
  109. Form::select('pid', '上级分类', (int)($info['pid'] ?? ''))->setOptions($this->getCateList(['pid' => 0, 'type' => $type, 'relation_id' => $relationId]))->filterable(true),
  110. Form::input('name', '分类名称', $info['name'] ?? '')->maxlength(20),
  111. ];
  112. }
  113. /**
  114. * 获取分类列表(添加修改)
  115. * @param array $where
  116. * @return mixed
  117. */
  118. public function getCateList(array $where)
  119. {
  120. $list = $this->dao->getList($where);
  121. $options = [['value' => 0, 'label' => '所有分类']];
  122. foreach ($list as $id => $cateName) {
  123. $options[] = ['label' => $cateName['name'], 'value' => $cateName['id']];
  124. }
  125. return $options;
  126. }
  127. /**
  128. * 保存新建的资源
  129. * @param array $data
  130. */
  131. public function save(array $data)
  132. {
  133. if ($this->dao->getOne(['name' => $data['name'], 'relation_id' => $data['relation_id'] ?? 0])) {
  134. throw new AdminException('该分类已经存在');
  135. }
  136. $res = $this->dao->save($data);
  137. if (!$res) throw new AdminException('新增失败!');
  138. return $res;
  139. }
  140. /**
  141. * 保存修改的资源
  142. * @param int $id
  143. * @param array $data
  144. */
  145. public function update(int $id, array $data)
  146. {
  147. $attachment = $this->dao->getOne(['name' => $data['name'], 'relation_id' => $data['relation_id'] ?? 0]);
  148. if ($attachment && $attachment['id'] != $id) {
  149. throw new AdminException('该分类已经存在');
  150. }
  151. $res = $this->dao->update($id, $data);
  152. if (!$res) throw new AdminException('编辑失败!');
  153. }
  154. /**
  155. * 删除分类
  156. * @param int $id
  157. */
  158. public function del(int $id)
  159. {
  160. $count = $this->dao->getCount(['pid' => $id]);
  161. if ($count) {
  162. throw new AdminException('请先删除下级分类!');
  163. } else {
  164. $res = $this->dao->delete($id);
  165. if (!$res) throw new AdminException('请先删除下级分类!');
  166. }
  167. }
  168. /**
  169. * 获取一条数据
  170. * @param $where
  171. * @return array
  172. * @throws \think\db\exception\DataNotFoundException
  173. * @throws \think\db\exception\DbException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. */
  176. public function getOne($where)
  177. {
  178. return $this->dao->getOne($where);
  179. }
  180. }