OrderDeviceController.php 7.1 KB

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