123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077 |
- <?php
- namespace App\Http\Controllers\App;
- use App\Filters\BikeFilter;
- use App\Filters\CardRidingOrderFilter;
- use App\Filters\DepositCardOrderFilter;
- use App\Filters\DepositFilter;
- use App\Filters\DepositOrderFilter;
- use App\Filters\OrderFilter;
- use App\Filters\OrderRentFilter;
- use App\Filters\RechargeOrderFilter;
- use App\Filters\UserFilter;
- use App\Filters\WalletLogFilter;
- use App\Http\Resources\App\OrderResource;
- use App\Models\AdminMerchant;
- use App\Models\AdminUser;
- use App\Models\AdminUserArea;
- use App\Models\Bike;
- use App\Models\CardRidingOrder;
- use App\Models\DepositCardOrder;
- use App\Models\DepositOrder;
- use App\Models\DepositRefund;
- use App\Models\LocationsLog;
- use App\Models\Order;
- use App\Models\OrderRent;
- use App\Models\RechargeOrder;
- 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\Cache;
- use Illuminate\Support\Facades\DB;
- 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)
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->count('id');
- $order_rent_total = OrderRent::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->whereIn('status', $riding_status)
- ->count('id');
- $riding_order_rent = OrderRent::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('area_id', $area_ids)
- ->filter($filter)
- ->where('status', Order::STATUS_CLOSE_BIKE)
- ->count('id');
- $waiting_pay_rent = OrderRent::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('area_id', $area_ids)
- ->filter($orderFilter)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->sum('pay_money');
- $totalOrderRentProfit = OrderRent::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('area_id', $area_ids)
- ->filter($orderFilter)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->where('created_at', '>', Carbon::today())
- ->sum('pay_money');
- $todayOrderRentProfit = OrderRent::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->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,BikeFilter $bikeFilter,RechargeOrderFilter $rechargeOrderFilter,CardRidingOrderFilter $cardRidingOrderFilter,DepositCardOrderFilter $depositCardOrderFilter)
- {
- $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()
- ->where(AdminMerchant::getMerchantWhere())
- ->filter($walletLogFilter)
- ->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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->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(AdminMerchant::getMerchantWhere())
- ->where('pay_status', OrderRent::PAY_STATUS_OK)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->whereIn('area_id', $area_ids)
- ->filter($orderRentFilter)
- ->sum('dispatch_money');
- // 已实名用户数
- $userTotalCardOk = User::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->filter($userFilter)
- ->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()
- ->where(AdminMerchant::getMerchantWhere())
- ->filter($userFilter)
- ->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(AdminMerchant::getMerchantWhere())
- ->filter($orderFilter)
- ->where('status', Order::STATUS_CLOSE_BIKE)
- ->whereIn('area_id', $area_ids)
- ->sum('pay_money');
- $orderRentWaitPay = OrderRent::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->filter($orderRentFilter)
- ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
- ->whereIn('area_id', $area_ids)
- ->sum('pay_money');
- // 待支付
- $wiatPayTotal = bcadd($orderWaitPay, $orderRentWaitPay, 2);
- $orderWaitPayNum = Order::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->filter($orderFilter)
- ->where('status', Order::STATUS_CLOSE_BIKE)
- ->whereIn('area_id', $area_ids)
- ->count();
- $orderRentWaitPayNum = OrderRent::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->filter($orderRentFilter)
- ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE)
- ->whereIn('area_id', $area_ids)
- ->count();
- $wiatNum = bcadd($orderWaitPayNum, $orderRentWaitPayNum);
- $bikeTotal = Bike::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->filter($bikeFilter)
- ->where('put_status', Bike::PUT_STATUS_YES)
- ->whereIn('put_area_id', $area_ids)
- ->count();
- $totalUsers = User::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->filter($userFilter)
- ->whereIn('register_area_id', $area_ids)
- ->count();
- $riddingCardOrderTotalProfit = CardRidingOrder::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('area_id', $area_ids)
- ->filter($cardRidingOrderFilter)
- ->where('pay_status', CardRidingOrder::PAY_STATUS_OK)
- ->sum('pay_money');
- $rechargeOrderTotalProfit = RechargeOrder::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('area_id', $area_ids)
- ->filter($rechargeOrderFilter)
- ->where('pay_status', RechargeOrder::PAY_STATUS_OK)
- ->sum('pay_money');
- $depositCardTotalProfit = DepositCardOrder::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->whereIn('area_id', $area_ids)
- ->filter($depositCardOrderFilter)
- ->where('pay_status', DepositCardOrder::PAY_STATUS_OK)
- ->sum('pay_money');
- $data = [
- 'totalProfit' => bcadd($orderTotalProfit, $orderRentTotalProfit, 2) ?? 0, // 新增收益
- 'depositTotal' => $depositTotal ?? 0, // 新增押金
- 'orderTotalProfit' => $orderTotalProfit ?? 0, // 新增普通订单收入 // 骑行花费
- 'orderTotal' => $orderTotal ?? 0, // 新增普通订单数量
- 'orderDispatchTotalProfit' => $orderDispatchTotalProfit ?? 0, // 新增普通订单调度费收入 // 调度费
- 'orderRentTotalProfit' => $orderRentTotalProfit ?? 0, // 新增日租订单收入
- 'orderRentTotal' => $orderRentTotal ?? 0, // 新增日租订单数量
- 'orderRentDispatchTotalProfit' => $orderRentDispatchTotalProfit ?? 0, // 新增日租订单调度费收入
- 'userTotalCardOk' => $userTotalCardOk ?? 0, // 新增身份认证用户
- 'userTotal' => $totalUsers ?? 0, // 总用户数
- 'userDepositTotal' => $userDepositTotal ?? 0, // 总缴纳押金用户数
- 'wiatPayTotal' => $wiatPayTotal ?? 0, //总待支付
- 'wiatNum' => $wiatNum ?? 0, //总待支付数量
- 'bikeTotal' => $bikeTotal ?? 0, // 总投放车辆
- 'days' => abs($days), // 天数
- 'riddingCardOrderTotalProfit' => $riddingCardOrderTotalProfit ?? 0, // 骑行卡收益
- 'rechargeOrderTotalProfit' => $rechargeOrderTotalProfit ?? 0, // 充值订单收益
- 'depositCardTotalProfit' => $depositCardTotalProfit ?? 0, // 免押金卡收益
- 'activityTotalProfit' => bcadd(bcadd($riddingCardOrderTotalProfit,$rechargeOrderTotalProfit,2),$depositCardTotalProfit,2), // 活动总收益
- ];
- 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()->where(AdminMerchant::getMerchantWhere())->where('id',$order_id)->first();
- 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),
- 'preferential_type_name' => $order->preferential_type_name, // 优惠方式
- 'preferential_money' => $order->preferential_money,// 优惠金额
- 'card_preferential_money' => $order->card_preferential_money,// 骑行卡优惠金额
- 'coupon_preferential_money' => $order->coupon_preferential_money,// 优惠券优惠金额
- 'is_coupon_name' => Order::$couponMaps[$order->is_coupon] ,// 优惠券优惠
- 'walletLogs' => $order->walletLogs,
- 'order_bike_operates' => $order->order_bike_operates,
- 'remark' => $order->remark ?? '',
- //'center_location' => GetCenterFromDegrees([['lat'=>$orderLocations[0][1],'lng'=>$orderLocations[0][0]],['lat'=>end($orderLocations)[1],'lng'=>end($orderLocations)[0]]])
- ];
- return $this->ok($data);
- }
- /**
- * orderLocation 订单轨迹
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function orderLocation(Request $request)
- {
- $order_id = $request->get('order_id') ?? '';
- if (empty($order_id)) return $this->error('参数错误');
- $orderLocations = [];
- $locationsTimes = [];
- try {
- $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();
- } catch (\Exception $exception) {
- Log::info($exception->getMessage());
- }
- if (!empty($locationsLog)) {
- foreach ($locationsLog as $vv) {
- $orderLocations[] = [$vv->longitude, $vv->latitude];
- $locationsTimes[] = Carbon::parse($vv->created_at)->format('Y-m-d H:i:s');
- }
- }
- $data = [
- 'orderLocations' => $orderLocations,
- 'locationsTimes' => $locationsTimes,
- ];
- return $this->ok($data);
- }
- /**
- * heatMap 热力图
- *
- * @param OrderFilter $orderFilter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function heatMap(OrderFilter $orderFilter)
- {
- $today = Carbon::today()->subMonth();
- //whereIn('area_id',self::$areaIds)->
- $order = Order::query()->filter($orderFilter)->where('created_at', '>', $today)->get();
- $data1 = [];
- $data2 = [];
- if (!empty($order)) {
- foreach ($order as $v) {
- $location = json_decode($v->start_use_bike_location);
- $location2 = json_decode($v->end_use_bike_location);
- $data1[] = ['lat' => $location->latitude, 'lng' => $location->longitude, 'count' => 3];
- if(!empty($location2)){
- $data2[] = ['lat' => $location2->latitude, 'lng' => $location2->longitude, 'count' => 3];
- }
- }
- }
- $data = [
- 'start' => $data1,
- 'end' => $data2
- ];
- return $this->ok($data);
- }
- /**
- * newOrderChart 新订单统计图
- *
- * @param Request $request
- * @param OrderFilter $orderFilter
- * @return array
- * @author Fx
- *
- */
- // public function newOrderChart(Request $request, OrderFilter $orderFilter)
- // {
- // $days = $request->get('days') ?? '';
- // $areaId = $request->get('area_id') ?? '';
- // if (empty($days) || empty($areaId)) return $this->error('缺少参数');
- // if (empty($days)) return $this->error('缺少参数');
- // $newOrders = Order::query()
- // ->filter($orderFilter)
- // ->where('status', Order::STATUS_COMPLETE_ORDER);
- //
- //// $admin_id = Admin::user()->id;
- //// if (!Admin::isAdministrator()) {
- //// $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- //// if (count($area_ids) !== 0) {
- //// $newOrders = $newOrders->whereIn('area_id', $area_ids);
- //// } else {
- //// $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
- //// $area_id = $area_id ?? 0;
- //// $newOrders = $newOrders->where('area_id', $area_id);
- //// }
- //// }
- //
- // switch ($days) {
- // case 'today':
- // $today = Carbon::today();
- // $newOrders = $newOrders->where('created_at', '>', $today)
- // ->selectRaw('DATE_FORMAT(created_at,"%m-%d %H:00") as date,count(id) as value,sum(pay_money) as moneys')
- // ->groupBy('date')->get()->toArray();
- // // 有数据得数组
- // $newOrdersKeyVal = [];
- // if (!empty($newOrders)) {
- // foreach ($newOrders as $v) {
- // $newOrdersKeyVal[$v['date']] = $v['value'];
- // }
- // }
- // // 为0得数组
- // $i = Carbon::now()->format('H');
- // $arr = [];
- // for ($i; $i >= 0; $i--) {
- // $str = Carbon::now()->subHours($i)->format('m-d H:00');
- // $arr[$str] = 0;
- // }
- // //合并
- // $merge = array_merge($arr, $newOrdersKeyVal);
- // $data = [];
- // foreach ($merge as $key=>$value){
- // $data[]=['date'=>$key,'value'=>$value];
- // }
- //
- // break;
- // case 'threeDays':
- // $i = 2;
- // break;
- // case 'sevenDays':
- // $i = 6;
- //
- // break;
- // case 'fifteenDays':
- // $i = 14;
- // break;
- // case 'thirtyDays':
- // $i = 29;
- // break;
- // default:
- // return $this->error('参数错误');
- //
- // }
- // if($days !== 'today'){
- // $paramDays = Carbon::today()->subDays($i);
- //
- // // 赋值一个值为0得数组
- // $arr = [];
- // for ($i ; $i >= 0; $i--){
- // $str = Carbon::today()->subDays($i)->format('Y/m/d');
- // $arr[$str] = 0;
- // }
- //
- // // 有数据得数组
- // $newOrders = $newOrders->where('created_at', '>', $paramDays)
- // ->selectRaw('DATE_FORMAT(created_at,"%Y/%m/%d") as date,count(id) as value,sum(pay_money) as moneys')
- // ->groupBy('date')->get()->toArray();
- // $newOrdersKeyVal = [];
- // $newOrdersKeyValMoney = [];
- // if(!empty($newOrders)){
- // foreach ($newOrders as $v){
- // $newOrdersKeyVal[$v['date']] = $v['value'];
- // $newOrdersKeyValMoney[$v['date']] = $v['moneys'];
- // }
- // }
- // // 合并覆盖0
- // $merge = array_merge($arr,$newOrdersKeyVal);
- // $mergemoney = array_merge($arr,$newOrdersKeyValMoney);
- // // return $this->ok($merge);
- //
- // // 重组结构
- // $data = [];
- // foreach ($merge as $ks=>$vs){
- // $data[]=['date'=>$ks,'value'=>$vs,'moneys'=>$mergemoney[$ks]];
- // }
- // }
- //
- // return $this->ok($data);
- // }
- public function orderChart($days, $orderFilter, $dispatch_money = false)
- {
- $newOrders = Order::query()
- ->where(AdminMerchant::getMerchantWhere())
- ->filter($orderFilter)
- ->where('status', Order::STATUS_COMPLETE_ORDER);
- if ($dispatch_money) {
- $newOrders = $newOrders->where('dispatch_money', '>', 0);
- }
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $newOrders = $newOrders->whereIn('area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
- $area_id = $area_id ?? 0;
- $newOrders = $newOrders->where('area_id', $area_id);
- }
- }
- switch ($days) {
- case 'today':
- $today = Carbon::today();
- $newOrders = $newOrders->where('created_at', '>', $today)
- ->selectRaw('DATE_FORMAT(created_at,"%m-%d %H:00") as date,count(id) as value')
- ->groupBy('date')->get()->toArray();
- // 有数据得数组
- $newOrdersKeyVal = [];
- if (!empty($newOrders)) {
- foreach ($newOrders as $v) {
- $newOrdersKeyVal[$v['date']] = $v['value'];
- }
- }
- // 为0得数组
- $i = Carbon::now()->format('H');
- $arr = [];
- for ($i; $i >= 0; $i--) {
- $str = Carbon::now()->subHours($i)->format('m-d H:00');
- $arr[$str] = 0;
- }
- //合并
- $merge = array_merge($arr, $newOrdersKeyVal);
- $data = [];
- foreach ($merge as $key => $value) {
- $data[] = ['date' => $key, 'value' => $value];
- }
- break;
- case 'threeDays':
- $i = 2;
- break;
- case 'sevenDays':
- $i = 6;
- break;
- case 'fifteenDays':
- $i = 14;
- break;
- case 'thirtyDays':
- $i = 29;
- break;
- default:
- return [];
- }
- if ($days !== 'today') {
- $paramDays = Carbon::today()->subDays($i);
- // 赋值一个值为0得数组
- $arr = [];
- for ($i; $i >= 0; $i--) {
- $str = Carbon::today()->subDays($i)->format('Y/m/d');
- $arr[$str] = 0;
- }
- // 有数据得数组
- $newOrders = $newOrders->where('created_at', '>', $paramDays)
- ->selectRaw('DATE_FORMAT(created_at,"%Y/%m/%d") as date,count(id) as value')
- ->groupBy('date')->get()->toArray();
- $newOrdersKeyVal = [];
- if (!empty($newOrders)) {
- foreach ($newOrders as $v) {
- $newOrdersKeyVal[$v['date']] = $v['value'];
- }
- }
- // 合并覆盖0
- $merge = array_merge($arr, $newOrdersKeyVal);
- // 重组结构
- $data = [];
- foreach ($merge as $ks => $vs) {
- $data[] = ['date' => $ks, 'value' => $vs];
- }
- }
- return $data;
- }
- public function newOrderChart(Request $request, OrderFilter $orderFilter)
- {
- $days = $request->get('days') ?? '';
- if (empty($days)) return $this->error('缺少参数');
- $newOrderChart = $this->orderChart($days, $orderFilter);
- $dispatchMoneyNewOrderChart = $this->orderChart($days, $orderFilter, true);
- return $this->ok([
- [
- 'name' => '总订单数',
- 'data' => $newOrderChart
- ], [
- 'name' => '有调度费的订单数',
- 'data' => $dispatchMoneyNewOrderChart
- ],
- ]);
- // return $this->ok([
- // $newOrderChart,$dispatchMoneyNewOrderChart
- // ]);
- }
- public function profitPolygonalChart($days, $filter, $model, $money = 'pay_money')
- {
- if (empty($days)) return $this->error('缺少参数');
- $newOrders = $model
- ->filter($filter)
- ->where('pay_status', 1);
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $newOrders = $newOrders->whereIn('area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
- $area_id = $area_id ?? 0;
- $newOrders = $newOrders->where('area_id', $area_id);
- }
- }
- switch ($days) {
- case 'today':
- $today = Carbon::today();
- $newOrders = $newOrders->where('pay_time', '>', $today)
- ->selectRaw("DATE_FORMAT(pay_time,'%m-%d %H:00') as date,sum($money) as value")
- ->groupBy('date')->get()->toArray();
- // 有数据得数组
- $newOrdersKeyVal = [];
- if (!empty($newOrders)) {
- foreach ($newOrders as $v) {
- $newOrdersKeyVal[$v['date']] = $v['value'];
- }
- }
- // 为0得数组
- $i = Carbon::now()->format('H');
- $arr = [];
- for ($i; $i >= 0; $i--) {
- $str = Carbon::now()->subHours($i)->format('m-d H:00');
- $arr[$str] = 0;
- }
- //合并
- $merge = array_merge($arr, $newOrdersKeyVal);
- $data = [];
- foreach ($merge as $key => $value) {
- $data[] = ['date' => $key, 'value' => $value];
- }
- break;
- case 'threeDays':
- $i = 2;
- break;
- case 'sevenDays':
- $i = 6;
- break;
- case 'fifteenDays':
- $i = 14;
- break;
- case 'thirtyDays':
- $i = 29;
- break;
- default:
- return $this->error('参数错误');
- }
- if ($days !== 'today') {
- $paramDays = Carbon::today()->subDays($i);
- // 赋值一个值为0得数组
- $arr = [];
- for ($i; $i >= 0; $i--) {
- $str = Carbon::today()->subDays($i)->format('Y/m/d');
- $arr[$str] = 0;
- }
- // 有数据得数组
- $newOrders = $newOrders->where('pay_time', '>', $paramDays)
- ->selectRaw("DATE_FORMAT(pay_time,'%Y/%m/%d') as date,sum($money) as value")
- ->groupBy('date')->get()->toArray();
- $newOrdersKeyVal = [];
- if (!empty($newOrders)) {
- foreach ($newOrders as $v) {
- $newOrdersKeyVal[$v['date']] = $v['value'];
- }
- }
- // 合并覆盖0
- $merge = array_merge($arr, $newOrdersKeyVal);
- // 重组结构
- $data = [];
- foreach ($merge as $ks => $vs) {
- $data[] = ['date' => $ks, 'value' => $vs];
- }
- }
- return $data;
- }
- public function profitChart(Request $request, OrderFilter $orderFilter, RechargeOrderFilter $rechargeOrderFilter, CardRidingOrderFilter $cardRidingOrderFilter, DepositCardOrderFilter $depositCardOrderFilter)
- {
- $days = $request->get('days') ?? '';
- $ordersChart = $this->profitPolygonalChart($days, $orderFilter, Order::query()->where(AdminMerchant::getMerchantWhere()));
- $ordersDispatchMoneyChart = $this->profitPolygonalChart($days, $orderFilter, Order::query()->where(AdminMerchant::getMerchantWhere()), 'dispatch_money');
- $rechargeOrdersChart = $this->profitPolygonalChart($days, $rechargeOrderFilter, RechargeOrder::query()->where(AdminMerchant::getMerchantWhere()));
- $cardRidingOrdersChart = $this->profitPolygonalChart($days, $cardRidingOrderFilter, CardRidingOrder::query()->where(AdminMerchant::getMerchantWhere()));
- $depositCardOrdersChart = $this->profitPolygonalChart($days, $depositCardOrderFilter, DepositCardOrder::query()->where(AdminMerchant::getMerchantWhere()));
- $total = [];
- foreach ($ordersChart as $k => $v) {
- $total[] = [
- 'date' => $v['date'],
- 'value' => bcadd(bcadd(bcadd($v['value'], $rechargeOrdersChart[$k]['value'], 2), $cardRidingOrdersChart[$k]['value'], 2), $depositCardOrdersChart[$k]['value'], 2)
- ];
- }
- if (Admin::isNormalAdministrator() || Admin::isAdministrator()){
- return $this->ok([
- [
- 'name' => '普通订单收益',
- 'data' => $ordersChart
- ],
- [
- 'name' => '订单调度费收益',
- 'data' => $ordersDispatchMoneyChart
- ],
- [
- 'name' => '充值收益',
- 'data' => $rechargeOrdersChart
- ],
- [
- 'name' => '骑行卡收益',
- 'data' => $cardRidingOrdersChart
- ],
- [
- 'name' => '免押金卡收益',
- 'data' => $depositCardOrdersChart
- ],
- [
- 'name' => '总收益',
- 'data' => $total
- ],
- ]);
- }else{
- return $this->ok([]);
- }
- }
- /**
- * 前七天小时订单统计
- * @return \Illuminate\Http\JsonResponse
- * User: Mead
- */
- public function hourOrderNumber(Request $request,OrderFilter $filter)
- {
- $day = [
- ["num" => 0, "hour" => 0],
- ["num" => 0, "hour" => 1],
- ["num" => 0, "hour" => 2],
- ["num" => 0, "hour" => 3],
- ["num" => 0, "hour" => 4],
- ["num" => 0, "hour" => 5],
- ["num" => 0, "hour" => 6],
- ["num" => 0, "hour" => 7],
- ["num" => 0, "hour" => 8],
- ["num" => 0, "hour" => 9],
- ["num" => 0, "hour" => 10],
- ["num" => 0, "hour" => 11],
- ["num" => 0, "hour" => 12],
- ["num" => 0, "hour" => 13],
- ["num" => 0, "hour" => 14],
- ["num" => 0, "hour" => 15],
- ["num" => 0, "hour" => 16],
- ["num" => 0, "hour" => 17],
- ["num" => 0, "hour" => 18],
- ["num" => 0, "hour" => 19],
- ["num" => 0, "hour" => 20],
- ["num" => 0, "hour" => 21],
- ["num" => 0, "hour" => 22],
- ["num" => 0, "hour" => 23]
- ];
- $weeks = [];
- $area_ids = self::$areaIds;
- for ($i = 1, $c = 7; $i < 7; $i++, $c--) {
- $now = Carbon::parse("-{$i}day");
- $date = $now->toDateString();
- $key = "{$date}-hour-order-num";
- $area_id = $request->get('area_id') ?? '';
- if(!empty($area_id)){
- $key = $key .'-area_id:'. $area_id;
- }
- // Cache::forget($key);
- $weeks["{$date}"] = Cache::remember($key, Carbon::parse("+{$c}day")->diffInMinutes(Carbon::now()), function () use ($date, $day,$filter,$area_ids) {
- return array_replace($day, Order::query()->where(AdminMerchant::getMerchantWhere())->filter($filter)->whereDate("created_at", $date)->whereIn('area_id',$area_ids)->select(DB::raw("count(*) as num,DATE_FORMAT(created_at,'%H') as hour"))->groupBy("hour")->get()->mapWithKeys(function ($item) {
- $arr = [
- 'num' => (integer)$item->num,
- 'hour' => (integer)$item->hour,
- ];
- return [(integer)$item->hour => $arr];
- })->toArray());
- });
- }
- $data = [
- 'date' => [],
- 'data' => []
- ];
- foreach ($weeks as $k => $v) {
- $data['date'][] = $k;
- $data['data'][$k] = [];
- foreach ($v as $value) {
- array_push($data['data'][$k], $value['num']);
- }
- }
- // dd($data);
- return $this->ok($data);
- }
- }
|