UserGroupServices.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\user\group;
  13. use app\services\BaseServices;
  14. use app\dao\user\group\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\group
  23. * @mixin UserGroupDao
  24. */
  25. class UserGroupServices extends BaseServices
  26. {
  27. use ServicesTrait;
  28. /**
  29. * UserGroupServices constructor.
  30. * @param UserGroupDao $dao
  31. */
  32. public function __construct(UserGroupDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 获取某一个分组
  38. * @param int $id
  39. * @return array|\think\Model|null
  40. */
  41. public function getGroup(int $id)
  42. {
  43. return $this->dao->get($id);
  44. }
  45. /**
  46. * 获取分组列表
  47. * @param string $field
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function getGroupList($field = 'id,group_name', bool $is_page = false)
  54. {
  55. $page = $limit = 0;
  56. if ($is_page) {
  57. [$page, $limit] = $this->getPageValue();
  58. $count = $this->dao->count([]);
  59. }
  60. $list = $this->dao->getList([], $field, $page, $limit);
  61. return $is_page ? compact('list', 'count') : $list;
  62. }
  63. /**
  64. * 获取一些用户的分组名称
  65. * @param array $ids
  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', '分组名称', '')->maxlength(20)->required();
  83. } else {
  84. $title = '修改分组';
  85. $field[] = Form::hidden('id', $id);
  86. $field[] = Form::input('group_name', '分组名称', $group->getData('group_name'))->maxlength(20)->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('数据不存在');
  102. }
  103. if ($groupName && $id != $groupName['id']) {
  104. throw new AdminException('该分组已经存在');
  105. }
  106. if ($this->dao->update($id, $data)) {
  107. return true;
  108. } else {
  109. throw new AdminException('修改失败或者您没有修改什么!');
  110. }
  111. } else {
  112. unset($data['id']);
  113. if ($groupName) {
  114. throw new AdminException('该分组已经存在');
  115. }
  116. if ($this->dao->save($data)) {
  117. return true;
  118. } else {
  119. throw new AdminException('添加失败!');
  120. }
  121. }
  122. }
  123. /**
  124. * 删除
  125. * @param int $id
  126. */
  127. public function delGroup(int $id)
  128. {
  129. if ($this->getGroup($id)) {
  130. if (!$this->dao->delete($id)) {
  131. throw new AdminException('删除失败,请稍候再试!');
  132. }
  133. }
  134. return '删除成功!';
  135. }
  136. }