PunishmentOrderController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Filters\PunishmentOrderFilter;
  4. use App\Http\Requests\PunishmentOrderRequest;
  5. use App\Http\Resources\PunishmentOrderResource;
  6. use App\Models\AdminMerchant;
  7. use App\Models\AdminUser;
  8. use App\Models\Bike;
  9. use App\Models\Order;
  10. use App\Models\OrderRent;
  11. use App\Models\PunishmentOrder;
  12. use App\Models\User;
  13. use App\Utils\Admin;
  14. use Illuminate\Http\Request;
  15. use App\Http\Controllers\Controller;
  16. class PunishmentOrderController extends Controller
  17. {
  18. /**
  19. * index
  20. *
  21. * @param PunishmentOrderFilter $filter
  22. * @param Request $request
  23. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
  24. * @author Fx
  25. *
  26. */
  27. public function index(PunishmentOrderFilter $filter, Request $request)
  28. {
  29. $punishments = PunishmentOrder::query()->filter($filter)->where(AdminMerchant::getMerchantWhere());
  30. $area_ids = AdminUser::getAreaIdsByAdminId(Admin::user()->id);
  31. $punishments = $punishments->whereIn('area_id', $area_ids)->orderByDesc('id');
  32. return $request->get('all') ? PunishmentOrderResource::collection($punishments->get()) : PunishmentOrderResource::collection($punishments->paginate());
  33. }
  34. /**
  35. * Show the form for creating a new resource.
  36. *
  37. * @return \Illuminate\Http\Response
  38. */
  39. public function create()
  40. {
  41. //
  42. }
  43. /**
  44. * store
  45. *
  46. * @param PunishmentOrderRequest $request
  47. * @return \Illuminate\Http\JsonResponse
  48. * @author Fx
  49. *
  50. */
  51. public function store(PunishmentOrderRequest $request)
  52. {
  53. //
  54. $inputs = $request->validated();
  55. if (!empty($inputs['order_no'])) {
  56. $order = Order::where('no', $inputs['order_no'])->first();
  57. if (empty($order)) {
  58. $order = OrderRent::where('no', $inputs['order_no'])->first();
  59. if (empty($order)) {
  60. return $this->error('找不到此订单号对应得订单');
  61. }
  62. }
  63. }
  64. $user = User::query()->where('mobile', $inputs['user_mobile'])->first();
  65. if (empty($user)) return $this->error('找不到用户信息,请检查用户手机号');
  66. $bike = Bike::query()->where('bike_no', $inputs['bike_no'])->first();
  67. if (empty($bike)) return $this->error('找不到车辆相关信息,请检查车牌号');
  68. $data = [
  69. 'user_id' => $user->id,
  70. 'area_id' => $bike->put_area_id,
  71. 'order_id' => empty($order) ? 0 : $order->id,
  72. 'order_no' => $inputs['order_no'] ?? '',
  73. 'bike_id' => $bike->id,
  74. 'no' => PunishmentOrder::makeNo(),
  75. 'detail' => $inputs['detail'],
  76. 'pay_money' => $inputs['pay_money'],
  77. 'occurrence_time' => $inputs['occurrence_time'],
  78. 'merchant_id' => AdminMerchant::putMerchantId(),
  79. ];
  80. $res = PunishmentOrder::create($data);
  81. return $this->created(PunishmentOrderResource::make($res));
  82. }
  83. /**
  84. * Show the form for editing the specified resource.
  85. *
  86. * @param int $id
  87. * @return \Illuminate\Http\Response
  88. */
  89. public function edit(PunishmentOrder $punishmentOrder)
  90. {
  91. //
  92. return PunishmentOrderResource::make($punishmentOrder);
  93. }
  94. /**
  95. * update
  96. *
  97. * @param PunishmentOrderRequest $request
  98. * @return \Illuminate\Http\JsonResponse
  99. * @author Fx
  100. *
  101. */
  102. public function update(PunishmentOrderRequest $request, PunishmentOrder $punishmentOrder)
  103. {
  104. //
  105. if ($punishmentOrder->pay_status == 1) return $this->error('订单已支付不可编辑');
  106. $inputs = $request->validated();
  107. if (!empty($inputs['order_no'])) {
  108. $order = Order::where('no', $inputs['order_no'])->first();
  109. if (empty($order)) {
  110. $order = OrderRent::where('no', $inputs['order_no'])->first();
  111. if (empty($order)) {
  112. return $this->error('找不到此订单号对应得订单');
  113. }
  114. }
  115. }
  116. $user = User::query()->where('mobile', $inputs['user_mobile'])->first();
  117. if (empty($user)) return $this->error('找不到用户信息,请检查用户手机号');
  118. $bike = Bike::query()->where('bike_no', $inputs['bike_no'])->first();
  119. if (empty($bike)) return $this->error('找不到车辆相关信息,请检查车牌号');
  120. $data = [
  121. 'user_id' => $user->id,
  122. 'area_id' => $bike->put_area_id,
  123. 'order_id' => empty($order) ? 0 : $order->id,
  124. 'bike_id' => $bike->id,
  125. 'order_no' => $inputs['order_no'] ?? '',
  126. 'detail' => $inputs['detail'],
  127. 'pay_money' => $inputs['pay_money'],
  128. 'occurrence_time' => $inputs['occurrence_time'],
  129. ];
  130. $punishmentOrder->update($data);
  131. return $this->created(PunishmentOrderResource::make($punishmentOrder));
  132. }
  133. /**
  134. * Remove the specified resource from storage.
  135. *
  136. * @param int $id
  137. * @return \Illuminate\Http\Response
  138. */
  139. public function destroy(PunishmentOrder $punishmentOrder)
  140. {
  141. //
  142. if ($punishmentOrder->pay_status == PunishmentOrder::PAY_STATUS_OK) {
  143. return $this->error('罚单已支付,不可删除');
  144. }
  145. $punishmentOrder->delete();
  146. return $this->noContent();
  147. }
  148. }