OrderDeviceController.php 9.1 KB

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