PopularitySharehelpService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Services\Share;
  3. use App\Exceptions\AlertException;
  4. use App\Models\Share\PopularitySharehelpModel;
  5. use App\Models\User\UserModel;
  6. use App\Models\User\UserSysTagModel;
  7. use App\Services\Service;
  8. use Illuminate\Support\Facades\DB;
  9. class PopularitySharehelpService extends Service
  10. {
  11. /**
  12. * 人气值分享助力
  13. * 一天能为别人助力2次,一天内不能重复对同一好友助力一天被助力上限为50次。一个老用户+2h,新用户+10h
  14. * @param int $share_uid
  15. * @param int $help_uid
  16. * @return array
  17. * @throws AlertException
  18. */
  19. public function check(int $share_uid, int $help_uid)
  20. {
  21. $shareUser = UserModel::findOrFail($share_uid);
  22. if ($share_uid == $help_uid) {
  23. throw new AlertException("不能为自己助力", 103);
  24. }
  25. $isnew = 0;
  26. $cnt = PopularitySharehelpModel::where([
  27. ['help_uid', $help_uid],
  28. ['created_at', '>', mktime(0, 0, 0)],
  29. ])->count();
  30. if ($cnt > 0) {
  31. $isnew = 1;
  32. }
  33. if (2 <= $cnt) {
  34. throw new AlertException("今日助力次数已用完", 101);
  35. }
  36. if (
  37. PopularitySharehelpModel::where([
  38. ['share_uid', $share_uid],
  39. ['help_uid', $help_uid],
  40. ['created_at', '>', mktime(0, 0, 0)],
  41. ])->exists()
  42. ) {
  43. throw new AlertException("你今日已经帮TA助力了", 102);
  44. }
  45. if (
  46. 50 <= PopularitySharehelpModel::where([
  47. ['share_uid', $share_uid],
  48. ['created_at', '>', mktime(0, 0, 0)],
  49. ])->count()
  50. ) {
  51. throw new AlertException("今日被助力次数到上限", 104);
  52. }
  53. $user = UserModel::findOrFail($help_uid);
  54. if ($user->created_at->timestamp > time() - 300) {
  55. $isnew = 1;
  56. }
  57. $add_at = (10 + 2 * $isnew) * 3600;
  58. DB::beginTransaction();
  59. try {
  60. $systag = UserSysTagModel::firstOrCreate(['uid' => $shareUser->uid], ['popularity_share_end_at' => 0]);
  61. if ($systag->popularity_share_end_at < time()) {
  62. $systag->popularity_share_end_at = time() + $add_at;
  63. } else {
  64. $systag->popularity_share_end_at += $add_at;
  65. }
  66. $systag->save();
  67. DB::table('kdgx_fpdx_popularity_sharehelp')->insert([
  68. 'created_at' => time(),
  69. 'share_uid' => $share_uid,
  70. 'help_uid' => $help_uid,
  71. 'end_at' => $systag->popularity_share_end_at,
  72. 'helpuid_type' => $isnew,
  73. ]);
  74. DB::commit();
  75. try {
  76. $ns = new NoticeService();
  77. $ns->popularitySharehelp2ShareUser($share_uid, ['help_uid' => $help_uid, 'isnew' => $isnew]);
  78. $ns->popularitySharehelp2HelpUser($help_uid, ['share_uid' => $share_uid, 'isnew' => $isnew]);
  79. } catch (\Exception $exception) {
  80. }
  81. return array(
  82. 'user' => [
  83. 'nickname' => $shareUser->nickname,
  84. ],
  85. );
  86. } catch (\Exception $exception) {
  87. DB::rollBack();
  88. throw $exception;
  89. }
  90. }
  91. /**
  92. * 助力页概览
  93. * @param int $uid
  94. * @return array
  95. */
  96. public function helpOverView(int $uid)
  97. {
  98. $systag = UserSysTagModel::firstOrCreate(['uid' => $uid], ['popularity_share_end_at' => 0]);
  99. $help_cnt = PopularitySharehelpModel::where('share_uid', $uid)->count();
  100. $new_cnt = PopularitySharehelpModel::where([['share_uid', $uid], ['helpuid_type', 1]])->count();
  101. return array(
  102. 'end_at' => $systag->popularity_share_end_at,
  103. 'help_cnt' => $help_cnt,
  104. 'new_cnt' => $new_cnt,
  105. );
  106. }
  107. /**
  108. * 加持中的助力
  109. * @param int $uid
  110. * @param array $pages
  111. * @return array
  112. */
  113. public function inglist(int $uid, array $pages): array
  114. {
  115. $datas = PopularitySharehelpModel::where([
  116. ['share_uid', $uid],
  117. ['end_at', ">", time()],
  118. ])->orderBy('id', 'asc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  119. $users = UserModel::whereIn('uid', $datas->pluck('help_uid'))->get(['uid', 'nickname', 'headimgurl']);
  120. $users = array_column($users->toArray(), null, 'uid');
  121. foreach ($datas as &$data) {
  122. $data->end_at = min($data->end_at - time(), (2 + 8 * $data->helpuid_type) * 3600);
  123. $data->total_time = (2 + 8 * $data->isnew) * 3600;
  124. $data->user = $users[$data->help_uid];
  125. }
  126. if ($datas->count() < $pages['limit']) {
  127. $total = (($pages['page'] - 1) * $pages['limit']) + $datas->count();
  128. } else {
  129. $total = PopularitySharehelpModel::where([['share_uid', $uid], ['end_at', '>', time()]])->count();
  130. }
  131. return [
  132. 'total' => $total,
  133. 'page' => $pages['page'],
  134. 'limit' => $pages['limit'],
  135. 'list' => $datas,
  136. ];
  137. }
  138. /**
  139. * 失效的助力
  140. * @param int $uid
  141. * @param array $pages
  142. * @return array
  143. */
  144. public function historylist(int $uid, array $pages): array
  145. {
  146. $datas = PopularitySharehelpModel::where([['share_uid', $uid], ['end_at', '<=', time()]])
  147. ->orderBy('id', 'desc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  148. $users = UserModel::whereIn('uid', $datas->pluck('help_uid'))->get(['uid', 'nickname', 'headimgurl']);
  149. $users = array_column($users->toArray(), null, 'uid');
  150. foreach ($datas as &$data) {
  151. $data->total_time = (2 + 8 * $data->helpuid_type) * 3600;
  152. $data->user = null;
  153. isset($users[$data->help_uid]) && $data->user = $users[$data->help_uid];
  154. }
  155. if ($datas->count() < $pages['limit']) {
  156. $total = (($pages['page'] - 1) * $pages['limit']) + $datas->count();
  157. } else {
  158. $total = PopularitySharehelpModel::where([['share_uid', $uid], ['end_at', '>', time()]])->count();
  159. }
  160. return [
  161. 'total' => $total,
  162. 'page' => $pages['page'],
  163. 'limit' => $pages['limit'],
  164. 'list' => $datas,
  165. ];
  166. }
  167. }