<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/15
 * Time: 16:40
 */
namespace  App\Transformers\Circle;

use App\Models\InterestCircleMessageComment;
use App\Models\PostComment;
use App\Traits\PostTrait;
use App\Traits\UserTrait;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use League\Fractal\TransformerAbstract;

class CommentTransformer extends TransformerAbstract
{
    use UserTrait;
    use PostTrait;
    public function __construct($msgId, $uid)
    {
        $this->msgId = $msgId;
        $this->uid = $uid;
    }

    public function transform(InterestCircleMessageComment $interestCircleMessageComment)
    {
        $reply = [];
        $replyKey = 'circle_message_new_reply_'.$interestCircleMessageComment['id'];
        $replyData = Redis::GET($replyKey);
        if($replyData){
            $reply = json_decode($replyData);
            foreach($reply as &$item){
                $item->created_at = Carbon::parse($item->created_at)->diffForHumans();
            }
        }else{
            $replies = InterestCircleMessageComment::where('parent_id', $interestCircleMessageComment['id'])->orderBy('id', 'desc')->limit(2)->get();
            $redisReply = [];
            foreach($replies as $val){
                $userComment = $this->userInfo($val->uid);
                $replyUsername = '';
                if($val->reply_uid){
                    $userReply = $this->userInfo($val->reply_uid);
                    $replyUsername = $userReply['username'];
                }
                $reply[] = [
                    'id' => $val->id,
                    'uid' => $val->uid,
                    'username' => $userComment['username'],
                    'avatar' => $userComment['avatar'],
                    'reply_username' => $replyUsername,
                    'content' => $val->is_delete?'该评论已被删除':$val->content,
                    'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
                    'is_delete' => $val->is_delete,
                ];
                $redisReply[] = [
                    'id' => $val->id,
                    'uid' => $val->uid,
                    'username' => $userComment['username'],
                    'avatar' => $userComment['avatar'],
                    'reply_username' => $replyUsername,
                    'content' => $val->is_delete?'该评论已被删除':$val->content,
                    'created_at' => $val->created_at,
                    'is_delete' => $val->is_delete,
                ];
            }
            Redis::SET($replyKey, json_encode($redisReply));
            Redis::EXPIRE($replyKey, 604800);
        }
        $user = $this->userInfo($interestCircleMessageComment['uid']);
        return [
            'id' => $interestCircleMessageComment['id'],
            'uid' => $interestCircleMessageComment['uid'],
            'username' => $user['username'],
            'avatar' => $user['avatar'],
            'content' => $interestCircleMessageComment['is_delete']?'该评论已被删除':$interestCircleMessageComment['content'],
            'created_at' => Carbon::parse($interestCircleMessageComment['created_at'])->diffForHumans(),
            'reply_count' => $interestCircleMessageComment['reply_count'],
            'reply' => $reply,
            'is_delete' => $interestCircleMessageComment['is_delete'],
        ];
    }
}