<?php

defined('IN_WEB') or die('Include Error!');

/**
 * 召回相关
 */
class Recall {

	private $_recallmoney = 0;//发送召回奖励的金币
	private $_backChips = 0; //回归一个玩家送多少金币
	protected $configFriend = array();

	public function __construct(){
		if( file_exists( PATH_CFG.'config.friend.php' ) ){
			$this->configFriend = include PATH_CFG.'config.friend.php';
		}
		$this->_recallmoney = $this->configFriend['recallSendChips'] ?? 500;
		$this->_backChips = $this->configFriend['recallBackChips'] ?? 50000;
	}

	/**
	 * @param type $param
	 * @return type
	 */
	public function getRecallID($param) {
		$array = array(
			'code' => 1,
			'id' => 0,
			'sk' => '',
		);
		$uid = oo::functions()->uint($param['uid']);
		if (empty($uid)) {
			$array['code'] = -1;
			$array['line'] = __LINE__;
			return json_encode($array);
		}

		$array['u'] = $uid;
		$array['id'] = oo::commonOprModel('recall')->getRecallID($uid);
		$array['sk'] = oo::commonOprModel('recall')->getRecallSkey($uid, $array['id']);
		$pushNews = oo::commonOprModel('recall')->getPushMsg();
		$array['msg'] = $pushNews['content'];
		return json_encode($array);
	}

	/**
	 * 客户端发送召回请求后调用,领取发送召回奖励
	 * @param type $param
	 * @return type
	 */
	public function report($param) {
		$array = array('code' => 1);
		$uid = oo::functions()->uint($param['uid']);
		if (empty($uid)) {
			$array['code'] = -1;
			return json_encode($array);
		}

		$data = isset($param['data']) ? htmlspecialchars($param['data']) : '';
		if (empty($data)) {
			$array['code'] = -2;
			return json_encode($array);
		}

		$list = isset($param['list']) ? htmlspecialchars($param['list']) : '';
		if (empty($list)) {
			$array['code'] = -3;
			return json_encode($array);
		}

		$requestid = isset($param['requestid']) ? htmlspecialchars($param['requestid']) : '';
		if (empty($requestid)) {
			$array['code'] = -4;
			return json_encode($array);
		}

		$recall = oo::commonOprModel('recall');
		$sig = isset($param['sig']) ? htmlspecialchars($param['sig']) : '';
		if ($recall->validateSig($sig, $list) === false) {
			$array['code'] = -5;
			return json_encode($array);
		}

		$check = $recall->check($data);
		if ($check === false) {
			$array['code'] = -6;
			return json_encode($array);
		}

		$result = $recall->setRecord($check[2], $uid, $list, $requestid);
		if ($result === false) {
			$array['code'] = -7;
			return json_encode($array);
		}

		$noreward = oo::functions()->uint($param['noreward']);//改参数是标识从 "一键召回" 来发送召回的
		if( $noreward == 1 ){
			$array['money'] = 0;
		}else{
			//判断玩家可以获得多少奖励
			$res = $recall->getRewardChip($uid, $list);
			if ($res === false) {
				$array['code'] = -8;
				$array['msg'] = 'get chips error.';
				return json_encode($array);
			}
			$money = (int) $res['chips'];
			$logArr = array('sender' => $uid, 'sendCount' => $res['count'], 'list' => $list, 'reward' => $money, 'reason' => 'send_recall_request', 'isok' => 0);

			//如果玩家可以获得奖励，给玩家发奖
			if (is_numeric($money) && $money > 0) {
				$reason = 1016;
				$ret = $recall->reward($uid, $money, $reason);
				if (!$ret) {
					$logArr['isok'] = 0;
					oo::logs()->debug3($logArr, 'recall_report.log');

					$array['code'] = -9;
					return json_encode($array);
				}
			}
			$logArr['isok'] = 1;
			oo::logs()->debug3($logArr, "recall_report.log");
			$array['count'] = $res['count'];
			$array['money'] = $money;
		}
		return json_encode($array);
	}

	/* 给好友发送邀请奖励 */

//	public function update($param) {//(wsc-2020.7.30-目测没用)
//		$array = array('code' => 1, 'ret' => 0);
//		$uid = oo::functions()->uint($param['uid']);
//		if (empty($uid)) {
//			$array['code'] = -1;
//			return json_encode($array);
//		}
//		$data = isset($param['data']) ? htmlspecialchars($param['data']) : '';
//		if (empty($data)) {
//			$array['code'] = -2;
//			return json_encode($array);
//		}
//		$recall = oo::commonOprModel('recall');
//		$check = $recall->check($data);
//		if ($check === false) {
//			$array['code'] = -3;
//			return json_encode($array);
//		}
//		$sender = $check[1];
//		$recallId = $check[2];
//		$record = $recall->getRecord($recallId, $sender);
//		if ($record === false) {
//			return json_encode(array('ret' => -4, 'line' => __LINE__, 'msg' => 'can not get record.'));
//		}
//		$recallerInfo = array();
//		foreach ((array) $record['becaller'] as $value) {
//			if ($uid == $value['uid']) {
//				$recallerInfo = $value;
//			}
//		}
//		if (empty($recallerInfo)) {
//			return json_encode(array('ret' => -4, 'line' => __LINE__, 'msg' => 'uid not be recall.'));
//		}
//		if(isset($record['ip'])){
//			$hasReward = $recall->hasReward($sender, $recallerInfo,$record['ip']);
//
//		}else {
//			$hasReward = $recall->hasReward($sender, $recallerInfo);
//		}
//		if ($hasReward === false) {
//			return json_encode(array('ret' => 0, 'line' => __LINE__, 'msg' => "sender:{$sender},recaller:{$uid} has reward."));
//		}
//		//给发送召回的用户发奖励
//		$backChips = (int) $this->_backChips;
//		$logArr = array('recaller' => $uid, 'sender' => $sender, 'reward' => $backChips, 'reason' => 'recall_back_success', 'isok' => 0);
//
//		$reason = 1017;
//		$ret = $recall->reward($sender, $backChips, $reason);
//		if (!$ret) {
//			$logArr['isok'] = 0;
//			oo::logs()->debug3($logArr, 'recall_err');
//
//			$array['code'] = -9;
//			return json_encode($array);
//		}
//		$logArr['isok'] = 1;
//		/* 给邀请的用户发送系统消息 */
//		$userInstance = oo::commonOprModel('member');
//		$userinfo = $userInstance->getUserInfo($uid);
//		if (empty($userinfo)) {
//			return json_encode(array('ret' => 0,'code' => -10, 'msg' => 'get user info error.'));
//		}
//
//		//发消息
//		$lang = include_once PATH_LNG;
//		oo::commonOprModel('usernews')->setUserMsg($sender, $lang['recall_backmsg'].$backChips, 1, 1, [], 1, '', 'recallBackmsg', [$backChips]);
//
//		return json_encode($array);
//	}


	/**
	 * 一键发召回功能
	 * @param $param
	 * @return string
	 */
	public function getCanRecallList($param){
		$data = array( 'code' => -1, 'money' => 0, 'fblist' => [] );
		$uid = oo::functions()->uint($param['uid']);//发送召回
		$type = oo::functions()->uint($param['type']);//列表
		if( empty($uid) ){
			return json_encode($data);
		}
		$type = ($type == 0) ? 1: $type;
		$data['code'] = 1;
		$fblist = array();
		$list = oo::commonOprModel('recall')->getCanRecallUserNum($uid, 2, $type, $fblist);
		if( empty($list) ){
			return json_encode($data);
		}
		$total = count($list);
		$money = $total * $this->_recallmoney;//总钱数
		$data['money'] = $money;
		$data['fblist'] = $fblist;
		return json_encode($data);
	}


	/**
	 * 一键召回所有可以被召回的好友
	 */
	public function sendRecallMulti($param){
		$data = array( 'code' => -1, 'money' => 0);
		$uid = oo::functions()->uint($param['uid']);//发送召回
		if( empty($uid) ){
			return json_encode($data);
		}
		$data = oo::commonOprModel('recall')->sendRecallMulti($uid);
		return json_encode($data);
	}


	/**
	*	单个发送召回
	*/
//	public function pushNews($param){
//
//		$uid = oo::functions()->uint($param['uid']);
//		$touid = oo::functions()->uint($param['touid']);
//
//		$data = array( 'code' => -1, 'money' => 0);
//		if( empty($uid) || empty($touid) ){
//			return json_encode($data);
//		}
//		//查询当天被推送玩家是否已经收到推送
//		$rs = oo::commonOprRedis('common')->get( okeys::recallToday($touid) );
//		if( $rs ){
//			$data['code'] = -2;//当天已经收到过
//			return json_encode($data);
//		}
//
//        $todayExp = mktime(23, 59, 59);
//		//发召回奖励
////		oo::commonOprModel('member')->optProperty($uid, array('money' => $this->_recallmoney), 1016);
//        oo::commonOprRedis('common')->set( okeys::recallToday($touid), 1 );//设置当天召回
//        oo::commonOprRedis('common')->expireAt( okeys::recallToday($touid), $todayExp );//设置当天召回
//
//        $ret = oo::commonOprModel('push')->getPushTokenByUid($touid);
//        if( $ret <= -1 ){
//            $data['code'] = -2;//没找到要推送玩家的推送信息
//            return json_encode($data);
//        }
//        $token = $ret['token'];
//        $sid = $ret['sid'];
//        $lang = $ret['lang'];
//
//		//发推送了
//		$userInfo = oo::commonOprModel('member')->getUserInfo($touid);
//		$nick = $userInfo['nick'];
//		$pushContent = $nick.' '.oo::getLangByKey('xgPush_content', $lang);
//
//		oo::commonOprModel('push')->pushNewsSingle(oo::getLangByKey('xgPush_title', $lang), $pushContent, $token, $sid);
//
//		$data['code'] = 1;
//		$data['money'] = $this->_recallmoney;
//		return json_encode($data);;
//	}

}
