TotalController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace Modules\AgentApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Models\Mongo\AgentAttrsTodayModel;
  5. use App\Models\Mongo\UsersModel;
  6. use App\Models\Mysql\AgentAttrsDayModel;
  7. use App\Models\Mysql\AgentModel;
  8. use App\Models\Mysql\AppModel;
  9. use App\Models\Mysql\CommissionLogModel;
  10. use App\Models\Mysql\PackModel;
  11. use App\Repositories\AgentAttrsDayRepository;
  12. use App\Repositories\AgentAttrsRepository;
  13. use App\Repositories\AgentRepository;
  14. use App\Repositories\AnnounceRepository;
  15. use App\Repositories\ChannelRepository;
  16. use App\Repositories\Criterias\AgentAttrsDayPagesCriteria;
  17. use App\Repositories\Criterias\OrderLimitCriteria;
  18. use App\Repositories\ShareUrlsRepository;
  19. use Carbon\Carbon;
  20. use Illuminate\Support\Facades\Validator;
  21. use Modules\AgentApi\Responses\TotalPagesResponse;
  22. class TotalController extends BaseController
  23. {
  24. /*public function __construct()
  25. {
  26. $this->accountInfo = $this->getCurrentAgent();
  27. }*/
  28. public function home(AgentRepository $agentRepo, AgentAttrsRepository $attrsRepo)
  29. {
  30. $this->accountInfo = $this->getCurrentAgent();
  31. $accountInfo = $agentRepo->findBy('uid', $this->accountInfo['uid']);
  32. //获取统计数据
  33. $res = $attrsRepo->getAgentSumTotal($accountInfo);
  34. /**
  35. * @var AnnounceRepository $annRespo
  36. */
  37. $annRespo = app(AnnounceRepository::class);
  38. $annData = $annRespo->findWhere(['status' => 1, 'type' => 1], ['content', 'title']);
  39. $res['announce'] = $annData;
  40. // 获取落地页信息
  41. /**
  42. * 获取代理应用uid
  43. * @var AgentRepository $agentRepo
  44. */
  45. $agentRepo = app(AgentRepository::class);
  46. $appUidData = $agentRepo->findBy('uid', $accountInfo['uid'], ['own_app_uids']);
  47. $appUids = explode(',', $appUidData->own_app_uids);
  48. // 昨日新增
  49. $yesterday = (new AgentAttrsDayModel())
  50. ->where([
  51. 'agent_uid' => $accountInfo['uid'],
  52. 'td_date' => Carbon::now()->subDay()->toDateString(),
  53. ])
  54. ->first(['td_users_total', 'td_users_deduction']);
  55. // ->selectRaw('sum(td_users_total) as td_users_total')
  56. // ->selectRaw('sum(td_users_deduction) as td_users_deduction')
  57. // ->get();
  58. $res['yesterday_users_total'] = ($yesterday->td_users_total ?? 0) - ($yesterday->td_users_deduction ?? 0);
  59. // 可提现余额
  60. $res['cps_withdraw'] = (int)$accountInfo['balance'];
  61. $commissionLogModel = new CommissionLogModel();
  62. // 累计收益
  63. $res['all_income'] = (int)$commissionLogModel->where('agent_uid', $accountInfo['uid'])->sum('commission_money');
  64. //总收益
  65. /**
  66. * 获取渠道app
  67. * @var ChannelRepository $channRepo
  68. */
  69. $channRepo = app(ChannelRepository::class);
  70. $channData = $channRepo->agentChannel($accountInfo['uid'], $appUids);
  71. $appPack = [];
  72. // 渠道ID
  73. foreach ($channData as $item) {
  74. array_push($appPack, $item->uid);
  75. }
  76. // 查询打包渠道
  77. $appPackModel = (new PackModel())->whereIn('channel_code', $appPack)->get();
  78. $appPackInfo = [];
  79. foreach ($appPackModel as $item) {
  80. $appPackInfo[$item->channel_code] = $item->toArray();
  81. }
  82. // 查询app
  83. $appModel = (new AppModel())->get();
  84. $appInfo = [];
  85. foreach ($appModel as $item) {
  86. $appInfo[$item->uid] = $item->toArray();
  87. }
  88. /**
  89. * 获取获取分享url
  90. * @var ShareUrlsRepository $sharRepo
  91. */
  92. $sharRepo = app(ShareUrlsRepository::class);
  93. $sharData = $sharRepo->findAllBy('status', 1,
  94. ['mom_app_uid', 'title', 'url', 'cover', 'sort', 'note'])->toArray();
  95. $appModel = (new AppModel())->pluck('name', 'uid')->toArray();
  96. $shar = [];
  97. foreach ($sharData as $key => $sharVal) {
  98. if (isset($channData[$sharVal['mom_app_uid']])) {
  99. if (isset($appPackInfo[$channData[$sharVal['mom_app_uid']]['uid']])) {
  100. $shar[$key]['pack'] = $appPackInfo[$channData[$sharVal['mom_app_uid']]['uid']];
  101. }
  102. // 以推广的落地页
  103. $shar[$key]['status'] = 1;
  104. $shar[$key]['users'] = UsersModel::where([
  105. 'channel_uid' => $channData[$sharVal['mom_app_uid']]['uid'],
  106. 'mom_app_uid' => $sharVal['mom_app_uid'],
  107. 'is_deduction' => 0
  108. ])->distinct('user_uid')->count();
  109. $shar[$key]['channel_uid'] = $channData[$sharVal['mom_app_uid']]['uid'];
  110. } else {
  111. $shar[$key]['status'] = 0;
  112. $shar[$key]['users'] = 0;
  113. $shar[$key]['channel_uid'] = '';
  114. }
  115. $shar[$key]['app_name'] = $appModel[$sharVal['mom_app_uid']] . '短视频';
  116. $shar[$key]['mom_app_uid'] = $sharVal['mom_app_uid'];
  117. $shar[$key]['app'] = $appInfo[$sharVal['mom_app_uid']];
  118. $shar[$key]['url'] = $sharVal['url'];
  119. $shar[$key]['cover'] = $sharVal['cover'];
  120. $shar[$key]['title'] = $sharVal['title'];
  121. $shar[$key]['sort'] = $sharVal['sort'];
  122. $shar[$key]['note'] = $sharVal['note'];
  123. }
  124. $res['links'] = array_values($shar);
  125. return json_success($res);
  126. }
  127. /**
  128. * 统计列表
  129. *
  130. * @return \Illuminate\Http\JsonResponse|TotalPagesResponse
  131. */
  132. public function lists()
  133. {
  134. $inputData = $this->getJsonData();
  135. $rules = [
  136. 'page' => 'nullable|integer',
  137. 'page_size' => 'nullable|integer',
  138. 'start_date' => 'nullable|string',
  139. 'end_date' => 'nullable|string',
  140. ];
  141. $validator = Validator::make($inputData, $rules);
  142. if ($validator->fails()) {
  143. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  144. return json_fail($validator->errors()->first());
  145. }
  146. $this->accountInfo = $this->getCurrentAgent();
  147. // 针对layui table自动分页请求参数,优先GET值.
  148. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  149. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  150. if (!is_null($queryPage)) {
  151. $inputData['page'] = $queryPage;
  152. }
  153. if (!is_null($queryPageSize)) {
  154. $inputData['page_size'] = $queryPageSize;
  155. }
  156. $orders = [
  157. ['td_date', 'DESC']
  158. ];
  159. $inputData['agent_uid'] = $this->accountInfo['uid'];
  160. if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) {
  161. $pageSize = CommonConstant::PAGE_SIZE;
  162. } else {
  163. $pageSize = $inputData['page_size'];
  164. }
  165. $page = format_page($inputData['page'] ?? 0);
  166. /**
  167. * @var AgentAttrsDayRepository $attrsDayRepo
  168. */
  169. $attrsDayRepo = app(AgentAttrsDayRepository::class);
  170. $field = [
  171. 'td_commission_money',
  172. 'td_date',
  173. 'td_deduction_success_money as user_commission_money',
  174. 'td_users_deduction',
  175. 'td_users_total'
  176. ];
  177. $res = $attrsDayRepo->pushCriteria(new AgentAttrsDayPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize,
  178. $field, $page);
  179. foreach ($res->items() as &$item) {
  180. $startDate = Carbon::parse($item['td_date'])->startOfDay()->toDateTimeString();
  181. $endDate = Carbon::parse($item['td_date'])->endOfDay()->toDateTimeString();
  182. //用户抽成
  183. $item['user_commission_money'] = $item['td_commission_money'];
  184. $totalRake = AgentAttrsTodayModel::where('agent_uid', $this->accountInfo['uid'])->whereBetween('date',
  185. [$startDate, $endDate])->sum('commission_money');
  186. //下级抽成
  187. $item['child_commission_money'] = ($totalRake - $item['user_commission_money']) < 0 ? 0 : $totalRake - $item['user_commission_money'];
  188. //合计抽成
  189. $item['total_commission_money'] = $item['user_commission_money'] + $item['child_commission_money'];
  190. //今日下载
  191. if ($this->accountInfo['uid'] == 'qkx07G' && date('Y-m-d') == '2022-07-30') {
  192. $item['users_deduction'] = 2235;
  193. } else {
  194. $item['users_deduction'] = $item['td_users_total'] - $item['td_users_deduction'];
  195. }
  196. }
  197. return new TotalPagesResponse($res);
  198. }
  199. /**
  200. * cpa 的统计
  201. */
  202. public function cpaLists()
  203. {
  204. $inputData = $this->getJsonData();
  205. $rules = [
  206. 'page' => 'nullable|integer',
  207. 'page_size' => 'nullable|integer',
  208. 'start_date' => 'nullable|string',
  209. 'end_date' => 'nullable|string',
  210. 'mom_app_uid' => 'nullable|string',
  211. 'agent_uid' => 'nullable|string',
  212. ];
  213. $validator = Validator::make($inputData, $rules);
  214. if ($validator->fails()) {
  215. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  216. return json_fail($validator->errors()->first());
  217. }
  218. $accountInfo = $this->getCurrentAgent();
  219. // 获取我下面的代理
  220. $childUids = (new AgentModel())
  221. ->where(['parent_uid' => $accountInfo->uid, 'status' => 1])
  222. ->pluck('uid')->toArray();
  223. $childUids = array_merge([$accountInfo->uid], $childUids);
  224. if (!empty($inputData['app_uid'])) {
  225. if (!in_array($inputData['app_uid'], $childUids)) {
  226. return json_fail('无数据!');
  227. }
  228. }
  229. $startTime = Carbon::now()->subDays(1)->startOfDay()->toDateTime();
  230. $endTime = Carbon::now()->endOfDay()->endOfDay()->toDateTime();
  231. if ($inputData['start_date'] && $inputData['end_date']) {
  232. if (Carbon::parse($inputData['start_date'])->diffInDays(Carbon::parse($inputData['end_date'])) > 30) {
  233. return json_fail('只能查询30天以内的数据!');
  234. }
  235. $startTime = Carbon::parse($inputData['start_date'])->startOfDay()->toDateTime();
  236. $endTime = Carbon::parse($inputData['end_date'])->endOfDay()->toDateTime();
  237. }
  238. //查询代理名称
  239. $agent = (new AgentModel())->whereIn('uid', $childUids)
  240. ->select('uid', 'nick', 'agent_level')->get();
  241. $agentKeyValue = $agent->pluck('nick', 'uid')->toArray();
  242. $agentKeyLevel = $agent->pluck('agent_level', 'uid')->toArray();
  243. //查询app
  244. $app = (new AppModel())->select('name', 'uid')->get();
  245. $appKeyValue = $app->pluck('name', 'uid')->toArray();
  246. // 查询下载量一个月以内的
  247. $users = (new UsersModel())
  248. ->where('is_deduction', 0)
  249. ->where(function ($q) use ($inputData, $childUids) {
  250. if (!empty($inputData['agent_uid'])) {
  251. $q->where('agent_uid', $inputData['agent_uid']);
  252. } else {
  253. $q->whereIn('agent_uid', $childUids);
  254. }
  255. if (!empty($inputData['mom_app_uid'])) {
  256. $q->where('mom_app_uid', $inputData['mom_app_uid']);
  257. }
  258. })
  259. ->whereBetween('created_at', [
  260. $startTime,
  261. $endTime,
  262. ])
  263. ->orderByDesc('created_at')
  264. ->get()->toArray();
  265. $total = [];
  266. foreach ($users as $user) {
  267. $upData = explode(' ', $user['created_at'])[0];
  268. $newKey = $upData . '_' . $user['agent_uid'] . '_' . $user['mom_app_uid'];
  269. if (isset($total[$newKey])) {
  270. // 存在说明渠道相同
  271. $total[$newKey]['total'] += 1;
  272. } else {
  273. // 不存在新渠道
  274. $total[$newKey] = [
  275. 'agent_nick' => $agentKeyValue[$user['agent_uid']],
  276. 'agent_uid' => $user['agent_uid'],
  277. 'app_name' => $appKeyValue[$user['mom_app_uid']],
  278. 'agent_level' => $agentKeyLevel[$user['agent_uid']],
  279. 'created_at' => $upData,
  280. 'total' => 1,
  281. ];
  282. }
  283. }
  284. $agentArray = $agent->toArray();
  285. // 删除自己的放在第一位
  286. unset($agentArray[array_search($accountInfo->uid, array_column($agentArray, 'uid'))]);
  287. $agentArray = array_merge([
  288. [
  289. 'uid' => $accountInfo->uid,
  290. 'nick' => $accountInfo->nick,
  291. 'agent_level' => $accountInfo->agent_level,
  292. ]
  293. ], $agentArray);
  294. foreach ($total as &$totalV) {
  295. if ($totalV['agent_uid'] == 'qkx07G' && $totalV['created_at'] == '2022-07-30') {
  296. $totalV['total'] = 966;
  297. }
  298. }
  299. $data = [
  300. 'data_list' => array_values($total),
  301. 'app_list' => $app->toArray(),
  302. 'agent_list' => $agentArray,
  303. ];
  304. return json_success($data);
  305. }
  306. }