SystemUserLevelServices.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\config;
  13. use app\services\user\UserServices;
  14. use qiniu\basic\BaseServices;
  15. use app\model\system\config\SystemUserLevel;
  16. use qiniu\exceptions\AdminException;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. /**
  21. * 系统设置用户等级
  22. * Class SystemUserLevelServices
  23. * @package app\services\user\level
  24. * @mixin SystemUserLevel
  25. */
  26. class SystemUserLevelServices extends BaseServices
  27. {
  28. /**
  29. * SystemUserLevelServices constructor.
  30. * @param SystemUserLevel $model
  31. */
  32. public function __construct(SystemUserLevel $model)
  33. {
  34. $this->model = $model;
  35. }
  36. /**
  37. * 单个等级
  38. * @param int|array $id
  39. * @param string $field
  40. * @return array
  41. * @throws DataNotFoundException
  42. * @throws DbException
  43. * @throws ModelNotFoundException
  44. */
  45. public function getLevel($id, string $field = '*')
  46. {
  47. if (is_array($id))
  48. return $this->getOne($id, $field);
  49. return $this->getOne(['id' => $id], $field);
  50. }
  51. /**
  52. * 获取所有等级列表
  53. * @param array $where
  54. * @param string $field
  55. * @return array
  56. * @throws DbException
  57. */
  58. public function getLevelList(array $where, string $field = '*')
  59. {
  60. $where_data = [];
  61. if (isset($where['is_show']) && $where['is_show'] !== '') $where_data[] = ['is_show', '=', $where['is_show']];
  62. if (isset($where['title']) && $where['title']) $where_data[] = ['name', 'LIKE', "%$where[title]%"];
  63. [$page, $limit] = $this->getPageValue();
  64. $list = $this->getList($where_data, $field ?? '*', $page, $limit);
  65. foreach ($list as &$item) {
  66. $item['image'] = set_file_url($item['image']);
  67. $item['icon'] = set_file_url($item['icon']);
  68. }
  69. $count = $this->getCount($where_data);
  70. return compact('list', 'count');
  71. }
  72. /**
  73. * 会员等级添加或者修改
  74. * @param array $data
  75. * @return mixed
  76. * @throws DataNotFoundException
  77. * @throws DbException
  78. * @throws ModelNotFoundException
  79. */
  80. public function create(array $data)
  81. {
  82. $gradeLevel = $this->getLevel(['grade' => $data['grade']]);
  83. $nameLevel = $this->getLevel(['name' => $data['name']]);
  84. if ($gradeLevel || $nameLevel) {
  85. throw new AdminException('已检测到您设置过的会员等级,此等级不可重复');
  86. }
  87. /** @var SystemUserLevelTaskServices $taskService */
  88. $taskService = app()->make(SystemUserLevelTaskServices::class);
  89. $data['task'] = $taskService->checkTask($data['task'] ?? []);
  90. return parent::create($data);
  91. }
  92. public function update($id, array $data, ?string $key = null)
  93. {
  94. $gradeLevel = $this->getLevel(['grade' => $data['grade']]);
  95. $nameLevel = $this->getLevel(['name' => $data['name']]);
  96. if (($gradeLevel && $gradeLevel['id'] != $id) || ($nameLevel && $nameLevel['id'] != $id)) {
  97. throw new AdminException('已检测到您设置过的会员等级,此等级不可重复');
  98. }
  99. /** @var SystemUserLevelTaskServices $taskService */
  100. $taskService = app()->make(SystemUserLevelTaskServices::class);
  101. $data['task'] = $taskService->checkTask($data['task'] ?? []);
  102. return parent::update($id, $data);
  103. }
  104. public function delete($id, ?string $key = null)
  105. {
  106. $level = $this->getLevel($id);
  107. if (!$level) {
  108. throw new AdminException('数据不存在');
  109. }
  110. /** @var UserServices $userServices */
  111. $userServices = app()->make(UserServices::class);
  112. if ($userServices->be(['level' => $level['id']])) {
  113. throw new AdminException('存在用户已是该等级,无法删除');
  114. }
  115. return parent::delete($id, $key); // TODO: Change the autogenerated stub
  116. }
  117. }