LikeInviteQuestionService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Services\User;
  3. use App\Exceptions\AlertException;
  4. use App\Models\Invite\QuestionTemplateModel;
  5. use App\Models\PraiseModel;
  6. use App\Models\User\LikeInviteQuestionModel;
  7. use App\Models\User\UserModel;
  8. use App\Services\Service;
  9. class LikeInviteQuestionService extends Service
  10. {
  11. /**
  12. * 心动考验->录制问题
  13. * @param int $uid
  14. * @param array $data
  15. * @return int
  16. */
  17. public function createQuestion(int $uid, array $data): int
  18. {
  19. $user = UserModel::findOrFail($uid);
  20. $liqm = new LikeInviteQuestionModel();
  21. $data['uid'] = $uid;
  22. $data['sex'] = $user->sex;
  23. $data['author'] = $user->nickname ?: "小象导";
  24. $question = $liqm->fill($data);
  25. $question->save();
  26. // 选择次数+1
  27. $template_id = $data['template_id'];
  28. QuestionTemplateModel::where('id', $template_id)->increment('choice_count');
  29. return $question->id;
  30. }
  31. /**
  32. * 我的录制->问题列表
  33. * @param int $uid
  34. * @param int $type
  35. * @param array $pages
  36. * @return array
  37. */
  38. public function list(int $uid, int $type, array $pages)
  39. {
  40. $liqm = new LikeInviteQuestionModel();
  41. $total = $liqm->where(array(['uid', $uid], ['question_type', $type]))->count();
  42. $datas = $liqm->where(array(['uid', $uid], ['question_type', $type]))->orderBy(
  43. 'id',
  44. 'desc'
  45. )->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get([
  46. 'id',
  47. 'created_at',
  48. 'uid',
  49. 'question',
  50. 'voice_src',
  51. 'question_type',
  52. ]);
  53. $users = UserModel::whereIn('uid', $datas->pluck('uid'))->get(['uid', 'nickname', 'headimgurl'])->toArray();
  54. $users = array_combine(array_column($users, 'uid'), $users);
  55. $datas->map(function (&$data) use ($users) {
  56. $data->user = $users[$data->uid];
  57. });
  58. return [
  59. 'total' => $total,
  60. 'page' => $pages['page'],
  61. 'limit' => $pages['limit'],
  62. 'list' => $datas,
  63. ];
  64. }
  65. /**
  66. * 删除问题
  67. * @param int $uid
  68. * @param int $question_id
  69. * @return bool
  70. * @throws AlertException
  71. */
  72. public function deleteQuestion(int $uid, int $question_id)
  73. {
  74. $liqm = new LikeInviteQuestionModel();
  75. $question = $liqm->findOrFail($question_id);
  76. if ($question->uid != $uid) {
  77. throw new AlertException("无权限", 101);
  78. }
  79. $cnt = $question->where(array(['uid', $uid], ['question_type', $question->question_type]))->count();
  80. if ($question->delete() && 1 == $cnt) {
  81. // 移除问题设置为心动考验
  82. if (1 == $question->question_type) {
  83. UserModel::where('uid', $uid)->update(['task_sing' => 0]);
  84. } else {
  85. UserModel::where('uid', $uid)->update(['task_question' => 0]);
  86. }
  87. }
  88. // 选择次数-1
  89. QuestionTemplateModel::where('id', $question->template_id)->decrement('choice_count');
  90. return true;
  91. }
  92. /**
  93. * 心动考验开关
  94. * @param int $uid
  95. * @param string $task_type
  96. * @param int $switch
  97. * @return bool
  98. * @throws AlertException
  99. * @version 2.17.0
  100. * @deprecated 2.17.0 以上版本弃用
  101. */
  102. public function likeInviteQuestionSwitch(int $uid, string $task_type, int $switch)
  103. {
  104. switch ($task_type) {
  105. case 'task_photo':
  106. break;
  107. case 'task_question':
  108. $question_type = 0;
  109. break;
  110. case 'task_sing':
  111. $question_type = 1;
  112. break;
  113. default:
  114. throw new AlertException("参数错误", 102);
  115. }
  116. if (
  117. 1 == $switch && "task_photo" != $task_type && !LikeInviteQuestionModel::where(array(
  118. ['uid', $uid],
  119. ['question_type', $question_type],
  120. ))->exists()
  121. ) {
  122. throw new AlertException("没有设置该类型的考验", 103);
  123. }
  124. UserModel::where('uid', $uid)->update([$task_type => $switch]);
  125. return true;
  126. }
  127. /**
  128. * 回答心动考验->换题列表
  129. * @param int $user_id
  130. * @param int $question_type
  131. * @return mixed
  132. */
  133. public function questionList(int $user_id, int $question_type)
  134. {
  135. switch ($user_id) {
  136. case 1:
  137. case 2:
  138. $datas = LikeInviteQuestionModel::where(array(
  139. ['uid', $user_id],
  140. ['question_type', $question_type],
  141. ))->take(20)->inRandomOrder()->get();
  142. break;
  143. default:
  144. $datas = LikeInviteQuestionModel::where(array(
  145. ['uid', $user_id],
  146. ['question_type', $question_type],
  147. ))->get();
  148. break;
  149. }
  150. return $datas;
  151. }
  152. /**
  153. * 心动考验页的取消喜欢|喜欢 某人
  154. * @param $uid
  155. * @param $question_id
  156. * @return array
  157. * @throws AlertException
  158. */
  159. public function thumb($uid, $question_id)
  160. {
  161. $question = LikeInviteQuestionModel::findOrFail($question_id);
  162. $user = UserModel::findOrFail($question->uid);
  163. if (!PraiseModel::where(array(['partner_id', $user->partner_id], ['uid', $uid]))->exists()) {
  164. $question->increment('thumbs', 1);
  165. }
  166. $ps = new PartnerService();
  167. $data = $ps->thumb($uid, $user->partner_id);
  168. return $data;
  169. }
  170. }