DeptService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/8/27 10:52
  7. */
  8. namespace App\Http\Services\Backend\Server\System;
  9. use App\Http\Services\Service;
  10. use App\Models\System\Dept;
  11. use App\Models\System\DictData;
  12. use App\Models\System\DictType;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class DeptService extends Service
  15. {
  16. protected array $simpleColumn = ['id', 'name', 'parent_id as parentId'];
  17. protected array $selectColumn = ['id', 'name', 'parent_id as parentId', 'sort', 'leader_user_id as leaderUserId', 'status'];
  18. protected array $selectAppendColumn = ['created_at as createTime'];
  19. public function getDeptSimpleList(): array
  20. {
  21. return Dept::query()->select($this->simpleColumn)->get()->all();
  22. }
  23. public function getDeptList($params)
  24. {
  25. $dept = Dept::query();
  26. isset($params['name']) && filled($params['name']) && $dept->whereLike('name', "%{$params['name']}%");
  27. isset($params['status']) && filled($params['status']) && $dept->where('status', $params['status']);
  28. $deptPage = $dept->paginate($params['pageSize'], [...$this->selectColumn, ...$this->selectAppendColumn], 'page', $params['pageNo']);
  29. return ['list' => $deptPage->items(), 'total' => $deptPage->total()];
  30. }
  31. public function createDictData($data)
  32. {
  33. // 校验字典类型有效
  34. // $this->validateDictTypeExists($data['dictType']);
  35. // // 校验字典数据的值的唯一性
  36. // $this->validateDictDataValueUnique($data['dictType'], $data['value']);
  37. //
  38. // // 插入字典类型
  39. // $dictData = self::toModel($data, DictData::class);
  40. // return $dictData->create($dictData->getAttributes())->id;
  41. }
  42. public function getDictData(int $id)
  43. {
  44. // return DictData::query()->select($this->selectColumn)->find($id);
  45. }
  46. public function updateDictData(array $data, int $id)
  47. {
  48. // 校验字典类型有效
  49. // $this->validateDictTypeExists($data['dictType']);
  50. //
  51. // // 校验字典数据的值的唯一性
  52. // $this->validateDictDataValueUnique($data['dictType'], $data['value'], $id);
  53. // // 修改字典数据
  54. // $dictData = self::toModel([...$data, 'id' => $id], DictData::class);
  55. // $dictData->update($dictData->getAttributes());
  56. }
  57. public function deleteDictData(int $id)
  58. {
  59. // $dictData = DictData::query()->find($id);
  60. // is_null($dictData) && self::error('DICT_DATA_NOT_EXISTS', Response::HTTP_UNPROCESSABLE_ENTITY);
  61. // return $dictData->delete();
  62. }
  63. protected function validateDictTypeExists($dictType)
  64. {
  65. $isExists = DictType::query()->where('type', $dictType)->exists();
  66. !$isExists && self::error('DICT_TYPE_NOT_EXISTS', Response::HTTP_UNPROCESSABLE_ENTITY);
  67. }
  68. protected function validateDictDataValueUnique(string $type, string $value, int $id = null)
  69. {
  70. $dictDataId = DictData::query()->where(['dict_type' => $type, 'value' => $value])->value('id');
  71. !empty($dictDataId) && $dictDataId !== $id && self::error('DICT_DATA_DUPLICATE', Response::HTTP_UNPROCESSABLE_ENTITY);
  72. }
  73. }