PartController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\DeviceName;
  4. use App\Models\InnerDevice;
  5. use App\Models\OrderDevice;
  6. use App\Models\Part;
  7. use Illuminate\Http\Request;
  8. class PartController extends BaseController
  9. {
  10. protected $model;
  11. protected $department;
  12. protected $model_name = '维修部件';
  13. protected $pre_uri = '/admin/Part/';
  14. protected $view_path = 'admin.parts.';
  15. protected $redirect_index = '/admin/Part/index';
  16. public function __construct()
  17. {
  18. $this->model = new Part();
  19. }
  20. public function index(Request $request)
  21. {
  22. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  23. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri'));
  24. }
  25. public function get(Request $request)
  26. {
  27. $items = $this->model->orderBy('created_at', 'desc');
  28. $tmp_items = collect(['name']);
  29. foreach($tmp_items as $tmp_item) {
  30. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  31. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  32. }
  33. }
  34. $select_items = collect(['repair_device_id']);
  35. foreach($select_items as $select_item) {
  36. if($request->has($select_item) && !empty($request->input($select_item))) {
  37. $items = $items->where($select_item, '=', $request->input($select_item));
  38. }
  39. }
  40. $items = $items->paginate();
  41. foreach ($items as $item) {
  42. $item->price = $item->price / 100;
  43. }
  44. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  45. }
  46. public function create()
  47. {
  48. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  49. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  50. }
  51. public function store(Request $request)
  52. {
  53. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  54. $validator = $this->model->getValidator($request, 'store');
  55. if($validator->fails()) {
  56. return back()->withErrors($validator)->withInput();
  57. }
  58. $data = $request->input('data');
  59. $res = $this->model->create($data);
  60. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  61. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  62. }
  63. public function edit(Request $request)
  64. {
  65. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  66. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  67. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  68. }
  69. public function update(Request $request)
  70. {
  71. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  72. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  73. $validator = $this->model->getValidator($request, 'update');
  74. if($validator->fails()) {
  75. return back()->withErrors($validator)->withInput();
  76. }
  77. $data = $request->input('data');
  78. $res = $this->model->where('id', $request->input('id'))->update($data);
  79. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  80. return back()->with(['sg_success_info' => '编辑成功']);
  81. }
  82. public function delete(Request $request)
  83. {
  84. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  85. $res = $item->delete();
  86. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  87. return response()->json(['status' => 'success', 'info' => '操作成功']);
  88. }
  89. }