2021-07-05 21:52:44 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
2021-07-13 22:53:13 +08:00
|
|
|
use App\Cache\LastMessage;
|
2021-07-18 23:41:25 +08:00
|
|
|
use App\Cache\VoteCache;
|
2021-07-19 23:58:14 +08:00
|
|
|
use App\Cache\VoteStatisticsCache;
|
2021-07-20 23:12:18 +08:00
|
|
|
use App\Constants\TalkEventConstant;
|
2021-07-08 23:44:43 +08:00
|
|
|
use App\Constants\TalkMessageType;
|
2021-07-20 23:12:18 +08:00
|
|
|
use App\Event\TalkEvent;
|
2021-07-17 00:07:24 +08:00
|
|
|
use App\Model\Group\GroupMember;
|
2021-07-09 22:57:19 +08:00
|
|
|
use App\Model\Talk\TalkRecordsCode;
|
2021-07-12 23:19:58 +08:00
|
|
|
use App\Model\Talk\TalkRecordsVote;
|
2021-07-17 00:07:24 +08:00
|
|
|
use App\Model\Talk\TalkRecordsVoteAnswer;
|
|
|
|
use App\Support\UserRelation;
|
2021-07-08 23:44:43 +08:00
|
|
|
use Exception;
|
2021-07-20 23:12:18 +08:00
|
|
|
use App\Constants\MediaTypeConstant;
|
2021-07-09 22:57:19 +08:00
|
|
|
use App\Model\Talk\TalkRecords;
|
|
|
|
use App\Model\Talk\TalkRecordsFile;
|
2021-07-08 23:44:43 +08:00
|
|
|
use Hyperf\DbConnection\Db;
|
2021-07-05 21:52:44 +08:00
|
|
|
|
|
|
|
class TalkMessageService
|
|
|
|
{
|
2021-07-08 23:44:43 +08:00
|
|
|
/**
|
|
|
|
* 创建代码块消息
|
|
|
|
*
|
|
|
|
* @param array $message
|
|
|
|
* @param array $code
|
2021-07-13 22:53:13 +08:00
|
|
|
* @return bool
|
2021-07-08 23:44:43 +08:00
|
|
|
*/
|
|
|
|
public function insertCodeMessage(array $message, array $code)
|
|
|
|
{
|
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
|
|
|
$message['msg_type'] = TalkMessageType::CODE_MESSAGE;
|
|
|
|
$message['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
$message['updated_at'] = date('Y-m-d H:i:s');
|
2021-07-05 21:52:44 +08:00
|
|
|
|
2021-07-08 23:44:43 +08:00
|
|
|
$insert = TalkRecords::create($message);
|
|
|
|
if (!$insert) {
|
|
|
|
throw new Exception('插入聊天记录失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
$code['record_id'] = $insert->id;
|
|
|
|
$code['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
if (!TalkRecordsCode::create($code)) {
|
|
|
|
throw new Exception('插入聊天记录(代码消息)失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Db::rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-20 23:12:18 +08:00
|
|
|
LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [
|
|
|
|
'text' => '[代码消息]',
|
|
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
|
|
]);
|
|
|
|
|
|
|
|
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [
|
2021-07-13 22:53:13 +08:00
|
|
|
'sender_id' => $insert->user_id,
|
|
|
|
'receiver_id' => $insert->receiver_id,
|
|
|
|
'talk_type' => $insert->talk_type,
|
|
|
|
'record_id' => $insert->id
|
|
|
|
]));
|
|
|
|
|
|
|
|
return true;
|
2021-07-08 23:44:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 创建文件类消息
|
|
|
|
*
|
|
|
|
* @param array $message
|
|
|
|
* @param array $file
|
2021-07-13 22:53:13 +08:00
|
|
|
* @return bool
|
2021-07-08 23:44:43 +08:00
|
|
|
*/
|
|
|
|
public function insertFileMessage(array $message, array $file)
|
|
|
|
{
|
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
|
|
|
$message['msg_type'] = TalkMessageType::FILE_MESSAGE;
|
|
|
|
$message['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
$message['updated_at'] = date('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
$insert = TalkRecords::create($message);
|
|
|
|
if (!$insert) {
|
|
|
|
throw new Exception('插入聊天记录失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
$file['record_id'] = $insert->id;
|
2021-07-20 23:12:18 +08:00
|
|
|
$file['file_type'] = MediaTypeConstant::getMediaType($file['file_suffix']);
|
2021-07-08 23:44:43 +08:00
|
|
|
$file['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
if (!TalkRecordsFile::create($file)) {
|
|
|
|
throw new Exception('插入聊天记录(代码消息)失败...');
|
|
|
|
}
|
|
|
|
|
|
|
|
Db::commit();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Db::rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-20 23:12:18 +08:00
|
|
|
LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [
|
|
|
|
'text' => '[图片消息]',
|
|
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
|
|
]);
|
|
|
|
|
|
|
|
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [
|
2021-07-13 22:53:13 +08:00
|
|
|
'sender_id' => $insert->user_id,
|
|
|
|
'receiver_id' => $insert->receiver_id,
|
|
|
|
'talk_type' => $insert->talk_type,
|
|
|
|
'record_id' => $insert->id
|
|
|
|
]));
|
|
|
|
|
|
|
|
return true;
|
2021-07-08 23:44:43 +08:00
|
|
|
}
|
2021-07-12 23:19:58 +08:00
|
|
|
|
|
|
|
/**
|
2021-07-17 00:07:24 +08:00
|
|
|
* 添加投票消息
|
|
|
|
*
|
2021-07-12 23:19:58 +08:00
|
|
|
* @param array $message
|
|
|
|
* @param array $vote
|
2021-07-17 00:07:24 +08:00
|
|
|
* @return bool
|
2021-07-12 23:19:58 +08:00
|
|
|
*/
|
|
|
|
public function insertVoteMessage(array $message, array $vote)
|
|
|
|
{
|
2021-07-17 00:07:24 +08:00
|
|
|
$answer_num = GroupMember::where('group_id', $message['receiver_id'])->where('is_quit', 0)->count();
|
|
|
|
|
2021-07-12 23:19:58 +08:00
|
|
|
Db::beginTransaction();
|
|
|
|
try {
|
2021-07-17 21:27:48 +08:00
|
|
|
$message['msg_type'] = TalkMessageType::VOTE_MESSAGE;
|
2021-07-12 23:19:58 +08:00
|
|
|
$message['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
$message['updated_at'] = date('Y-m-d H:i:s');
|
|
|
|
|
2021-07-17 21:27:48 +08:00
|
|
|
$insert = TalkRecords::create($message);
|
2021-07-17 00:07:24 +08:00
|
|
|
$options = [];
|
2021-07-17 21:27:48 +08:00
|
|
|
foreach ($vote['answer_option'] as $k => $option) {
|
2021-07-17 00:07:24 +08:00
|
|
|
$options[chr(65 + $k)] = $option;
|
2021-07-12 23:19:58 +08:00
|
|
|
}
|
|
|
|
|
2021-07-17 21:27:48 +08:00
|
|
|
$vote['record_id'] = $insert->id;
|
|
|
|
$vote['answer_option'] = $options;
|
|
|
|
$vote['answer_num'] = $answer_num;
|
|
|
|
$vote['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
$vote['updated_at'] = $vote['created_at'];
|
|
|
|
|
2021-07-12 23:19:58 +08:00
|
|
|
if (!TalkRecordsVote::create($vote)) {
|
|
|
|
throw new Exception('插入聊天记录(投票消息)失败...');
|
|
|
|
}
|
|
|
|
Db::commit();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Db::rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-20 23:12:18 +08:00
|
|
|
LastMessage::getInstance()->save($insert->talk_type, $insert->user_id, $insert->receiver_id, [
|
|
|
|
'text' => '[投票消息]',
|
|
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
|
|
]);
|
|
|
|
|
|
|
|
event()->dispatch(new TalkEvent(TalkEventConstant::EVENT_TALK, [
|
2021-07-13 22:53:13 +08:00
|
|
|
'sender_id' => $insert->user_id,
|
|
|
|
'receiver_id' => $insert->receiver_id,
|
|
|
|
'talk_type' => $insert->talk_type,
|
|
|
|
'record_id' => $insert->id
|
|
|
|
]));
|
|
|
|
|
|
|
|
return true;
|
2021-07-12 23:19:58 +08:00
|
|
|
}
|
2021-07-17 00:07:24 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 群投票处理方法
|
|
|
|
*
|
|
|
|
* @param int $user_id
|
|
|
|
* @param array $params
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-07-19 23:58:14 +08:00
|
|
|
public function handleVote(int $user_id, array $params): array
|
2021-07-17 00:07:24 +08:00
|
|
|
{
|
|
|
|
$record = TalkRecords::join('talk_records_vote as vote', 'vote.record_id', '=', 'talk_records.id')
|
|
|
|
->where('talk_records.id', $params['record_id'])
|
2021-07-17 21:27:48 +08:00
|
|
|
->withCasts([
|
|
|
|
'answer_option' => 'array'
|
|
|
|
])
|
2021-07-17 00:07:24 +08:00
|
|
|
->first([
|
2021-07-17 21:27:48 +08:00
|
|
|
'talk_records.id', 'talk_records.receiver_id', 'talk_records.talk_type', 'talk_records.msg_type',
|
2021-07-17 00:07:24 +08:00
|
|
|
'vote.id as vote_id', 'vote.answer_mode', 'vote.answer_option', 'vote.answer_num', 'vote.status as vote_status'
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!$record) return false;
|
|
|
|
|
|
|
|
if ($record->msg_type != TalkMessageType::VOTE_MESSAGE) {
|
2021-07-19 23:58:14 +08:00
|
|
|
return [false, []];
|
2021-07-17 00:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!UserRelation::isFriendOrGroupMember($user_id, $record->receiver_id, $record->talk_type)) {
|
2021-07-19 23:58:14 +08:00
|
|
|
return [false, []];
|
2021-07-17 00:07:24 +08:00
|
|
|
}
|
|
|
|
|
2021-07-18 23:41:25 +08:00
|
|
|
if (TalkRecordsVoteAnswer::where('vote_id', $record->vote_id)->where('user_id', $user_id)->exists()) {
|
2021-07-19 23:58:14 +08:00
|
|
|
return [false, []];
|
2021-07-17 00:07:24 +08:00
|
|
|
}
|
|
|
|
|
2021-07-18 23:41:25 +08:00
|
|
|
$options = $params['options'];
|
|
|
|
|
2021-07-17 00:07:24 +08:00
|
|
|
sort($options);
|
|
|
|
|
|
|
|
foreach ($options as $value) {
|
2021-07-19 23:58:14 +08:00
|
|
|
if (!isset($record->answer_option[$value])) return [false, []];
|
2021-07-17 00:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 单选模式取第一个
|
2021-07-18 23:41:25 +08:00
|
|
|
if ($record->answer_mode == 0) {
|
2021-07-17 00:07:24 +08:00
|
|
|
$options = [$options[0]];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Db::transaction(function () use ($options, $record, $user_id) {
|
|
|
|
TalkRecordsVote::where('id', $record->vote_id)->update([
|
|
|
|
'answered_num' => Db::raw('answered_num + 1'),
|
|
|
|
'status' => Db::raw('if(answered_num >= answer_num, 1, 0)'),
|
|
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($options as $option) {
|
|
|
|
TalkRecordsVoteAnswer::create([
|
|
|
|
'vote_id' => $record->vote_id,
|
|
|
|
'user_id' => $user_id,
|
|
|
|
'option' => $option,
|
|
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (\Exception $e) {
|
2021-07-19 23:58:14 +08:00
|
|
|
return [false, []];
|
2021-07-17 00:07:24 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:14 +08:00
|
|
|
// 更新投票缓存
|
2021-07-18 23:41:25 +08:00
|
|
|
VoteCache::getInstance()->updateVoteCache($record->vote_id);
|
2021-07-19 23:58:14 +08:00
|
|
|
$cache = VoteStatisticsCache::getInstance()->updateVoteCache($record->vote_id);
|
2021-07-18 23:41:25 +08:00
|
|
|
|
2021-07-19 23:58:14 +08:00
|
|
|
// todo 推送消息
|
|
|
|
|
|
|
|
return [true, $cache];
|
2021-07-17 00:07:24 +08:00
|
|
|
}
|
2021-07-05 21:52:44 +08:00
|
|
|
}
|