<?php
namespace Api\PhpServices\Comment;

use Api\PhpUtils\Http\Request;

class CommentService
{
    /**
     * 错误码
     */
    const ERROR_CODE = [
        1 => '参数有误',
        2 => '添加评论失败',
        3 => '获取评论数据失败',
        4 => '点赞评论失败',
        5 => '获取评论表情失败',
        6 => '删除评论失败',
        7 => '举报评论失败'
    ];

    /**
     * 成功响应
     * @param null $data
     * @return array|int[]
     */
    private function success($data = null)
    {
        if (is_null($data)) {
            return ['code' => 0];
        }
        return ['code' => 0, 'data'=>$data];
    }

    /**
     * 异常响应
     * @param $code
     * @return array
     */
    private function error($code)
    {
        return ["code" => $code, "msg" => self::ERROR_CODE[$code]];
    }

    /**
     * 添加评论/回复评论
     * 运营中台接口文档地址http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * 接口名称 comment/addComment
     * @param array $params
     * @return array
     */
    public function add(array $params)
    {
        $url =  config("interface", "service.comment.add");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->post($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(2);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 评论列表
     * 运营中台接口文档地址http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * 接口名称 comment/getCommentByDocId
     * @param array $params
     * @return array
     */
    public function getListByDocid(array $params)
    {
        $url =  config("interface", "service.comment.list_by_docid");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(3);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 评论点赞
     * 运营中台接口文档地址http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * 接口名称 like/getLikeNumber
     * @param array $params
     * @return array
     */
    public function like(array $params)
    {
        $url =  config("interface", "service.comment.like");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->post($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(4);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 表情
     * 营中台接口文档地址http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * 接口名称 expression/getAppExpression
     * @param int $businessId 业务ID运营中台定义
     * @return array
     */
    public function getExpression(int $businessId)
    {
        $params = [
            'businessId'=>$businessId
        ];
        $url =  config("interface", "service.comment.expression");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(5);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 评论删除
     * 运营中台接口文档地址http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * 接口名称 comment/deleteComment
     * @param array $params
     * @return array
     */
    public function delete(array $params)
    {
        $url =  config("interface", "service.comment.delete");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->post($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(6);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 根据userid和docid获取用户评论列表
     * 运营中台接口文档地址 http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * 接口名称 comment/getCommentByUserId
     * @param array $params 请求参数 详细的参数看运营中台接口文档
     * @return
     */
    public function getLikeListByUseridByDocid(array $params)
    {
        $url =  config("interface", "service.comment.like_list_byuserid_bydocid");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(3);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 根据commentId获取评论详情
     * @doc 运营中台接口文档地址http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * @doc method 接口名称 根据commentId获取评论 接口地址 comment/getCommentByCommentId
     * @param array $params 请求参数 详细的参数看运营中台接口文档
     * @return array
     */
    public function getDetailByTopCommentId(array $params)
    {
        $url =  config("interface", "service.comment.list_bytop_commentid");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(3);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 根据userid获取用户评论列表
     * 运营中台接口文档地址http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * 接口名称 comment/getCommentByUserId
     * @param array $params
     * @return array
     */
    public function getListByUserid(array $params)
    {
        $url =  config("interface", "service.comment.list_byuserid");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(3);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 根据userid和docid获取用户评论列表方法
     * 运营中台接口文档地址http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * 接口名称 comment/getCommentByUserId
     * @param array $params
     * @return array
     */
    public function getListByUseridByDocid(array $params)
    {
        $url =  config("interface", "service.comment.list_byuserid_bydocid");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(3);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 根据docid获取评论的数量
     * 运营中台接口文档地址http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * 接口名称 comment/getCommentNumberByDocId
     * @param int $businessId 业务ID
     * @param string $docId 文章ID
     * @return array
     */
    public function getCommentNumByDocid(int $businessId, string $docId)
    {
        $params = [
            'businessId'=>$businessId,
            'docId'=>$docId
        ];
        $url =  config("interface", "service.comment.num_bydocid");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(3);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 根据commentId获取回复数量
     * 运营中台接口文档地址http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=59701834
     * 接口名称 comment/getCommentNumberByCommentId
     * @param int $businessId 业务ID
     * @param int $commentId 评论ID
     * @return array
     */
    public function getReplyNumByCommentId(int $commentId, int $businessId)
    {
        $params = [
            'commentId'=>$commentId,
            'businessId'=>$businessId
        ];
        $url =  config("interface", "service.comment.replynum_bycommentid");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(3);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 根据userid获取回复数量
     * @param array $params
     * @return array
     */
    public function getReplyNumByUserid(array $params)
    {
        $url =  config("interface", "service.comment.replynum_byuserid");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(3);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 根据commentid获取点赞数量方法
     * @param int $commentId
     * @param int $businessId
     * @param int $type
     * @return array
     */
    public function getLikeNumByCommentId(int $commentId, int $businessId, int $type)
    {
        $params = [
            'commentId'=>$commentId,
            'businessId'=>$businessId,
            'type'=>$type
        ];
        $url =  config("interface", "service.comment.likenum_bycommentid");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(3);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 根据userid获取点赞数量
     * @param int $userid
     * @param int $businessId
     * @param int $type
     * @return array
     */
    public function getLikeNumByUserid(int $userid, int $businessId, int $type)
    {
        $params = [
            'userId'=>$userid,
            'businessId'=>$businessId,
            'type'=>$type
        ];
        $url =  config("interface", "service.comment.likenum_byuserid");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->get($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(3);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 评论举报
     * @param array $params
     * @return array
     */
    public function report(array $params)
    {
        $url =  config("interface", "service.comment.report");
        if (empty($url)) {
            return $this->error(1);
        }
        $res = (new Request())->post($url, $params);
        if (!isset($res['response']['data']) || empty($res['response']['data'])) {
            return $this->error(7);
        }
        return $this->success($res['response']['data']);
    }

    /**
     * 异步并发请求文章评论和文章下用户对评论点赞
     * @param array $params [ 0 => [根据docid获取列表的参数], 1 => [根据用户ID+文章ID获取点赞评论]]
     * @return array
     */
    public function getDocListAndUserLike($params)
    {
        if (empty($params)) {
            return $this->error(1);
        }
        $urls = [
            0 => config("interface", "service.comment.list_by_docid"),
            1 => config("interface", "service.comment.like_list_byuserid_bydocid")
        ];
        $res = (new Request())->concurrencyGet($urls, $params);
        if (empty($res) || !is_array($res)) {
            return $this->error(3);
        }
        $ret = [];
        foreach ($res as $k => $v) {
            $ret[$k]['data'] = $v['response']['data'] ?? [];
        }
        return $this->success($ret);
    }

    /**
     * 批量获取文章的评论数
     * @param int $businessId 业务ID
     * @param array $docids   多个文章ID
     * @return array
     */
    public function getCommentNumByDocids(int $businessId, array $docids)
    {
        $return_data = [];
        $url =  config("interface", "service.comment.num_bydocid");
        if (!$url) {
            return $return_data;
        }
        $urls = $params = [];
        foreach ($docids as $docid) {
            $urls[] = $url;
            $params[] = [
                'businessId'=>$businessId,
                'docId'=>$docid
            ];
        }
        $res = (new Request())->concurrencyGet($urls, $params);
        if (empty($res) || !is_array($res)) {
            return $this->error(3);
        }
        $ret = [];
        foreach ($docids as $k => $docid) {
            if (isset($res[$k]['response']['data']) && !empty($res[$k]['response']['data'])) {
                $ret[$docid] = $res[$k]['response']['data'];
            } else {
                $ret[$docid] = [];
            }
        }
        return $this->success($ret);
    }
}
