RoleService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Role;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Services\RoleMenuService;
  9. /**
  10. * 菜单
  11. */
  12. class RoleService extends BaseService
  13. {
  14. public static string $MODEL = Role::class;
  15. /**
  16. * @description: 模型
  17. * @return {string}
  18. */
  19. public static function model(): string
  20. {
  21. return Role::class;
  22. }
  23. /**
  24. * @description: 枚举
  25. * @return {*}
  26. */
  27. public static function enum(): string
  28. {
  29. return '';
  30. }
  31. /**
  32. * @description: 获取查询条件
  33. * @param {array} $search 查询内容
  34. * @return {array}
  35. */
  36. public static function getWhere(array $search = []): array
  37. {
  38. $where = [];
  39. if (isset($search['id']) && !empty($search['id'])) {
  40. $where[] = ['id', '=', $search['id']];
  41. }
  42. if (isset($search['ids']) && !empty($search['ids'])) {
  43. $where[] = ['id', 'in', $search['ids']];
  44. }
  45. if (isset($search['name']) && !empty($search['name'])) {
  46. $where[] = ['name', '=', $search['name']];
  47. }
  48. if (isset($search['display_name']) && !empty($search['display_name'])) {
  49. $where[] = ['display_name', 'like', '%'.$search['display_name'].'%'];
  50. }
  51. return $where;
  52. }
  53. /**
  54. * @description: 查询单条数据
  55. * @param array $search
  56. * @return \App\Models\Coin|null
  57. */
  58. public static function findOne(array $search): ?Role
  59. {
  60. return self::model()::with(['menus'])->where(self::getWhere($search))->first();
  61. }
  62. /**
  63. * @description: 查询所有数据
  64. * @param array $search
  65. * @return \Illuminate\Database\Eloquent\Collection
  66. */
  67. public static function findAll(array $search = [])
  68. {
  69. return self::model()::with(['menus'])->where(self::getWhere($search))->get();
  70. }
  71. /**
  72. * @description: 分页查询
  73. * @param array $search
  74. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  75. */
  76. public static function paginate(array $search = [])
  77. {
  78. $limit = isset($search['limit']) ? $search['limit'] : 15;
  79. $paginator = self::model()::with(['menus'])->where(self::getWhere($search))
  80. // ->orderBy("sort", 'asc')
  81. ->paginate($limit);
  82. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  83. }
  84. /**
  85. * @description:
  86. * @param {*} $params
  87. * @return {*}
  88. */
  89. public static function submit($params = [])
  90. {
  91. $result = false;
  92. $msg['code'] = self::NOT;
  93. $msg['msg'] = '';
  94. // 2. 判断是否是更新
  95. if (!empty($params['id'])) {
  96. // 更新
  97. $info = self::findOne(['id'=>$params['id']] );
  98. if (!$info) {
  99. $msg['msg'] = '角色不存在!';
  100. }else{
  101. $result = $info->update($params);
  102. $id = $params['id'];
  103. }
  104. } else {
  105. // 创建
  106. $result = $info = self::model()::create($params);
  107. $id = $result->id;
  108. }
  109. if($result){
  110. if(is_array($params['menus_ids'])){
  111. $menus = $params['menus_ids'];
  112. }else{
  113. if(empty($params['menus_ids'])){
  114. $menus = [];
  115. }else{
  116. $menus = explode(',',$params['menus_ids']);
  117. }
  118. }
  119. RoleMenuService::submit($id,$menus);
  120. $msg['code'] = self::YES;
  121. $msg['msg'] = '设置成功';
  122. }else{
  123. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  124. }
  125. return $msg;
  126. }
  127. }