1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705 |
- <?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 App\Models\Area;
- use App\Models\Statistic;
- 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);
- }
- /**
- * 商户列表
- * @return \Illuminate\Http\JsonResponse
- * @author ht
- */
- public function adminMerchantList()
- {
- $data = [];
- // 管理员可以获取商户列表
- if (Admin::isAdministrator()) {
- // status 状态(1正常0暂停)
- $data = AdminMerchant::where('status', 1)->select(['id', 'username', 'name'])->get()->toArray();
- }
- return $this->ok($data);
- }
- /**
- * 区域列表 - 依据所选商户获取对应区域
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author ht
- */
- public function areaList(Request $request)
- {
- $admin_id = Admin::user()->id;
- if (Admin::isAdministrator()) {
- $merchant_id = $request->get('merchant_id') ?? 0;
- if ($merchant_id) {
- $data = Area::where('status', 1)->where('merchant_id', $merchant_id)->orderByDesc('id')->select(['id', 'name'])->get()->toArray();
- } else {
- $data = Area::where('status', 1)->orderByDesc('id')->select(['id', 'name'])->get()->toArray();
- }
- } else {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- $data = Area::whereIn('id', $area_ids)->where('status', 1)->orderByDesc('id')->select(['id', 'name'])->get()->toArray();
- }
- 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_back(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);
- }
- // 验证是否有该区域的权限
- private function validateAreaId($area_id)
- {
- $area_ids = AdminUser::getAreaIdsByAdminId(Admin::user()->id);
- if (count($area_ids) !== 0 && in_array($area_id, $area_ids)) {
- return false;
- } else {
- $id = AdminUserArea::query()->where('admin_id', Admin::user()->id)->value('area_id');
- $id = $id ?? 0;
- if ($area_id == $id) return false;
- }
- return true;
- }
- /**
- * 重构 profitDetail
- * 收益详情统计
- * 区分管理员和商户,依据不同角色查询
- * 添加商户列表和区域列表接口
- * 查询时间做个限制,最多查询三个月的记录
- * 重构总收益和总押金,累计净押金的时候,
- * 注意历史数据可以直接查询统计表statistics,当天数据的查询可以直接查询wallet_logs
- */
- public function profitDetail(Request $request, OrderFilter $orderFilter, OrderRentFilter $orderRentFilter, DepositFilter $depositFilter, WalletLogFilter $walletLogFilter, UserFilter $userFilter,BikeFilter $bikeFilter,RechargeOrderFilter $rechargeOrderFilter,CardRidingOrderFilter $cardRidingOrderFilter,DepositCardOrderFilter $depositCardOrderFilter)
- {
- $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);
- // 如果选择的时间段超过了三个月,拒绝查询
- if($days > 92) return $this->error('选择的时间段超过了三个月');
- $area_id = $request->get('area_id') ?? '';
- if ($area_id) {
- $flag = $this->validateAreaId($area_id);
- if ($flag) return $this->error('无权限查看该区域');
- }
- $merchant_id = $request->get('merchant_id') ?? '';
- $startTime = date('Y-m-d', strtotime($time_between[0]));
- $endTime = date('Y-m-d', strtotime($time_between[1]));
- // 总收益
- $totalProfit = $this->getTotalProfit($area_id,$merchant_id,$startTime,$endTime,Statistic::SLUG_PROFIT_STATIC);
- $resDa = $this->getTotalProfit($area_id,$merchant_id,$startTime,$endTime,Statistic::SLUG_DEPOSIT_STATIC);
- $depositTotal = $resDa[0]; // 总押金
- $cumulativeDepositTotal = $resDa[1]; // 累计净押金
- // 普通订单
- $orderTotalProfit = $this->getOrderTotalProfit($area_id,$merchant_id,$startTime,$endTime,$orderFilter,'pay_money');
- // 骑行花费
- $orderTotalTimeProfit = $this->getOrderTotalProfit($area_id,$merchant_id,$startTime,$endTime,$orderFilter,'time_money');
- // 订单数量
- $orderTotal = $this->getOrderTotalProfit($area_id,$merchant_id,$startTime,$endTime,$orderFilter);
- // 里程费用
- $orderDispatchTotalProfit = $this->getOrderTotalProfit($area_id,$merchant_id,$startTime,$endTime,$orderFilter,'dispatch_money');
- // 日租订单
- $orderRentTotalProfit = $this->getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'pay_money');
- // 日租超时花费
- $orderRentTotalTimeProfit = $this->getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'time_money');
- // 日租租金
- $orderRentTotalRentProfit = $this->getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'rent_money');
- // 日租订单数量
- $orderRentTotal = $this->getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter);
- // 日租调度费
- $orderRentDispatchTotalProfit = $this->getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'dispatch_money');
- // 已实名用户数
- $userTotalCardOk = $this->getUserDetails($area_id,$merchant_id,$startTime,$endTime,$userFilter,'is_card_certified');
- // 已缴纳押金用户数
- $userDepositTotal = $this->getUserDetails($area_id,$merchant_id,$startTime,$endTime,$userFilter,'is_deposit');
- // 骑车结束,待支付总金额
- $orderWaitPay = $this->getOrderWaitPay($area_id,$merchant_id,$startTime,$endTime,$orderFilter,'order','pay_money');
- $orderRentWaitPay = $this->getOrderWaitPay($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'orderRent','pay_money');
- $wiatPayTotal = bcadd($orderWaitPay, $orderRentWaitPay, 2);
- // 骑车结束,待支付总订单数量
- $orderWaitPayNum = $this->getOrderWaitPay($area_id,$merchant_id,$startTime,$endTime,$orderFilter,'order');
- $orderRentWaitPayNum = $this->getOrderWaitPay($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,'orderRent');
- $wiatNum = bcadd($orderWaitPayNum, $orderRentWaitPayNum);
- // 总投放车辆
- $bikeTotal = $this->getBikeTotal($area_id,$merchant_id,$startTime,$endTime,$bikeFilter);
- // 总用户数
- $totalUsers = $this->getUserDetails($area_id,$merchant_id,$startTime,$endTime,$userFilter,'total_num');
- // 骑行卡收益
- $riddingCardOrderTotalProfit = $this->getRiddingCardOrderProfit($area_id,$merchant_id,$startTime,$endTime,$cardRidingOrderFilter);
- // 充值订单收益
- $rechargeOrderTotalProfit = $this->getRechargeOrderProfit($area_id,$merchant_id,$startTime,$endTime,$rechargeOrderFilter);
- // 免押金卡收益
- $depositCardTotalProfit = $this->getDepositCardTotalProfit($area_id,$merchant_id,$startTime,$endTime,$depositCardOrderFilter);
- $data = [
- 'totalProfit' => bcadd($orderTotalProfit, $orderRentTotalProfit, 2) ?? 0, // 新增收益
- 'depositTotal' => $depositTotal ?? 0, // 新增押金
- 'cumulativeDepositTotal' => $cumulativeDepositTotal ?? 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);
- }
- // profitDetail 收益详情 - 总收益 - 总押金 - 累计净押金
- private function getTotalProfit($area_id,$merchant_id,$startTime,$endTime,$slug)
- {
- if (Admin::isAdministrator()) {
- if ($merchant_id) {
- $newOrders = Statistic::query()->where('merchant_id', $merchant_id)->where('slug', $slug);
- } else {
- $newOrders = Statistic::query()->where('merchant_id', 0)->where('slug', $slug);
- }
- if(empty($area_id)){
- $newOrders = $newOrders->where('area_id', config('statistic.all'));
- }
- } else {
- $newOrders = Statistic::query()->where(AdminMerchant::getMerchantWhere())->where('slug', $slug);
- if(empty($area_id)){
- $newOrders = $newOrders->where('area_id', 0);
- }
- }
- if ($area_id) {
- $newOrders = $newOrders->where('area_id', $area_id);
- }
- $data = $newOrders->whereBetween('date', [$startTime,$endTime])->select('body')->get()->toArray();
-
- if($slug == Statistic::SLUG_PROFIT_STATIC){
- $totalMoney = 0.00;
- }else{
- $totalMoney = [0.00,0.00];
- }
- if (!empty($data)) {
- switch($slug){
- case Statistic::SLUG_PROFIT_STATIC: // 总收益
- foreach ($data as $v) {
- $body = json_decode($v['body'], true);
- $totalMoney = bcadd($totalMoney,bcadd($body['profit'],$body['card_profit'],2),2);
- }
- break;
- case Statistic::SLUG_DEPOSIT_STATIC:
- // 总押金
- foreach ($data as $v) {
- $body = json_decode($v['body'], true);
- $money_all = isset($body['money_all']) ? $body['money_all'] : 0;
- $totalMoney[0] = bcadd($totalMoney[0],$money_all,2);
- }
- // 累计净押金
- foreach ($data as $v) {
- $body = json_decode($v['body'], true);
- $money_all = isset($body['money_all']) ? $body['money_all'] : 0;
- $money_refund_all = isset($body['money_refund_all']) ? $body['money_refund_all'] : 0.00;
- $totalMoney[1] = bcadd($totalMoney[1],bcsub($money_all,$money_refund_all,2),2);
- }
- break;
- default:
- }
- }
- if($slug == Statistic::SLUG_PROFIT_STATIC){
- // 时间区间包含今天的时候
- if($endTime == date('Y-m-d') || (strtotime($endTime) >= strtotime(date('Y-m-d 00:00:00')))){
- // 再加上今天的收益 = 纯收益 + 骑行卡收益
- $totayMoney = $this->getTotayTotalProfit($area_id, $merchant_id);
- $totalMoney = bcadd($totalMoney,$totayMoney,2);
- }
- }else{
- // 时间区间包含今天的时候
- if($endTime == date('Y-m-d') || (strtotime($endTime) >= strtotime(date('Y-m-d 00:00:00')))){
- // 再加上今天的押金 和 累计净押金
- $resDa = $this->getTotayDeposit($area_id, $merchant_id);
- $totalMoney[0] = bcadd($totalMoney[0],$resDa[0],2);
- $totalMoney[1] = bcadd($totalMoney[1],$resDa[0],2);
- }
- }
- return $totalMoney;
- }
- // profitDetail 收益详情 - 总收益 - 今天收益
- private function getTotayTotalProfit($area_id, $merchant_id)
- {
- $totalProfit = 0.00;
- // 管理员,并且选择了商户
- if (Admin::isAdministrator() && $merchant_id) {
- $walletLogProfit = WalletLog::query()->where('merchant_id', $merchant_id);
- $cardRidingOrdersProfit = CardRidingOrder::query()->where('merchant_id',$merchant_id);
- } else {
- $walletLogProfit = WalletLog::query()->where(AdminMerchant::getMerchantWhere());
- $cardRidingOrdersProfit = CardRidingOrder::query()->where(AdminMerchant::getMerchantWhere());
- }
- if (!empty($area_id)) {
- $walletLogProfit = $walletLogProfit->where('area_id', $area_id);
- $cardRidingOrdersProfit = $cardRidingOrdersProfit->where('area_id', $area_id);
- } else {
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $walletLogProfit = $walletLogProfit->whereIn('area_id', $area_ids);
- $cardRidingOrdersProfit = $cardRidingOrdersProfit->whereIn('area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
- $area_id = $area_id ?? 0;
- $walletLogProfit = $walletLogProfit->where('area_id', $area_id);
- $cardRidingOrdersProfit = $cardRidingOrdersProfit->where('area_id', $area_id);
- }
- }
- }
- // 口袋钱包纯收益
- $walletLogProfit = $walletLogProfit
- ->whereIn('type', WalletLog::$subType) // 交易类型 - subType 存收益
- ->where('status', WalletLog::STATUS_OK) // 数据状态 - 有效
- ->where('created_at','>=',Carbon::today())
- ->sum('money');
- // 骑行卡总收益
- $cardRidingOrdersProfit = $cardRidingOrdersProfit
- ->where('pay_type', CardRidingOrder::PAY_TYPE_WECHAT) // 支付方式 - 微信支付
- ->where('pay_status', CardRidingOrder::PAY_STATUS_OK) // 支付状态 - 支付成功
- ->where('status', CardRidingOrder::STATUS_OK) // 订单状态 - 成功
- ->where('pay_time','>=',Carbon::today())
- ->sum('pay_money');
- $totalProfit = abs((float)$walletLogProfit);
- $totalProfit = bcadd($totalProfit,(float)$cardRidingOrdersProfit,2);
- return $totalProfit;
- }
- // profitDetail 收益详情 - 总押金 - 累计净押金 - 今天金额
- private function getTotayDeposit($area_id, $merchant_id)
- {
- $totalProfit = [];
- // 管理员,并且选择了商户
- if (Admin::isAdministrator() && $merchant_id) {
- $depositTotal = DepositOrder::query()->where('merchant_id', $merchant_id);
- $deposiRefund = DepositRefund::query()->where('merchant_id', $merchant_id);
- } else {
- $depositTotal = DepositOrder::query()->where(AdminMerchant::getMerchantWhere());
- $deposiRefund = DepositRefund::query()->where(AdminMerchant::getMerchantWhere());
- }
- if (!empty($area_id)) {
- $depositTotal = $depositTotal->where('area_id', $area_id);
- } else {
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $depositTotal = $depositTotal->whereIn('area_id', $area_ids);
- $deposiRefund = $deposiRefund->whereIn('area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
- $area_id = $area_id ?? 0;
- $depositTotal = $depositTotal->where('area_id', $area_id);
- $deposiRefund = $deposiRefund->where('area_id', $area_id);
- }
- }
- }
- $totalProfit[0] = $depositTotal
- ->where('pay_status', DepositOrder::PAY_STATUS_OK)
- ->where('is_refund', DepositOrder::REFUND_NO)
- ->where('pay_time','>=',Carbon::today())
- ->sum('pay_money');
- $totalProfit[1] = $deposiRefund
- ->where('pay_status', DepositRefund::PAY_STATUS_OK)
- ->where('pay_time', '>=', Carbon::today())
- ->sum('pay_money');
- $totalProfit[1] = bcsub($totalProfit[0],$totalProfit[1],2);
- return $totalProfit;
- }
- // profitDetail 收益详情 - 普通订单 - 骑行花费 - 订单数量 - 里程费用
- private function getOrderTotalProfit($area_id,$merchant_id,$startTime,$endTime,$orderFilter,$money='')
- {
- // 管理员,并且选择了商户
- if (Admin::isAdministrator() && $merchant_id) {
- $orderTotalProfit = Order::query()->where('merchant_id',$merchant_id);
- }else{
- $orderTotalProfit = Order::query()->where(AdminMerchant::getMerchantWhere());
- }
- if (!empty($area_id)) {
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- } else {
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
- $area_id = $area_id ?? 0;
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- }
- }
- }
- $orderTotalProfit = $orderTotalProfit
- ->where('pay_status', Order::PAY_STATUS_OK)
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->whereBetween('pay_time', [$startTime,$endTime])
- ->filter($orderFilter);
- if($money){
- $orderTotalProfit = $orderTotalProfit->sum($money);
- }else{
- $orderTotalProfit = $orderTotalProfit->count();
- }
- return $orderTotalProfit;
- }
- // profitDetail 收益详情 - 日租订单 - 日租超时花费 - 日租租金 - 日租订单数量 - 日租调度费
- private function getOrderRentProfit($area_id,$merchant_id,$startTime,$endTime,$orderRentFilter,$money='')
- {
- // 管理员,并且选择了商户
- if (Admin::isAdministrator() && $merchant_id) {
- $orderTotalProfit = OrderRent::query()->where('merchant_id',$merchant_id);
- }else{
- $orderTotalProfit = OrderRent::query()->where(AdminMerchant::getMerchantWhere());
- }
- if (!empty($area_id)) {
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- } else {
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
- $area_id = $area_id ?? 0;
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- }
- }
- }
- $orderTotalProfit = $orderTotalProfit
- ->where('pay_status', OrderRent::PAY_STATUS_OK)
- ->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->whereBetween('pay_time', [$startTime,$endTime])
- ->filter($orderRentFilter);
- if($money){
- $orderTotalProfit = $orderTotalProfit->sum($money);
- }else{
- $orderTotalProfit = $orderTotalProfit->count();
- }
- return $orderTotalProfit;
- }
- // profitDetail 收益详情 - 已实名用户数 - 已缴纳押金用户数 - 总用户数
- private function getUserDetails($area_id,$merchant_id,$startTime,$endTime,$userFilter,$money='')
- {
- // 管理员,并且选择了商户
- if (Admin::isAdministrator() && $merchant_id) {
- $orderTotalProfit = User::query()->where('merchant_id',$merchant_id);
- }else{
- $orderTotalProfit = User::query()->where(AdminMerchant::getMerchantWhere());
- }
- if (!empty($area_id)) {
- $orderTotalProfit = $orderTotalProfit->where('register_area_id', $area_id);
- } else {
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $orderTotalProfit = $orderTotalProfit->whereIn('register_area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
- $area_id = $area_id ?? 0;
- $orderTotalProfit = $orderTotalProfit->where('register_area_id', $area_id);
- }
- }
- }
- $orderTotalProfit = $orderTotalProfit->filter($userFilter)
- ->whereBetween('created_at', [$startTime,$endTime]);
- switch($money){
- case 'is_card_certified':
- $orderTotalProfit = $orderTotalProfit
- ->where('is_card_certified', User::CARD_OK)
- ->count();
- break;
- case 'is_deposit':
- $orderTotalProfit = $orderTotalProfit
- ->where('is_deposit', User::DEPOSIT_OK)
- ->count();
- break;
- case 'total_num':
- $orderTotalProfit = $orderTotalProfit->count();
- break;
- default:
- }
- return $orderTotalProfit;
- }
- // profitDetail 收益详情 - (普通和日租) - 骑车结束,待支付总金额 - 待支付总订单数量
- private function getOrderWaitPay($area_id,$merchant_id,$startTime,$endTime,$orderFilter,$modelsType,$money='')
- {
- $models = ($modelsType == 'order') ? Order::query() : OrderRent::query();
- // 管理员,并且选择了商户
- if (Admin::isAdministrator() && $merchant_id) {
- $orderTotalProfit = $models->where('merchant_id',$merchant_id);
- }else{
- $orderTotalProfit = $models->where(AdminMerchant::getMerchantWhere());
- }
- if (!empty($area_id)) {
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- } else {
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
- $area_id = $area_id ?? 0;
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- }
- }
- }
- $orderTotalProfit = $orderTotalProfit->filter($orderFilter)->whereBetween('created_at', [$startTime,$endTime]);
- if($modelsType == 'order'){
- $orderTotalProfit = $orderTotalProfit
- ->where('status', Order::STATUS_CLOSE_BIKE);
- }else{
- $orderTotalProfit = $orderTotalProfit
- ->where('status', OrderRent::STATUS_CLOSE_RENT_BIKE);
- }
- if($money){
- $orderTotalProfit = $orderTotalProfit->sum($money);
- }else{
- $orderTotalProfit = $orderTotalProfit->count();
- }
- return $orderTotalProfit;
- }
- // profitDetail 收益详情 - 总投放车辆
- private function getBikeTotal($area_id,$merchant_id,$startTime,$endTime,$bikeFilter)
- {
- // 管理员,并且选择了商户
- if (Admin::isAdministrator() && $merchant_id) {
- $orderTotalProfit = Bike::query()->where('merchant_id',$merchant_id);
- }else{
- $orderTotalProfit = Bike::query()->where(AdminMerchant::getMerchantWhere());
- }
- if (!empty($area_id)) {
- $orderTotalProfit = $orderTotalProfit->where('put_area_id', $area_id);
- } else {
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $orderTotalProfit = $orderTotalProfit->whereIn('put_area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
- $area_id = $area_id ?? 0;
- $orderTotalProfit = $orderTotalProfit->where('put_area_id', $area_id);
- }
- }
- }
- $orderTotalProfit = $orderTotalProfit
- ->filter($bikeFilter)
- ->where('put_status', Bike::PUT_STATUS_YES)
- ->whereBetween('put_time', [$startTime,$endTime])
- ->count();
- return $orderTotalProfit;
- }
- // profitDetail 收益详情 - 骑行卡收益
- private function getRiddingCardOrderProfit($area_id,$merchant_id,$startTime,$endTime,$cardRidingOrderFilter)
- {
- // 管理员,并且选择了商户
- if (Admin::isAdministrator() && $merchant_id) {
- $orderTotalProfit = CardRidingOrder::query()->where('merchant_id',$merchant_id);
- }else{
- $orderTotalProfit = CardRidingOrder::query()->where(AdminMerchant::getMerchantWhere());
- }
- if (!empty($area_id)) {
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- } else {
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
- $area_id = $area_id ?? 0;
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- }
- }
- }
- $orderTotalProfit = $orderTotalProfit
- ->filter($cardRidingOrderFilter)
- ->where('pay_status', CardRidingOrder::PAY_STATUS_OK)
- ->whereBetween('pay_time', [$startTime,$endTime])
- ->sum('pay_money');
- return $orderTotalProfit;
- }
- // profitDetail 收益详情 - 充值订单收益
- private function getRechargeOrderProfit($area_id,$merchant_id,$startTime,$endTime,$rechargeOrderFilter)
- {
- // 管理员,并且选择了商户
- if (Admin::isAdministrator() && $merchant_id) {
- $orderTotalProfit = RechargeOrder::query()->where('merchant_id',$merchant_id);
- }else{
- $orderTotalProfit = RechargeOrder::query()->where(AdminMerchant::getMerchantWhere());
- }
- if (!empty($area_id)) {
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- } else {
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
- $area_id = $area_id ?? 0;
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- }
- }
- }
- $orderTotalProfit = $orderTotalProfit
- ->filter($rechargeOrderFilter)
- ->where('pay_status', RechargeOrder::PAY_STATUS_OK)
- ->whereBetween('pay_time', [$startTime,$endTime])
- ->sum('pay_money');
- return $orderTotalProfit;
- }
- // profitDetail 收益详情 - 免押金卡收益
- private function getDepositCardTotalProfit($area_id,$merchant_id,$startTime,$endTime,$depositCardOrderFilter)
- {
- // 管理员,并且选择了商户
- if (Admin::isAdministrator() && $merchant_id) {
- $orderTotalProfit = DepositCardOrder::query()->where('merchant_id',$merchant_id);
- }else{
- $orderTotalProfit = DepositCardOrder::query()->where(AdminMerchant::getMerchantWhere());
- }
- if (!empty($area_id)) {
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- } else {
- $admin_id = Admin::user()->id;
- if (!Admin::isAdministrator()) {
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- if (count($area_ids) !== 0) {
- $orderTotalProfit = $orderTotalProfit->whereIn('area_id', $area_ids);
- } else {
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->value('area_id');
- $area_id = $area_id ?? 0;
- $orderTotalProfit = $orderTotalProfit->where('area_id', $area_id);
- }
- }
- }
- $orderTotalProfit = $orderTotalProfit
- ->whereBetween('pay_time', [$startTime,$endTime])
- ->filter($depositCardOrderFilter)
- ->where('pay_status', DepositCardOrder::PAY_STATUS_OK)
- ->sum('pay_money');
- return $orderTotalProfit;
- }
- /**
- * 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);
- }
- }
|