123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- <?php
- /**
- * Rate.php UTF-8
- * 优惠类
- *
- * @date : 2019/4/20 16:47
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.5
- */
- namespace huoRate\controller;
- use huo\logic\agent\AgentLogic;
- use huo\logic\order\OrderLogic;
- use huo\model\user\UserModel;
- use huolib\constant\AgentConst;
- use huolib\constant\CommonConst;
- use huolib\constant\GameConst;
- use huolib\constant\OrderConst;
- use huoRate\model\AgentGameRateModel;
- class Rate {
- private $rate
- = [
- 'game_rate' => 0,/* 游戏分成比例 */
- 'agent_rebate' => 0,/* 渠道返利比例 */
- 'sub_agent_rebate' => 0,/* 下级渠道返利比例 */
- 'gp_agent_rebate' => 0,/* 三级渠道返利比例 */
- 'agent_rate' => 1,/* 渠道分成比例 */
- 'sub_agent_rate' => 1,/* 下级渠道分成比例 */
- 'gp_agent_rate' => 1,/* 三级渠道分成比例 */
- 'benefit_type' => 1,/* 玩家折扣类型 */
- 'mem_rate' => 1,/* 玩家折扣 */
- 'first_mem_rate' => 1,/* 玩家首充折扣 */
- 'mem_rebate' => 0,/* 玩家返利比例 */
- 'first_mem_rebate' => 0,/* 玩家首充返利比例 */
- 'mem_agent_reward' => 0.00,/* 玩家上级奖励 */
- 'mem_reward' => 0.00,/* 玩家奖励 */
- 'promote_switch' => 1,/* 推广状态 */
- 'agent_benefit_type' => 1,/* 渠道推广模式 */
- 'agent_reward' => 0.00,/* 渠道奖励 渠道cpa单价*/
- 'sub_agent_reward' => 0.00,/* 二级渠道奖励 渠道cpa单价*/
- ];
- private $promote_switch = GameConst::GAME_PROMOTE_SWITCH_CHECK;
- private $app_id;
- private $agent_id;
- const GAME_RATE_STR = 'game_rate';
- public function __construct($app_id, $agent_id = 0) {
- if ($agent_id < 0) {
- $agent_id = 0;
- }
- if ($app_id < 0) {
- $app_id = 0;
- }
- $this->app_id = $app_id;
- $this->agent_id = $agent_id;
- if (!empty($this->app_id)) {
- $this->setRate($this->app_id, $this->agent_id);
- }
- }
- /**
- * 设置优惠
- *
- * @param int $app_id 应用ID
- * @param int $agent_id 渠道ID
- *
- * @return bool
- */
- public function setRate($app_id, $agent_id = 0) {
- /* 1. 获取游戏折扣 */
- if (empty($app_id)) {
- return true;
- }
- $_agr_model = new AgentGameRateModel();
- $this->rate[self::GAME_RATE_STR] = $_agr_model->getGameRateByGameAgent($app_id, 0);
- /* 存在数据 直接返回 */
- $_rate_data = $_agr_model->getInfoByGameAgent($app_id, $agent_id);
- if (!empty($_rate_data)) {
- return $this->setRateFromDb($app_id, $agent_id);
- }
- /* 不存在数据 取用上级与游戏优惠数据 */
- $_user_model = new UserModel();
- $_parent_agent_id = $_user_model->getParentIdById($agent_id);
- $_rate_data = $_agr_model->getInfoByGameAgent($app_id, $_parent_agent_id);
- if (!empty($_rate_data)) {
- /* 不存在数据 查询父级数据 */
- $_rs = $this->setRateFromDb($app_id, $_parent_agent_id);
- } else {
- /* 父级数据不存在 则设置游戏数据 */
- $_rs = $this->setRateFromDb($app_id, CommonConst::CONST_ZERO);
- }
- if (false == $_rs) {
- return $_rs;
- }
- /* 二级渠道取用上级二级折扣 */
- $_role_type = (new AgentLogic())->getRoleTypeById($agent_id);
- if (AgentConst::ROLE_TYPE_AGENT == $_role_type) {
- $this->rate['agent_rate'] = $this->rate['sub_agent_rate'];
- $this->rate['agent_rebate'] = $this->rate['sub_agent_rebate'];
- $this->rate['agent_reward'] = $this->rate['sub_agent_reward'];
- }
- return true;
- }
- /**
- * 通过数据库设置优惠
- *
- * @param int $app_id 应用ID
- * @param int $agent_id 渠道ID
- *
- * @return bool
- */
- public function setRateFromDb($app_id, $agent_id) {
- if (empty($app_id)) {
- return true;
- }
- $_agr_model = new AgentGameRateModel();
- $this->promote_switch = $_agr_model->getPromoteSwitchByGameAgent($app_id, $agent_id);
- /* 不可推广 */
- if (GameConst::GAME_PROMOTE_SWITCH_NO == $this->promote_switch
- || (!empty($agent_id) && GameConst::GAME_PROMOTE_SWITCH_CHECK == $this->promote_switch)) {
- return true;
- }
- $_agr_data = $_agr_model->getInfoByGameAgent($app_id, $agent_id);
- if (empty($_agr_data)) {
- return true;
- }
- foreach ($this->rate as $_k => $_v) {
- if (self::GAME_RATE_STR == $_k) {
- /* 不改变游戏 */
- continue;
- }
- $this->rate[$_k] = $_agr_data[$_k];
- }
- if (GameConst::RATE_BENEFIT_RATE == $this->rate['benefit_type']) {
- /* 折扣类型设置返利为0 */
- $this->rate['mem_rebate'] = 0;/* 玩家返利比例 */
- $this->rate['first_mem_rebate'] = 0;/* 玩家首充返利比例 */
- } elseif (GameConst::RATE_BENEFIT_REBATE == $this->rate['benefit_type']) {
- /* 返利类型设置折扣为0 */
- $this->rate['mem_rate'] = 1;/* 玩家折扣 */
- $this->rate['first_mem_rate'] = 1;/* 玩家首充折扣 */
- } elseif (GameConst::RATE_BENEFIT_NO == $this->rate['benefit_type']) {
- /* 无优惠则返利折扣设为默认 */
- $this->rate['mem_rate'] = 1;/* 玩家折扣 */
- $this->rate['first_mem_rate'] = 1;/* 玩家首充折扣 */
- $this->rate['mem_rebate'] = 0;/* 玩家返利比例 */
- $this->rate['first_mem_rebate'] = 0;/* 玩家首充返利比例 */
- }
- if (GameConst::RATE_AGENT_BENEFIT_CPA == $this->rate['agent_benefit_type']) {
- /* 渠道推广模式为cpa 设置渠道折扣为0 */
- $this->rate['agent_rate'] = 1;/* 渠道折扣 */
- $this->rate['sub_agent_rate'] = 1;/* 渠道折扣 */
- $this->rate['agent_rebate'] = 0;/* 渠道返点 */
- $this->rate['sub_agent_rebate'] = 0;/* 渠道返点 */
- } elseif (GameConst::RATE_AGENT_BENEFIT_CPS == $this->rate['agent_benefit_type']) {
- /* 渠道推广模式为cps 设置渠道cpa单价为0 */
- $this->rate['agent_reward'] = 0;/* cpa 单价 */
- $this->rate['sub_agent_reward'] = 0;/* cpa 单价 */
- } elseif (GameConst::RATE_BENEFIT_NO == $this->rate['agent_benefit_type']) {
- /* 无优惠则返利折扣cpa单价设为默认 */
- $this->rate['agent_rate'] = 1;/* 渠道折扣 */
- $this->rate['sub_agent_rate'] = 1;/* 渠道折扣 */
- $this->rate['agent_rebate'] = 0;/* 渠道返点 */
- $this->rate['sub_agent_rebate'] = 0;/* 渠道返点 */
- $this->rate['agent_reward'] = 0;/* cpa 单价 */
- $this->rate['sub_agent_reward'] = 0;/* cpa 单价 */
- }
- return true;
- }
- /**
- * 获取折扣渠道信息
- *
- * @return array
- */
- public function getRate() {
- return $this->rate;
- }
- /**
- * @return int
- */
- public function getPromoteSwitch() {
- return $this->promote_switch;
- }
- /**
- * @param int $promote_switch
- */
- public function setPromoteSwitch($promote_switch) {
- $this->promote_switch = $promote_switch;
- $this->rate['promote_switch'] = $promote_switch;
- }
- /**
- * 获取渠道CPA价格
- *
- * @return int
- */
- public function getAgentReward() {
- return $this->rate['agent_reward'];
- }
- /**
- * 获取二级渠道CPA价格
- *
- * @return int
- */
- public function getSubAgentReward() {
- return $this->rate['sub_agent_reward'];
- }
- /**
- * 获取玩家CPA价格
- *
- * @return float
- */
- public function getMemReward() {
- return $this->rate['mem_reward'];
- }
- /**
- *
- * @return int
- */
- public function getBenefitType() {
- return $this->rate['benefit_type'];
- }
- /**
- *
- * @return int
- */
- public function getAgentBenefitType() {
- return $this->rate['agent_benefit_type'];
- }
- /**
- * 获取游戏分成比例
- *
- * @return float
- */
- public function getGameRate() {
- return $this->rate['game_rate'];
- }
- /**
- * 获取渠道返利比例
- *
- * @return float
- */
- public function getAgentRebate() {
- return $this->rate['agent_rebate'];
- }
- /**
- * 获取下级渠道返利比例
- *
- * @return float
- */
- public function getSubAgentRebate() {
- return $this->rate['sub_agent_rebate'];
- }
- /**
- * 获取渠道返利比例
- *
- * @return float
- */
- public function getAgentRate() {
- return $this->rate['agent_rate'];
- }
- /**
- * 获取下级渠道返利比例
- *
- * @return float
- */
- public function getSubAgentRate() {
- return $this->rate['sub_agent_rate'];
- }
- /**
- * 获取玩家折扣
- *
- * @param int $mem_id 玩家ID
- *
- * @return float
- */
- public function getMemRate($mem_id = 0) {
- if (empty($mem_id) || true == $this->isFirstCharge($this->app_id, $mem_id)) {
- return $this->rate['first_mem_rate'];
- }
- return $this->rate['mem_rate'];
- }
- /**
- * 获取玩家首充折扣
- *
- * @return float
- */
- public function getFirstMemRate() {
- return $this->rate['first_mem_rate'];
- }
- /**
- * 获取玩家玩家返利比例
- *
- * @param int $mem_id 玩家ID
- *
- * @return float
- */
- public function getMemRebate($mem_id = 0) {
- if (empty($mem_id) || true == $this->isFirstCharge($this->app_id, $mem_id)) {
- return $this->rate['first_mem_rebate'];
- }
- return $this->rate['mem_rebate'];
- }
- /**
- * 获取玩家首充返利比例
- *
- * @return float
- */
- public function getFirstMemRebate() {
- return $this->rate['first_mem_rate'];
- }
- /**
- * 判断是否首充
- *
- * @param int $app_id
- * @param int $mem_id
- *
- * @return int|string
- */
- public function isFirstCharge($app_id, $mem_id = 0) {
- if (empty($mem_id)) {
- return false;
- }
- $_map = [
- 'mem_id' => $mem_id,
- 'status' => OrderConst::PAY_STATUS_SUC
- ];
- if (!empty($app_id)) {
- $_map['app_id'] = $app_id;
- }
- $_rs = (new OrderLogic())->isFirstCharge($_map, false);
- return $_rs;
- }
- }
|