<?php

/**
 * @function 点赞公共服务
 * @author zhangxiaojing-inc.yidian.com
 * @date 2021-02-08
 * TODO:是否需要修改redis 是否需要文章点赞数增加 是否允许未发布状态的文章点赞
 */

namespace Api\PhpServices\ThumbsUp;

use Api\PhpServices\ThumbsUp\models\ThumbsUp as ThumbsUpModel;
use Api\PhpUtils\Common\IP;

/**
 * Class ThumbsUp
 * @package Api\PhpServices\ThumbsUp
 */
class ThumbsUp
{
    protected $model;

    /**
     * 错误码
     */
    const ERROR_CODE = [
        1 => '参数有误',
        2 => '已点赞',
        3 => '点赞失败',
        4 => '取消点赞失败',
        5 => '查询点赞列表失败',
        6 => '获取点赞数失败'
    ];

    public function __construct()
    {
        $this->model = ThumbsUpModel::getInstance();
    }

    /**
     * 是否已点赞
     * @param string $appid
     * @param int $userid
     * @param string $docid
     * @return bool
     */
    public function hasThumbsUp($appid, $userid, $docid)
    {
        $params = [
            'userid'    =>  intval($userid),
            'appid'     =>  $appid,
            'docid'     =>  $docid,
        ];
        $has = $this->model->count($params);
        if ($has > 0) {
            return true;
        }
        return false;
    }

    /**
     * 点赞
     * @param string $appid
     * @param int $userid
     * @param string $docid
     * @return array
     */
    public function thumbsUp($appid, $userid, $docid)
    {
        if (!$this->hasThumbsUp($appid, $userid, $docid)) {
            $insert = [
                'id'            => md5($userid . '-doc-' . $docid . '-appid-' . $appid),
                'userid'        => intval($userid),
                'docid'         => $docid,
                'appid'         => $appid,
                'ip'            => IP::ip(),
                'createTime'    => $_SERVER['REQUEST_TIME'],
            ];
            $res = $this->model->add($insert);
            if ($res) {
                return $this->success();
            }
            return $this->error(3);
        }
        return $this->success();
    }

    /**
     * 批量取消&取消点赞
     * @param string $appid
     * @param int $userid
     * @param array $doc_ids
     * @return array
     */
    public function del($appid, $userid, array $doc_ids)
    {
        $res = $this->model->delete([
            'appid' => $appid,
            'userid'    => intval($userid),
            'docid'        => [
                '$in' => $doc_ids
            ]
        ]);
        if ($res) {
            return $this->success();
        }
        return $this->error(4);
    }

    /**
     * 赞过的列表
     * @param string $appid
     * @param int $userid
     * @param int $start
     * @param int $limit
     * @param array $order
     * @return array
     */
    public function getList($appid, $userid, $start = 0, $limit = 20, $order = ["createTime" => -1])
    {
        //去查询点赞的表。返回对应的docid的list
        $params = [
            'userid' => intval($userid),
            'appid' => $appid,
        ];
        $fields = [
            '_id'   => 0
        ];
        $sort = $order;
        $limit = [
            'start' => $start,
            'count' => $limit
        ];
        $res = $this->model->getMany($params, $fields, $sort, $limit);
        return $this->success($res);
    }

    /**
     * 获取用户赞数
     * @param string $appid
     * @param int $userid
     * @return array
     */
    public function userThumbsUpNum($appid, $userid)
    {
        $params = [
            'userid' =>  intval($userid),
            'appid' =>  $appid
        ];
        return $this->model->count($params);
    }

    /**
     * 用户是否已点赞文章（批量）
     * @param array $userid
     * @param array $doc_ids
     * @return array
     */
    public function isThumbsUp($userid, array $doc_ids)
    {
        $res = [];
        if(empty($userid) || !is_array($doc_ids) || empty($doc_ids)){
            return $res;
        }
        $params = [
            'userid' =>  $userid,
            'docid'        => [
                '$in' => $doc_ids
            ]
        ];
        $fields = [
            '_id'   => 0,
            'docid' => 1
        ];
        $res = $this->model->getMany($params, $fields);
        return $res;
    }

    /**
     * 成功响应
     * @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]];
    }
}
