SystemAttachmentCategoryServices.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\model\system\attachment\SystemAttachmentCategory;
  14. use qiniu\basic\BaseServices;
  15. use qiniu\exceptions\AdminException;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\facade\Route as Url;
  20. /**
  21. *
  22. * Class SystemAttachmentCategoryServices
  23. * @package app\services\attachment
  24. * @mixin SystemAttachmentCategory
  25. */
  26. class SystemAttachmentCategoryServices extends BaseServices
  27. {
  28. /**
  29. * SystemAttachmentCategoryServices constructor.
  30. * @param SystemAttachmentCategory $model
  31. */
  32. public function __construct(SystemAttachmentCategory $model)
  33. {
  34. $this->model = $model;
  35. }
  36. /**
  37. * 获取分类列表
  38. * @param array $where
  39. * @return array
  40. */
  41. public function getAll(array $where)
  42. {
  43. $list = $this->getList($where);
  44. $list = get_tree_children($list);
  45. return compact('list');
  46. }
  47. /**
  48. * 格式化列表
  49. * @param $menusList
  50. * @param int $pid
  51. * @param array $navList
  52. * @return array
  53. */
  54. public function tidyMenuTier($menusList, $pid = 0, $navList = [])
  55. {
  56. foreach ($menusList as $k => $menu) {
  57. $menu['title'] = $menu['name'];
  58. if ($menu['pid'] == $pid) {
  59. unset($menusList[$k]);
  60. $menu['children'] = $this->tidyMenuTier($menusList, $menu['id']);
  61. if ($menu['children']) $menu['expand'] = true;
  62. $navList[] = $menu;
  63. }
  64. }
  65. return $navList;
  66. }
  67. /**
  68. * 获取分类列表(添加修改)
  69. * @param array $where
  70. * @return mixed
  71. */
  72. public function getCateList(array $where)
  73. {
  74. $list = $this->getList($where);
  75. $options = [['value' => 0, 'label' => '所有分类']];
  76. foreach ($list as $id => $cateName) {
  77. $options[] = ['label' => $cateName['name'], 'value' => $cateName['id']];
  78. }
  79. return $options;
  80. }
  81. /**
  82. * 保存新建的资源
  83. * @param array $data
  84. * @return \qiniu\basic\BaseModel|\think\Model
  85. * @throws DataNotFoundException
  86. * @throws DbException
  87. * @throws ModelNotFoundException
  88. */
  89. public function save(array $data)
  90. {
  91. if ($this->getOne(['name' => $data['name'], 'relation_id' => $data['relation_id'] ?? 0])) {
  92. throw new AdminException('该分类已经存在');
  93. }
  94. $res = $this->create($data);
  95. if (!$res) throw new AdminException('新增失败!');
  96. return $res;
  97. }
  98. /**
  99. * 保存修改的资源
  100. * @param int $id
  101. * @param array $data
  102. * @param string|null $key
  103. * @throws DataNotFoundException
  104. * @throws DbException
  105. * @throws ModelNotFoundException
  106. */
  107. public function update($id, array $data, ?string $key = null)
  108. {
  109. $attachment = $this->getOne(['name' => $data['name'], 'relation_id' => $data['relation_id'] ?? 0]);
  110. if ($attachment && $attachment['id'] != $id) {
  111. throw new AdminException('该分类已经存在');
  112. }
  113. return parent::update($id, $data, $key);
  114. }
  115. /**
  116. * 删除分类
  117. * @param int $id
  118. * @throws DbException
  119. */
  120. public function del(int $id)
  121. {
  122. $count = $this->be(['pid' => $id]);
  123. if ($count) {
  124. throw new AdminException('请先删除下级分类!');
  125. } else {
  126. $res = $this->delete($id);
  127. if (!$res) throw new AdminException('请先删除下级分类!');
  128. }
  129. }
  130. }