UserGroupServices.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\user;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserGroupDao;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\FormBuilder as Form;
  17. use think\facade\Route as Url;
  18. /**
  19. *
  20. * Class UserGroupServices
  21. * @package app\services\user
  22. */
  23. class UserGroupServices extends BaseServices
  24. {
  25. /**
  26. * UserGroupServices constructor.
  27. * @param UserGroupDao $dao
  28. */
  29. public function __construct(UserGroupDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取某一个分组
  35. * @param int $id
  36. * @return array|\think\Model|null
  37. */
  38. public function getGroup(int $id)
  39. {
  40. return $this->dao->get($id);
  41. }
  42. /**
  43. * 获取分组列表
  44. * @param string $field
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function getGroupList(string $field = 'id,group_name', bool $is_page = false): array
  51. {
  52. $page = $limit = 0;
  53. if ($is_page) {
  54. [$page, $limit] = $this->getPageValue();
  55. $count = $this->dao->count([]);
  56. }
  57. $list = $this->dao->getList([], $field, $page, $limit);
  58. return $is_page ? compact('list', 'count') : $list;
  59. }
  60. /**
  61. * 获取一些用户的分组名称
  62. * @param array $ids
  63. * @return array
  64. */
  65. public function getUsersGroupName(array $ids)
  66. {
  67. return $this->dao->getColumn([['id', 'IN', $ids]], 'group_name', 'id');
  68. }
  69. /**
  70. * 添加/修改分组页面
  71. * @param int $id
  72. * @return string
  73. */
  74. public function add(int $id)
  75. {
  76. $group = $this->getGroup($id);
  77. $field = array();
  78. if (!$group) {
  79. $title = '添加分组';
  80. $field[] = Form::input('group_name', '分组名称', '')->required();
  81. } else {
  82. $title = '修改分组';
  83. $field[] = Form::hidden('id', $id);
  84. $field[] = Form::input('group_name', '分组名称', $group->getData('group_name'))->required();
  85. }
  86. return create_form($title, $field, Url::buildUrl('/user/user_group/save'), 'POST');
  87. }
  88. /**
  89. * 添加|修改
  90. * @param int $id
  91. * @param array $data
  92. * @return mixed
  93. */
  94. public function save(int $id, array $data)
  95. {
  96. $groupName = $this->dao->getOne(['group_name' => $data['group_name']]);
  97. if ($id) {
  98. if (!$this->getGroup($id)) {
  99. throw new AdminException(100026);
  100. }
  101. if ($groupName && $id != $groupName['id']) {
  102. throw new AdminException(400666);
  103. }
  104. if ($this->dao->update($id, $data)) {
  105. return true;
  106. } else {
  107. throw new AdminException(100007);
  108. }
  109. } else {
  110. unset($data['id']);
  111. if ($groupName) {
  112. throw new AdminException(400666);
  113. }
  114. if ($this->dao->save($data)) {
  115. return true;
  116. } else {
  117. throw new AdminException(100022);
  118. }
  119. }
  120. }
  121. /**
  122. * 删除
  123. * @param int $id
  124. * @return string
  125. */
  126. public function delGroup(int $id)
  127. {
  128. if ($this->getGroup($id)) {
  129. if (!$this->dao->delete($id)) {
  130. throw new AdminException(100008);
  131. }
  132. }
  133. return '删除成功!';
  134. }
  135. }