<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/7/30
 * Time: 下午3:47
 */

defined('InShopNC') or exit('Access Invalid!');

class ugc_commentModel extends Model
{
    public function __construct()
    {
        parent::__construct('ugc_comment');
    }
    
    public function top_counts($special_id)
    {
        return $this->table('ugc_comment')->where("comment_id=top_id and special_id={$special_id}")->count();
    }

    public function comment_counts($special_id,$comment_id)
    {
        return $this->table('ugc_comment')->where("top_id={$comment_id} and special_id={$special_id}")->count();
    }

    public function comments_counts($special_id,$comment_ids)
    {
        return $this->table('ugc_comment')->field('count(*) - 1 as nc_count,comment_id')->where(array('special_id' => $special_id,'top_id' => ['in',$comment_ids]))->group('top_id')->select();
    }

    public function getSubCommentList($special_id,$comment_id,$field='*',$page_size = 0,$total_count = 0,$lock = false)
    {
        return $this->table('ugc_comment')->field($field)->where("top_id={$comment_id} and special_id={$special_id}")->order('top_id desc')->page($page_size, $total_count)->lock($lock)->select();
    }

    public function getTopCommentList($special_id,$field='*',$page_size = 0,$total_count = 0,$lock = false)
    {
        return $this->table('ugc_comment')->field($field)->where("comment_id=top_id and special_id={$special_id}")->order('top_id desc')->page($page_size, $total_count)->lock($lock)->select();
    }

    public function getCommentList($cond,$field='*',$limit=false,$lock = false)
    {
        return $this->table('ugc_comment')->field($field)->where($cond)->order('top_id desc,comment_id asc')->limit($limit)->lock($lock)->select();
    }

    public function getCommentByID($comment_id,$lock=false)
    {
        return $this->table('ugc_comment')->field('*')->lock($lock)->find($comment_id);
    }

    public function comment($special_id,$user_id,$content)
    {
        $data['special_id'] = $special_id;
        $data['likes']      = 0;
        $data['user_id']    = $user_id;
        $data['response_id'] = 0;
        $data['content'] = $content;
        $data['addtime'] = time();

        $ret = $this->insert($data);
        if($ret != false) {
            $this->table('ugc_comment')->where(['comment_id' => $ret])->update(['top_id' => $ret]);
            $this->table('mb_special')->where(['special_id' => $special_id])->update(array('comments' => ['exp', 'comments + 1']));
        }
        return $ret;
    }

    private function top_id($comment_id)
    {
        $item = $this->getCommentByID($comment_id);
        if(empty($item)) {
            $item = $this->getCommentByID($comment_id,true);
        }

        if(empty($item)) {
            return 0;
        } else {
            return intval($item['top_id']);
        }
    }

    public function respond_comment($special_id,$comment_id,$user_id,$content)
    {
        $data['special_id'] = $special_id;
        $data['likes']      = 0;
        $data['user_id']    = $user_id;
        $data['response_id'] = $comment_id;
        $data['content'] = $content;
        $data['addtime'] = time();
        $data['top_id'] = $this->top_id($comment_id);

        $ret = $this->table('ugc_comment')->insert($data);
        if($ret != false) {
            $this->table('ugc_comment')->where(['special_id' => $special_id,'comment_id' => $comment_id])->update(['sub_comments' => ['exp', 'sub_comments + 1'] ]);
        }

        return $ret;
    }
}