SentHistoryController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Http\Controllers;
  3. require_once __DIR__.'/../../../public/app/ucenter/function.php';
  4. use App\Models\SentHistory;
  5. use Illuminate\Http\Request;
  6. use App\Http\Resources\SentHistoryResource;
  7. class SentHistoryController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index(Request $request)
  15. {
  16. //
  17. switch ($request->view) {
  18. case 'sentence':
  19. $table = SentHistory::where('sent_uid',$request->get('id'));
  20. break;
  21. default:
  22. return $this->error('known view');
  23. break;
  24. }
  25. $table->orderBy($request->get('order','created_at'),$request->get('dir','desc'));
  26. $count = $table->count();
  27. $table->skip($request->get("offset",0))
  28. ->take($request->get('limit',1000));
  29. $result = $table->get();
  30. return $this->ok(["rows"=>SentHistoryResource::collection($result),"count"=>$count]);
  31. }
  32. public function contribution(Request $request){
  33. /**
  34. * 计算用户贡献度
  35. * 算法:统计句子历史记录里的用户贡献句子的数量
  36. * TODO:
  37. * 应该祛除重复的句子,一个句子的多次修改只计算一次
  38. * 只统计一个月内的数值
  39. */
  40. $result = SentHistory::select('user_uid')
  41. ->selectRaw('count(*)')
  42. ->groupBy('user_uid')
  43. ->orderBy('count','desc')
  44. ->take(10)
  45. ->get();
  46. $userinfo = new \UserInfo();
  47. foreach ($result as $key => $user) {
  48. # code...
  49. $user->username = $userinfo->getName($user->user_uid);
  50. }
  51. return $this->ok($result);
  52. }
  53. /**
  54. * Store a newly created resource in storage.
  55. *
  56. * @param \Illuminate\Http\Request $request
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function store(Request $request)
  60. {
  61. //
  62. }
  63. /**
  64. * Display the specified resource.
  65. *
  66. * @param \App\Models\SentHistory $sentHistory
  67. * @return \Illuminate\Http\Response
  68. */
  69. public function show(SentHistory $sentHistory)
  70. {
  71. //
  72. }
  73. /**
  74. * Update the specified resource in storage.
  75. *
  76. * @param \Illuminate\Http\Request $request
  77. * @param \App\Models\SentHistory $sentHistory
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function update(Request $request, SentHistory $sentHistory)
  81. {
  82. //
  83. }
  84. /**
  85. * Remove the specified resource from storage.
  86. *
  87. * @param \App\Models\SentHistory $sentHistory
  88. * @return \Illuminate\Http\Response
  89. */
  90. public function destroy(SentHistory $sentHistory)
  91. {
  92. //
  93. }
  94. }