123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- <?php
- namespace App\Http\Controllers\App;
- use App\Filters\DepositFilter;
- use App\Filters\DepositOrderFilter;
- use App\Filters\OrderFilter;
- use App\Filters\OrderRentFilter;
- use App\Filters\UserFilter;
- use App\Filters\WalletLogFilter;
- use App\Http\Resources\App\OrderResource;
- use App\Models\AdminUser;
- use App\Models\Bike;
- use App\Models\DepositOrder;
- use App\Models\DepositRefund;
- use App\Models\LocationsLog;
- use App\Models\Order;
- use App\Models\OrderRent;
- use App\Models\User;
- use App\Models\WalletLog;
- use App\Utils\Admin;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\Log;
- class OrderController extends AppBaseController
- {
- /**
- * orderList 订单列表
- *
- * @param OrderFilter $filter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function orderList(OrderFilter $filter)
- {
- $area_ids = self::$areaIds;
- $order = Order::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->with('users')
- ->where('status', '!=', Order::STATUS_CLOSE_ORDER)
- ->orderByDesc('id')
- ->paginate();
- return $this->ok(OrderResource::collection($order));
- }
- /**
- * orderStatistics 订单统计
- *
- * @param OrderFilter $filter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function orderStatistics(OrderFilter $filter)
- {
- $area_ids = self::$areaIds;
- // 总订单数
- $order_total = Order::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->count('id');
- $order_rent_total = OrderRent::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->count('id');
- $order_total += $order_rent_total;
- // 今日新增订单数
- $today_add_order = Order::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('created_at', '>', Carbon::today())
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->count('id');
- $today_add_order_rent = OrderRent::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('pay_time', '>', Carbon::today())
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->count('id');
- $today_add_order += $today_add_order_rent;
- // 骑行中订单数
- $riding_status = [
- Order::STATUS_RIDE_BIKE,
- // Order::STATUS_CLOSE_BIKE, // 待支付
- Order::STATUS_PAUSE_BIKE,
- ];
- $riding_order = Order::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->whereIn('status', $riding_status)
- ->count('id');
- $riding_order_rent = OrderRent::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('status', OrderRent::STATUS_RENT_BIKE)
- ->count('id');
- $riding_order += $riding_order_rent;
- // 待支付
- $waiting_pay_order = Order::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('status', Order::STATUS_CLOSE_BIKE)
- ->count('id');
- $waiting_pay_rent = OrderRent::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
- ->count('id');
- $waiting_pay_order += $waiting_pay_rent;
- // 今日新增收入
- $today_add_money = Order::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('pay_time', '>', Carbon::today())
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->sum('pay_money');
- $today_add_money_rent = OrderRent::query()
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('pay_time', '>', Carbon::today())
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->sum('pay_money');
- $today_add_money += $today_add_money_rent;
- $order = [
- 'order_total' => $order_total ?? 0,
- 'today_add_order' => $today_add_order ?? 0,
- 'riding_order' => $riding_order ?? 0,
- 'today_add_money' => $today_add_money ?? 0,
- 'waiting_pay' => $waiting_pay_order ?? 0,
- ];
- return $this->ok($order);
- }
- /**
- * orderProfitStatistics 订单收益统计
- *
- * @param WalletLogFilter $walletLogFilter
- * @param DepositFilter $depositFilter
- * @param OrderFilter $orderFilter
- * @param OrderRentFilter $orderRentFilter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function orderProfitStatistics(WalletLogFilter $walletLogFilter, DepositFilter $depositFilter, OrderFilter $orderFilter, OrderRentFilter $orderRentFilter)
- {
- $area_ids = self::$areaIds;
- $admin_type = Admin::user()->type;
- if ($admin_type == AdminUser::TYPE_WORKER) {
- $data = [
- 'totalProfit' => $totalProfit ?? 0,
- 'depositTotal' => $depositTotal ?? 0,
- 'todayProfit' => $todayProfit ?? 0,
- 'depositToday' => $depositToday ?? 0,
- 'monthProfit' => $monthProfit ?? 0,
- ];
- return $this->ok($data);
- }
- // 总收益
- // $totalProfit = WalletLog::query()
- // ->whereIn('area_id', $area_ids)
- // ->filter($walletLogFilter)
- // ->whereIn('type', WalletLog::$subType)
- // ->sum('money');
- $totalOrderProfit = Order::query()
- ->whereIn('area_id', $area_ids)
- ->filter($orderFilter)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->sum('pay_money');
- $totalOrderRentProfit = OrderRent::query()
- ->whereIn('area_id', $area_ids)
- ->filter($orderRentFilter)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->sum('pay_money');
- $totalProfit = bcadd($totalOrderProfit, $totalOrderRentProfit, 0);
- // 总押金
- $depositTotal = DepositOrder::query()
- ->where('pay_status', DepositOrder::PAY_STATUS_OK)
- ->where('is_refund', DepositOrder::REFUND_NO)
- ->whereIn('area_id', $area_ids)
- ->filter($depositFilter)
- ->sum('pay_money');
- // 今日收益
- // $todayProfit = WalletLog::query()
- // ->whereIn('area_id', $area_ids)
- // ->filter($walletLogFilter)
- // ->whereIn('type', WalletLog::$subType)
- // ->where('created_at', '>', Carbon::today())
- // ->sum('money');
- $todayOrderProfit = Order::query()
- ->whereIn('area_id', $area_ids)
- ->filter($orderFilter)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->where('created_at', '>', Carbon::today())
- ->sum('pay_money');
- $todayOrderRentProfit = OrderRent::query()
- ->whereIn('area_id', $area_ids)
- ->filter($orderRentFilter)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->where('created_at', '>', Carbon::today())
- ->sum('pay_money');
- $todayProfit = bcadd($todayOrderProfit, $todayOrderRentProfit, 0);
- // 今日新增押金
- $depositToday = DepositOrder::query()
- ->where('pay_status', DepositOrder::PAY_STATUS_OK)
- ->where('is_refund', DepositOrder::REFUND_NO)
- ->whereIn('area_id', $area_ids)
- ->filter($depositFilter)
- ->where('created_at', '>', Carbon::today())
- ->sum('pay_money');
- // 本月收益
- // $monthProfit = WalletLog::query()
- // ->whereIn('area_id', $area_ids)
- // ->filter($walletLogFilter)
- // ->whereIn('type', WalletLog::$subType)
- // ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
- // ->sum('money');
- $monthOrderProfit = Order::query()
- ->whereIn('area_id', $area_ids)
- ->filter($orderFilter)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
- ->sum('pay_money');
- $monthOrderRentProfit = OrderRent::query()
- ->whereIn('area_id', $area_ids)
- ->filter($orderRentFilter)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->where('created_at', '>', Carbon::today()->format('Y-m-01 00:00:00'))
- ->sum('pay_money');
- $monthProfit = bcadd($monthOrderProfit, $monthOrderRentProfit, 0);
- $data = [
- 'totalProfit' => floor(abs($totalProfit)) ?? 0,
- 'depositTotal' => floor($depositTotal) ?? 0,
- 'todayProfit' => floor($todayProfit) ?? 0,
- 'depositToday' => floor($depositToday) ?? 0,
- 'monthProfit' => floor($monthProfit) ?? 0,
- ];
- return $this->ok($data);
- }
- /**
- * profitDetail 收益详情
- *
- * @param Request $request
- * @param OrderFilter $orderFilter
- * @param OrderRentFilter $orderRentFilter
- * @param DepositFilter $depositFilter
- * @param WalletLogFilter $walletLogFilter
- * @param UserFilter $userFilter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function profitDetail(Request $request, OrderFilter $orderFilter, OrderRentFilter $orderRentFilter, DepositFilter $depositFilter, WalletLogFilter $walletLogFilter, UserFilter $userFilter)
- {
- $area_ids = self::$areaIds;
- $time_between = $request->get('pay_time_between') ?? [];
- if (empty($time_between)) return $this->error('参数错误');
- $t1 = Carbon::make($time_between[0]);
- $t2 = Carbon::make($time_between[1]);
- $days = $t1->diffInDays($t2);
- // 总收益
- $totalProfit = WalletLog::query()
- ->whereIn('area_id', $area_ids)
- ->where('created_at', '>', date('Y-m-d H:i:s', strtotime($time_between[0])))
- ->where('created_at', '<', date('Y-m-d H:i:s', strtotime($time_between[1])))
- ->whereIn('type', WalletLog::$subType)
- ->sum('money');
- // 总押金
- $depositTotal = DepositOrder::query()
- ->where('pay_status', DepositOrder::PAY_STATUS_OK)
- ->where('is_refund', DepositOrder::REFUND_NO)
- ->whereIn('area_id', $area_ids)
- ->filter($depositFilter)
- ->sum('pay_money');
- // 普通订单
- $orderTotalProfit = Order::query()
- ->where('pay_status', Order::PAY_STATUS_OK)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->whereIn('area_id', $area_ids)
- ->filter($orderFilter)
- ->sum('pay_money');
- // 骑行花费
- $orderTotalTimeProfit = Order::query()
- ->where('pay_status', Order::PAY_STATUS_OK)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->whereIn('area_id', $area_ids)
- ->filter($orderFilter)
- ->sum('time_money');
- $orderTotal = Order::query()
- ->where('pay_status', Order::PAY_STATUS_OK)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->whereIn('area_id', $area_ids)
- ->filter($orderFilter)
- ->count();
- $orderDispatchTotalProfit = Order::query()
- ->where('pay_status', Order::PAY_STATUS_OK)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->whereIn('area_id', $area_ids)
- ->filter($orderFilter)
- ->sum('dispatch_money');
- // 日租订单
- $orderRentTotalProfit = OrderRent::query()
- ->where('pay_status', OrderRent::PAY_STATUS_OK)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->whereIn('area_id', $area_ids)
- ->filter($orderRentFilter)
- ->sum('pay_money');
- // 日租超时花费
- $orderRentTotalTimeProfit = OrderRent::query()
- ->where('pay_status', OrderRent::PAY_STATUS_OK)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->whereIn('area_id', $area_ids)
- ->filter($orderRentFilter)
- ->sum('time_money');
- // 日租租金
- $orderRentTotalRentProfit = OrderRent::query()
- ->where('pay_status', OrderRent::PAY_STATUS_OK)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->whereIn('area_id', $area_ids)
- ->filter($orderRentFilter)
- ->sum('rent_money');
- $orderRentTotal = OrderRent::query()
- ->where('pay_status', OrderRent::PAY_STATUS_OK)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->whereIn('area_id', $area_ids)
- ->filter($orderRentFilter)
- ->count();
- // 日租调度费
- $orderRentDispatchTotalProfit = OrderRent::query()
- ->where('pay_status', OrderRent::PAY_STATUS_OK)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->whereIn('area_id', $area_ids)
- ->filter($orderRentFilter)
- ->sum('dispatch_money');
- // 已实名用户数
- $userTotal = User::query()
- ->whereIn('register_area_id', $area_ids)
- ->where('is_card_certified', User::CARD_OK)
- ->where('created_at', '>', date('Y-m-d H:i:s', strtotime($time_between[0])))
- ->where('created_at', '<', date('Y-m-d H:i:s', strtotime($time_between[1])))
- ->count();
- // 已缴纳押金用户数
- $userDepositTotal = User::query()
- ->whereIn('register_area_id', $area_ids)
- ->where('is_card_certified', User::CARD_OK)
- ->where('is_card_certified', User::CARD_OK)
- ->where('is_deposit', User::DEPOSIT_OK)
- ->count();
- $orderWaitPay = Order::query()
- ->where('status', Order::STATUS_CLOSE_BIKE)
- ->whereIn('area_id', $area_ids)
- ->sum('pay_money');
- $orderRentWaitPay = OrderRent::query()
- ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
- ->whereIn('area_id', $area_ids)
- ->sum('pay_money');
- // 待支付
- $wiatPayTotal = bcadd($orderWaitPay, $orderRentWaitPay, 2);
- $orderWaitPayNum = Order::query()
- ->where('status', Order::STATUS_CLOSE_BIKE)
- ->whereIn('area_id', $area_ids)
- ->count();
- $orderRentWaitPayNum = OrderRent::query()
- ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
- ->whereIn('area_id', $area_ids)
- ->count();
- $wiatNum = bcadd($orderWaitPayNum, $orderRentWaitPayNum);
- $bikeTotal = Bike::query()
- ->where('put_status', Bike::PUT_STATUS_YES)
- ->count();
- $data = [
- 'totalProfit' => bcadd($orderTotalProfit,$orderRentTotalProfit,2) ?? 0, // 新增收益
- 'depositTotal' => $depositTotal ?? 0, // 新增押金
- 'orderTotalProfit' => $orderTotalTimeProfit ?? 0, // 新增普通订单收入 // 骑行花费
- 'orderTotal' => $orderTotal ?? 0, // 新增普通订单数量
- 'orderDispatchTotalProfit' => $orderDispatchTotalProfit ?? 0, // 新增普通订单调度费收入 // 调度费
- 'orderRentTotalProfit' => bcadd($orderRentTotalTimeProfit,$orderRentTotalRentProfit,2) ?? 0, // 新增日租订单收入
- 'orderRentTotal' => $orderRentTotal ?? 0, // 新增日租订单数量
- 'orderRentDispatchTotalProfit' => $orderRentDispatchTotalProfit ?? 0, // 新增日租订单调度费收入
- 'userTotal' => $userTotal ?? 0, // 新增身份认证用户
- 'userDepositTotal' => $userDepositTotal ?? 0, // 总缴纳押金用户数
- 'wiatPayTotal' => $wiatPayTotal ?? 0, //总待支付
- 'wiatNum' => $wiatNum ?? 0, //总待支付数量
- 'bikeTotal' => $bikeTotal ?? 0, // 总投放车辆
- 'days' => abs($days), // 天数
- ];
- return $this->ok($data);
- }
- /**
- * orderDetail 订单详情
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function orderDetail(Request $request)
- {
- $order_id = $request->get('order_id') ?? '';
- if (empty($order_id)) return $this->error('参数错误');
- $order = Order::query()->find($order_id);
- if (empty($order)) return $this->error('找不到该订单,参数错误');
- $orderLocations = [];
- $wx_orderLocations = [];
- // 订单轨迹
- $locationsLog = LocationsLog::query()->where('order_id', (int)$order_id)->where('is_rent', LocationsLog::RENT_NO)->whereBetween('latitude', [3, 53])->whereBetween('longitude', [73, 136])->orderBy('created_at', 'asc')->get();
- if (!empty($locationsLog)) {
- foreach ($locationsLog as $vv) {
- $orderLocations[] = [$vv->longitude, $vv->latitude];
- $wx_orderLocations[] = ['longitude' => $vv->longitude, 'latitude' => $vv->latitude];
- }
- }
- // Log::info($orderLocations);
- $data = [
- 'order_status' => Order::$statusMaps[$order->status],
- 'start_use_bike_time' => date('Y/m/d H:i:s', strtotime($order->start_use_bike_time)),
- 'end_use_bike_time' => $order->end_use_bike_time ? date('Y/m/d H:i:s', strtotime($order->end_use_bike_time)) : '',
- 'pay_money' => $order->pay_money,
- 'dispatch_money' => $order->dispatch_money,
- 'time_money' => $order->time_money,
- 'bike_no' => $order->bike_no,
- 'bike_id' => $order->bike_id,
- 'user_id' => $order->user_id,
- 'nickname' => $order->users->nickname ?? '',
- 'mobile' => $order->users->mobile ?? '',
- 'orderLocations' => $orderLocations,
- 'start_location' => empty($orderLocations) ? [] : current($orderLocations),
- 'end_location' => empty($orderLocations) ? [] : end($orderLocations),
- 'wx_orderLocations' => $wx_orderLocations,
- 'wx_start_location' => empty($wx_orderLocations) ? [] : current($wx_orderLocations),
- 'wx_end_location' => empty($wx_orderLocations) ? [] : end($wx_orderLocations),
- //'center_location' => GetCenterFromDegrees([['lat'=>$orderLocations[0][1],'lng'=>$orderLocations[0][0]],['lat'=>end($orderLocations)[1],'lng'=>end($orderLocations)[0]]])
- ];
- return $this->ok($data);
- }
- }
|