2
0

UserStatisticController.php 5.1 KB

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