UserStatisticController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\UserApi;
  4. use App\Models\DhammaTerm;
  5. use App\Models\Sentence;
  6. use App\Models\UserDict;
  7. use App\Models\UserOperationDaily;
  8. use App\Models\UserOperationLog;
  9. use App\Models\Wbw;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Http\Response;
  12. use Illuminate\Support\Facades\Cache;
  13. class UserStatisticController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return Response
  19. */
  20. public function index()
  21. {
  22. //
  23. }
  24. /**
  25. * Show the form for creating a new resource.
  26. *
  27. * @return Response
  28. */
  29. public function create()
  30. {
  31. //
  32. }
  33. /**
  34. * Store a newly created resource in storage.
  35. *
  36. * @return Response
  37. */
  38. public function store(Request $request)
  39. {
  40. //
  41. }
  42. /**
  43. * Display the specified resource.
  44. *
  45. * @param UserOperationDaily $userOperationDaily
  46. * @return Response
  47. */
  48. public function show(Request $request, string $userName)
  49. {
  50. //
  51. $queryUserId = UserApi::getIntIdByName($userName);
  52. $queryUserUuid = UserApi::getIdByName($userName);
  53. $cacheExpiry = config('mint.cache.expire');
  54. $expSum = 0;
  55. $wbwCount = 0;
  56. $lookupCount = 0;
  57. $translationCount = 0;
  58. $translationCountPub = 0;
  59. $termCount = 0;
  60. $termCountWithNote = 0;
  61. $myDictCount = 0;
  62. // 总经验值
  63. if (! $request->has('view') || $request->input('view') === 'exp-sum') {
  64. $expSum = Cache::remember(
  65. "user/{$userName}/exp/sum",
  66. $cacheExpiry,
  67. function () use ($queryUserId) {
  68. return UserOperationDaily::where('user_id', $queryUserId)
  69. ->sum('duration');
  70. }
  71. );
  72. }
  73. // 逐词解析
  74. if (! $request->has('view') || $request->input('view') === 'wbw-count') {
  75. $wbwCount = Cache::remember(
  76. "user/{$userName}/wbw/count",
  77. $cacheExpiry,
  78. function () use ($queryUserId) {
  79. return Wbw::where('editor_id', $queryUserId)
  80. ->count();
  81. }
  82. );
  83. }
  84. // 查字典次数
  85. if (! $request->has('view') || $request->input('view') === 'lookup-count') {
  86. $lookupCount = Cache::remember(
  87. "user/{$userName}/lookup/count",
  88. $cacheExpiry,
  89. function () use ($queryUserId) {
  90. return UserOperationLog::where('user_id', $queryUserId)
  91. ->where('op_type', 'dict_lookup')
  92. ->count();
  93. }
  94. );
  95. }
  96. // 译文
  97. // TODO 判断是否是译文channel
  98. if (! $request->has('view') || $request->input('view') === 'translation-count') {
  99. $translationCount = Cache::remember(
  100. "user/{$userName}/translation/count",
  101. $cacheExpiry,
  102. function () use ($queryUserUuid) {
  103. return Sentence::where('editor_uid', $queryUserUuid)
  104. ->count();
  105. }
  106. );
  107. $translationCountPub = Cache::remember(
  108. "user/{$userName}/translation/count-pub",
  109. $cacheExpiry,
  110. function () use ($queryUserUuid) {
  111. return Sentence::where('editor_uid', $queryUserUuid)
  112. ->where('status', 30)
  113. ->count();
  114. }
  115. );
  116. }
  117. // 术语
  118. if (! $request->has('view') || $request->input('view') === 'term-count') {
  119. $termCount = Cache::remember(
  120. "user/{$userName}/term/count",
  121. $cacheExpiry,
  122. function () use ($queryUserId) {
  123. return DhammaTerm::where('editor_id', $queryUserId)
  124. ->count();
  125. }
  126. );
  127. $termCountWithNote = Cache::remember(
  128. "user/{$userName}/term/count-note",
  129. $cacheExpiry,
  130. function () use ($queryUserId) {
  131. return DhammaTerm::where('editor_id', $queryUserId)
  132. ->where('note', '<>', '')
  133. ->count();
  134. }
  135. );
  136. }
  137. // 单词本
  138. if (! $request->has('view') || $request->input('view') === 'my-dict-count') {
  139. $myDictCount = Cache::remember(
  140. "user/{$userName}/dict/count",
  141. $cacheExpiry,
  142. function () use ($queryUserId) {
  143. return UserDict::where('creator_id', $queryUserId)
  144. ->count();
  145. }
  146. );
  147. }
  148. return $this->ok([
  149. 'exp' => ['sum' => (int) $expSum],
  150. 'wbw' => ['count' => (int) $wbwCount],
  151. 'lookup' => ['count' => (int) $lookupCount],
  152. 'translation' => [
  153. 'count' => (int) $translationCount,
  154. 'count_pub' => (int) $translationCountPub,
  155. ],
  156. 'term' => [
  157. 'count' => (int) $termCount,
  158. 'count_with_note' => (int) $termCountWithNote,
  159. ],
  160. 'dict' => ['count' => (int) $myDictCount],
  161. ]);
  162. }
  163. /**
  164. * Show the form for editing the specified resource.
  165. *
  166. * @return Response
  167. */
  168. public function edit(UserOperationDaily $userOperationDaily)
  169. {
  170. //
  171. }
  172. /**
  173. * Update the specified resource in storage.
  174. *
  175. * @return Response
  176. */
  177. public function update(Request $request, UserOperationDaily $userOperationDaily)
  178. {
  179. //
  180. }
  181. /**
  182. * Remove the specified resource from storage.
  183. *
  184. * @return Response
  185. */
  186. public function destroy(UserOperationDaily $userOperationDaily)
  187. {
  188. //
  189. }
  190. }