| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace app\extra\service\system;
- use app\extra\basic\Service;
- use app\extra\dao\system\SystemAttachmentCategoryDao;
- use think\exception\ValidateException;
- class SystemAttachmentCategoryService extends Service
- {
- /**
- * 设置模型
- * @return string
- */
- public function __construct()
- {
- $this->dao = new SystemAttachmentCategoryDao();
- }
- /**
- * 获取分类列表
- * @param array $where
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getAll(array $where)
- {
- $list = $this->dao->getList($this->searchFilter($where));
- foreach ($list as &$item) {
- $item['title'] = $item['name'];
- $item['children'] = [];
- if (($where['name']??'') == '' && $this->dao->count(['pid' => $item['id'], 'file_type' => $where['file_type']])) $item['loading'] = false;
- }
- return compact('list');
- }
- protected function searchFilter(array $param = []): array
- {
- $filter = [];
- !empty($param['name']) && $filter[] = ["name", '=', $param['name']];
- !empty($param['pid']) && $filter[] = ["pid", '=', $param['pid']];
- !empty($param['file_type']) && $filter[] = ["file_type", '=', $param['file_type']];
- return $filter;
- }
- /**
- * 获取分类列表(添加修改)
- * @param array $where
- * @return mixed
- */
- public function getCateList(array $where)
- {
- $list = $this->dao->getList(['pid' => 0, 'file_type' => $where['file_type']]);
- $options = [['value' => 0, 'label' => '所有分类']];
- foreach ($list as $id => $cateName) {
- $options[] = ['label' => $cateName['name'], 'value' => $cateName['id']];
- }
- return $options;
- }
- /**
- * 保存新建的资源
- * @param array $data
- */
- public function save(array $data)
- {
- if ($this->dao->getOne(['name' => $data['name']])) {
- throw new ValidateException('该分类已经存在');
- }
- $res = $this->dao->save($data);
- if (!$res) throw new ValidateException('新增失败!');
- return $res;
- }
- /**
- * 保存修改的资源
- * @param int $id
- * @param array $data
- */
- public function update(int $id, array $data)
- {
- $attachment = $this->dao->getOne(['name' => $data['name']]);
- if ($attachment && $attachment['id'] != $id) {
- throw new ValidateException('该分类已经存在');
- }
- $res = $this->dao->update($id, $data);
- if (!$res) throw new ValidateException('编辑失败!');
- return $res;
- }
- /**
- * 删除分类
- * @param int $id
- */
- public function del(int $id)
- {
- $count = $this->dao->count(['pid' => $id]);
- if ($count) {
- throw new ValidateException('请先删除下级分类!');
- } else {
- $res = $this->dao->delete($id);
- if (!$res) throw new ValidateException('请先删除下级分类!');
- }
- }
- }
|