<?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_common_tagitem extends discuz_table {
	public static function t() {
		static $_instance;
		if(!isset($_instance)) {
			$_instance = new self();
		}
		return $_instance;
	}

	public function __construct() {
		$this->_table = 'common_tagitem';
		$this->_pk = '';
		parent::__construct();
	}

	public function replace($tagid, $itemid, $idtype) {
		return DB::query('REPLACE INTO %t (tagid, itemid, idtype, created_at) VALUES (%d, %d, %s, %d)', [$this->_table, $tagid, $itemid, $idtype, TIMESTAMP]);
	}

	public function select($tagid = 0, $itemid = 0, $idtype = '', $orderfield = '', $ordertype = 'DESC', $limit = 0, $count = 0, $itemidglue = '=', $returnnum = 0) {
		$data = $this->make_where($tagid, $itemid, $idtype, $itemidglue);
		$ordersql = $limitsql = '';
		if($orderfield) {
			$ordersql = ' ORDER BY '.DB::order($orderfield, $ordertype);
		}
		if($limit || $count) {
			$limitsql = DB::limit($limit, $count);
		}
		if($data) {
			if($returnnum) {
				return DB::result_first('SELECT count(*) FROM %t WHERE '.$data['where'], $data['data']);
			}
			return DB::fetch_all('SELECT * FROM %t WHERE '.$data['where'].$ordersql.$limitsql, $data['data']);
		} else {
			return false;
		}
	}

	public function delete($val, $unbuffered = false, $null = '') {
		
		if(defined('DISCUZ_DEPRECATED')) {
			throw new Exception('NotImplementedException');
			return parent::delete($val, $unbuffered);
		} else {
			$unbuffered = $unbuffered === false ? 0 : $unbuffered;
			return $this->delete_tagitem($val, $unbuffered, $null);
		}
	}

	public function delete_tagitem($tagid = 0, $itemid = 0, $idtype = '') {
		$data = $this->make_where($tagid, $itemid, $idtype);
		if($data) {
			return DB::query('DELETE FROM %t WHERE '.$data['where'], $data['data']);
		} else {
			return false;
		}
	}

	private function make_where($tagid = 0, $itemid = 0, $idtype = '', $itemidglue = '=') {
		$wheresql = ' 1';
		$data = [];
		$data['data'][] = $this->_table;
		if($tagid) {
			$wheresql .= !is_array($tagid) ? ' AND tagid=%d' : ' AND tagid IN (%n)';
			$data['data'][] = $tagid;
		}
		if($itemid) {
			$wheresql .= !is_array($itemid) ? ' AND ' .DB::field('itemid', $itemid, $itemidglue) : ' AND ' .DB::field('itemid', $itemid);
		}
		if($idtype) {
			$wheresql .= ' AND idtype=%s';
			$data['data'][] = $idtype;
		}
		if($wheresql == ' 1') {
			return false;
		}
		$data['where'] = $wheresql;
		return $data;
	}

	public function unique($tagid, $itemid, $idtype) {
		DB::query('DELETE FROM %t WHERE tagid<>%d AND itemid=%d AND idtype=%s', [$this->_table, $tagid, $itemid, $idtype]);
	}

	public function merge_by_tagids($newid, $tagidarray) {
		if(!is_array($tagidarray)) {
			$tagidarray = [$tagidarray];
		}
		DB::query('UPDATE %t SET tagid=%d WHERE tagid IN (%n)', [$this->_table, $newid, $tagidarray]);
	}

	public function count_by_tagid($tagid) {
		return DB::result_first('SELECT count(*) FROM ' .DB::table('common_tagitem')." WHERE tagid='".intval($tagid)."'");
	}

	
	public function fetch_all_by_tagid_and_time($tagid, $time) {
		return DB::fetch_all('SELECT * FROM %t WHERE tagid=%d AND created_at>=%d ORDER BY created_at DESC', [$this->_table, $tagid, $time]);
	}

	
	public function fetch_hot_tags($time, $limit = 20) {
		return DB::fetch_all('SELECT tagid, COUNT(*) AS count FROM %t WHERE created_at>=%d GROUP BY tagid ORDER BY count DESC LIMIT %d', [$this->_table, $time, $limit]);
	}
}
