123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Filters\DepositFilter;
- use App\Http\Resources\DepositOrderResource;
- use App\Models\AdminUser;
- use App\Models\AdminUserArea;
- use App\Models\Area;
- use App\Models\DepositOrder;
- use App\Models\DepositRefund;
- use App\Models\Statistic;
- use App\Utils\Admin;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\Log;
- /**
- * Class DepositController
- * @package App\Http\Controllers\Admin
- */
- class DepositController extends Controller
- {
- /**
- * index
- *
- * @param Request $request
- * @param DepositFilter $filter
- * @return \Illuminate\Http\JsonResponse *@author Fx
- *
- */
- public function index(Request $request, DepositFilter $filter)
- {
- //
- $admin_id = Admin::user()->id;
- $depositOrder = DepositOrder::query()
- ->filter($filter)
- ->where('pay_status', DepositOrder::PAY_STATUS_OK)
- ->orderByDesc('id');
- if (!Admin::isAdministrator()) {
- //判断哪些区域是此管理员创建得
- $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
- $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray();
- if (count($area_ids) !== 0) {
- $area_idss = array_merge($area_ids,$area_id);
- $depositOrder = $depositOrder->whereIn('area_id', $area_idss);
- } else {
- //$area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
- $depositOrder = $depositOrder->whereIn('area_id', $area_id);
- }
- }
- $depositOrder = $request->get('all') ? $depositOrder->get() : $depositOrder->paginate();
- return $this->ok(DepositOrderResource::collection($depositOrder));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- //
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- //
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- //
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- //
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- //
- }
- /**
- * depositAnalysis 押金分析
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse *@author Fx
- *
- */
- public function depositAnalysis(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'] = DepositOrder::query()
- ->where('pay_time', '>', $today)
- ->where('pay_status', DepositOrder::PAY_STATUS_OK);
- //本日退还
- $data['new_refund'] = DepositRefund::query()
- ->where('pay_time', '>', $today)
- ->where('pay_status', DepositRefund::PAY_STATUS_OK);
- //本月累计押金
- $data['this_money_total'] = DepositOrder::query()
- ->where('pay_time', '>', $first_day)
- ->where('pay_status', DepositRefund::PAY_STATUS_OK)
- ->where('is_refund', DepositOrder::REFUND_NO);
- //总计押金(数据量大导致整个接口请求变慢 单独做一个接口)
- /*$data['deposit_total'] = DepositOrder::query()
- ->where('pay_status',DepositRefund::PAY_STATUS_OK)
- ->where('is_refund',DepositOrder::REFUND_NO);*/
- 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'] = $data['new_add']
- ->whereIn('area_id', $area_ids);
- //本日退还
- $data['new_refund'] = $data['new_refund']
- ->whereIn('area_id', $area_ids);
- //本月累计押金
- $data['this_money_total'] = $data['this_money_total']
- ->whereIn('area_id', $area_ids);
- }
- //本日新增
- $data['new_add'] = $data['new_add']
- ->sum('money');
- //本日退还
- $data['new_refund'] = $data['new_refund']
- ->sum('money');
- //本月累计押金
- $data['this_money_total'] = $data['this_money_total']
- ->sum('money');
- //总计押金
- /*$data['deposit_total'] = $data['deposit_total']
- ->sum('money');*/
- } else {
- //本日新增
- $data['new_add'] = $data['new_add']
- ->where('area_id', $area_id)
- ->sum('money');
- //本日退还
- $data['new_refund'] = $data['new_refund']
- ->where('area_id', $area_id)
- ->sum('money');
- //本月累计押金
- $data['this_money_total'] = $data['this_money_total']
- ->where('area_id', $area_id)
- ->sum('money');
- //总计押金
- /*$data['deposit_total'] = $data['deposit_total']
- ->where('area_id',$area_id)
- ->sum('money');*/
- }
- //押金增长统计图数据
- $data['deposit_statistics'] = $this->depositStatistics($area_id);
- return $this->ok($data);
- }
- /**
- * depositTotal 总计押金
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse *@author Fx
- *
- */
- public function depositTotal(Request $request)
- {
- $area_id = $request->get('area_id') ?? "";
- // Log::info($area_id);
- if (empty($area_id)) return $this->error(['缺少参数']);
- $depositTotal = DepositOrder::query()
- ->where('pay_status', DepositOrder::PAY_STATUS_OK)
- ->where('is_refund', DepositOrder::REFUND_NO);
- 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);
- $depositTotal = $depositTotal
- ->whereIn('area_id', $area_ids);
- }
- $depositTotal = $depositTotal
- ->sum('money');
- } else {
- // 区域
- $depositTotal = $depositTotal
- ->where('area_id', $area_id)
- ->sum('money');
- }
- return $this->ok($depositTotal);
- }
- //
- /**
- * depositStatistics 取出统计数据
- *
- * @param $area_id
- * @return array *@author Fx
- *
- */
- private function depositStatistics($area_id)
- {
- $deposit_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);
- $deposit_statistics = Statistic::query()
- ->where('slug', Statistic::SLUG_DEPOSIT_STATIC)
- ->where('date', '>', $deposit_statistic_date_start)
- ->whereIn('area_id', $area_ids)
- ->select(['date', 'body'])
- ->orderByDesc('date')
- ->get()
- ->toArray();
- $arr1 = [];
- foreach ($deposit_statistics as $v) {
- // 非超级管理员 需要重新组装叠加总收益统计
- $profit_statistics_arr = object_array(json_decode($v['body']));
- if (array_key_exists($v['date'], $arr1)) {
- $arr1[$v['date']]['money_all'] += $profit_statistics_arr['money_all'] ?? 0;
- $arr1[$v['date']]['count_id'] += $profit_statistics_arr['count_id'] ?? 0;
- $arr1[$v['date']]['money_refund_all'] += $profit_statistics_arr['money_refund_all'] ?? 0;
- } else {
- $arr1[$v['date']]['money_all'] = $profit_statistics_arr['money_all'] ?? 0;
- $arr1[$v['date']]['count_id'] = $profit_statistics_arr['count_id'] ?? 0;
- $arr1[$v['date']]['money_refund_all'] = $profit_statistics_arr['money_refund_all'] ?? 0;
- }
- }
- $res = [];
- foreach ($arr1 as $k => $v) {
- $arr2 = [];
- $arr2 = $v;
- $arr2['date'] = $k;
- $res[] = $arr2;
- }
- // Log::info($res);
- return $res;
- }
- }
- // 其他情况 均直接按照区域id查询
- $deposit_statistics = Statistic::query()
- ->where('slug', Statistic::SLUG_DEPOSIT_STATIC)
- ->where('date', '>', $deposit_statistic_date_start)
- ->where('area_id', $area_id)
- ->select(['date', 'body'])
- ->orderByDesc('date')
- ->get()
- ->toArray();
- $res = [];
- foreach ($deposit_statistics as $v) {
- $deposit_statistics_arr = object_array(json_decode($v['body']));
- // Log::info($deposit_statistics_arr);
- $deposit_statistics_arr = $deposit_statistics_arr ?? [];
- $deposit_statistics_arr['date'] = $v['date'];
- $res[] = $deposit_statistics_arr;
- }
- return $res;
- }
- }
|