<?php

/**
 * [Discuz!] (C)2001-2099 Discuz! Team
 * This is NOT a freeware, use is subject to license terms
 * https://license.discuz.vip
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class table_forum_activityapply extends discuz_table {
	public static function t() {
		static $_instance;
		if(!isset($_instance)) {
			$_instance = new self();
		}
		return $_instance;
	}

	public function __construct() {

		$this->_table = 'forum_activityapply';
		$this->_pk = 'applyid';

		parent::__construct();
	}

	public function fetch_info_for_user($uid, $tid) {
		return DB::fetch_first('SELECT * FROM %t WHERE tid=%d AND uid=%d', [$this->_table, $tid, $uid]);
	}

	public function delete_for_user($uid, $tid) {
		DB::query('DELETE FROM %t WHERE tid=%d AND uid=%d', [$this->_table, $tid, $uid]);
	}

	public function delete_by_tid($tids) {
		return DB::delete($this->_table, DB::field('tid', $tids));
	}

	public function delete_for_thread($tid, $applyids = []) {
		if($applyids) {
			$pksql = ' AND '.DB::field('applyid', $applyids);
		}
		DB::query("DELETE FROM %t WHERE tid=%d $pksql", [$this->_table, $tid]);
	}

	public function fetch_count_for_thread($tid) {
		return DB::result_first("SELECT COUNT(*) FROM %t WHERE tid=%d AND verified='1'", [$this->_table, $tid]);
	}

	public function fetch_all_for_thread($tid, $start = 0, $limit = 100, $uid = 0, $master = 0) {
		$verifiedsql = empty($master) ? ' AND verified=1' : '';
		if(intval($uid)) {
			$verifiedsql .= ' AND uid='.intval($uid);
		}
		return DB::fetch_all("SELECT * FROM %t WHERE tid=%d $verifiedsql ORDER BY dateline DESC".DB::limit($start, $limit), [$this->_table, $tid]);
	}

	public function update_verified_for_thread($verified, $tid, $applyid) {
		DB::query('UPDATE %t SET verified=%d WHERE tid=%d AND applyid IN (%n)', [$this->_table, $verified, $tid, $applyid]);
	}
}

