OrderController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Option;
  4. use App\Models\Order;
  5. use App\Models\Project;
  6. use Illuminate\Http\Request;
  7. class OrderController extends BaseController
  8. {
  9. protected $model;
  10. protected $project;
  11. protected $model_name = '订单';
  12. protected $pre_uri = '/admin/Order/';
  13. protected $view_path = 'admin.orders.';
  14. protected $redirect_index = '/admin/Order/index';
  15. public function __construct()
  16. {
  17. $this->model = new Order();
  18. $this->project = new Project();
  19. }
  20. public function index()
  21. {
  22. $project_options = $this->project->getOptions();
  23. $status_options = Option::get('orders', 'status');
  24. $type_options = [['name' => '外部租赁', 'id' => 1], ['name' => '内部调用', 'id' => 2]];
  25. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  26. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri', 'project_options', 'status_options', 'type_options'));
  27. }
  28. public function get(Request $request)
  29. {
  30. $items = $this->model->where('id', '>', 0)->with('project', 'user');
  31. $tmp_items = collect(['order_number']);
  32. foreach($tmp_items as $tmp_item) {
  33. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  34. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  35. }
  36. }
  37. $select_items = collect(['status', 'type', 'project_id']);
  38. foreach($select_items as $select_item) {
  39. if($request->has($select_item) && !empty($request->input($select_item))) {
  40. $items = $items->where($select_item, '=', $request->input($select_item));
  41. }
  42. }
  43. $items = $items->paginate();
  44. foreach($items as $item) {
  45. $item->project_name = $item->project ? $item->project->name : '';
  46. $item->user_name = $item->user ? $item->user->name : '';
  47. $item->status_name = $item->getStatusName();
  48. $item->option = Option::find($item->status);
  49. $color = $item->option ? $item->option->color : '';
  50. $item->status_name = '<span style="color: ' . $color . '">' . $item->status_name . '</span>';
  51. $item->type_name = $item->type == 1 ? '<span class="layui-badge layui-bg-green">外部租赁</span>' : '<span class="layui-badge layui-bg-blue">内部调用</span>';
  52. }
  53. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  54. }
  55. public function create()
  56. {
  57. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  58. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  59. }
  60. public function store(Request $request)
  61. {
  62. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  63. $validator = $this->model->getValidator($request, 'store');
  64. if($validator->fails()) {
  65. return back()->withErrors($validator)->withInput();
  66. }
  67. $data = $request->input('data');
  68. $res = $this->model->create($data);
  69. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  70. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  71. }
  72. public function edit(Request $request)
  73. {
  74. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  75. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  76. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  77. }
  78. public function update(Request $request)
  79. {
  80. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  81. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  82. $validator = $this->model->getValidator($request, 'update');
  83. if($validator->fails()) {
  84. return back()->withErrors($validator)->withInput();
  85. }
  86. $data = $request->input('data');
  87. $res = $this->model->where('id', $request->input('id'))->update($data);
  88. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  89. return back()->with(['sg_success_info' => '编辑成功']);
  90. }
  91. public function delete(Request $request)
  92. {
  93. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  94. $res = $item->delete();
  95. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  96. return response()->json(['status' => 'success', 'info' => '操作成功']);
  97. }
  98. }