InnerDeviceController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Exceptions\ImportError;
  4. use App\Imports\InnerDeviceImport;
  5. use App\Models\Device;
  6. use App\Models\DeviceName;
  7. use App\Models\InnerDevice;
  8. use App\Models\Option;
  9. use Illuminate\Http\Request;
  10. use Maatwebsite\Excel\Facades\Excel;
  11. class InnerDeviceController extends BaseController
  12. {
  13. protected $model;
  14. protected $device;
  15. protected $device_name;
  16. protected $department;
  17. protected $model_name = '设备';
  18. protected $pre_uri = '/admin/InnerDevice/';
  19. protected $view_path = 'admin.inner-devices.';
  20. protected $redirect_index = '/admin/InnerDevice/index';
  21. public function __construct()
  22. {
  23. $this->model = new InnerDevice();
  24. $this->device = new Device();
  25. $this->device_name = new DeviceName();
  26. }
  27. public function index()
  28. {
  29. $options = $this->device_name->getNameSpecOptions();
  30. $statuses = Option::get('inner_devices', 'status');
  31. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  32. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri', 'options', 'statuses'));
  33. }
  34. public function get(Request $request)
  35. {
  36. $items = $this->model->where('id', '>', 0)->with(['device', 'spec', 'device_name']);
  37. $tmp_items = collect(['number']);
  38. foreach($tmp_items as $tmp_item) {
  39. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  40. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  41. }
  42. }
  43. $select_items = collect(['device_id', 'device_name_id', 'spec_id', 'status', 'work_point_id']);
  44. foreach($select_items as $select_item) {
  45. if($request->has($select_item) && !empty($request->input($select_item))) {
  46. $items = $items->where($select_item, '=', $request->input($select_item));
  47. }
  48. }
  49. $items = $items->orderBy('updated_at', 'desc')->paginate();
  50. foreach ($items as $item) {
  51. $item->device_type = $item->device ? $item->device->name : '';
  52. $item->device_name_name = $item->device_name ? $item->device_name->name : '';
  53. $item->spec_name = $item->spec ? $item->spec->name : '';
  54. $item->status = $item->getStatusLabel();
  55. $item->work_point_name = $item->workPoint ? $item->workPoint->name : '';
  56. $item->project_name = $item->project ? $item->project->name : '';
  57. }
  58. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  59. }
  60. public function create()
  61. {
  62. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  63. $options = $this->device_name->getNameSpecOptions();
  64. $status_options = Option::get('inner_devices', 'status');
  65. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri', 'options', 'status_options'));
  66. }
  67. public function transObject($items)
  68. {
  69. return json_decode(json_encode($items));
  70. }
  71. public function store(Request $request)
  72. {
  73. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  74. $validator = $this->model->getValidator($request, 'store');
  75. if($validator->fails()) {
  76. return back()->withErrors($validator)->withInput();
  77. }
  78. $data = $request->input('data');
  79. $data['device_id'] = $request->input('device_id');
  80. $data['device_name_id'] = $request->input('device_name_id');
  81. $data['spec_id'] = $request->input('spec_id');
  82. $res = $this->model->create($data);
  83. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  84. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  85. }
  86. public function exportTemplate(Request $request)
  87. {
  88. return response()->download(public_path() . '/设备导入模板.xlsx', '设备导入模板.xlsx', [
  89. 'Content-Type: application/vnd.ms-excel'
  90. ]);
  91. }
  92. public function edit(Request $request)
  93. {
  94. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  95. $options = $this->device_name->getNameSpecOptions();
  96. $status_options = Option::get('inner_devices', 'status');
  97. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  98. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item', 'options', 'status_options'));
  99. }
  100. public function update(Request $request)
  101. {
  102. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  103. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  104. $validator = $this->model->getValidator($request, 'update');
  105. if($validator->fails()) {
  106. return back()->withErrors($validator)->withInput();
  107. }
  108. $data = $request->input('data');
  109. $data['device_id'] = $request->input('device_id');
  110. $data['device_name_id'] = $request->input('device_name_id');
  111. $data['spec_id'] = $request->input('spec_id');
  112. $res = $this->model->where('id', $request->input('id'))->update($data);
  113. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  114. return back()->with(['sg_success_info' => '编辑成功']);
  115. }
  116. public function delete(Request $request)
  117. {
  118. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  119. $res = $item->delete();
  120. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  121. return response()->json(['status' => 'success', 'info' => '操作成功']);
  122. }
  123. public function import(Request $request)
  124. {
  125. if(!$request->hasFile('file')) {
  126. return response()->json(['status' => 'error', 'info' => '参数错误']);
  127. }
  128. $file = $request->file('file');
  129. try {
  130. Excel::import(new InnerDeviceImport(), $file);
  131. } catch (ImportError $e) {
  132. return response()->json(['status' => 'error', 'info' => $e->getMessage()]);
  133. }
  134. return response()->json(['status' => 'success', 'info' => '上传成功']);
  135. }
  136. }