UserGroupServices.php 4.1 KB

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