RepairDeviceController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\InnerDevice;
  4. use App\Models\InnerDeviceNamesModel;
  5. use App\Models\Option;
  6. use App\Models\Part;
  7. use App\Models\RepairDevice;
  8. use Illuminate\Http\Request;
  9. class RepairDeviceController extends BaseController
  10. {
  11. protected $model;
  12. protected $department;
  13. protected $model_name = '维修记录';
  14. protected $pre_uri = '/admin/RepairDevice/';
  15. protected $view_path = 'admin.repair-devices.';
  16. protected $redirect_index = '/admin/RepairDevice/index';
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->model = new RepairDevice();
  21. }
  22. public function index(Request $request)
  23. {
  24. $inner_device = InnerDevice::find($request->input('inner_device_id'));
  25. $inner_device->device_name_name = $inner_device->device_name ? $inner_device->device_name->name : '';
  26. $inner_device->spec_name = $inner_device->spec ? $inner_device->spec->name : '';
  27. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  28. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri', 'inner_device'));
  29. }
  30. public function all(Request $request)
  31. {
  32. $status_label = Option::get('inner_devices','status');
  33. $inner_device_name = InnerDeviceNamesModel::getOptions();
  34. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  35. return view($this->view_path . 'all', compact('model', 'model_name','pre_uri','status_label','inner_device_name'));
  36. }
  37. public function get(Request $request)
  38. {
  39. $items = $this->model->orderBy('created_at', 'desc')->with(['inner_device']);
  40. //搜索名字
  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. //搜索内部设备id
  48. $select_items = collect(['inner_device_id']);
  49. foreach($select_items as $select_item) {
  50. if($request->has($select_item) && !empty($request->input($select_item))) {
  51. $items = $items->where($select_item, '=', $request->input($select_item));
  52. }
  53. }
  54. //查询状态
  55. if ($request->input('status_label'))
  56. {
  57. $items->whereHas('inner_device',function ($query){
  58. $query->where('status','=',request('status_label'));
  59. });
  60. }
  61. //查询设备名称
  62. if ($request->input('inner_device_name'))
  63. {
  64. $items->whereHas('inner_device',function ($query){
  65. $query->whereHas('device_name',function ($query){
  66. $query->where('id','=',request('inner_device_name'));
  67. });
  68. });
  69. }
  70. //查询固定资产编号
  71. if ($request->input('number'))
  72. {
  73. $items->whereHas('inner_device',function ($query){
  74. $query->where('number','like','%'.request('number').'%');
  75. });
  76. }
  77. //查询规格型号
  78. if ($request->input('spec_name'))
  79. {
  80. $items->whereHas('inner_device',function ($query){
  81. $query->where('spec_name','like','%'.request('spec_name').'%');
  82. });
  83. }
  84. //提交人
  85. if ($request->input('user_name'))
  86. {
  87. $items->whereHas('user',function ($query){
  88. $query->where('name','like','%'.request('user_name').'%');
  89. });
  90. }
  91. $items = $items->paginate();
  92. foreach ($items as $item) {
  93. $item->user_name = $item->user ? $item->user->name : '';
  94. $item->user_phone = $item->user ? $item->user->phone : '';
  95. $item->project_name = $item->project ? $item->project->name : '';
  96. $item->number = $item->inner_device ? $item->inner_device->number : '';
  97. $item->spec_name = $item->inner_device ? ($item->inner_device->spec_name ? $item->inner_device->spec_name : '') : '';
  98. $item->device_name = $item->inner_device ?
  99. ($item->inner_device->device_name ?
  100. $item->inner_device->device_name->name ? $item->inner_device->device_name->name : ''
  101. : '')
  102. : '';
  103. $item->work_point_name = $item->work_point ? $item->work_point->name : '';
  104. $item->status_label = $item->inner_device ? ($item->inner_device->option ? $item->inner_device->option->name :'' ) : '';
  105. $item->money = $item->money ? $item->money/100 : '';
  106. }
  107. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  108. }
  109. public function create()
  110. {
  111. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  112. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  113. }
  114. public function store(Request $request)
  115. {
  116. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  117. $validator = $this->model->getValidator($request, 'store');
  118. if($validator->fails()) {
  119. return back()->withErrors($validator)->withInput();
  120. }
  121. $data = $request->input('data');
  122. $res = $this->model->create($data);
  123. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  124. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  125. }
  126. public function edit(Request $request)
  127. {
  128. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  129. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  130. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  131. }
  132. public function update(Request $request)
  133. {
  134. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  135. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  136. $validator = $this->model->getValidator($request, 'update');
  137. if($validator->fails()) {
  138. return back()->withErrors($validator)->withInput();
  139. }
  140. $data = $request->input('data');
  141. $res = $this->model->where('id', $request->input('id'))->update($data);
  142. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  143. return back()->with(['sg_success_info' => '编辑成功']);
  144. }
  145. public function delete(Request $request)
  146. {
  147. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  148. Part::where('repair_device_id', $item->id)->delete();
  149. $res = $item->delete();
  150. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  151. return response()->json(['status' => 'success', 'info' => '操作成功']);
  152. }
  153. }