* @version : HUOSDK 8.0 */ namespace huoAgentSwitch\controller; use huo\controller\agent\AgentCache; use huo\controller\game\GameCache; use huo\controller\member\MemCache; use huo\controller\pay\PaySwitch; use huo\controller\pay\SdkOrderCache; use huo\controller\request\Mem; use huo\logic\game\GamePaySwitchLogic; use huo\model\game\GameOrderSwitchModel; use huoAgentSwitch\model\AgentGameSwitchModel; use huolib\constant\CommonConst; use huolib\constant\OrderConst; use huolib\tool\Rand; use huolib\tool\StrUtils; use huoRate\model\AgentGameModel; use huoRate\model\AgentGameRateModel; class AgentSwitchController { /** * 玩家注册切量 * * @param Mem $member */ public function memSwitch(Mem $member) { $_ag_info = (new AgentGameModel())->getInfoByAgentIdAppId($member->getAgentId(), $member->getAppId()); if (!empty($_ag_info['is_switch']) && OrderConst::PAY_SWITCH_YES == $_ag_info['is_switch']) { $_ag_rate_info = (new AgentGameRateModel())->getInfoByGameAgent($member->getAppId(), $member->getAgentId()); //概率性切换玩家 $_is_switch = StrUtils::formatNumber($_ag_rate_info['switch_rate'] * 100); $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch]; $_key = Rand::getRand($_arr); if ('is_switch' == $_key) { $member->setSwitch(OrderConst::PAY_SWITCH_YES); } } // $_agent_data = AgentCache::ins()->getInfoByAgentId($member->getAgentId()); // if (!empty($_agent_data['is_switch']) && OrderConst::PAY_SWITCH_YES == $_agent_data['is_switch']) { // //概率性切换玩家 // $_is_switch = $_agent_data['switch_rate']; // $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch]; // $_key = Rand::getRand($_arr); // if ('is_switch' == $_key) { // $member->setSwitch(OrderConst::PAY_SWITCH_YES); // } // } // $_agent_data = AgentCache::ins()->getInfoByAgentId($member->getOriginAgentId()); // if (!empty($_agent_data['is_switch']) && OrderConst::PAY_SWITCH_YES == $_agent_data['is_switch']) { // //概率性切换玩家 // $_is_switch = $_agent_data['switch_rate']; // $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch]; // $_key = Rand::getRand($_arr); // if ('is_switch' == $_key) { // $member->setSwitch(OrderConst::PAY_SWITCH_YES); // } // } } /** * 订单切量 * * @param $_data * * @return mixed */ public function orderSwitch($_data) { $_data['is_switch'] = OrderConst::PAY_SWITCH_NO; /* 切量判断 */ $_mem_data = MemCache::ins()->getInfoById($_data['mem_id']); if (OrderConst::PAY_SWITCH_YES == $_mem_data['is_switch']) { //玩家已切一定切 $_data['is_switch'] = OrderConst::PAY_SWITCH_YES; return $_data; } if (isset($_data['h5mp']) && $_data['h5mp'] == CommonConst::STATUS_YES) { $_h5mp_app_id = !empty($_data['h5mp_app_id']) ? $_data['h5mp_app_id'] : 0; if ($_data['app_id'] != $_mem_data['app_id'] && $_h5mp_app_id != $_mem_data['app_id']) { //非注册游戏,自动切量 $_data['is_switch'] = OrderConst::PAY_SWITCH_YES; return $_data; } } //切量时间处理 指定切量时间后的订单都会被切量 if (!empty($_mem_data['switch_time']) && $_mem_data['switch_time'] < time()) { if (empty($_mem_data['switch_money']) || $_data['real_amount'] >= $_mem_data['switch_money']) { $_data['is_switch'] = OrderConst::PAY_SWITCH_YES; return $_data; } } //游戏订单切量 $_app_id = !empty($_h5mp_app_id) ? $_h5mp_app_id : $_data['app_id']; $_game_cache = GameCache::ins(); $_game_info = $_game_cache->getInfoByAppId($_app_id); if (!isset($_game_info['order_switch'])) { $_game_info = $_game_cache->getInfoByAppId($_app_id); } if (!empty($_game_info['order_switch']) && $_game_info['order_switch'] == CommonConst::STATUS_YES) { $_game_order_switch = new GameOrderSwitchModel(); $_switch_rule = $_game_order_switch->getInfoById($_app_id); if (!empty($_switch_rule)) { /* 根据时间判断是否是否切换 */ $_rs = $this->switchTime($_switch_rule['start_time'], $_switch_rule['end_time']); if (true === $_rs) { $_data['is_switch'] = OrderConst::PAY_SWITCH_YES; return $_data; } /* 根据金额是否切换 */ $_rs = $this->switchMoney($_data['real_amount'], $_switch_rule['price']); if (true === $_rs) { $_data['is_switch'] = OrderConst::PAY_SWITCH_YES; return $_data; } } } $_agent_data = AgentCache::ins()->getInfoByAgentId($_data['agent_id']); if (OrderConst::PAY_SWITCH_YES == $_agent_data['is_switch']) { //概率性切换订单 $_is_switch = $_agent_data['switch_rate']; $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch]; $_key = Rand::getRand($_arr); if ('is_switch' == $_key) { $_data['is_switch'] = OrderConst::PAY_SWITCH_YES; } } return $_data; } /** * 判断当前玩家或订单是否切量 * * @param int $mem_id * @param string $order_id * * @return int */ public function isSwitch($mem_id = 0, $order_id = '', $app_id = 0) { $_is_switch = OrderConst::PAY_SWITCH_NO; //默认不切换 if (!empty($mem_id)) { $_mem_data = MemCache::ins()->getInfoById($mem_id); if (!empty($_mem_data) && OrderConst::PAY_SWITCH_YES == $_mem_data['is_switch']) { $_is_switch = OrderConst::PAY_SWITCH_YES; } } if (!empty($order_id)) { $_order_data = SdkOrderCache::ins()->getInfoByOrderId($order_id); if (!empty($_order_data) && OrderConst::PAY_SWITCH_YES == $_order_data['is_switch']) { $_is_switch = OrderConst::PAY_SWITCH_YES; } } if (!empty($_mem_data) && $app_id != $_mem_data['app_id']) { if (!empty($_order_data)) { if ($app_id != $_order_data['app_id'] && $app_id != $_order_data['h5mp_app_id']) { $_is_switch = OrderConst::PAY_SWITCH_YES; } } else { $_is_switch = OrderConst::PAY_SWITCH_YES; } } return $_is_switch; } /** * 判断渠道是否切量 * * @param int $agent_id * * @return int */ public function agentSwitch($agent_id = 0) { $_agent_data = AgentCache::ins()->getInfoByAgentId($agent_id); if (!empty($_agent_data['is_switch']) && OrderConst::PAY_SWITCH_YES == $_agent_data['is_switch']) { //概率性切换玩家 $_is_switch = $_agent_data['switch_rate']; $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch]; $_key = Rand::getRand($_arr); if ('is_switch' == $_key) { return OrderConst::PAY_SWITCH_YES; } } return OrderConst::PAY_SWITCH_NO; } /** * 判断渠道游戏支付切量 * * @param $data * * @return int PAY_SWITCH_YES 1 切换 PAY_SWITCH_NO 2 不切换 */ public function getAgPaySwitch($data) { if (empty($data['app_id']) || empty($data['agent_id'])) { /* 不是渠道游戏的不切 */ return OrderConst::PAY_SWITCH_NO; } $_ags_model = new AgentGameSwitchModel(); /* 获取切换规则 */ $_switch_rule = $_ags_model->getInfoByAgentApp($data['agent_id'], $data['app_id']); if (empty($_switch_rule)) { /* 规则为空时不切 */ return OrderConst::PAY_SWITCH_NO; } /* 根据时间判断是否是否切换 */ $_pay_switch_class = new PaySwitch(); $_rs = $_pay_switch_class->switchTime($_switch_rule['start_time'], $_switch_rule['end_time']); if (OrderConst::PAY_SWITCH_NO == $_rs) { return OrderConst::PAY_SWITCH_NO; } if (isset($data['mem_id'])) { /* 根据首充是否切换 */ $_rs = $_pay_switch_class->switchFirst($data['mem_id'], $data['app_id'], $_switch_rule['is_first']); if (OrderConst::PAY_SWITCH_NO == $_rs) { return OrderConst::PAY_SWITCH_NO; } } if (isset($data['real_amount'])) { /* 根据金额是否切换 */ $_rs = $_pay_switch_class->switchMoney($data['real_amount'], $_switch_rule['price']); if (OrderConst::PAY_SWITCH_NO == $_rs) { return OrderConst::PAY_SWITCH_NO; } } /* 3 IP */ if (isset($data['ip'])) { $_pay_switch = (new GamePaySwitchLogic())->getPaySwitchByIp($data['ip']); if (OrderConst::PAY_SWITCH_NO == $_pay_switch) { /* 切换黑名单 此名单中个体 不切换 */ return OrderConst::PAY_SWITCH_NO; } elseif (OrderConst::PAY_SWITCH_YES == $_pay_switch) { /* 切换黑名单 此名单中个体切换 */ return OrderConst::PAY_SWITCH_YES; } } return OrderConst::PAY_SWITCH_YES; } /** * 判断是否在时间范围内 * * @param String $start_time * @param INT $end_time * * @return bool */ protected function switchTime($start_time, $end_time) { if (empty($start_time)) { $start_time = '00:00:00'; } if (empty($end_time)) { $end_time = '23:59:59'; } if ($end_time == '00:00:00') { $end_time = '23:59:59'; } if ($start_time == $end_time) { $start_time = '00:00:00'; $end_time = '23:59:59'; } $_time = time(); $_start_time = strtotime(date('Y-m-d').' '.$start_time); $_end_time = strtotime(date('Y-m-d').' '.$end_time); if ($_start_time <= $_time && $_time <= $_end_time) { return true; } return false; } /** * 判断金额是否切量 * * @param float $amount 订单金额 * @param float $rule_money 规则金额 * * @return bool */ protected function switchMoney($amount, $rule_money = 0.00) { if ($amount >= $rule_money) { return true; } return false; } }