id; $profitOrder = WalletLog::query() ->filter($filter) ->whereIn('type', WalletLog::$subType) ->orderByDesc('id'); if (!Admin::isAdministrator()) { //判断哪些区域是此管理员创建得 $area_ids = AdminUser::getAreaIdsByAdminId($admin_id); if (count($area_ids) !== 0) { $profitOrder = $profitOrder->whereIn('area_id', $area_ids); } else { $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id'); $area_id = $area_id->area_id ?? 0; $profitOrder = $profitOrder->where('area_id', $area_id); } } $profitOrder = $request->get('all') ? $profitOrder->get() : $profitOrder->paginate(); return $this->ok(ProfitResource::collection($profitOrder)); } /** * profitAnalysis 收益分析 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function profitAnalysis(Request $request) { $area_id = $request->get('area_id') ?? ""; // Log::info($area_id); if (empty($area_id)) return $this->error(['缺少参数']); $today = Carbon::today(); $first_day = date('Y-m-01', strtotime($today)); //每月一号 $deposit_statistic_start = date('Y-m-d', strtotime("-13 day")); //当日新增 $data['new_add_profit'] = WalletLog::query() ->where('created_at', '>', $today) ->whereIn('type', WalletLog::$subType); //当月总收益 $data['this_month_total_profit'] = WalletLog::query() ->where('created_at', '>', $first_day) ->whereIn('type', WalletLog::$subType); if ($area_id == config('statistic.all')) { // 总和 // 判断是否为超级管理员 $admin_id = Admin::user()->id; if (!Admin::isAdministrator()) { $area_ids = AdminUser::getAreaIdsByAdminId($admin_id); $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); $area_ids = array_merge($area_ids,$area_id); $data['new_add_profit'] = $data['new_add_profit']->whereIn('area_id', $area_ids); $data['this_month_total_profit'] = $data['this_month_total_profit']->whereIn('area_id', $area_ids); } $data['new_add_profit'] = $data['new_add_profit']->sum('money'); $data['this_month_total_profit'] = $data['this_month_total_profit']->sum('money'); $data['new_add_profit'] = abs($data['new_add_profit']); $data['this_month_total_profit'] = abs($data['this_month_total_profit']); } else { // 区域 $data['new_add_profit'] = $data['new_add_profit']->where('area_id', $area_id)->sum('money'); $data['this_month_total_profit'] = $data['this_month_total_profit']->where('area_id', $area_id)->sum('money'); $data['new_add_profit'] = abs($data['new_add_profit']); $data['this_month_total_profit'] = abs($data['this_month_total_profit']); } // 统计图 $data['profit_statistics'] = $this->profitStatistics($area_id); // Log::info($data['profit_statistics']); // return $this->ok($data); } /** * profitTotal 总收益 * * @param Request $request * @return \Illuminate\Http\JsonResponse * @author Fx * */ public function profitTotal(Request $request) { $area_id = $request->get('area_id') ?? ""; // Log::info($area_id); if (empty($area_id)) return $this->error(['缺少参数']); $totalProfit = WalletLog::query() ->whereIn('type', WalletLog::$subType); if ($area_id == config('statistic.all')) { // 总和 // 判断是否为超级管理员 $admin_id = Admin::user()->id; if (!Admin::isAdministrator()) { $area_ids = AdminUser::getAreaIdsByAdminId($admin_id); $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); $area_ids = array_merge($area_ids,$area_id); $totalProfit = $totalProfit->whereIn('area_id', $area_ids); } $totalProfit = $totalProfit ->sum('money'); } else { // 区域 $totalProfit = $totalProfit ->where('area_id', $area_id) ->sum('money'); } return $this->ok(abs($totalProfit)); } /** * profitStatistics 日收益统计信息 * * @param $area_id * @return array * @author Fx * */ private function profitStatistics($area_id) { $profit_statistic_date_start = date('Y-m-d', strtotime("-14 day")); if ($area_id == config('statistic.all')) { // 判断是否为超级管理员 $admin_id = Admin::user()->id; if (!Admin::isAdministrator()) { $area_ids = AdminUser::getAreaIdsByAdminId($admin_id); $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); $area_ids = array_merge($area_ids,$area_id); $profit_statistics = Statistic::query() ->where('slug', Statistic::SLUG_PROFIT_STATIC) ->where('date', '>', $profit_statistic_date_start) // ->where('area_id',$area_id) ->whereIn('area_id', $area_ids) ->select(['date', 'body']) ->orderByDesc('date') ->get() ->toArray(); // Log::info($profit_statistics); $arr1 = []; foreach ($profit_statistics as $v) { // 非超级管理员 需要重新组装叠加总收益统计 $profit_statistics_arr = object_array(json_decode($v['body'])); if (array_key_exists($v['date'], $arr1)) { $arr1[$v['date']] += $profit_statistics_arr['profit'] ?? 0; } else { $arr1[$v['date']] = $profit_statistics_arr['profit'] ?? 0; } } $res = []; foreach ($arr1 as $k => $v) { $arr2 = []; $arr2['profit'] = $v; $arr2['date'] = $k; $res[] = $arr2; } return $res; } } // 其他均走下面 按照区域id查询 $profit_statistics = Statistic::query() ->where('slug', Statistic::SLUG_PROFIT_STATIC) ->where('date', '>', $profit_statistic_date_start) ->where('area_id', $area_id) ->select(['date', 'body']) ->orderByDesc('date') ->get() ->toArray(); $res = []; // dd($profit_statistics); foreach ($profit_statistics as $v) { $profit_statistics_arr = object_array(json_decode($v['body'])); $profit_arr_statistics = []; $profit_arr_statistics['profit'] = $profit_statistics_arr['profit'] ?? 0; $profit_arr_statistics['date'] = $v['date']; $res[] = $profit_arr_statistics; } return $res; } }