RoleService.php 3.8 KB

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