| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- /**
- * 评论仓库.
- *
- * @author jim <jim458734573@gmail.com>
- * @date 2020-01-28 20:17:45
- */
- declare(strict_types=1);
- namespace App\Repositories;
- use App\Model\CommentModel;
- use App\Model\TopicModel;
- use App\Model\VideoModel;
- use App\Repositories\Service\CommonService;
- use Carbon\Carbon;
- use Hyperf\Database\Model\Builder;
- use Hyperf\HttpServer\Contract\RequestInterface;
- use Hyperf\Utils\ApplicationContext;
- use KZ2\Repositories\Model\Repository;
- class CommentRepository extends Repository
- {
- public function model()
- {
- return CommentModel::class;
- }
- /**
- * 创建一个评论.
- * @param $setarr
- * @return \Hyperf\DbConnection\Model\Model
- */
- public function createComment($setarr)
- {
- $commonService = make(CommonService::class);
- $setarr['parent_user_id'] = '';
- $setarr['floor'] = 1;
- $setarr['quote'] = '';
- $setarr['replaied_at'] = null;
- // 这里可以随机伪造支持,反对数据.
- $setarr['total_support'] = 0;
- $setarr['total_against'] = 0;
- $setarr['total_quote'] = 0;
- if (isset($setarr['ip'])) {
- // 命令行本地执行,取不到request
- $clientIp = '113.238.142.208';
- $arrUa = [
- 'cli run cmd',
- ];
- } else {
- $request = request();
- // $serverParams = $request->getServerParams();
- $clientIp = $commonService->getIp();
- $arrUa = $request->getHeader('user-agent');
- }
- // 取第一个.
- $ua = current($arrUa);
- $setarr['ip'] = $clientIp;
- $setarr['ua'] = $ua;
- $parentObj = $relationObj = null;
- if (!empty($setarr['parent_id'])) {
- // 处理父级引用回复.
- $parentObj = CommentModel::where('id', $setarr['parent_id'])->first();
- if (!empty($parentObj)) {
- $parentFloor = intval($parentObj->floor);
- $setarr['parent_user_id'] = $parentObj->user_id;
- $setarr['floor'] = $parentFloor + 1;
- if (empty($parentObj->quote)) {
- $setarr['quote'] = str_pad('', $parentFloor, '>', STR_PAD_RIGHT) . $parentObj->content;
- } else {
- $setarr['quote'] = $parentObj->quote . "\n" . str_pad('', $parentFloor, '>',
- STR_PAD_RIGHT) . $parentObj->content;
- }
- // 引用了就代表是回复.
- $setarr['replaied_at'] = Carbon::now()->toDateTimeString();
- }
- }
- // 处理关联模块.
- if (!empty($setarr['module'])) {
- switch ($setarr['module']) {
- case 'topic':
- if (!empty($setarr['svalue'])) {
- $id = intval($setarr['svalue']);
- $relationObj = TopicModel::where('id', $id)->first();
- if (!empty($relationObj->user_id)) {
- $setarr['module_user_uid'] = $relationObj->user_id;
- }
- }
- break;
- case 'video':
- if (!empty($setarr['svalue'])) {
- $id = intval($setarr['svalue']);
- $relationObj = VideoModel::where('id', $id)->first();
- if (!empty($relationObj->uid)) {
- $setarr['module_user_uid'] = $relationObj->uid;
- }
- if (empty($setarr['subject'])) {
- $setarr['subject'] = mb_substr($relationObj->title, 0, 60);
- }
- }
- break;
- default:
- break;
- }
- }
- $item = CommentModel::create($setarr);
- if ($item) {
- if (!empty($parentObj)) {
- // 父级累计引用次数累加+1
- $parentObj->total_quote = intval($parentObj->total_quote) + 1;
- $parentObj->save();
- }
- if (!empty($relationObj)) {
- // 累计追加话题统计.
- if ($setarr['module'] == 'topic') {
- $relationObj->total_comment = intval($relationObj->total_comment) + 1;
- } elseif ($setarr['module'] == 'video') {
- $relationObj->comment = intval($relationObj->comment) + 1;
- }
- $relationObj->save();
- }
- }
- return $item;
- }
- /**
- * 递增支持.
- * @return int
- */
- public function incSupport()
- {
- return (new CommentModel)->increment('total_support', 1);
- }
- /**
- * 统计某人今天评论数
- * @param $userId
- * @param string $module
- * @return int
- */
- public function countToday($userId, $module = '')
- {
- if (empty($userId)) {
- return 0;
- }
- $startToday = Carbon::today()->startOfDay();
- $endToday = Carbon::today()->endOfDay();
- $model = CommentModel::query()->where(['user_id' => $userId]);
- if (!empty($module)) {
- $model = $model->where(['module' => $module]);
- }
- $model = $model->whereBetween('created_at', [$startToday, $endToday]);
- return $model->count();
- }
- /**
- * 检查权限.
- * @param string $userCode
- * @param string $module
- * @return array
- */
- public function checkPermission($userCode, $module = '')
- {
- $result = [
- 'amount' => 0,
- 'posted' => 0,
- 'check_result' => false,
- ];
- $userRepo = make(UserRepository::class);
- $userInfo = $userRepo->getUserInfoFromCache($userCode);
- $vipLevel = 1;
- $dayLimit = 0;
- if ($userInfo['is_vip'] == 'y') {
- $vipLevel = isset($userInfo['vip_level']) ? intval($userInfo['vip_level']) : 1;
- $dayLimit = $this->getDayLimitByVipLevel($vipLevel); // vip每天送的
- }
- $todayTaskVideoNum = make(TaskAwarkLogRepository::class)->coutTodayOnce($userInfo['code'], 2);
- $maxComment = intval($userInfo['max_comment']); // 永久的奖励,非vip里的
- $amount = $maxComment + $dayLimit + $todayTaskVideoNum;
- $postedNum = $this->countToday($userInfo['code'], $module);
- $result['amount'] = $amount;
- $result['posted'] = $postedNum;
- if ($postedNum >= $amount) {
- $result['check_result'] = false;
- } else {
- $result['check_result'] = true;
- }
- return $result;
- }
- /**
- * 根据vip等级获取每天评论限制.
- * @param int $vipLevel
- * @return int
- */
- public function getDayLimitByVipLevel($vipLevel = 1)
- {
- $vipLists = make(VipRepository::class)->getCanUseVipList();
- $limit = 0;
- if (isset($vipLists[$vipLevel])) {
- $arrFeatures = $vipLists[$vipLevel]['jdata']['feature'] ?? [];
- if (!empty($arrFeatures)) {
- foreach ($arrFeatures as $v) {
- if ($v['type'] == 'comment') {
- if (isset($v['count'])) {
- $limit = $v['count'];
- }
- break;
- }
- }
- }
- }
- return $limit;
- }
- /**
- * 获取所有数据
- * @param array $inputData
- * @return object
- */
- public function getLists(array $inputData): object
- {
- $digest = $inputData['digest'] ?? '';
- $top = $inputData['top'] ?? '';
- $page = $inputData['page'] ?? 1;
- $page_size = $inputData['page_size'] ?? 30;
- $list = CommentModel::query()->where(function ($q) use ($inputData, $digest, $top) {
- if ($inputData['user_uid']) {
- $q->where('user_id', $inputData['user_uid']);
- }
- if ($inputData['module']) {
- $q->where('module', $inputData['module']);
- }
- if ($digest !== '') {
- $q->where('is_digest', $inputData['digest']);
- }
- if ($top !== '') {
- $q->where('is_top', $inputData['top']);
- }
- if ($inputData['like_content']) {
- $q->where('content', 'like', '%' . $inputData['like_content'] . '%');
- }
- if ($inputData['stime'] && $inputData['otime']) {
- $q->whereBetween(
- 'updated_at',
- [
- Carbon::parse($inputData['stime'])->toDateString(),
- Carbon::parse($inputData['otime'])->toDateString()
- ]
- );
- }
- })
- ->orderByDesc('id')
- ->paginate($page_size, ['*'], 'page', $page);
- return $list;
- }
- /**
- * 获取分组
- */
- public function getModule():object
- {
- return CommentModel::query()
- ->groupBy(['module'])
- ->pluck('module');
- }
- }
|