ProjectController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\AdminRoleModel;
  4. use App\Models\Department;
  5. use App\Models\Notification;
  6. use App\Models\Project;
  7. use App\Models\Road;
  8. use App\Models\Role;
  9. use Illuminate\Http\Request;
  10. class ProjectController extends BaseController
  11. {
  12. protected $model;
  13. protected $department;
  14. protected $model_name = '项目';
  15. protected $pre_uri = '/admin/Project/';
  16. protected $view_path = 'admin.projects.';
  17. protected $redirect_index = '/admin/Project/index';
  18. public function __construct()
  19. {
  20. $this->model = new Project();
  21. }
  22. public function index()
  23. {
  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'));
  26. }
  27. public function applyIndex()
  28. {
  29. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  30. return view($this->view_path . 'apply-index', compact('model', 'model_name','pre_uri'));
  31. }
  32. public function get(Request $request)
  33. {
  34. $items = $this->model->orderBy('created_at', 'desc');
  35. $tmp_items = collect(['name']);
  36. foreach($tmp_items as $tmp_item) {
  37. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  38. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  39. }
  40. }
  41. $select_items = collect([]);
  42. foreach($select_items as $select_item) {
  43. if($request->has($select_item) && !empty($request->input($select_item))) {
  44. $items = $items->where($select_item, '=', $request->input($select_item));
  45. }
  46. }
  47. if($request->input('type') == 'apply') {
  48. $items = $items->whereNotNull('user_id');
  49. }
  50. $items = $items->paginate();
  51. foreach($items as $item) {
  52. $item->active_label = $item->getNameOrLabel('active', 'label');
  53. $item->manager = $item->getManager();
  54. $item->manager_name = $item->manager ? $item->manager->name : '';
  55. $item->manager_phone = $item->manager ? $item->manager->phone : '';
  56. $item->user_name = $item->user ? $item->user->name : '';
  57. $item->user_avatar = $item->user ? $item->user->getAvatar() : '';
  58. $item->user_phone = $item->user ? $item->user->phone : '';
  59. }
  60. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  61. }
  62. public function create()
  63. {
  64. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  65. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  66. }
  67. public function store(Request $request)
  68. {
  69. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  70. $validator = $this->model->getValidator($request, 'store');
  71. if($validator->fails()) {
  72. return back()->withErrors($validator)->withInput();
  73. }
  74. $data = $request->input('data');
  75. $res = $this->model->create($data);
  76. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  77. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  78. }
  79. public function edit(Request $request)
  80. {
  81. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  82. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  83. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  84. }
  85. public function update(Request $request)
  86. {
  87. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  88. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  89. $validator = $this->model->getValidator($request, 'update');
  90. if($validator->fails()) {
  91. return back()->withErrors($validator)->withInput();
  92. }
  93. $data = $request->input('data');
  94. $res = $this->model->where('id', $request->input('id'))->update($data);
  95. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  96. return back()->with(['sg_success_info' => '编辑成功']);
  97. }
  98. public function delete(Request $request)
  99. {
  100. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  101. $res = $item->delete();
  102. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  103. return response()->json(['status' => 'success', 'info' => '操作成功']);
  104. }
  105. public function change(Request $request)
  106. {
  107. $this->model->where('id', $request->input('id'))->update(['active' => $request->input('active')]);
  108. Notification::sendProjectInfo($request->input('id'));
  109. return response()->json(['status' => 'success', 'info' => '操作成功']);
  110. }
  111. public function active(Request $request)
  112. {
  113. $this->model->where('id', $request->input('id'))->update(['active' => 1]);
  114. return response()->json(['status' => 'success', 'info' => '操作成功']);
  115. }
  116. }