OrderDeviceController.php 8.6 KB

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