PunishmentOrderController.php 5.3 KB

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