SystemAttachmentCategoryService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\extra\service\system;
  3. use app\extra\basic\Service;
  4. use app\extra\dao\system\SystemAttachmentCategoryDao;
  5. use think\exception\ValidateException;
  6. class SystemAttachmentCategoryService extends Service
  7. {
  8. /**
  9. * 设置模型
  10. * @return string
  11. */
  12. public function __construct()
  13. {
  14. $this->dao = new SystemAttachmentCategoryDao();
  15. }
  16. /**
  17. * 获取分类列表
  18. * @param array $where
  19. * @return array
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\DbException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. */
  24. public function getAll(array $where)
  25. {
  26. $list = $this->dao->getList($this->searchFilter($where));
  27. foreach ($list as &$item) {
  28. $item['title'] = $item['name'];
  29. $item['children'] = [];
  30. if (($where['name']??'') == '' && $this->dao->count(['pid' => $item['id'], 'file_type' => $where['file_type']])) $item['loading'] = false;
  31. }
  32. return compact('list');
  33. }
  34. protected function searchFilter(array $param = []): array
  35. {
  36. $filter = [];
  37. !empty($param['name']) && $filter[] = ["name", '=', $param['name']];
  38. !empty($param['pid']) && $filter[] = ["pid", '=', $param['pid']];
  39. !empty($param['file_type']) && $filter[] = ["file_type", '=', $param['file_type']];
  40. return $filter;
  41. }
  42. /**
  43. * 获取分类列表(添加修改)
  44. * @param array $where
  45. * @return mixed
  46. */
  47. public function getCateList(array $where)
  48. {
  49. $list = $this->dao->getList(['pid' => 0, 'file_type' => $where['file_type']]);
  50. $options = [['value' => 0, 'label' => '所有分类']];
  51. foreach ($list as $id => $cateName) {
  52. $options[] = ['label' => $cateName['name'], 'value' => $cateName['id']];
  53. }
  54. return $options;
  55. }
  56. /**
  57. * 保存新建的资源
  58. * @param array $data
  59. */
  60. public function save(array $data)
  61. {
  62. if ($this->dao->getOne(['name' => $data['name']])) {
  63. throw new ValidateException('该分类已经存在');
  64. }
  65. $res = $this->dao->save($data);
  66. if (!$res) throw new ValidateException('新增失败!');
  67. return $res;
  68. }
  69. /**
  70. * 保存修改的资源
  71. * @param int $id
  72. * @param array $data
  73. */
  74. public function update(int $id, array $data)
  75. {
  76. $attachment = $this->dao->getOne(['name' => $data['name']]);
  77. if ($attachment && $attachment['id'] != $id) {
  78. throw new ValidateException('该分类已经存在');
  79. }
  80. $res = $this->dao->update($id, $data);
  81. if (!$res) throw new ValidateException('编辑失败!');
  82. return $res;
  83. }
  84. /**
  85. * 删除分类
  86. * @param int $id
  87. */
  88. public function del(int $id)
  89. {
  90. $count = $this->dao->count(['pid' => $id]);
  91. if ($count) {
  92. throw new ValidateException('请先删除下级分类!');
  93. } else {
  94. $res = $this->dao->delete($id);
  95. if (!$res) throw new ValidateException('请先删除下级分类!');
  96. }
  97. }
  98. }