<?php

namespace Api\PhpServices\Collect;

use Api\PhpServices\Collect\models\Collect as CollectModels;
use Api\PhpUtils\Common\IP;

/**
 * Class Doc
 * @package Api\PhpServices\Collect
 */
class Collect
{
    /**
     * 错误码
     */
    const ERROR_CODE = [
        1 => "已收藏",
        2 => "收藏失败",
        3 => "取消收藏失败"
    ];

    /**
     * 文章类型的收藏 type 字段定值
     */
    const DOC_TYPE = 'doc';

    /**
     * mongo 模型
     * @var object
     */
    private $model;


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


    /**
     * 收藏文章
     * @param $userid
     * @param $docid
     * @return array
     */
    public function addDoc($userid, $docid, $ctype,  array $data = []){
        // 查重
        $params = [
            'userid'    =>  $userid,
            'type'      =>  self::DOC_TYPE,
            'docid'     =>  $docid,
        ];
        $has = $this->model->count($params);
        if( $has > 0 ){
            return $this->error(1);
        }

        // 存库
        $insert = array_merge($data, [
            'uid'            => md5($userid.'-doc-'.$docid),
            'userid'        => $userid,
            'docid'         => $docid,
            'type'          => self::DOC_TYPE,
            'ctype'         => $ctype,
            'createTime'    => $_SERVER['REQUEST_TIME'],
            'updateTime'    => $_SERVER['REQUEST_TIME'],
            'ip'            => IP::ip()
        ]);

        $res = $this->model->add($insert);
        if($res){
            return $this->success();
        }
        return $this->error(2);
    }


    /**
     * 批量取消收藏
     */
    public function del($userid, array $ids){
        $res = $this->model->delete([
            'userid'    => $userid,
            'docid'        => [
                '$in' => $ids
            ]
        ]);
        if( is_int($res) ){
            return $this->success();
        }
        return $this->error(3);
    }


    /**
     * 收藏列表
     * @param $userid
     * @param int $start
     * @param int $limit
     * @param int[] $order
     * @return
     */
    public function list($userid, $start = 0, $limit = 20, $order = ["createTime"=>-1]){
        $params = [
            'userid' => $userid
        ];
        $fields = [
            '_id'   => 0
        ];
        $sort = $order;
        $limit = [
            'start' => $start,
            'count' => $limit
        ];
        $res = $this->model->getMany($params, $fields, $sort, $limit);
        return $res;
    }


    /**
     * 获取用户收藏数
     * @param $userid
     */
    public function collectNum($userid){
        $params = [
            'userid' =>  $userid
        ];
        return $this->model->count($params);
    }

    /**
     * 用户是否已收藏文章（批量）
     * @param $userid
     * @param array $docs
     */
    public function isCollected($userid, array $docs){
        $params = [
            'userid' =>  $userid,
            'docid'        => [
                '$in' => $docs
            ]
        ];
        $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]];
    }
}