OrderController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\FormData;
  4. use App\Models\Order;
  5. use Carbon\Carbon;
  6. use Illuminate\Http\Request;
  7. class OrderController extends Controller
  8. {
  9. protected $redirect_index = '/admin/Order/index';
  10. protected $view_path = 'admin.orders.';
  11. protected $pre_uri = '/admin/Order/';
  12. protected $model_name = '订单';
  13. protected $model;
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $this->model = new Order();
  18. }
  19. public function index(Request $request)
  20. {
  21. $list = $this->model->where('id', '>', 0);
  22. if(!empty($request->input('pay_status')) && in_array($request->input('pay_status'), [1, 2, 3, 4, 5])) {
  23. $list = $list->where('pay_status', $request->input('pay_status'));
  24. }
  25. if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
  26. $keyword = '%' . trim($request->input('keyword')) . '%';
  27. $list = $list->whereHas('student', function ($query) use($keyword) {
  28. $query->where('name', 'like', $keyword);
  29. });
  30. }
  31. if(!empty($request->input('start_time'))) {
  32. $start_time = Carbon::createFromTimestamp(strtotime($request->input('start_time')))->toDateTimeString();
  33. } else {
  34. $start_time = Carbon::now()->subYears(10)->toDateTimeString();
  35. }
  36. if(!empty($request->input('end_time'))) {
  37. $end_time = Carbon::createFromTimestamp(strtotime($request->input('end_time')))->addDay()->toDateTimeString();
  38. } else {
  39. $end_time = Carbon::now()->addYears(10)->toDateTimeString();
  40. }
  41. $list = $list->where([
  42. ['created_at', '>=', $start_time],
  43. ['created_at', '<', $end_time],
  44. ]);
  45. $list = $list->orderBy('created_at', 'desc')->paginate()->withPath($this->getPaginateUrl());
  46. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  47. $seven_days_ago = Carbon::now()->subDays(7)->toDateTimeString();
  48. $one_month_ago = Carbon::now()->subMonth()->toDateTimeString();
  49. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model', 'model_name', 'seven_days_ago', 'one_month_ago'));
  50. }
  51. public function show(Request $request)
  52. {
  53. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  54. return $this->showWarning('找不到订单');
  55. }
  56. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  57. $form_data = FormData::find($item->form_data_id);
  58. $form_data->text_1_arr = explode(':', $form_data->text_1, 2);
  59. $form_data->text_2_arr = explode(':', $form_data->text_2, 2);
  60. $form_data->text_3_arr = explode(':', $form_data->text_3, 2);
  61. $form_data->text_4_arr = explode(':', $form_data->text_4, 2);
  62. $form_data->multi_text_arr = explode(':', $form_data->multi_text, 2);
  63. $form_data->radio_arr = explode(':', $form_data->radio, 2);
  64. $form_data->checkbox_arr = explode(':', $form_data->checkbox, 2);
  65. return view($this->view_path . 'show', compact('item','pre_uri', 'model', 'model_name', 'form_data'));
  66. }
  67. public function showRemarkForm(Request $request)
  68. {
  69. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  70. return $this->showWarning('找不到订单');
  71. }
  72. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  73. return view($this->view_path . 'show-remark-form', compact('item','pre_uri', 'model', 'model_name'));
  74. }
  75. public function updateRemark(Request $request)
  76. {
  77. if(!$request->isMethod('POST')) {
  78. return $this->showWarning('访问错误');
  79. }
  80. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  81. return $this->showWarning('找不到订单');
  82. }
  83. $item->remark = $request->input('remark', '');
  84. if(!$item->save()) {
  85. return $this->showWarning('保存失败');
  86. }
  87. return $this->showMessage('操作成功');
  88. }
  89. public function delete(Request $request)
  90. {
  91. if(!$request->isMethod('POST')) {
  92. return $this->showWarning('访问错误');
  93. }
  94. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  95. return $this->showWarning('访问错误');
  96. }
  97. $res = $item->delete();
  98. if(!$res) {
  99. return $this->showWarning('数据库删除失败');
  100. }
  101. return $this->showMessage('操作成功');
  102. }
  103. }