StatisticalService.php 7.0 KB

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