<?php
/**
*	玩家反馈
*/
defined( 'IN_WEB') or die( 'Include Error!');

class Feedback
{
	
	protected $table = '';
	public function __construct()
	{
		$this->table = otable::feedback2();
	}


	public function getList($param)
	{
		$data = array('list' => array(), 'total' => 0);
		$stime = oo::functions()->uint($param['stime']);	
		$etime = oo::functions()->uint($param['etime']);
		$uid = oo::functions()->uint($param['uid']);
		$replyStatus = oo::functions()->uint($param['replyStatus']);
        $whereStatus = '';
		if($replyStatus == 1) {
            $whereStatus .= ' AND freplystatus = 1 ';
        }
        if($replyStatus == 2) {
            $whereStatus .= ' AND freplystatus = 0 ';
        }
        if(!empty($uid)) {
		    $whereStatus .= " AND fuid = {$uid} ";
        }

		if( empty($stime) ){$stime = strtotime('-7 days 0:0:1');}//默认为7天之前的
		if( empty($etime) ){$etime = time();}

		$page = oo::functions()->uint($param['page']);//起始页数
		$limit = oo::functions()->uint($param['limit']);//每页数量

		$table = $this->table;
		$sql = " SELECT * FROM {$table} WHERE ftime >= {$stime} AND ftime <= {$etime} {$whereStatus} group by uid ORDER BY freplystatus ASC, ftime DESC LIMIT $page, $limit ";
		$list = oo::commonOprDb('common')->getAll($sql, MYSQLI_ASSOC);

		//查看总数
		$sqlTotal = " SELECT COUNT(*) AS t FROM {$table} WHERE ftime >= {$stime} AND ftime <= {$etime} {$whereStatus} ";
		$totalArr = oo::commonOprDb('common')->getOne($sqlTotal, MYSQLI_ASSOC);
		$total = $totalArr['t'];

		$data['list'] = $list;
		$data['total'] = $total;
		die( json_encode($data) );
	}


	/**
	*	回复玩家反馈
	*/
	public function actionfeedback($param)
	{
		$fid = oo::functions()->uint($param['fid']);
		$uid = oo::functions()->uint($param['uid']);
		$reply = oo::functions()->escape($param['reply']);
		$table = $this->table;
		$sql = " UPDATE {$table} SET freply = '{$reply}' , freplystatus = 1 WHERE fid = {$fid} LIMIT 1 ";
		oo::commonOprDb('common')->query($sql);
		$num = oo::commonOprDb('common')->affectedRows();
		$data['code'] = ( $num > 0 ) ? 1 : 0;
		if( $num > 0 ){
			$data['code'] = 1;
			//发送消息通知
			if( $uid > 1 ){
				oo::commonOprModel('usernews')->setUserMsg($uid, $reply);
			}
		}else{
			$data['code'] = 0;
		}
		die( json_encode($data) );
	}

	/**
	 * 更新玩家反馈
	 */
	public function updateFeedback($param)
    {
        $fid = oo::functions()->uint($param['fid']);
        $replyStatus = oo::functions()->uint($param['replyStatus']);
        if(empty($fid)) {
            return 0;
        }
        $replyStatus = $replyStatus == 2 ? 0 : 1;
        $table = $this->table;
        $sql = " UPDATE {$table} SET freplystatus = {$replyStatus} WHERE fid = {$fid} LIMIT 1 ";
        oo::commonOprDb('common')->query($sql);
        $num = oo::commonOprDb('common')->affectedRows();
        return $num;
    }

	/**
	*	删除玩家反馈
	*/
	public function delfeedback($param)
	{
		$fid = oo::functions()->uint($param['fid']);
		$table = $this->table;
		$sql = " DELETE FROM {$table} WHERE fid = {$fid} LIMIT 1 ";
		oo::commonOprDb('common')->query($sql);
		$num = oo::commonOprDb('common')->affectedRows();
		$data['code'] = ( $num > 0 ) ? 1 : 0;
		die( json_encode($data) );
	}
	
	/**
	*	用户反馈 前端没有传版本号，临时查询版本号方法
	*/
	public function getUsersInfo($param){
		//$uids = oo::functions()->uint($param['uidss']);
		$uids = $param['uidss'];
		$uids = json_decode($uids);
		$userinfo = array();
        oo::commonOprModel('member')->getuserinfomulti(array('keylist' => $uids),$userinfo);
        $ret['data'] = $userinfo;
        return json_encode($ret);
	}
	
}