123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace App\Services\User;
- use App\Exceptions\AlertException;
- use App\Models\Invite\QuestionTemplateModel;
- use App\Models\PraiseModel;
- use App\Models\User\LikeInviteQuestionModel;
- use App\Models\User\UserModel;
- use App\Services\Service;
- class LikeInviteQuestionService extends Service
- {
- /**
- * 心动考验->录制问题
- * @param int $uid
- * @param array $data
- * @return int
- */
- public function createQuestion(int $uid, array $data): int
- {
- $user = UserModel::findOrFail($uid);
- $liqm = new LikeInviteQuestionModel();
- $data['uid'] = $uid;
- $data['sex'] = $user->sex;
- $data['author'] = $user->nickname ?: "小象导";
- $question = $liqm->fill($data);
- $question->save();
- // 选择次数+1
- $template_id = $data['template_id'];
- QuestionTemplateModel::where('id', $template_id)->increment('choice_count');
- return $question->id;
- }
- /**
- * 我的录制->问题列表
- * @param int $uid
- * @param int $type
- * @param array $pages
- * @return array
- */
- public function list(int $uid, int $type, array $pages)
- {
- $liqm = new LikeInviteQuestionModel();
- $total = $liqm->where(array(['uid', $uid], ['question_type', $type]))->count();
- $datas = $liqm->where(array(['uid', $uid], ['question_type', $type]))->orderBy(
- 'id',
- 'desc'
- )->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get([
- 'id',
- 'created_at',
- 'uid',
- 'question',
- 'voice_src',
- 'question_type',
- ]);
- $users = UserModel::whereIn('uid', $datas->pluck('uid'))->get(['uid', 'nickname', 'headimgurl'])->toArray();
- $users = array_combine(array_column($users, 'uid'), $users);
- $datas->map(function (&$data) use ($users) {
- $data->user = $users[$data->uid];
- });
- return [
- 'total' => $total,
- 'page' => $pages['page'],
- 'limit' => $pages['limit'],
- 'list' => $datas,
- ];
- }
- /**
- * 删除问题
- * @param int $uid
- * @param int $question_id
- * @return bool
- * @throws AlertException
- */
- public function deleteQuestion(int $uid, int $question_id)
- {
- $liqm = new LikeInviteQuestionModel();
- $question = $liqm->findOrFail($question_id);
- if ($question->uid != $uid) {
- throw new AlertException("无权限", 101);
- }
- $cnt = $question->where(array(['uid', $uid], ['question_type', $question->question_type]))->count();
- if ($question->delete() && 1 == $cnt) {
- // 移除问题设置为心动考验
- if (1 == $question->question_type) {
- UserModel::where('uid', $uid)->update(['task_sing' => 0]);
- } else {
- UserModel::where('uid', $uid)->update(['task_question' => 0]);
- }
- }
- // 选择次数-1
- QuestionTemplateModel::where('id', $question->template_id)->decrement('choice_count');
- return true;
- }
- /**
- * 心动考验开关
- * @param int $uid
- * @param string $task_type
- * @param int $switch
- * @return bool
- * @throws AlertException
- * @version 2.17.0
- * @deprecated 2.17.0 以上版本弃用
- */
- public function likeInviteQuestionSwitch(int $uid, string $task_type, int $switch)
- {
- switch ($task_type) {
- case 'task_photo':
- break;
- case 'task_question':
- $question_type = 0;
- break;
- case 'task_sing':
- $question_type = 1;
- break;
- default:
- throw new AlertException("参数错误", 102);
- }
- if (
- 1 == $switch && "task_photo" != $task_type && !LikeInviteQuestionModel::where(array(
- ['uid', $uid],
- ['question_type', $question_type],
- ))->exists()
- ) {
- throw new AlertException("没有设置该类型的考验", 103);
- }
- UserModel::where('uid', $uid)->update([$task_type => $switch]);
- return true;
- }
- /**
- * 回答心动考验->换题列表
- * @param int $user_id
- * @param int $question_type
- * @return mixed
- */
- public function questionList(int $user_id, int $question_type)
- {
- switch ($user_id) {
- case 1:
- case 2:
- $datas = LikeInviteQuestionModel::where(array(
- ['uid', $user_id],
- ['question_type', $question_type],
- ))->take(20)->inRandomOrder()->get();
- break;
- default:
- $datas = LikeInviteQuestionModel::where(array(
- ['uid', $user_id],
- ['question_type', $question_type],
- ))->get();
- break;
- }
- return $datas;
- }
- /**
- * 心动考验页的取消喜欢|喜欢 某人
- * @param $uid
- * @param $question_id
- * @return array
- * @throws AlertException
- */
- public function thumb($uid, $question_id)
- {
- $question = LikeInviteQuestionModel::findOrFail($question_id);
- $user = UserModel::findOrFail($question->uid);
- if (!PraiseModel::where(array(['partner_id', $user->partner_id], ['uid', $uid]))->exists()) {
- $question->increment('thumbs', 1);
- }
- $ps = new PartnerService();
- $data = $ps->thumb($uid, $user->partner_id);
- return $data;
- }
- }
|