123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Filters\PunishmentOrderFilter;
- use App\Http\Requests\PunishmentOrderRequest;
- use App\Http\Resources\PunishmentOrderResource;
- use App\Models\AdminMerchant;
- use App\Models\AdminUser;
- use App\Models\Bike;
- use App\Models\Order;
- use App\Models\OrderRent;
- use App\Models\PunishmentOrder;
- use App\Models\User;
- use App\Utils\Admin;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- class PunishmentOrderController extends Controller
- {
- /**
- * index
- *
- * @param PunishmentOrderFilter $filter
- * @param Request $request
- * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
- * @author Fx
- *
- */
- public function index(PunishmentOrderFilter $filter, Request $request)
- {
- $punishments = PunishmentOrder::query()->filter($filter)->where(AdminMerchant::getMerchantWhere());
- $area_ids = AdminUser::getAreaIdsByAdminId(Admin::user()->id);
- $punishments = $punishments->whereIn('area_id', $area_ids)->orderByDesc('id');
- return $request->get('all') ? PunishmentOrderResource::collection($punishments->get()) : PunishmentOrderResource::collection($punishments->paginate());
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- //
- }
- /**
- * store
- *
- * @param PunishmentOrderRequest $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function store(PunishmentOrderRequest $request)
- {
- //
- $inputs = $request->validated();
- if (!empty($inputs['order_no'])) {
- $order = Order::where('no', $inputs['order_no'])->first();
- if (empty($order)) {
- $order = OrderRent::where('no', $inputs['order_no'])->first();
- if (empty($order)) {
- return $this->error('找不到此订单号对应得订单');
- }
- }
- }
- $user = User::query()->where('mobile', $inputs['user_mobile'])->first();
- if (empty($user)) return $this->error('找不到用户信息,请检查用户手机号');
- $bike = Bike::query()->where('bike_no', $inputs['bike_no'])->first();
- if (empty($bike)) return $this->error('找不到车辆相关信息,请检查车牌号');
- $data = [
- 'user_id' => $user->id,
- 'area_id' => $bike->put_area_id,
- 'order_id' => empty($order) ? 0 : $order->id,
- 'order_no' => $inputs['order_no'] ?? '',
- 'bike_id' => $bike->id,
- 'no' => PunishmentOrder::makeNo(),
- 'detail' => $inputs['detail'],
- 'pay_money' => $inputs['pay_money'],
- 'occurrence_time' => $inputs['occurrence_time'],
- 'merchant_id' => AdminMerchant::putMerchantId(),
- ];
- $res = PunishmentOrder::create($data);
- return $this->created(PunishmentOrderResource::make($res));
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit(PunishmentOrder $punishmentOrder)
- {
- //
- return PunishmentOrderResource::make($punishmentOrder);
- }
- /**
- * update
- *
- * @param PunishmentOrderRequest $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function update(PunishmentOrderRequest $request, PunishmentOrder $punishmentOrder)
- {
- //
- if ($punishmentOrder->pay_status == 1) return $this->error('订单已支付不可编辑');
- $inputs = $request->validated();
- if (!empty($inputs['order_no'])) {
- $order = Order::where('no', $inputs['order_no'])->first();
- if (empty($order)) {
- $order = OrderRent::where('no', $inputs['order_no'])->first();
- if (empty($order)) {
- return $this->error('找不到此订单号对应得订单');
- }
- }
- }
- $user = User::query()->where('mobile', $inputs['user_mobile'])->first();
- if (empty($user)) return $this->error('找不到用户信息,请检查用户手机号');
- $bike = Bike::query()->where('bike_no', $inputs['bike_no'])->first();
- if (empty($bike)) return $this->error('找不到车辆相关信息,请检查车牌号');
- $data = [
- 'user_id' => $user->id,
- 'area_id' => $bike->put_area_id,
- 'order_id' => empty($order) ? 0 : $order->id,
- 'bike_id' => $bike->id,
- 'order_no' => $inputs['order_no'] ?? '',
- 'detail' => $inputs['detail'],
- 'pay_money' => $inputs['pay_money'],
- 'occurrence_time' => $inputs['occurrence_time'],
- ];
- $punishmentOrder->update($data);
- return $this->created(PunishmentOrderResource::make($punishmentOrder));
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy(PunishmentOrder $punishmentOrder)
- {
- //
- if ($punishmentOrder->pay_status == PunishmentOrder::PAY_STATUS_OK) {
- return $this->error('罚单已支付,不可删除');
- }
- $punishmentOrder->delete();
- return $this->noContent();
- }
- }
|