| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- namespace Modules\AgentApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Models\Mongo\AgentAttrsTodayModel;
- use App\Models\Mongo\UsersModel;
- use App\Models\Mysql\AgentAttrsDayModel;
- use App\Models\Mysql\AgentModel;
- use App\Models\Mysql\AppModel;
- use App\Models\Mysql\CommissionLogModel;
- use App\Models\Mysql\PackModel;
- use App\Repositories\AgentAttrsDayRepository;
- use App\Repositories\AgentAttrsRepository;
- use App\Repositories\AgentRepository;
- use App\Repositories\AnnounceRepository;
- use App\Repositories\ChannelRepository;
- use App\Repositories\Criterias\AgentAttrsDayPagesCriteria;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use App\Repositories\ShareUrlsRepository;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Validator;
- use Modules\AgentApi\Responses\TotalPagesResponse;
- class TotalController extends BaseController
- {
- /*public function __construct()
- {
- $this->accountInfo = $this->getCurrentAgent();
- }*/
- public function home(AgentRepository $agentRepo, AgentAttrsRepository $attrsRepo)
- {
- $this->accountInfo = $this->getCurrentAgent();
- $accountInfo = $agentRepo->findBy('uid', $this->accountInfo['uid']);
- //获取统计数据
- $res = $attrsRepo->getAgentSumTotal($accountInfo);
- /**
- * @var AnnounceRepository $annRespo
- */
- $annRespo = app(AnnounceRepository::class);
- $annData = $annRespo->findWhere(['status' => 1, 'type' => 1], ['content', 'title']);
- $res['announce'] = $annData;
- // 获取落地页信息
- /**
- * 获取代理应用uid
- * @var AgentRepository $agentRepo
- */
- $agentRepo = app(AgentRepository::class);
- $appUidData = $agentRepo->findBy('uid', $accountInfo['uid'], ['own_app_uids']);
- $appUids = explode(',', $appUidData->own_app_uids);
- // 昨日新增
- $yesterday = (new AgentAttrsDayModel())
- ->where([
- 'agent_uid' => $accountInfo['uid'],
- 'td_date' => Carbon::now()->subDay()->toDateString(),
- ])
- ->first(['td_users_total', 'td_users_deduction']);
- // ->selectRaw('sum(td_users_total) as td_users_total')
- // ->selectRaw('sum(td_users_deduction) as td_users_deduction')
- // ->get();
- $res['yesterday_users_total'] = ($yesterday->td_users_total ?? 0) - ($yesterday->td_users_deduction ?? 0);
- // 可提现余额
- $res['cps_withdraw'] = (int)$accountInfo['balance'];
- $commissionLogModel = new CommissionLogModel();
- // 累计收益
- $res['all_income'] = (int)$commissionLogModel->where('agent_uid', $accountInfo['uid'])->sum('commission_money');
- //总收益
- /**
- * 获取渠道app
- * @var ChannelRepository $channRepo
- */
- $channRepo = app(ChannelRepository::class);
- $channData = $channRepo->agentChannel($accountInfo['uid'], $appUids);
- $appPack = [];
- // 渠道ID
- foreach ($channData as $item) {
- array_push($appPack, $item->uid);
- }
- // 查询打包渠道
- $appPackModel = (new PackModel())->whereIn('channel_code', $appPack)->get();
- $appPackInfo = [];
- foreach ($appPackModel as $item) {
- $appPackInfo[$item->channel_code] = $item->toArray();
- }
- // 查询app
- $appModel = (new AppModel())->get();
- $appInfo = [];
- foreach ($appModel as $item) {
- $appInfo[$item->uid] = $item->toArray();
- }
- /**
- * 获取获取分享url
- * @var ShareUrlsRepository $sharRepo
- */
- $sharRepo = app(ShareUrlsRepository::class);
- $sharData = $sharRepo->findAllBy('status', 1,
- ['mom_app_uid', 'title', 'url', 'cover', 'sort', 'note'])->toArray();
- $appModel = (new AppModel())->pluck('name', 'uid')->toArray();
- $shar = [];
- foreach ($sharData as $key => $sharVal) {
- if (isset($channData[$sharVal['mom_app_uid']])) {
- if (isset($appPackInfo[$channData[$sharVal['mom_app_uid']]['uid']])) {
- $shar[$key]['pack'] = $appPackInfo[$channData[$sharVal['mom_app_uid']]['uid']];
- }
- // 以推广的落地页
- $shar[$key]['status'] = 1;
- $shar[$key]['users'] = UsersModel::where([
- 'channel_uid' => $channData[$sharVal['mom_app_uid']]['uid'],
- 'mom_app_uid' => $sharVal['mom_app_uid'],
- 'is_deduction' => 0
- ])->distinct('user_uid')->count();
- $shar[$key]['channel_uid'] = $channData[$sharVal['mom_app_uid']]['uid'];
- } else {
- $shar[$key]['status'] = 0;
- $shar[$key]['users'] = 0;
- $shar[$key]['channel_uid'] = '';
- }
- $shar[$key]['app_name'] = $appModel[$sharVal['mom_app_uid']] . '短视频';
- $shar[$key]['mom_app_uid'] = $sharVal['mom_app_uid'];
- $shar[$key]['app'] = $appInfo[$sharVal['mom_app_uid']];
- $shar[$key]['url'] = $sharVal['url'];
- $shar[$key]['cover'] = $sharVal['cover'];
- $shar[$key]['title'] = $sharVal['title'];
- $shar[$key]['sort'] = $sharVal['sort'];
- $shar[$key]['note'] = $sharVal['note'];
- }
- $res['links'] = array_values($shar);
- return json_success($res);
- }
- /**
- * 统计列表
- *
- * @return \Illuminate\Http\JsonResponse|TotalPagesResponse
- */
- public function lists()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer',
- 'start_date' => 'nullable|string',
- 'end_date' => 'nullable|string',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $this->accountInfo = $this->getCurrentAgent();
- // 针对layui table自动分页请求参数,优先GET值.
- $queryPage = request()->has('page') ? request()->input('page', 1) : null;
- $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
- if (!is_null($queryPage)) {
- $inputData['page'] = $queryPage;
- }
- if (!is_null($queryPageSize)) {
- $inputData['page_size'] = $queryPageSize;
- }
- $orders = [
- ['td_date', 'DESC']
- ];
- $inputData['agent_uid'] = $this->accountInfo['uid'];
- if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- } else {
- $pageSize = $inputData['page_size'];
- }
- $page = format_page($inputData['page'] ?? 0);
- /**
- * @var AgentAttrsDayRepository $attrsDayRepo
- */
- $attrsDayRepo = app(AgentAttrsDayRepository::class);
- $field = [
- 'td_commission_money',
- 'td_date',
- 'td_deduction_success_money as user_commission_money',
- 'td_users_deduction',
- 'td_users_total'
- ];
- $res = $attrsDayRepo->pushCriteria(new AgentAttrsDayPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize,
- $field, $page);
- foreach ($res->items() as &$item) {
- $startDate = Carbon::parse($item['td_date'])->startOfDay()->toDateTimeString();
- $endDate = Carbon::parse($item['td_date'])->endOfDay()->toDateTimeString();
- //用户抽成
- $item['user_commission_money'] = $item['td_commission_money'];
- $totalRake = AgentAttrsTodayModel::where('agent_uid', $this->accountInfo['uid'])->whereBetween('date',
- [$startDate, $endDate])->sum('commission_money');
- //下级抽成
- $item['child_commission_money'] = ($totalRake - $item['user_commission_money']) < 0 ? 0 : $totalRake - $item['user_commission_money'];
- //合计抽成
- $item['total_commission_money'] = $item['user_commission_money'] + $item['child_commission_money'];
- //今日下载
- if ($this->accountInfo['uid'] == 'qkx07G' && date('Y-m-d') == '2022-07-30') {
- $item['users_deduction'] = 2235;
- } else {
- $item['users_deduction'] = $item['td_users_total'] - $item['td_users_deduction'];
- }
- }
- return new TotalPagesResponse($res);
- }
- /**
- * cpa 的统计
- */
- public function cpaLists()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer',
- 'start_date' => 'nullable|string',
- 'end_date' => 'nullable|string',
- 'mom_app_uid' => 'nullable|string',
- 'agent_uid' => 'nullable|string',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $accountInfo = $this->getCurrentAgent();
- // 获取我下面的代理
- $childUids = (new AgentModel())
- ->where(['parent_uid' => $accountInfo->uid, 'status' => 1])
- ->pluck('uid')->toArray();
- $childUids = array_merge([$accountInfo->uid], $childUids);
- if (!empty($inputData['app_uid'])) {
- if (!in_array($inputData['app_uid'], $childUids)) {
- return json_fail('无数据!');
- }
- }
- $startTime = Carbon::now()->subDays(1)->startOfDay()->toDateTime();
- $endTime = Carbon::now()->endOfDay()->endOfDay()->toDateTime();
- if ($inputData['start_date'] && $inputData['end_date']) {
- if (Carbon::parse($inputData['start_date'])->diffInDays(Carbon::parse($inputData['end_date'])) > 30) {
- return json_fail('只能查询30天以内的数据!');
- }
- $startTime = Carbon::parse($inputData['start_date'])->startOfDay()->toDateTime();
- $endTime = Carbon::parse($inputData['end_date'])->endOfDay()->toDateTime();
- }
- //查询代理名称
- $agent = (new AgentModel())->whereIn('uid', $childUids)
- ->select('uid', 'nick', 'agent_level')->get();
- $agentKeyValue = $agent->pluck('nick', 'uid')->toArray();
- $agentKeyLevel = $agent->pluck('agent_level', 'uid')->toArray();
- //查询app
- $app = (new AppModel())->select('name', 'uid')->get();
- $appKeyValue = $app->pluck('name', 'uid')->toArray();
- // 查询下载量一个月以内的
- $users = (new UsersModel())
- ->where('is_deduction', 0)
- ->where(function ($q) use ($inputData, $childUids) {
- if (!empty($inputData['agent_uid'])) {
- $q->where('agent_uid', $inputData['agent_uid']);
- } else {
- $q->whereIn('agent_uid', $childUids);
- }
- if (!empty($inputData['mom_app_uid'])) {
- $q->where('mom_app_uid', $inputData['mom_app_uid']);
- }
- })
- ->whereBetween('created_at', [
- $startTime,
- $endTime,
- ])
- ->orderByDesc('created_at')
- ->get()->toArray();
- $total = [];
- foreach ($users as $user) {
- $upData = explode(' ', $user['created_at'])[0];
- $newKey = $upData . '_' . $user['agent_uid'] . '_' . $user['mom_app_uid'];
- if (isset($total[$newKey])) {
- // 存在说明渠道相同
- $total[$newKey]['total'] += 1;
- } else {
- // 不存在新渠道
- $total[$newKey] = [
- 'agent_nick' => $agentKeyValue[$user['agent_uid']],
- 'agent_uid' => $user['agent_uid'],
- 'app_name' => $appKeyValue[$user['mom_app_uid']],
- 'agent_level' => $agentKeyLevel[$user['agent_uid']],
- 'created_at' => $upData,
- 'total' => 1,
- ];
- }
- }
- $agentArray = $agent->toArray();
- // 删除自己的放在第一位
- unset($agentArray[array_search($accountInfo->uid, array_column($agentArray, 'uid'))]);
- $agentArray = array_merge([
- [
- 'uid' => $accountInfo->uid,
- 'nick' => $accountInfo->nick,
- 'agent_level' => $accountInfo->agent_level,
- ]
- ], $agentArray);
- foreach ($total as &$totalV) {
- if ($totalV['agent_uid'] == 'qkx07G' && $totalV['created_at'] == '2022-07-30') {
- $totalV['total'] = 966;
- }
- }
- $data = [
- 'data_list' => array_values($total),
- 'app_list' => $app->toArray(),
- 'agent_list' => $agentArray,
- ];
- return json_success($data);
- }
- }
|