<?php
include('action.abs.php');
class user_verify extends action {
		var $db = null;
		function user_verify() {
				parent :: action();
				$this->db = APP :: ADP('db');
				$this->db->setTable(T_USER_VERIFY);
		}

		function add() {
				if ($this->_isPost()) {
					$user = V('p:user');
					if (!$user) {
						$this->_error('请填写要认证的用户昵称', array('add'));
					}
					$user = trim($user, ', ');
					$user = explode(',', $user);
					if (sizeof($user) <=0 ) {
						$this->_error('没有可以插入的数据', array('add'));
					}
					$data = array();
					foreach ($user as $v) {
						$data[] = '("' . mysql_escape_string($v). '","' . time() . '","' .$this->_getUid(). '")';
					}
				
					$data = implode(',', $data);
					
					$sql = 'INSERT IGNORE INTO ' . $this->db->getTable(T_USER_VERIFY) . '(`nick`,`add_time`,`operator`) VALUES' . $data;
					
					$rs = $this->db->execute($sql);
					if ($rs && $this->db->getAffectedRows() > 0 ) {
							$cache = CACHE::get(CACHE_USER_VERIFY);
							if (!is_array($cache) || empty($cache)) {
									$this->_synchronousMemcache();
							} else {
								$size = sizeof($cache);
								foreach($user as $v) {
									$cache[$v] = '1';
								}
								if (sizeof($cache) > $size) {
									CACHE :: set(CACHE_USER_VERIFY, $cache);
								}
							}
					}
					$this->_redirect('userList');
				}
				TPL :: display('admin/user_verify/user_verify_add', '', 0, false);
		}

		function userList() {
				$page = V('g:page', 1);
				$each = V('g:each', 20);
				$keyword = V('g:keyword', '');
				$offset = ($page -1) * $each;

				$select_count = 'SELECT COUNT(*) FROM ' . $this->db->getTable(T_USER_VERIFY) ;
				$count = $this->db->getOne($select_count);

				$cache  = CACHE :: get(CACHE_USER_VERIFY);
				if (!is_array($cache) || empty($cache)) {
					$this->_synchronousMemcache();
				}

				$pager = APP :: N('pager');
				$page_param = array('currentPage'=> $page, 'pageSize' => $each, 'recordCount' => $count, 'linkNumber' => 10);
				$pager->setParam($page_param);
				TPL :: assign('pager', $pager->makePage());

				$sql = 'SELECT a.*,b.name FROM ' . $this->db->getTable(T_USER_VERIFY) . ' AS a LEFT JOIN ' . $this->db->getTable(T_ADMIN_USER) . ' AS b ON a.operator=b.id ORDER BY a.id DESC LIMIT ' . $offset . ',' . $each;
				$rs = $this->db->query($sql);
				TPL :: assign('list', $rs);
				TPL :: display('admin/user_verify/user_verify_list', '', 0, false);
		}

		function delete() {
				$id = V('r:id');
				if (!is_array($id)) {
					$id = (string)$id;
					$id = trim($id, ', ');
					$id = explode(',', $id);
				}
				$data = $this->db->query('SELECT nick FROM ' . $this->db->getTable() . ' WHERE id IN(' . implode(',', $id) . ')');
				$rs = $this->db->delete($id);

				// 同步cache
				if ($rs && $this->db->getAffectedRows()) {
					$cache = CACHE :: get(CACHE_USER_VERIFY);
					if (!is_array($cache) || empty($cache)) {
							$this->_synchronousMemcache();
					} else {
						$size = sizeof($cache);
						foreach ($data as $row) {
							unset($cache[$row['nick']]);
						}
						if ($size != sizeof($cache)) {
							CACHE :: set(CACHE_USER_VERIFY, $cache);
						}
					}
				}
				$this->_redirect('userList');
		}
		/**
		 * 同步Memcache
		 */
		function _synchronousMemcache($type = null) {
				$sql = 'SELECT `nick` FROM ' . $this->db->getTable();
				$data = $this->db->query($sql);
				$cache = array();
				while($r = array_pop($data)) {
					$cache[$r['nick']] = 1;
				}
				CACHE :: set(CACHE_USER_VERIFY, $cache);
		}

}
