OrderDeviceController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. if($request->input('work_point_id')){
  58. $items->whereHas('order',function ($query){
  59. $query->where('work_point_id','=',request('work_point_id'));
  60. });
  61. }
  62. } else if($request->input('type') == 'apply') {
  63. $items = $items->whereNotNull('inner_device_id');
  64. }
  65. $items = $items->with('order');
  66. $tmp_items = collect(['name']);
  67. foreach($tmp_items as $tmp_item) {
  68. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  69. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  70. }
  71. }
  72. $select_items = collect(['inner_device_id','device_id','device_name_id','spec_id']);
  73. foreach($select_items as $select_item) {
  74. if($request->has($select_item) && !empty($request->input($select_item))) {
  75. $items = $items->where($select_item, '=', $request->input($select_item));
  76. }
  77. }
  78. if ($request->input('order_id'))
  79. {
  80. $items->where('order_id','like','%'.$request->input('order_id').'%');
  81. }
  82. if ($request->input('number'))
  83. {
  84. $items->whereHas('inner_device',function ($query){
  85. $query->where('number','like','%'.request('number').'%');
  86. });
  87. }
  88. if ($request->input('spec_name'))
  89. {
  90. $items->whereHas('inner_device',function ($query){
  91. $query->where('spec_name','like','%'.request('spec_name').'%');
  92. });
  93. }
  94. if ($request->input('project_id'))
  95. {
  96. $items->whereHas('project',function ($query){
  97. $query->where('id','=',request('project_id'));
  98. });
  99. }
  100. if ($request->input('inner_device_name'))
  101. {
  102. $items->whereHas('inner_device',function ($query){
  103. $query->whereHas('device_name',function ($query){
  104. $query->where('id','=',request('inner_device_name'));
  105. });
  106. });
  107. }
  108. $items = $items->paginate();
  109. if($request->input('type') == 'apply') {
  110. foreach ($items as $item) {
  111. $item->number = $item->inner_device ? $item->inner_device->number : '';
  112. $item->device_name_name = $item->inner_device ? ($item->inner_device->device_name ? $item->inner_device->device_name->name : '') : '';
  113. $item->spec_name = $item->inner_device ? ($item->inner_device->spec_name ? $item->inner_device->spec_name : '') : '';
  114. $item->project_name = $item->project ? $item->project->name : '';
  115. $item->work_point_name = $item->inner_device ? ($item->inner_device->workPoint ? $item->inner_device->workPoint->name : '') : '';
  116. $item->status_label = $item->inner_device ? $item->inner_device->getStatusLabel() : '';
  117. }
  118. } else {
  119. foreach ($items as $item) {
  120. $item->user_name = $item->user ? $item->user->name : '';
  121. $item->project_name = $item->project ? $item->project->name : '';
  122. $item->work_point_name = $item->order ? ($item->order->workPoint ? $item->order->workPoint->name : '') : '';
  123. $item->money = ($item->price * $item->quantity) / 100;
  124. $item->device_type_name = $item->device ? $item->device->name : '';
  125. $item->device_name_name = $item->device_name ? $item->device_name->name : '';
  126. $item->spec_name = $item->spec ? $item->spec->name : '';
  127. $item->rent_type_name = $item->rent_type ? $item->rent_type->name : '';
  128. $item->price = $item->price / 100;
  129. }
  130. }
  131. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  132. }
  133. public function create()
  134. {
  135. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  136. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  137. }
  138. public function store(Request $request)
  139. {
  140. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  141. $validator = $this->model->getValidator($request, 'store');
  142. if($validator->fails()) {
  143. return back()->withErrors($validator)->withInput();
  144. }
  145. $data = $request->input('data');
  146. $res = $this->model->create($data);
  147. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  148. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  149. }
  150. public function edit(Request $request)
  151. {
  152. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  153. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  154. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  155. }
  156. public function update(Request $request)
  157. {
  158. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  159. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  160. $validator = $this->model->getValidator($request, 'update');
  161. if($validator->fails()) {
  162. return back()->withErrors($validator)->withInput();
  163. }
  164. $data = $request->input('data');
  165. $res = $this->model->where('id', $request->input('id'))->update($data);
  166. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  167. return back()->with(['sg_success_info' => '编辑成功']);
  168. }
  169. public function delete(Request $request)
  170. {
  171. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  172. $res = $item->delete();
  173. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  174. return response()->json(['status' => 'success', 'info' => '操作成功']);
  175. }
  176. }