AgentSwitchController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * AgentSwitchController.php UTF-8
  4. * 渠道切量处理
  5. *
  6. * @date : 2019/1/26 11:33
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huoAgentSwitch\controller;
  13. use huo\controller\agent\AgentCache;
  14. use huo\controller\game\GameCache;
  15. use huo\controller\member\MemCache;
  16. use huo\controller\pay\PaySwitch;
  17. use huo\controller\pay\SdkOrderCache;
  18. use huo\controller\request\Mem;
  19. use huo\logic\game\GamePaySwitchLogic;
  20. use huo\model\game\GameOrderSwitchModel;
  21. use huoAgentSwitch\model\AgentGameSwitchModel;
  22. use huolib\constant\CommonConst;
  23. use huolib\constant\OrderConst;
  24. use huolib\tool\Rand;
  25. use huolib\tool\StrUtils;
  26. use huoRate\model\AgentGameModel;
  27. use huoRate\model\AgentGameRateModel;
  28. class AgentSwitchController {
  29. /**
  30. * 玩家注册切量
  31. *
  32. * @param Mem $member
  33. */
  34. public function memSwitch(Mem $member) {
  35. $_ag_info = (new AgentGameModel())->getInfoByAgentIdAppId($member->getAgentId(), $member->getAppId());
  36. if (!empty($_ag_info['is_switch']) && OrderConst::PAY_SWITCH_YES == $_ag_info['is_switch']) {
  37. $_ag_rate_info = (new AgentGameRateModel())->getInfoByGameAgent($member->getAppId(), $member->getAgentId());
  38. //概率性切换玩家
  39. $_is_switch = StrUtils::formatNumber($_ag_rate_info['switch_rate'] * 100);
  40. $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch];
  41. $_key = Rand::getRand($_arr);
  42. if ('is_switch' == $_key) {
  43. $member->setSwitch(OrderConst::PAY_SWITCH_YES);
  44. }
  45. }
  46. // $_agent_data = AgentCache::ins()->getInfoByAgentId($member->getAgentId());
  47. // if (!empty($_agent_data['is_switch']) && OrderConst::PAY_SWITCH_YES == $_agent_data['is_switch']) {
  48. // //概率性切换玩家
  49. // $_is_switch = $_agent_data['switch_rate'];
  50. // $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch];
  51. // $_key = Rand::getRand($_arr);
  52. // if ('is_switch' == $_key) {
  53. // $member->setSwitch(OrderConst::PAY_SWITCH_YES);
  54. // }
  55. // }
  56. // $_agent_data = AgentCache::ins()->getInfoByAgentId($member->getOriginAgentId());
  57. // if (!empty($_agent_data['is_switch']) && OrderConst::PAY_SWITCH_YES == $_agent_data['is_switch']) {
  58. // //概率性切换玩家
  59. // $_is_switch = $_agent_data['switch_rate'];
  60. // $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch];
  61. // $_key = Rand::getRand($_arr);
  62. // if ('is_switch' == $_key) {
  63. // $member->setSwitch(OrderConst::PAY_SWITCH_YES);
  64. // }
  65. // }
  66. }
  67. /**
  68. * 订单切量
  69. *
  70. * @param $_data
  71. *
  72. * @return mixed
  73. */
  74. public function orderSwitch($_data) {
  75. $_data['is_switch'] = OrderConst::PAY_SWITCH_NO;
  76. /* 切量判断 */
  77. $_mem_data = MemCache::ins()->getInfoById($_data['mem_id']);
  78. if (OrderConst::PAY_SWITCH_YES == $_mem_data['is_switch']) {
  79. //玩家已切一定切
  80. $_data['is_switch'] = OrderConst::PAY_SWITCH_YES;
  81. return $_data;
  82. }
  83. if (isset($_data['h5mp']) && $_data['h5mp'] == CommonConst::STATUS_YES) {
  84. $_h5mp_app_id = !empty($_data['h5mp_app_id']) ? $_data['h5mp_app_id'] : 0;
  85. if ($_data['app_id'] != $_mem_data['app_id'] && $_h5mp_app_id != $_mem_data['app_id']) {
  86. //非注册游戏,自动切量
  87. $_data['is_switch'] = OrderConst::PAY_SWITCH_YES;
  88. return $_data;
  89. }
  90. }
  91. //切量时间处理 指定切量时间后的订单都会被切量
  92. if (!empty($_mem_data['switch_time']) && $_mem_data['switch_time'] < time()) {
  93. if (empty($_mem_data['switch_money']) || $_data['real_amount'] >= $_mem_data['switch_money']) {
  94. $_data['is_switch'] = OrderConst::PAY_SWITCH_YES;
  95. return $_data;
  96. }
  97. }
  98. //游戏订单切量
  99. $_app_id = !empty($_h5mp_app_id) ? $_h5mp_app_id : $_data['app_id'];
  100. $_game_cache = GameCache::ins();
  101. $_game_info = $_game_cache->getInfoByAppId($_app_id);
  102. if (!isset($_game_info['order_switch'])) {
  103. $_game_info = $_game_cache->getInfoByAppId($_app_id);
  104. }
  105. if (!empty($_game_info['order_switch']) && $_game_info['order_switch'] == CommonConst::STATUS_YES) {
  106. $_game_order_switch = new GameOrderSwitchModel();
  107. $_switch_rule = $_game_order_switch->getInfoById($_app_id);
  108. if (!empty($_switch_rule)) {
  109. /* 根据时间判断是否是否切换 */
  110. $_rs = $this->switchTime($_switch_rule['start_time'], $_switch_rule['end_time']);
  111. if (true === $_rs) {
  112. $_data['is_switch'] = OrderConst::PAY_SWITCH_YES;
  113. return $_data;
  114. }
  115. /* 根据金额是否切换 */
  116. $_rs = $this->switchMoney($_data['real_amount'], $_switch_rule['price']);
  117. if (true === $_rs) {
  118. $_data['is_switch'] = OrderConst::PAY_SWITCH_YES;
  119. return $_data;
  120. }
  121. }
  122. }
  123. $_agent_data = AgentCache::ins()->getInfoByAgentId($_data['agent_id']);
  124. if (OrderConst::PAY_SWITCH_YES == $_agent_data['is_switch']) {
  125. //概率性切换订单
  126. $_is_switch = $_agent_data['switch_rate'];
  127. $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch];
  128. $_key = Rand::getRand($_arr);
  129. if ('is_switch' == $_key) {
  130. $_data['is_switch'] = OrderConst::PAY_SWITCH_YES;
  131. }
  132. }
  133. return $_data;
  134. }
  135. /**
  136. * 判断当前玩家或订单是否切量
  137. *
  138. * @param int $mem_id
  139. * @param string $order_id
  140. *
  141. * @return int
  142. */
  143. public function isSwitch($mem_id = 0, $order_id = '', $app_id = 0) {
  144. $_is_switch = OrderConst::PAY_SWITCH_NO; //默认不切换
  145. if (!empty($mem_id)) {
  146. $_mem_data = MemCache::ins()->getInfoById($mem_id);
  147. if (!empty($_mem_data) && OrderConst::PAY_SWITCH_YES == $_mem_data['is_switch']) {
  148. $_is_switch = OrderConst::PAY_SWITCH_YES;
  149. }
  150. }
  151. if (!empty($order_id)) {
  152. $_order_data = SdkOrderCache::ins()->getInfoByOrderId($order_id);
  153. if (!empty($_order_data) && OrderConst::PAY_SWITCH_YES == $_order_data['is_switch']) {
  154. $_is_switch = OrderConst::PAY_SWITCH_YES;
  155. }
  156. }
  157. if (!empty($_mem_data) && $app_id != $_mem_data['app_id']) {
  158. if (!empty($_order_data)) {
  159. if ($app_id != $_order_data['app_id'] && $app_id != $_order_data['h5mp_app_id']) {
  160. $_is_switch = OrderConst::PAY_SWITCH_YES;
  161. }
  162. } else {
  163. $_is_switch = OrderConst::PAY_SWITCH_YES;
  164. }
  165. }
  166. return $_is_switch;
  167. }
  168. /**
  169. * 判断渠道是否切量
  170. *
  171. * @param int $agent_id
  172. *
  173. * @return int
  174. */
  175. public function agentSwitch($agent_id = 0) {
  176. $_agent_data = AgentCache::ins()->getInfoByAgentId($agent_id);
  177. if (!empty($_agent_data['is_switch']) && OrderConst::PAY_SWITCH_YES == $_agent_data['is_switch']) {
  178. //概率性切换玩家
  179. $_is_switch = $_agent_data['switch_rate'];
  180. $_arr = ['is_switch' => $_is_switch, 'no_switch' => CommonConst::NUMBER_100 - $_is_switch];
  181. $_key = Rand::getRand($_arr);
  182. if ('is_switch' == $_key) {
  183. return OrderConst::PAY_SWITCH_YES;
  184. }
  185. }
  186. return OrderConst::PAY_SWITCH_NO;
  187. }
  188. /**
  189. * 判断渠道游戏支付切量
  190. *
  191. * @param $data
  192. *
  193. * @return int PAY_SWITCH_YES 1 切换 PAY_SWITCH_NO 2 不切换
  194. */
  195. public function getAgPaySwitch($data) {
  196. if (empty($data['app_id']) || empty($data['agent_id'])) {
  197. /* 不是渠道游戏的不切 */
  198. return OrderConst::PAY_SWITCH_NO;
  199. }
  200. $_ags_model = new AgentGameSwitchModel();
  201. /* 获取切换规则 */
  202. $_switch_rule = $_ags_model->getInfoByAgentApp($data['agent_id'], $data['app_id']);
  203. if (empty($_switch_rule)) {
  204. /* 规则为空时不切 */
  205. return OrderConst::PAY_SWITCH_NO;
  206. }
  207. /* 根据时间判断是否是否切换 */
  208. $_pay_switch_class = new PaySwitch();
  209. $_rs = $_pay_switch_class->switchTime($_switch_rule['start_time'], $_switch_rule['end_time']);
  210. if (OrderConst::PAY_SWITCH_NO == $_rs) {
  211. return OrderConst::PAY_SWITCH_NO;
  212. }
  213. if (isset($data['mem_id'])) {
  214. /* 根据首充是否切换 */
  215. $_rs = $_pay_switch_class->switchFirst($data['mem_id'], $data['app_id'], $_switch_rule['is_first']);
  216. if (OrderConst::PAY_SWITCH_NO == $_rs) {
  217. return OrderConst::PAY_SWITCH_NO;
  218. }
  219. }
  220. if (isset($data['real_amount'])) {
  221. /* 根据金额是否切换 */
  222. $_rs = $_pay_switch_class->switchMoney($data['real_amount'], $_switch_rule['price']);
  223. if (OrderConst::PAY_SWITCH_NO == $_rs) {
  224. return OrderConst::PAY_SWITCH_NO;
  225. }
  226. }
  227. /* 3 IP */
  228. if (isset($data['ip'])) {
  229. $_pay_switch = (new GamePaySwitchLogic())->getPaySwitchByIp($data['ip']);
  230. if (OrderConst::PAY_SWITCH_NO == $_pay_switch) {
  231. /* 切换黑名单 此名单中个体 不切换 */
  232. return OrderConst::PAY_SWITCH_NO;
  233. } elseif (OrderConst::PAY_SWITCH_YES == $_pay_switch) {
  234. /* 切换黑名单 此名单中个体切换 */
  235. return OrderConst::PAY_SWITCH_YES;
  236. }
  237. }
  238. return OrderConst::PAY_SWITCH_YES;
  239. }
  240. /**
  241. * 判断是否在时间范围内
  242. *
  243. * @param String $start_time
  244. * @param INT $end_time
  245. *
  246. * @return bool
  247. */
  248. protected function switchTime($start_time, $end_time) {
  249. if (empty($start_time)) {
  250. $start_time = '00:00:00';
  251. }
  252. if (empty($end_time)) {
  253. $end_time = '23:59:59';
  254. }
  255. if ($end_time == '00:00:00') {
  256. $end_time = '23:59:59';
  257. }
  258. if ($start_time == $end_time) {
  259. $start_time = '00:00:00';
  260. $end_time = '23:59:59';
  261. }
  262. $_time = time();
  263. $_start_time = strtotime(date('Y-m-d').' '.$start_time);
  264. $_end_time = strtotime(date('Y-m-d').' '.$end_time);
  265. if ($_start_time <= $_time && $_time <= $_end_time) {
  266. return true;
  267. }
  268. return false;
  269. }
  270. /**
  271. * 判断金额是否切量
  272. *
  273. * @param float $amount 订单金额
  274. * @param float $rule_money 规则金额
  275. *
  276. * @return bool
  277. */
  278. protected function switchMoney($amount, $rule_money = 0.00) {
  279. if ($amount >= $rule_money) {
  280. return true;
  281. }
  282. return false;
  283. }
  284. }