ProjectRole.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\Auth;
  4. class ProjectRole extends BaseModel
  5. {
  6. /**
  7. * @param $key
  8. * 工区负责人(work)
  9. * 机电负责人(machine)
  10. * 项目副经理(assist)
  11. * 项目经理(manager)
  12. * 管理员子账号(sub)
  13. * 领导账号(leader)
  14. * 管理员(admin)
  15. * @param $column
  16. * @return mixed
  17. */
  18. public static function getByKey($key, $column = null)
  19. {
  20. $role = self::where('key', $key)->first();
  21. if($column) return $role ? $role[$column] : '';
  22. return $role;
  23. }
  24. public function getNext($column = null, $inner = false)
  25. {
  26. $right_key = $inner ? 'applyHandle' : 'rentHandle';
  27. $right = Right::where('key', $right_key)->first();
  28. $project_role = null;
  29. if($right) {
  30. $project_role_ids = ProjectRoleRight::where('right_id', $right->id)->pluck('project_role_id');
  31. $project_role = ProjectRole::whereIn('id', $project_role_ids)->where('level', '>', $this['level'])->orderBy('level')->first();
  32. if(!$project_role) $project_role = ProjectRole::whereIn('id', $project_role_ids)->orderBy('level')->first();
  33. }
  34. return $column ? ($project_role ? $project_role[$column] : '') : $project_role;
  35. }
  36. public static function getOptions()
  37. {
  38. return self::where('id', '>', 0)->get()->toArray();
  39. }
  40. public static function getNotLeaderOptions(){
  41. return self::where('id','>',0)->where('id','!=',6)->get()->toArray();
  42. }
  43. public static function getFirstRole(Order $order)
  44. {
  45. return self::getFirstOrLastRole($order, 'asc');
  46. }
  47. public static function getLastRole(Order $order)
  48. {
  49. return self::getFirstOrLastRole($order, 'desc');
  50. }
  51. public static function getFirstOrLastRole(Order $order, $order_by)
  52. {
  53. $right_key = $order['type'] == 1 ? 'rentHandle' : 'applyHandle';
  54. $right = Right::where('key', $right_key)->first();
  55. $project_role = null;
  56. if($right) {
  57. $project_role_ids = ProjectRoleRight::where('right_id', $right->id)->pluck('project_role_id');
  58. $project_role = ProjectRole::whereIn('id', $project_role_ids)->orderBy('level', $order_by)->first();
  59. }
  60. return $project_role;
  61. }
  62. public function getRights()
  63. {
  64. $rights = Right::all();
  65. $items = [];
  66. foreach($rights as $right) {
  67. $has_right = ProjectRoleRight::where([
  68. ['project_role_id', $this['id']],
  69. ['right_id', $right->id]
  70. ])->first() != null;
  71. $items[$right->key] = $has_right;
  72. }
  73. return $items;
  74. }
  75. /**
  76. * @param $type (rent外部租赁|apply内部调用)
  77. * @param $is_draft (1是2否)
  78. * @return ProjectRole
  79. */
  80. public static function getCreateRole($type, $is_draft)
  81. {
  82. $right_key = $type == 'rent' ? 'rentHandle' : 'applyHandle';
  83. $right = Right::where('key', $right_key)->first();
  84. if($right) {
  85. $project_role_ids = ProjectRoleRight::where('right_id', $right->id)->pluck('project_role_id');
  86. $project_role = ProjectRole::whereIn('id', $project_role_ids)->orderBy('level')->first();
  87. if($is_draft == 1) return $project_role;
  88. if($project_role && $is_draft == 2) return ProjectRole::whereIn('id', $project_role_ids)->where('level', '>', $project_role->level)->orderBy('level')->first();
  89. }
  90. return null;
  91. }
  92. }