OrderDeviceController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Illuminate\Http\Request;
  7. class OrderDeviceController extends BaseController
  8. {
  9. protected $model;
  10. protected $department;
  11. protected $model_name = '调用记录';
  12. protected $pre_uri = '/admin/OrderDevice/';
  13. protected $view_path = 'admin.order-devices.';
  14. protected $redirect_index = '/admin/OrderDevice/index';
  15. public function __construct()
  16. {
  17. $this->model = new OrderDevice();
  18. }
  19. public function index(Request $request)
  20. {
  21. $inner_device = InnerDevice::find($request->input('inner_device_id'));
  22. $inner_device->device_name_name = $inner_device->device_name ? $inner_device->device_name->name : '';
  23. $inner_device->spec_name = $inner_device->spec ? $inner_device->spec->name : '';
  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', 'inner_device'));
  26. }
  27. public function get(Request $request)
  28. {
  29. $items = $this->model->orderBy('created_at', 'desc');
  30. $tmp_items = collect(['name']);
  31. foreach($tmp_items as $tmp_item) {
  32. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  33. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  34. }
  35. }
  36. $select_items = collect(['inner_device_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->project_name = $item->project ? $item->project->name : '';
  46. $item->money = $item->money / 100;
  47. }
  48. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  49. }
  50. public function create()
  51. {
  52. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  53. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  54. }
  55. public function store(Request $request)
  56. {
  57. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  58. $validator = $this->model->getValidator($request, 'store');
  59. if($validator->fails()) {
  60. return back()->withErrors($validator)->withInput();
  61. }
  62. $data = $request->input('data');
  63. $res = $this->model->create($data);
  64. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  65. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  66. }
  67. public function edit(Request $request)
  68. {
  69. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  70. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  71. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  72. }
  73. public function update(Request $request)
  74. {
  75. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  76. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  77. $validator = $this->model->getValidator($request, 'update');
  78. if($validator->fails()) {
  79. return back()->withErrors($validator)->withInput();
  80. }
  81. $data = $request->input('data');
  82. $res = $this->model->where('id', $request->input('id'))->update($data);
  83. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  84. return back()->with(['sg_success_info' => '编辑成功']);
  85. }
  86. public function delete(Request $request)
  87. {
  88. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  89. $res = $item->delete();
  90. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  91. return response()->json(['status' => 'success', 'info' => '操作成功']);
  92. }
  93. }