Rate.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * Rate.php UTF-8
  4. * 优惠类
  5. *
  6. * @date : 2019/4/20 16:47
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.5
  11. */
  12. namespace huoRate\controller;
  13. use huo\logic\agent\AgentLogic;
  14. use huo\logic\order\OrderLogic;
  15. use huo\model\user\UserModel;
  16. use huolib\constant\AgentConst;
  17. use huolib\constant\CommonConst;
  18. use huolib\constant\GameConst;
  19. use huolib\constant\OrderConst;
  20. use huoRate\model\AgentGameRateModel;
  21. class Rate {
  22. private $rate
  23. = [
  24. 'game_rate' => 0,/* 游戏分成比例 */
  25. 'agent_rebate' => 0,/* 渠道返利比例 */
  26. 'sub_agent_rebate' => 0,/* 下级渠道返利比例 */
  27. 'gp_agent_rebate' => 0,/* 三级渠道返利比例 */
  28. 'agent_rate' => 1,/* 渠道分成比例 */
  29. 'sub_agent_rate' => 1,/* 下级渠道分成比例 */
  30. 'gp_agent_rate' => 1,/* 三级渠道分成比例 */
  31. 'benefit_type' => 1,/* 玩家折扣类型 */
  32. 'mem_rate' => 1,/* 玩家折扣 */
  33. 'first_mem_rate' => 1,/* 玩家首充折扣 */
  34. 'mem_rebate' => 0,/* 玩家返利比例 */
  35. 'first_mem_rebate' => 0,/* 玩家首充返利比例 */
  36. 'mem_agent_reward' => 0.00,/* 玩家上级奖励 */
  37. 'mem_reward' => 0.00,/* 玩家奖励 */
  38. 'promote_switch' => 1,/* 推广状态 */
  39. 'agent_benefit_type' => 1,/* 渠道推广模式 */
  40. 'agent_reward' => 0.00,/* 渠道奖励 渠道cpa单价*/
  41. 'sub_agent_reward' => 0.00,/* 二级渠道奖励 渠道cpa单价*/
  42. ];
  43. private $promote_switch = GameConst::GAME_PROMOTE_SWITCH_CHECK;
  44. private $app_id;
  45. private $agent_id;
  46. const GAME_RATE_STR = 'game_rate';
  47. public function __construct($app_id, $agent_id = 0) {
  48. if ($agent_id < 0) {
  49. $agent_id = 0;
  50. }
  51. if ($app_id < 0) {
  52. $app_id = 0;
  53. }
  54. $this->app_id = $app_id;
  55. $this->agent_id = $agent_id;
  56. if (!empty($this->app_id)) {
  57. $this->setRate($this->app_id, $this->agent_id);
  58. }
  59. }
  60. /**
  61. * 设置优惠
  62. *
  63. * @param int $app_id 应用ID
  64. * @param int $agent_id 渠道ID
  65. *
  66. * @return bool
  67. */
  68. public function setRate($app_id, $agent_id = 0) {
  69. /* 1. 获取游戏折扣 */
  70. if (empty($app_id)) {
  71. return true;
  72. }
  73. $_agr_model = new AgentGameRateModel();
  74. $this->rate[self::GAME_RATE_STR] = $_agr_model->getGameRateByGameAgent($app_id, 0);
  75. /* 存在数据 直接返回 */
  76. $_rate_data = $_agr_model->getInfoByGameAgent($app_id, $agent_id);
  77. if (!empty($_rate_data)) {
  78. return $this->setRateFromDb($app_id, $agent_id);
  79. }
  80. /* 不存在数据 取用上级与游戏优惠数据 */
  81. $_user_model = new UserModel();
  82. $_parent_agent_id = $_user_model->getParentIdById($agent_id);
  83. $_rate_data = $_agr_model->getInfoByGameAgent($app_id, $_parent_agent_id);
  84. if (!empty($_rate_data)) {
  85. /* 不存在数据 查询父级数据 */
  86. $_rs = $this->setRateFromDb($app_id, $_parent_agent_id);
  87. } else {
  88. /* 父级数据不存在 则设置游戏数据 */
  89. $_rs = $this->setRateFromDb($app_id, CommonConst::CONST_ZERO);
  90. }
  91. if (false == $_rs) {
  92. return $_rs;
  93. }
  94. /* 二级渠道取用上级二级折扣 */
  95. $_role_type = (new AgentLogic())->getRoleTypeById($agent_id);
  96. if (AgentConst::ROLE_TYPE_AGENT == $_role_type) {
  97. $this->rate['agent_rate'] = $this->rate['sub_agent_rate'];
  98. $this->rate['agent_rebate'] = $this->rate['sub_agent_rebate'];
  99. $this->rate['agent_reward'] = $this->rate['sub_agent_reward'];
  100. }
  101. return true;
  102. }
  103. /**
  104. * 通过数据库设置优惠
  105. *
  106. * @param int $app_id 应用ID
  107. * @param int $agent_id 渠道ID
  108. *
  109. * @return bool
  110. */
  111. public function setRateFromDb($app_id, $agent_id) {
  112. if (empty($app_id)) {
  113. return true;
  114. }
  115. $_agr_model = new AgentGameRateModel();
  116. $this->promote_switch = $_agr_model->getPromoteSwitchByGameAgent($app_id, $agent_id);
  117. /* 不可推广 */
  118. if (GameConst::GAME_PROMOTE_SWITCH_NO == $this->promote_switch
  119. || (!empty($agent_id) && GameConst::GAME_PROMOTE_SWITCH_CHECK == $this->promote_switch)) {
  120. return true;
  121. }
  122. $_agr_data = $_agr_model->getInfoByGameAgent($app_id, $agent_id);
  123. if (empty($_agr_data)) {
  124. return true;
  125. }
  126. foreach ($this->rate as $_k => $_v) {
  127. if (self::GAME_RATE_STR == $_k) {
  128. /* 不改变游戏 */
  129. continue;
  130. }
  131. $this->rate[$_k] = $_agr_data[$_k];
  132. }
  133. if (GameConst::RATE_BENEFIT_RATE == $this->rate['benefit_type']) {
  134. /* 折扣类型设置返利为0 */
  135. $this->rate['mem_rebate'] = 0;/* 玩家返利比例 */
  136. $this->rate['first_mem_rebate'] = 0;/* 玩家首充返利比例 */
  137. } elseif (GameConst::RATE_BENEFIT_REBATE == $this->rate['benefit_type']) {
  138. /* 返利类型设置折扣为0 */
  139. $this->rate['mem_rate'] = 1;/* 玩家折扣 */
  140. $this->rate['first_mem_rate'] = 1;/* 玩家首充折扣 */
  141. } elseif (GameConst::RATE_BENEFIT_NO == $this->rate['benefit_type']) {
  142. /* 无优惠则返利折扣设为默认 */
  143. $this->rate['mem_rate'] = 1;/* 玩家折扣 */
  144. $this->rate['first_mem_rate'] = 1;/* 玩家首充折扣 */
  145. $this->rate['mem_rebate'] = 0;/* 玩家返利比例 */
  146. $this->rate['first_mem_rebate'] = 0;/* 玩家首充返利比例 */
  147. }
  148. if (GameConst::RATE_AGENT_BENEFIT_CPA == $this->rate['agent_benefit_type']) {
  149. /* 渠道推广模式为cpa 设置渠道折扣为0 */
  150. $this->rate['agent_rate'] = 1;/* 渠道折扣 */
  151. $this->rate['sub_agent_rate'] = 1;/* 渠道折扣 */
  152. $this->rate['agent_rebate'] = 0;/* 渠道返点 */
  153. $this->rate['sub_agent_rebate'] = 0;/* 渠道返点 */
  154. } elseif (GameConst::RATE_AGENT_BENEFIT_CPS == $this->rate['agent_benefit_type']) {
  155. /* 渠道推广模式为cps 设置渠道cpa单价为0 */
  156. $this->rate['agent_reward'] = 0;/* cpa 单价 */
  157. $this->rate['sub_agent_reward'] = 0;/* cpa 单价 */
  158. } elseif (GameConst::RATE_BENEFIT_NO == $this->rate['agent_benefit_type']) {
  159. /* 无优惠则返利折扣cpa单价设为默认 */
  160. $this->rate['agent_rate'] = 1;/* 渠道折扣 */
  161. $this->rate['sub_agent_rate'] = 1;/* 渠道折扣 */
  162. $this->rate['agent_rebate'] = 0;/* 渠道返点 */
  163. $this->rate['sub_agent_rebate'] = 0;/* 渠道返点 */
  164. $this->rate['agent_reward'] = 0;/* cpa 单价 */
  165. $this->rate['sub_agent_reward'] = 0;/* cpa 单价 */
  166. }
  167. return true;
  168. }
  169. /**
  170. * 获取折扣渠道信息
  171. *
  172. * @return array
  173. */
  174. public function getRate() {
  175. return $this->rate;
  176. }
  177. /**
  178. * @return int
  179. */
  180. public function getPromoteSwitch() {
  181. return $this->promote_switch;
  182. }
  183. /**
  184. * @param int $promote_switch
  185. */
  186. public function setPromoteSwitch($promote_switch) {
  187. $this->promote_switch = $promote_switch;
  188. $this->rate['promote_switch'] = $promote_switch;
  189. }
  190. /**
  191. * 获取渠道CPA价格
  192. *
  193. * @return int
  194. */
  195. public function getAgentReward() {
  196. return $this->rate['agent_reward'];
  197. }
  198. /**
  199. * 获取二级渠道CPA价格
  200. *
  201. * @return int
  202. */
  203. public function getSubAgentReward() {
  204. return $this->rate['sub_agent_reward'];
  205. }
  206. /**
  207. * 获取玩家CPA价格
  208. *
  209. * @return float
  210. */
  211. public function getMemReward() {
  212. return $this->rate['mem_reward'];
  213. }
  214. /**
  215. *
  216. * @return int
  217. */
  218. public function getBenefitType() {
  219. return $this->rate['benefit_type'];
  220. }
  221. /**
  222. *
  223. * @return int
  224. */
  225. public function getAgentBenefitType() {
  226. return $this->rate['agent_benefit_type'];
  227. }
  228. /**
  229. * 获取游戏分成比例
  230. *
  231. * @return float
  232. */
  233. public function getGameRate() {
  234. return $this->rate['game_rate'];
  235. }
  236. /**
  237. * 获取渠道返利比例
  238. *
  239. * @return float
  240. */
  241. public function getAgentRebate() {
  242. return $this->rate['agent_rebate'];
  243. }
  244. /**
  245. * 获取下级渠道返利比例
  246. *
  247. * @return float
  248. */
  249. public function getSubAgentRebate() {
  250. return $this->rate['sub_agent_rebate'];
  251. }
  252. /**
  253. * 获取渠道返利比例
  254. *
  255. * @return float
  256. */
  257. public function getAgentRate() {
  258. return $this->rate['agent_rate'];
  259. }
  260. /**
  261. * 获取下级渠道返利比例
  262. *
  263. * @return float
  264. */
  265. public function getSubAgentRate() {
  266. return $this->rate['sub_agent_rate'];
  267. }
  268. /**
  269. * 获取玩家折扣
  270. *
  271. * @param int $mem_id 玩家ID
  272. *
  273. * @return float
  274. */
  275. public function getMemRate($mem_id = 0) {
  276. if (empty($mem_id) || true == $this->isFirstCharge($this->app_id, $mem_id)) {
  277. return $this->rate['first_mem_rate'];
  278. }
  279. return $this->rate['mem_rate'];
  280. }
  281. /**
  282. * 获取玩家首充折扣
  283. *
  284. * @return float
  285. */
  286. public function getFirstMemRate() {
  287. return $this->rate['first_mem_rate'];
  288. }
  289. /**
  290. * 获取玩家玩家返利比例
  291. *
  292. * @param int $mem_id 玩家ID
  293. *
  294. * @return float
  295. */
  296. public function getMemRebate($mem_id = 0) {
  297. if (empty($mem_id) || true == $this->isFirstCharge($this->app_id, $mem_id)) {
  298. return $this->rate['first_mem_rebate'];
  299. }
  300. return $this->rate['mem_rebate'];
  301. }
  302. /**
  303. * 获取玩家首充返利比例
  304. *
  305. * @return float
  306. */
  307. public function getFirstMemRebate() {
  308. return $this->rate['first_mem_rate'];
  309. }
  310. /**
  311. * 判断是否首充
  312. *
  313. * @param int $app_id
  314. * @param int $mem_id
  315. *
  316. * @return int|string
  317. */
  318. public function isFirstCharge($app_id, $mem_id = 0) {
  319. if (empty($mem_id)) {
  320. return false;
  321. }
  322. $_map = [
  323. 'mem_id' => $mem_id,
  324. 'status' => OrderConst::PAY_STATUS_SUC
  325. ];
  326. if (!empty($app_id)) {
  327. $_map['app_id'] = $app_id;
  328. }
  329. $_rs = (new OrderLogic())->isFirstCharge($_map, false);
  330. return $_rs;
  331. }
  332. }