WorkPointController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Imports\WorkPointImport;
  4. use App\Models\Option;
  5. use App\Models\Project;
  6. use App\Models\ProjectRole;
  7. use App\Models\WorkPoint;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Auth;
  10. use Maatwebsite\Excel\Facades\Excel;
  11. class WorkPointController extends BaseController
  12. {
  13. protected $model;
  14. protected $department;
  15. protected $model_name = '工点管理';
  16. protected $pre_uri = '/admin/WorkPoint/';
  17. protected $view_path = 'admin.work-points.';
  18. protected $redirect_index = '/admin/WorkPoint/index';
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->model = new WorkPoint();
  23. }
  24. public function index()
  25. {
  26. $project_id = Project::getOptions();
  27. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  28. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri','project_id'));
  29. }
  30. public function innerDevices()
  31. {
  32. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  33. return view($this->view_path . 'inner-devices', compact('model', 'model_name','pre_uri'));
  34. }
  35. public function get(Request $request)
  36. {
  37. $items = $this->model->orderBy('id', 'desc')->orderBy('created_at', 'desc');
  38. $tmp_items = collect(['name']);
  39. foreach($tmp_items as $tmp_item) {
  40. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  41. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  42. }
  43. }
  44. $select_items = collect([]);
  45. foreach($select_items as $select_item) {
  46. if($request->has($select_item) && !empty($request->input($select_item))) {
  47. $items = $items->where($select_item, '=', $request->input($select_item));
  48. }
  49. }
  50. if ($request->input('project_id'))
  51. {
  52. $items->whereHas('project',function ($query){
  53. $query->where('id','=',request('project_id'));
  54. });
  55. }
  56. $items = $items->paginate();
  57. foreach ($items as $item) {
  58. $item->status_label = $item->getNameOrLabel('status', 'label');
  59. $item->project_name = $item->project ? $item->project->name : '';
  60. }
  61. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  62. }
  63. public function create()
  64. {
  65. $status_options = $this->model->getStatusOptions();
  66. $projects = Project::getOptions();
  67. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  68. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri', 'status_options', 'projects'));
  69. }
  70. public function store(Request $request)
  71. {
  72. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  73. $validator = $this->model->getValidator($request, 'store');
  74. if($validator->fails()) {
  75. return back()->withErrors($validator)->withInput();
  76. }
  77. $data = $request->input('data');
  78. if (empty($data['status']))
  79. {
  80. return back()->withErrors(['sg_error_info' => '状态值为空']);
  81. }
  82. $res = $this->model->create($data);
  83. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  84. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  85. }
  86. public function edit(Request $request)
  87. {
  88. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  89. $status_options = $this->model->getStatusOptions();
  90. $projects = Project::getOptions();
  91. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  92. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item', 'status_options', 'projects'));
  93. }
  94. public function update(Request $request)
  95. {
  96. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  97. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  98. $validator = $this->model->getValidator($request, 'update');
  99. if($validator->fails()) {
  100. return back()->withErrors($validator)->withInput();
  101. }
  102. $data = $request->input('data');
  103. $res = $this->model->where('id', $request->input('id'))->update($data);
  104. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  105. return back()->with(['sg_success_info' => '编辑成功']);
  106. }
  107. public function delete(Request $request)
  108. {
  109. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  110. $res = $item->delete();
  111. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  112. return response()->json(['status' => 'success', 'info' => '操作成功']);
  113. }
  114. public function change(Request $request)
  115. {
  116. $this->model->where('id', $request->input('id'))->update(['status' => $request->input('status')]);
  117. return response()->json(['status' => 'success', 'info' => '操作成功']);
  118. }
  119. public function exportTemplate(Request $request)
  120. {
  121. return response()->download(public_path() . '/工点导入模板.xlsx', '工点导入模板.xlsx', [
  122. 'Content-Type: application/vnd.ms-excel'
  123. ]);
  124. }
  125. public function import(Request $request)
  126. {
  127. if(!$request->hasFile('file')) {
  128. return response()->json(['status' => 'error', 'info' => '参数错误']);
  129. }
  130. $file = $request->file('file');
  131. try {
  132. Excel::import(new WorkPointImport(), $file);
  133. } catch (ImportError $e) {
  134. return response()->json(['status' => 'error', 'info' => $e->getMessage()]);
  135. }
  136. return response()->json(['status' => 'success', 'info' => '上传成功']);
  137. }
  138. }