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); } }