123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Filters\WalletLogFilter;
- use App\Http\Resources\ProfitResource;
- use App\Models\AdminUser;
- use App\Models\AdminUserArea;
- use App\Models\Area;
- use App\Models\Statistic;
- use App\Models\WalletLog;
- use App\Utils\Admin;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\Log;
- /**
- * Class ProfitController
- * @package App\Http\Controllers\Admin
- */
- class ProfitController extends Controller
- {
- //
- /**
- * index 收益列表
- *
- * @param Request $request
- * @param WalletLogFilter $filter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- */
- public function index(Request $request, WalletLogFilter $filter)
- {
- //收益明细
- // Log::info(22);
- $admin_id = Admin::user()->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;
- }
- }
|