UserGroupServices.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 $feild
  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($feild = 'id,group_name', bool $is_page = false)
  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([], $feild, $page, $limit);
  60. return $is_page ? compact('list', 'count') : $list;
  61. }
  62. /**
  63. * 获取一些用户的分组名称
  64. * @param array $ids
  65. */
  66. public function getUsersGroupName(array $ids)
  67. {
  68. return $this->dao->getColumn([['id', 'IN', $ids]], 'group_name', 'id');
  69. }
  70. /**
  71. * 添加/修改分组页面
  72. * @param int $id
  73. * @return string
  74. */
  75. public function add(int $id)
  76. {
  77. $group = $this->getGroup($id);
  78. $field = array();
  79. if (!$group) {
  80. $title = '添加分组';
  81. $field[] = Form::input('group_name', '分组名称', '')->required();
  82. } else {
  83. $title = '修改分组';
  84. $field[] = Form::hidden('id', $id);
  85. $field[] = Form::input('group_name', '分组名称', $group->getData('group_name'))->required();
  86. }
  87. return create_form($title, $field, Url::buildUrl('/user/user_group/save'), 'POST');
  88. }
  89. /**
  90. * 添加|修改
  91. * @param int $id
  92. * @param array $data
  93. * @return mixed
  94. */
  95. public function save(int $id, array $data)
  96. {
  97. $groupName = $this->dao->getOne(['group_name' => $data['group_name']]);
  98. if ($id) {
  99. if (!$this->getGroup($id)) {
  100. throw new AdminException(100026);
  101. }
  102. if ($groupName && $id != $groupName['id']) {
  103. throw new AdminException(400666);
  104. }
  105. if ($this->dao->update($id, $data)) {
  106. return true;
  107. } else {
  108. throw new AdminException(100007);
  109. }
  110. } else {
  111. unset($data['id']);
  112. if ($groupName) {
  113. throw new AdminException(400666);
  114. }
  115. if ($this->dao->save($data)) {
  116. return true;
  117. } else {
  118. throw new AdminException(100022);
  119. }
  120. }
  121. }
  122. /**
  123. * 删除
  124. * @param int $id
  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. }