StatisticalService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Services;
  3. use App\Models\CommissionLog;
  4. use App\Models\Order;
  5. use App\Models\Stat;
  6. use App\Models\StatServer;
  7. use App\Models\StatUser;
  8. use App\Models\User;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\DB;
  11. class StatisticalService {
  12. protected $userStats;
  13. protected $startAt;
  14. protected $endAt;
  15. protected $serverStats;
  16. public function __construct()
  17. {
  18. ini_set('memory_limit', -1);
  19. }
  20. public function setStartAt($timestamp) {
  21. $this->startAt = $timestamp;
  22. }
  23. public function setEndAt($timestamp) {
  24. $this->endAt = $timestamp;
  25. }
  26. public function setServerStats() {
  27. $this->serverStats = Cache::get("stat_server_{$this->startAt}");
  28. $this->serverStats = json_decode($this->serverStats, true) ?? [];
  29. if (!is_array($this->serverStats)) {
  30. $this->serverStats = [];
  31. }
  32. }
  33. public function setUserStats() {
  34. $this->userStats = Cache::get("stat_user_{$this->startAt}");
  35. $this->userStats = json_decode($this->userStats, true) ?? [];
  36. if (!is_array($this->userStats)) {
  37. $this->userStats = [];
  38. }
  39. }
  40. public function generateStatData(): array
  41. {
  42. $startAt = $this->startAt;
  43. $endAt = $this->endAt;
  44. if (!$startAt || !$endAt) {
  45. $startAt = strtotime(date('Y-m-d'));
  46. $endAt = strtotime('+1 day', $startAt);
  47. }
  48. $data = [];
  49. $data['order_count'] = Order::where('created_at', '>=', $startAt)
  50. ->where('created_at', '<', $endAt)
  51. ->count();
  52. $data['order_total'] = Order::where('created_at', '>=', $startAt)
  53. ->where('created_at', '<', $endAt)
  54. ->sum('total_amount');
  55. $data['paid_count'] = Order::where('paid_at', '>=', $startAt)
  56. ->where('paid_at', '<', $endAt)
  57. ->whereNotIn('status', [0, 2])
  58. ->count();
  59. $data['paid_total'] = Order::where('paid_at', '>=', $startAt)
  60. ->where('paid_at', '<', $endAt)
  61. ->whereNotIn('status', [0, 2])
  62. ->sum('total_amount');
  63. $commissionLogBuilder = CommissionLog::where('created_at', '>=', $startAt)
  64. ->where('created_at', '<', $endAt);
  65. $data['commission_count'] = $commissionLogBuilder->count();
  66. $data['commission_total'] = $commissionLogBuilder->sum('get_amount');
  67. $data['register_count'] = User::where('created_at', '>=', $startAt)
  68. ->where('created_at', '<', $endAt)
  69. ->count();
  70. $data['invite_count'] = User::where('created_at', '>=', $startAt)
  71. ->where('created_at', '<', $endAt)
  72. ->whereNotNull('invite_user_id')
  73. ->count();
  74. $data['transfer_used_total'] = StatServer::where('created_at', '>=', $startAt)
  75. ->where('created_at', '<', $endAt)
  76. ->select(DB::raw('SUM(u) + SUM(d) as total'))
  77. ->value('total') ?? 0;
  78. return $data;
  79. }
  80. public function statServer($serverId, $serverType, $u, $d)
  81. {
  82. $this->serverStats[$serverType] = $this->serverStats[$serverType] ?? [];
  83. if (isset($this->serverStats[$serverType][$serverId])) {
  84. $this->serverStats[$serverType][$serverId][0] += $u;
  85. $this->serverStats[$serverType][$serverId][1] += $d;
  86. } else {
  87. $this->serverStats[$serverType][$serverId] = [$u, $d];
  88. }
  89. Cache::set("stat_server_{$this->startAt}", json_encode($this->serverStats));
  90. }
  91. public function statUser($rate, $userId, $u, $d)
  92. {
  93. $this->userStats[$rate] = $this->userStats[$rate] ?? [];
  94. if (isset($this->userStats[$rate][$userId])) {
  95. $this->userStats[$rate][$userId][0] += $u;
  96. $this->userStats[$rate][$userId][1] += $d;
  97. } else {
  98. $this->userStats[$rate][$userId] = [$u, $d];
  99. }
  100. Cache::set("stat_user_{$this->startAt}", json_encode($this->userStats));
  101. }
  102. public function getStatUserByUserID($userId): array
  103. {
  104. $stats = [];
  105. foreach (array_keys($this->userStats) as $rate) {
  106. if (!isset($this->userStats[$rate][$userId])) continue;
  107. $stats[] = [
  108. 'record_at' => $this->startAt,
  109. 'server_rate' => $rate,
  110. 'u' => $this->userStats[$rate][$userId][0],
  111. 'd' => $this->userStats[$rate][$userId][1],
  112. 'user_id' => $userId
  113. ];
  114. }
  115. return $stats;
  116. }
  117. public function getStatUser()
  118. {
  119. $stats = [];
  120. foreach ($this->userStats as $k => $v) {
  121. foreach (array_keys($v) as $userId) {
  122. if (isset($v[$userId])) {
  123. $stats[] = [
  124. 'server_rate' => $k,
  125. 'u' => $v[$userId][0],
  126. 'd' => $v[$userId][1],
  127. 'user_id' => $userId
  128. ];
  129. }
  130. }
  131. }
  132. return $stats;
  133. }
  134. public function getStatServer()
  135. {
  136. $stats = [];
  137. foreach ($this->serverStats as $serverType => $v) {
  138. foreach (array_keys($v) as $serverId) {
  139. if (isset($v[$serverId])) {
  140. $stats[] = [
  141. 'server_id' => $serverId,
  142. 'server_type' => $serverType,
  143. 'u' => $v[$serverId][0],
  144. 'd' => $v[$serverId][1],
  145. ];
  146. }
  147. }
  148. }
  149. return $stats;
  150. }
  151. public function clearStatUser()
  152. {
  153. Cache::forget("stat_user_{$this->startAt}");
  154. }
  155. public function clearStatServer()
  156. {
  157. Cache::forget("stat_server_{$this->startAt}");
  158. }
  159. public function getStatRecord($type)
  160. {
  161. switch ($type) {
  162. case "paid_total": {
  163. return Stat::select([
  164. '*',
  165. DB::raw('paid_total / 100 as paid_total')
  166. ])
  167. ->where('record_at', '>=', $this->startAt)
  168. ->where('record_at', '<', $this->endAt)
  169. ->orderBy('record_at', 'ASC')
  170. ->get();
  171. }
  172. case "commission_total": {
  173. return Stat::select([
  174. '*',
  175. DB::raw('commission_total / 100 as commission_total')
  176. ])
  177. ->where('record_at', '>=', $this->startAt)
  178. ->where('record_at', '<', $this->endAt)
  179. ->orderBy('record_at', 'ASC')
  180. ->get();
  181. }
  182. case "register_count": {
  183. return Stat::where('record_at', '>=', $this->startAt)
  184. ->where('record_at', '<', $this->endAt)
  185. ->orderBy('record_at', 'ASC')
  186. ->get();
  187. }
  188. }
  189. }
  190. public function getRanking($type, $limit = 20)
  191. {
  192. switch ($type) {
  193. case 'server_traffic_rank': {
  194. return StatServer::select([
  195. 'server_id',
  196. 'server_type',
  197. DB::raw('sum(u) as u'),
  198. DB::raw('sum(d) as d'),
  199. DB::raw('sum(u) + sum(d) as total')
  200. ])
  201. ->where('record_at', '>=', $this->startAt)
  202. ->where('record_at', '<', $this->endAt)
  203. ->groupBy('server_id', 'server_type')
  204. ->orderBy('total', 'DESC')
  205. ->limit($limit)
  206. ->get();
  207. }
  208. case 'user_consumption_rank': {
  209. $stats = StatUser::select([
  210. 'user_id',
  211. DB::raw('sum(u) as u'),
  212. DB::raw('sum(d) as d'),
  213. DB::raw('sum(u) + sum(d) as total')
  214. ])
  215. ->where('record_at', '>=', $this->startAt)
  216. ->where('record_at', '<', $this->endAt)
  217. ->groupBy('user_id')
  218. ->orderBy('total', 'DESC')
  219. ->limit($limit)
  220. ->get();
  221. $users = User::whereIn('id', $stats->pluck('user_id')->toArray())->get()->keyBy('id');
  222. foreach ($stats as $k => $v) {
  223. if (!isset($users[$v['user_id']])) continue;
  224. $stats[$k]['email'] = $users[$v['user_id']]['email'];
  225. }
  226. return $stats;
  227. }
  228. }
  229. }
  230. }