Role.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * 用户角色
  4. * @author qqiu@qq.com
  5. * @version 1.0
  6. * @date 2015年10月9日
  7. *
  8. */
  9. namespace App\Services\Admin;
  10. use App\Services\Base\BaseProcess;
  11. use App\Models\AdminRoleModel;
  12. class Role extends BaseProcess
  13. {
  14. /**
  15. * 模型
  16. *
  17. * @var object
  18. *
  19. */
  20. private $objModel;
  21. /**
  22. * 初始化
  23. */
  24. public function __construct()
  25. {
  26. if( !$this->objModel) $this->objModel = new AdminRoleModel();
  27. }
  28. public function model()
  29. {
  30. $this->setSucessCode();
  31. return $this->objModel;
  32. }
  33. /**
  34. * 搜索
  35. * @param $search
  36. * @param $pagesize
  37. */
  38. public function search($search, $orderby, $pagesize = PAGE_NUMS)
  39. {
  40. $currentQuery = $this->objModel;
  41. if(isset($search['keyword']) && !empty($search['keyword'])) {
  42. $keywords = '%' . $search['keyword'] . '%';
  43. $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
  44. $query->where('name' , 'like', $keywords)
  45. ->orwhere('mark', 'like', $keywords);
  46. });
  47. }
  48. if(isset($search['department_ids']) && !empty($search['department_ids'])) {
  49. $department_ids = $search['department_ids'];
  50. $currentQuery = $currentQuery->where(function ($query) use ($department_ids) {
  51. $query->whereIn('department_id', $department_ids);
  52. });
  53. }
  54. if($orderby && is_array($orderby)){
  55. foreach ($orderby AS $field => $value){
  56. $currentQuery = $currentQuery -> orderBy($field, $value);
  57. }
  58. }else{
  59. $currentQuery = $currentQuery->orderBy('id', 'DESC');
  60. }
  61. $currentQuery = $currentQuery->where('level', '>=', $search['level'])->paginate($pagesize);
  62. return $currentQuery;
  63. }
  64. /**
  65. * 添加
  66. * @param $data
  67. */
  68. public function create($data)
  69. {
  70. return $this->objModel->create($data);
  71. }
  72. /**
  73. * 更新
  74. * @param $id
  75. * @param $data
  76. */
  77. public function update($id, $data)
  78. {
  79. $obj = $this->objModel->find($id);
  80. if(!$obj) {
  81. return false;
  82. }
  83. $ok = $obj->update($data);
  84. return $ok;
  85. }
  86. /**
  87. * 更新状态
  88. * @param $id
  89. * @param $status
  90. */
  91. public function updateStatus($id, $status)
  92. {
  93. $data = $this->objModel->find($id);
  94. $data->status = $status;
  95. return $data->save();
  96. }
  97. /**
  98. * 删除
  99. * @param $id
  100. */
  101. public function destroy($id)
  102. {
  103. return $this->objModel->destroy($id);
  104. }
  105. /**
  106. * 获取一行数据
  107. * @param $where
  108. * @return
  109. */
  110. public function find($id)
  111. {
  112. return $this->objModel->find($id);
  113. }
  114. /**
  115. * 获取角色权限节点(level越小权限越大)
  116. */
  117. public function getLevelNode($role_ids)
  118. {
  119. $role_ids = is_array($role_ids) ? $role_ids : explode(',', $role_ids);
  120. return $this->objModel->whereIn('id', $role_ids)->take(1)->orderBy('level', 'ASC')->first();
  121. }
  122. /**
  123. * 根据状态查询数据
  124. */
  125. public function getValidData($status = 1)
  126. {
  127. return $this->objModel->where('status', $status)->orderBy('id', 'DESC')->get();
  128. }
  129. /**
  130. * 查询多条数据
  131. */
  132. public function get($pagesize = PAGE_NUMS) {
  133. return $this->objModel->take($pagesize)->orderBy('level', 'ASC')->get();
  134. }
  135. /**
  136. * 查询多条数据并分页
  137. */
  138. public function getPage($pagesize = PAGE_NUMS) {
  139. return $this->objModel->orderBy('id', 'DESC')->paginate($pagesize);
  140. }
  141. /**
  142. * 获取子等级数据
  143. */
  144. public function getChildByLevel($level, $departmentIds=array())
  145. {
  146. $currentQuery = $this->objModel;
  147. $currentQuery = $currentQuery->where('level', '>=', $level);
  148. $currentQuery = $currentQuery->where('status', '=', 1);
  149. if($departmentIds){
  150. $currentQuery = $currentQuery->whereIn('department_id', $departmentIds);
  151. }
  152. return $currentQuery = $currentQuery->orderBy('level', 'ASC')->get();
  153. }
  154. /**
  155. * 根据部门ID获取角色个数
  156. */
  157. public function getCountByDepartmentId($department_id)
  158. {
  159. return $this->objModel->where('department_id', '=', $department_id)->count();
  160. }
  161. }