WorkPointController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Option;
  4. use App\Models\Project;
  5. use App\Models\ProjectRole;
  6. use App\Models\WorkPoint;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. class WorkPointController extends BaseController
  10. {
  11. protected $model;
  12. protected $department;
  13. protected $model_name = '工点管理';
  14. protected $pre_uri = '/admin/WorkPoint/';
  15. protected $view_path = 'admin.work-points.';
  16. protected $redirect_index = '/admin/WorkPoint/index';
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->model = new WorkPoint();
  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 innerDevices()
  28. {
  29. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  30. return view($this->view_path . 'inner-devices', 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. $items = $items->paginate();
  48. foreach ($items as $item) {
  49. $item->status_label = $item->getNameOrLabel('status', 'label');
  50. $item->project_name = $item->project ? $item->project->name : '';
  51. }
  52. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  53. }
  54. public function create()
  55. {
  56. $status_options = $this->model->getStatusOptions();
  57. $projects = Project::getOptions();
  58. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  59. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri', 'status_options', 'projects'));
  60. }
  61. public function store(Request $request)
  62. {
  63. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  64. $validator = $this->model->getValidator($request, 'store');
  65. if($validator->fails()) {
  66. return back()->withErrors($validator)->withInput();
  67. }
  68. $data = $request->input('data');
  69. $res = $this->model->create($data);
  70. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  71. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  72. }
  73. public function edit(Request $request)
  74. {
  75. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  76. $status_options = $this->model->getStatusOptions();
  77. $projects = Project::getOptions();
  78. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  79. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item', 'status_options', 'projects'));
  80. }
  81. public function update(Request $request)
  82. {
  83. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  84. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  85. $validator = $this->model->getValidator($request, 'update');
  86. if($validator->fails()) {
  87. return back()->withErrors($validator)->withInput();
  88. }
  89. $data = $request->input('data');
  90. $res = $this->model->where('id', $request->input('id'))->update($data);
  91. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  92. return back()->with(['sg_success_info' => '编辑成功']);
  93. }
  94. public function delete(Request $request)
  95. {
  96. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  97. $res = $item->delete();
  98. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  99. return response()->json(['status' => 'success', 'info' => '操作成功']);
  100. }
  101. public function change(Request $request)
  102. {
  103. $this->model->where('id', $request->input('id'))->update(['status' => $request->input('status')]);
  104. return response()->json(['status' => 'success', 'info' => '操作成功']);
  105. }
  106. }