OrderDeviceController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. parent::__construct();
  18. $this->model = new OrderDevice();
  19. }
  20. public function index(Request $request)
  21. {
  22. $inner_device = InnerDevice::find($request->input('inner_device_id'));
  23. $inner_device->device_name_name = $inner_device->device_name ? $inner_device->device_name->name : '';
  24. $inner_device->spec_name = $inner_device->spec ? $inner_device->spec->name : '';
  25. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  26. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri', 'inner_device'));
  27. }
  28. public function rentList()
  29. {
  30. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  31. return view($this->view_path . 'rent-list', compact('model', 'model_name','pre_uri'));
  32. }
  33. public function rent()
  34. {
  35. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  36. return view($this->view_path . 'rent', compact('model', 'model_name','pre_uri'));
  37. }
  38. public function applyList()
  39. {
  40. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  41. return view($this->view_path . 'apply-list', compact('model', 'model_name','pre_uri'));
  42. }
  43. public function get(Request $request)
  44. {
  45. $items = $this->model->orderBy('created_at', 'desc');
  46. if($request->input('type') == 'rent') {
  47. $items = $items->whereNull('inner_device_id');
  48. } else if($request->input('type') == 'apply') {
  49. $items = $items->whereNotNull('inner_device_id');
  50. }
  51. $tmp_items = collect(['name']);
  52. foreach($tmp_items as $tmp_item) {
  53. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  54. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  55. }
  56. }
  57. $select_items = collect(['inner_device_id', 'work_point_id']);
  58. foreach($select_items as $select_item) {
  59. if($request->has($select_item) && !empty($request->input($select_item))) {
  60. $items = $items->where($select_item, '=', $request->input($select_item));
  61. }
  62. }
  63. $items = $items->paginate();
  64. if($request->input('type') == 'apply') {
  65. foreach ($items as $item) {
  66. $item->number = $item->inner_device ? $item->inner_device->number : '';
  67. $item->device_name_name = $item->inner_device ? ($item->inner_device->device_name ? $item->inner_device->device_name->name : '') : '';
  68. $item->spec_name = $item->inner_device ? ($item->inner_device->spec ? $item->inner_device->spec->name : '') : '';
  69. $item->project_name = $item->project ? $item->project->name : '';
  70. $item->work_point_name = $item->inner_device ? ($item->inner_device->workPoint ? $item->inner_device->workPoint->name : '') : '';
  71. $item->status_label = $item->inner_device ? $item->inner_device->getStatusLabel() : '';
  72. }
  73. } else {
  74. foreach ($items as $item) {
  75. $item->user_name = $item->user ? $item->user->name : '';
  76. $item->project_name = $item->project ? $item->project->name : '';
  77. $item->work_point_name = $item->order ? ($item->order->workPoint ? $item->order->workPoint->name : '') : '';
  78. $item->money = ($item->price * $item->quantity) / 100;
  79. $item->device_type_name = $item->device ? $item->device->name : '';
  80. $item->device_name_name = $item->device_name ? $item->device_name->name : '';
  81. $item->spec_name = $item->spec ? $item->spec->name : '';
  82. $item->rent_type_name = $item->rent_type ? $item->rent_type->name : '';
  83. $item->price = $item->price / 100;
  84. }
  85. }
  86. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  87. }
  88. public function create()
  89. {
  90. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  91. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  92. }
  93. public function store(Request $request)
  94. {
  95. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  96. $validator = $this->model->getValidator($request, 'store');
  97. if($validator->fails()) {
  98. return back()->withErrors($validator)->withInput();
  99. }
  100. $data = $request->input('data');
  101. $res = $this->model->create($data);
  102. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  103. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  104. }
  105. public function edit(Request $request)
  106. {
  107. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  108. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  109. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  110. }
  111. public function update(Request $request)
  112. {
  113. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  114. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  115. $validator = $this->model->getValidator($request, 'update');
  116. if($validator->fails()) {
  117. return back()->withErrors($validator)->withInput();
  118. }
  119. $data = $request->input('data');
  120. $res = $this->model->where('id', $request->input('id'))->update($data);
  121. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  122. return back()->with(['sg_success_info' => '编辑成功']);
  123. }
  124. public function delete(Request $request)
  125. {
  126. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  127. $res = $item->delete();
  128. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  129. return response()->json(['status' => 'success', 'info' => '操作成功']);
  130. }
  131. }