SpecController.php 6.0 KB

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