<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/8/16
 * Time: 下午8:17
 */

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

class ugc_answerModel extends Model
{
    public function __construct()
    {
        parent::__construct('special_answer');
    }
    public function counts($special_id)
    {
        $ret = $this->where("special_id={$special_id}")->count();
        return intval($ret);
    }

    public function answer_list($special_id,$field='*',$page_size = 0,$total_count = 0,$lock = false)
    {
        return $this->field($field)->where(['special_id' => $special_id])->order('correct_num desc')->page($page_size, $total_count)->lock($lock)->select();
    }
    public function lt_count($special_id,$correct_num)
    {
        return $this->where(array('special_id' => $special_id, 'correct_num' =>['lt', $correct_num]))->count();
    }

    public function getAnswer($specialid,$memberid)
    {
        $items = $this->field('*')->where(['special_id' => $specialid,'member_id' => $memberid])->select();
        if(empty($items)) {
            $items = $this->field('*')->where(['special_id' => $specialid,'member_id' => $memberid])->lock(true)->select();
        }

        if(!empty($items)) {
            return $items[0];
        }

        return $items;
    }
}