ProjectRole.php 3.6 KB

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