ProjectUserController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Project;
  4. use App\Models\ProjectRole;
  5. use App\Models\ProjectUser;
  6. use App\Models\User;
  7. use Illuminate\Http\Request;
  8. class ProjectUserController extends BaseController
  9. {
  10. protected $model;
  11. protected $department;
  12. protected $model_name = '项目角色';
  13. protected $pre_uri = '/admin/ProjectUser/';
  14. protected $view_path = 'admin.project-users.';
  15. protected $redirect_index = '/admin/ProjectUser/index';
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->model = new ProjectUser();
  20. }
  21. public function index(Request $request)
  22. {
  23. $project = Project::find($request->input('project_id'));
  24. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  25. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri', 'project'));
  26. }
  27. public function get(Request $request)
  28. {
  29. $items = $this->model->with('user', 'projectRole');
  30. if($request->input('name')) {
  31. $user_ids = User::where('name', 'like', '%' . $request->input('name') . '')->pluck('id')->unique();
  32. if(count($user_ids) > 0) {
  33. $items = $items->whereIn('user_id', $user_ids);
  34. }
  35. }
  36. $select_items = collect(['project_id', 'user_id']);
  37. foreach($select_items as $select_item) {
  38. if($request->has($select_item) && !empty($request->input($select_item))) {
  39. $items = $items->where($select_item, '=', $request->input($select_item));
  40. }
  41. }
  42. $items = $items->paginate();
  43. foreach($items as $item) {
  44. $item->user_name = $item->user ? $item->user->name : '';
  45. $item->role_name = $item->projectRole ? $item->projectRole->name : '';
  46. $item->project_name = $item->project ? $item->project->name : '';
  47. }
  48. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  49. }
  50. public function create()
  51. {
  52. $user_options = (new User())->getOptions();
  53. $role_options = (new ProjectRole())->getOptions();
  54. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  55. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri', 'user_options', 'role_options'));
  56. }
  57. public function store(Request $request)
  58. {
  59. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  60. $validator = $this->model->getValidator($request, 'store');
  61. if($validator->fails()) {
  62. return back()->withErrors($validator)->withInput();
  63. }
  64. $data = $request->input('data');
  65. $project_id = $data['project_id'];
  66. $user_id = $data['user_id'];
  67. $project_role_id = $data['project_role_id'];
  68. //新加的角色是项目经理
  69. if ($project_role_id == 4)
  70. {
  71. $m_user = ProjectUser::where('project_id',$project_id)->where('project_role_id',4)->value('user_id');
  72. ProjectUser::where('project_id',$project_id)->where('project_role_id',4)->where('user_id',$m_user)->update(['project_role_id'=>3]);
  73. }
  74. //新加的角色是机电负责人
  75. if ($project_role_id == 2)
  76. {
  77. $s_user = ProjectUser::where('project_id',$project_id)->where('project_role_id',2)->value('user_id');
  78. ProjectUser::where('project_id',$project_id)->where('project_role_id',2)->where('user_id',$s_user)->update(['project_role_id'=>1]);
  79. }
  80. $this->model->where('user_id', $data['user_id'])->delete();
  81. $res = $this->model->create($data);
  82. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  83. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  84. }
  85. public function edit(Request $request)
  86. {
  87. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  88. $user_options = (new User())->getOptions();
  89. $role_options = (new ProjectRole())->getOptions();
  90. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  91. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item', 'user_options', 'role_options'));
  92. }
  93. public function update(Request $request)
  94. {
  95. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  96. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  97. $validator = $this->model->getValidator($request, 'update');
  98. if($validator->fails()) {
  99. return back()->withErrors($validator)->withInput();
  100. }
  101. $data = $request->input('data');
  102. $res = $this->model->where('id', $request->input('id'))->update($data);
  103. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  104. return back()->with(['sg_success_info' => '编辑成功']);
  105. }
  106. public function delete(Request $request)
  107. {
  108. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  109. $res = $item->delete();
  110. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  111. return response()->json(['status' => 'success', 'info' => '操作成功']);
  112. }
  113. }