123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- <?php
- /**
- * Income.php UTF-8
- * 玩家渠道收益计算
- *
- * @date : 2017/9/29 22:55
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 7.5
- * Added by wuyonghong BEGIN 2017/10/12 ISSUES:3772 玩家渠道、渠道收益计算重写
- */
- namespace huo\controller\finance;
- use huo\controller\agent\Agent;
- use huo\controller\agent\AgentCache;
- use huo\controller\agent\AgentOrder;
- use huo\controller\member\MemCache;
- use huo\controller\pay\SdkOrderCache;
- use huo\controller\rate\PtbRate;
- use huo\controller\rate\Rate;
- use huo\model\agent\AgentCpaLogModel;
- use huo\model\finance\GmChargeModel;
- use huo\model\finance\PtbAgentChargeModel;
- use huo\model\finance\PtbChargeModel;
- use huo\model\member\MemoauthModel;
- use huolib\constant\AgentConst;
- use huolib\constant\CommonConst;
- use huolib\constant\GameConst;
- use huolib\constant\OrderConst;
- use huolib\constant\WalletConst;
- use huolib\status\CommonStatus;
- use huolib\status\OrderStatus;
- use huolib\status\SettleStatus;
- use huolib\tool\StrUtils;
- use think\Log;
- class Income {
- /**
- * 从玩家游戏充值中计算收益
- *
- * @param string $order_id
- *
- * @return bool
- */
- public function incomeFromSdkOrder($order_id) {
- if (empty($order_id)) {
- $_code = OrderStatus::ORDER_NOT_EXISTS;
- return $_code;
- }
- $_order_data = SdkOrderCache::ins()->getInfoByOrderId($order_id);
- if (empty($_order_data)) {
- $_code = OrderStatus::ORDER_NOT_EXISTS;
- return $_code;
- }
- if (OrderConst::PAY_SWITCH_YES == $_order_data['is_switch']) {
- $_code = SettleStatus::ORDER_IS_SWITCH;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&msg='.SettleStatus::getMsg($_code)."&order="
- .http_build_query($_order_data),
- LOG::LOG
- );
- return $_code;
- }
- if (OrderConst::PAY_STATUS_SUC != $_order_data['status']) {
- $_code = SettleStatus::ORDER_NOT_PAYED;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&msg='.SettleStatus::getMsg($_code)."&order="
- .http_build_query($_order_data),
- LOG::ERROR
- );
- return $_code;
- }
- $_aor_class = new AoRequest();
- $_aor_class->setDataFromSdkOrder($_order_data);
- $_agent_id = $_aor_class->getAgentId();
- $_agent_class = new Agent();
- $_role_type = $_agent_class->getRoleType($_agent_id);
- if (!empty($_agent_id) && empty($_role_type)) {
- $_code = SettleStatus::INNER_ERROR;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&role_type=error&msg='.SettleStatus::getMsg(
- $_code
- )."&order="
- .http_build_query($_order_data),
- LOG::ERROR
- );
- return $_code;
- }
- /* 玩家游戏充值 */
- $_rate_class = new Rate($_aor_class->getAppId(), $_aor_class->getAgentId());
- if (GameConst::RATE_AGENT_BENEFIT_REBATE != $_rate_class->getAgentBenefitType()) {
- $_code = SettleStatus::INNER_ERROR;
- /* 非CPS 模式不计算收益 */
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&role_type=error&msg='.SettleStatus::getMsg(
- $_code
- )."&order="
- .http_build_query($_order_data),
- LOG::ERROR
- );
- return $_code;
- }
- $_ai_class = new AgentIncome();
- if (AgentConst::ROLE_TYPE_GROUP == $_role_type) {
- /* 一级渠道的直接玩家 */
- $_aor_class->setParentId($_aor_class->getAgentId());
- $_aor_class->setParentRebate($_rate_class->getAgentRebate());
- return $_ai_class->setLevelOne($_aor_class);
- } else if (AgentConst::ROLE_TYPE_AGENT == $_role_type) {
- /* 二级渠道的直接玩家 */
- $_aor_class->setAgentRebate($_rate_class->getAgentRebate());
- $_parent_id = $_agent_class->getParentId($_agent_id);
- $_parent_role_type = $_agent_class->getRoleType($_parent_id);
- if (AgentConst::ROLE_TYPE_GROUP == $_parent_role_type) {
- $_aor_class->setParentId($_parent_id);
- $_rate_class = new Rate($_aor_class->getAppId(), $_aor_class->getParentId());
- $_aor_class->setParentRebate($_rate_class->getAgentRebate());
- }
- return $_ai_class->setLevelTwo($_aor_class);
- } else if (AgentConst::ROLE_TYPE_MEMBER == $_role_type) {
- $_aor_class->setFlag($_aor_class::FLAG_MEM_MEM_SDK);
- $_deduct_fee = 0;
- // TODO: wuyonghong 2018/5/14 计算积分价值
- $_parent_id = $_agent_class->getParentId($_agent_id);
- if (empty($_parent_id)) {
- /* 官方渠道不需计算 */
- return SettleStatus::NO_ERROR;
- }
- $_aor_class->setAgentId($_parent_id);
- $_parent_role_type = $_agent_class->getRoleType($_parent_id);
- if (AgentConst::ROLE_TYPE_GROUP == $_parent_role_type) {
- /* 一级渠道的直接玩家的玩家 */
- $_aor_class->setParentId($_aor_class->getAgentId());
- $_rate_class->setAgRate($_aor_class->getAppId(), $_aor_class->getParentId());
- $_aor_class->setParentRebate($_rate_class->getAgentRebate());
- return $_ai_class->setLevelOne($_aor_class, $_deduct_fee);
- } else if (AgentConst::ROLE_TYPE_AGENT == $_parent_role_type) {
- /* 二级渠道的直接玩家的玩家 */
- $_rate_class->setAgRate($_aor_class->getAppId(), $_aor_class->getAgentId());
- $_aor_class->setAgentRebate($_rate_class->getAgentRebate());
- $_rate_class->setAgRate($_aor_class->getAppId(), $_aor_class->getParentId());
- $_pp_id = $_agent_class->getParentId($_parent_id);
- $_pp_role_type = $_agent_class->getRoleType($_pp_id);
- if (AgentConst::ROLE_TYPE_GROUP == $_pp_role_type) {
- $_aor_class->setParentId($_pp_id);
- $_rate_class->setAgRate($_aor_class->getAppId(), $_aor_class->getParentId());
- $_aor_class->setParentRebate($_rate_class->getAgentRebate());
- }
- return $_ai_class->setLevelTwo($_aor_class, $_deduct_fee);
- }
- }
- return SettleStatus::NO_ERROR;
- }
- /**
- * 从渠道平台币充值中计算收益
- *
- * @param string $order_id
- *
- * @return bool
- */
- public function incomeFromPac($order_id) {
- if (empty($order_id)) {
- $_code = OrderStatus::ORDER_NOT_EXISTS;
- return $_code;
- }
- $_pac_model = new PtbAgentChargeModel();
- $_order_data = $_pac_model->getDetail($order_id);
- if (empty($_order_data)) {
- $_code = OrderStatus::ORDER_NOT_EXISTS;
- return $_code;
- }
- if (OrderConst::PAY_STATUS_SUC != $_order_data['status']) {
- $_code = SettleStatus::ORDER_NOT_PAYED;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&msg='.SettleStatus::getMsg($_code)."&order="
- .http_build_query($_order_data),
- LOG::ERROR
- );
- return $_code;
- }
- $_aor_class = new AoRequest();
- $_aor_class->setDataFromPacOrder($_order_data);
- $_agent_id = $_aor_class->getAgentId();
- $_agent_class = new Agent();
- $_role_type = $_agent_class->getRoleType($_agent_id);
- if (empty($_role_type)) {
- $_code = SettleStatus::INNER_ERROR;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&role_type=error&msg='.SettleStatus::getMsg(
- $_code
- )."&order="
- .http_build_query($_order_data),
- LOG::ERROR
- );
- return $_code;
- }
- if (AgentConst::ROLE_TYPE_AGENT != $_role_type) {
- return CommonStatus::NO_ERROR;
- }
- $_parent_id = $_agent_class->getParentId($_agent_id);
- $_parent_role_type = $_agent_class->getRoleType($_parent_id);
- /* 渠道折扣 */
- $_rate_class = new PtbRate($_parent_id);
- $_ai_class = new AgentIncome();
- if (AgentConst::ROLE_TYPE_GROUP == $_parent_role_type) {
- /* 一级渠道 */
- $_aor_class->setFromId($_agent_id);
- $_aor_class->setParentId($_parent_id);
- $_aor_class->setParentRebate($_rate_class->getAgentRate());
- return $_ai_class->setLevelOne($_aor_class);
- }
- return SettleStatus::NO_ERROR;
- }
- /**
- * 从玩家平台币充值中计算收益
- *
- * @param string $order_id
- *
- * @return bool
- */
- public function incomeFromPmc($order_id) {
- if (empty($order_id)) {
- $_code = OrderStatus::ORDER_NOT_EXISTS;
- return $_code;
- }
- $_order_data = (new PtbChargeModel())->getDetail($order_id);
- if (empty($_order_data)) {
- $_code = OrderStatus::ORDER_NOT_EXISTS;
- return $_code;
- }
- if (OrderConst::PAY_STATUS_SUC != $_order_data['status']) {
- $_code = SettleStatus::ORDER_NOT_PAYED;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&msg='.SettleStatus::getMsg($_code)."&order="
- .http_build_query($_order_data),
- LOG::ERROR
- );
- return $_code;
- }
- $_aor_class = new AoRequest();
- $_aor_class->setDataFromPcOrder($_order_data);
- $_agent_id = $_aor_class->getAgentId();
- $_agent_class = new Agent();
- $_role_type = $_agent_class->getRoleType($_agent_id);
- if (empty($_role_type)) {
- $_code = SettleStatus::INNER_ERROR;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&role_type=error&msg='.SettleStatus::getMsg(
- $_code
- )."&order="
- .http_build_query($_order_data),
- LOG::ERROR
- );
- return $_code;
- }
- /* 玩家平台币充值 */
- $_rate_class = new PtbRate($_aor_class->getAgentId());
- $_ai_class = new AgentIncome();
- if (AgentConst::ROLE_TYPE_GROUP == $_role_type) {
- /* 一级渠道的直接玩家 */
- $_aor_class->setParentId($_aor_class->getAgentId());
- $_aor_class->setParentRate($_rate_class->getAgentRate());
- return $_ai_class->setLevelOne($_aor_class);
- } else if (AgentConst::ROLE_TYPE_AGENT == $_role_type) {
- /* 二级渠道的直接玩家 */
- $_aor_class->setAgentRate($_rate_class->getAgentRate());
- $_parent_id = $_agent_class->getParentId($_agent_id);
- $_parent_role_type = $_agent_class->getRoleType($_parent_id);
- if (AgentConst::ROLE_TYPE_GROUP == $_parent_role_type) {
- $_aor_class->setParentId($_parent_id);
- $_rate_class->setARate($_aor_class->getParentId());
- $_aor_class->setParentRate($_rate_class->getAgentRate());
- }
- return $_ai_class->setLevelTwo($_aor_class);
- } else if (AgentConst::ROLE_TYPE_MEMBER == $_role_type) {
- $_aor_class->setFlag($_aor_class::FLAG_MEM_MEM_SDK);
- // TODO: wuyonghong 2018/5/14 计算积分价值
- $_deduct_fee = 0;
- $_parent_id = $_agent_class->getParentId($_agent_id);
- if (empty($_parent_id)) {
- /* 官方渠道不需计算 */
- return SettleStatus::NO_ERROR;
- }
- $_aor_class->setAgentId($_parent_id);
- $_parent_role_type = $_agent_class->getRoleType($_parent_id);
- if (AgentConst::ROLE_TYPE_GROUP == $_parent_role_type) {
- /* 一级渠道的直接玩家的玩家 */
- $_aor_class->setParentId($_parent_id);
- $_rate_class->setARate($_parent_id);
- $_aor_class->setParentRate($_rate_class->getAgentRate());
- return $_ai_class->setLevelOne($_aor_class, $_deduct_fee);
- } else if (AgentConst::ROLE_TYPE_AGENT == $_parent_role_type) {
- /* 二级渠道的直接玩家的玩家 */
- $_aor_class->setAgentId($_parent_id);
- $_rate_class->setARate($_parent_id);
- $_aor_class->setAgentRate($_rate_class->getAgentRate());
- $_pp_id = $_agent_class->getParentId($_parent_id);
- $_pp_role_type = $_agent_class->getRoleType($_pp_id);
- if (AgentConst::ROLE_TYPE_GROUP == $_pp_role_type) {
- $_aor_class->setParentId($_pp_id);
- $_rate_class->setARate($_pp_id);
- $_aor_class->setAgentRate($_rate_class->getAgentRate());
- }
- return $_ai_class->setLevelTwo($_aor_class, $_deduct_fee);
- }
- }
- return SettleStatus::NO_ERROR;
- }
- /**
- * 从渠道代充游戏币中计算收益
- *
- * @param string $order_id
- *
- * @return bool
- */
- public function incomeFroGmc($order_id) {
- if (empty($order_id)) {
- $_code = OrderStatus::ORDER_NOT_EXISTS;
- return $_code;
- }
- $_order_data = (new GmChargeModel())->getDetail($order_id);
- if (empty($_order_data)) {
- $_code = OrderStatus::ORDER_NOT_EXISTS;
- return $_code;
- }
- if (OrderConst::PAY_STATUS_SUC != $_order_data['status']) {
- $_code = SettleStatus::ORDER_NOT_PAYED;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&msg='.SettleStatus::getMsg($_code)."&order="
- .http_build_query($_order_data),
- LOG::ERROR
- );
- return $_code;
- }
- $_aor_class = new AoRequest();
- $_aor_class->setDataFromGmcOrder($_order_data);
- $_agent_id = $_aor_class->getAgentId();
- $_agent_class = new Agent();
- $_role_type = $_agent_class->getRoleType($_agent_id);
- if (empty($_role_type)) {
- $_code = SettleStatus::INNER_ERROR;
- Log::write(
- "func=".__FUNCTION__."&class=".__CLASS__."&code=".$_code.'&role_type=error&msg='.SettleStatus::getMsg(
- $_code
- )."&order="
- .http_build_query($_order_data),
- LOG::ERROR
- );
- return $_code;
- }
- /* 玩家平台币充值 */
- $_ai_class = new AgentIncome();
- if (AgentConst::ROLE_TYPE_AGENT == $_role_type) {
- /* 二级渠道代充 给上级计算收益*/
- $_agent_info = (new AgentCache())->getInfoByAgentId($_agent_id);
- /*有父渠道*/
- if (!empty($_agent_info['parent_id'])) {
- $_rate_class = new Rate($_aor_class->getAppId(), $_aor_class->getAgentId());
- $_prate_class = new Rate($_aor_class->getAppId(), $_agent_info['parent_id']);
- if ($_rate_class->getAgentRate() > $_prate_class->getAgentRate()) {
- $_income = $_aor_class->getAmount() * ($_rate_class->getAgentRate() - $_prate_class->getAgentRate(
- ));
- $_aor_class->setParentGain($_income);
- $_aor_class->setParentRate($_prate_class->getAgentRate());
- }
- $_aor_class->setParentId($_agent_info['parent_id']);
- }
- return $_ai_class->setParent($_aor_class);
- }
- return SettleStatus::NO_ERROR;
- }
- /**
- * 计算cpa收益
- *
- * @param $mem_id
- *
- * @return int
- */
- public function incomeFroCpa($mem_id) {
- if (empty($mem_id)) {
- return SettleStatus::NO_ERROR;
- }
- $_mem_data = MemCache::ins()->getInfoById($mem_id);
- if (empty($_mem_data['agent_id']) || empty($_mem_data['app_id']) || empty($_mem_data['reg_ip'])) {
- /* 非渠道游戏注册的不需要计算,没IP不计算 */
- return SettleStatus::NO_ERROR;
- }
- $_rate_class = new Rate($_mem_data['app_id'], $_mem_data['agent_id']);
- if (GameConst::RATE_AGENT_BENEFIT_REWARD != $_rate_class->getAgentBenefitType()) {
- /* 非CPA模式无需计算 */
- return SettleStatus::NO_ERROR;
- }
- $_acl_model = new AgentCpaLogModel();
- $_id = $_acl_model->getIdByMemId($mem_id);
- if (!empty($_id)) {
- /* 已存在玩家不需要重复计算 */
- return SettleStatus::NO_ERROR;
- }
- /* 获取当前玩家是否微信用户 */
- $_open_id = (new MemoauthModel())->getOpenidByMemId('', $mem_id);
- if (empty($_open_id)) {
- /* 非第三方注册用户需要ip 排重 */
- $_id = $_acl_model->getIdByIp($_mem_data['reg_ip']);
- if (!empty($_id)) {
- /* 已存在IP不需要重复计算 */
- Log::write(
- 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&mem_id='.$mem_id.'&ip='
- .$_mem_data['reg_ip'].'&error=当前IP已计算过CPA', Log::ERROR
- );
- return SettleStatus::NO_ERROR;
- }
- }
- $_aor_class = new AoRequest();
- $_order_id = StrUtils::genOrderId(
- $_mem_data['app_id'], $_mem_data['agent_id'], $mem_id, WalletConst::WALLET_ORDER_PREFIX_CPA
- );
- $_aor_class->setOrderId($_order_id);
- $_aor_class->setFlag(AoRequest::FLAG_AGENT_CPA);
- $_aor_class->setMemId($mem_id);
- $_aor_class->setAppId($_mem_data['app_id']);
- $_aor_class->setAgentId($_mem_data['agent_id']);
- $_agent_id = $_aor_class->getAgentId();
- $_agent_class = new Agent();
- $_role_type = $_agent_class->getRoleType($_agent_id);
- if (empty($_role_type)) {
- $_code = SettleStatus::INNER_ERROR;
- Log::write(
- 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&mem_id='.$mem_id.'&agent_id='
- .$_agent_id.'&error=渠道类型错误', Log::ERROR
- );
- return $_code;
- }
- if (AgentConst::ROLE_TYPE_GROUP == $_role_type) {
- /* 一级渠道的直接玩家 */
- $_aor_class->setAgentId(0);
- $_aor_class->setParentId($_aor_class->getAgentId());
- $_aor_class->setParentGain($_rate_class->getAgentReward());
- $_rs = (new AgentOrder())->createAoOrder($_aor_class);
- } else if (AgentConst::ROLE_TYPE_AGENT == $_role_type) {
- /* 二级渠道的直接玩家 */
- $_agent_gain = $_rate_class->getAgentReward();
- $_aor_class->setAgentGain($_agent_gain);
- $_parent_id = $_agent_class->getParentId($_agent_id);
- $_parent_role_type = $_agent_class->getRoleType($_parent_id);
- if (AgentConst::ROLE_TYPE_GROUP == $_parent_role_type) {
- $_aor_class->setParentId($_parent_id);
- $_parent_rate_class = new Rate($_mem_data['app_id'], $_parent_id);
- if (GameConst::RATE_AGENT_BENEFIT_REWARD == $_parent_rate_class->getAgentBenefitType()) {
- $_parent_gain = $_parent_rate_class->getAgentReward();
- $_parent_gain = $_parent_gain - $_agent_gain;
- $_parent_gain = $_parent_gain >= CommonConst::CONST_ZERO ? $_parent_gain : CommonConst::CONST_ZERO;
- $_aor_class->setParentGain($_parent_gain);
- }
- }
- $_rs = (new AgentOrder())->createAoOrder($_aor_class);
- } else {
- return SettleStatus::NO_ERROR;
- }
- if (SettleStatus::NO_ERROR != $_rs) {
- Log::write(
- 'line='.__LINE__.'&func='.__FUNCTION__.'&class='.__CLASS__.'&mem_id='.$mem_id.'&agent_id='
- .$_agent_id.'&error='.$_rs.'&aor_class='.json_encode($_aor_class), Log::ERROR
- );
- return $_rs;
- }
- /* 插入记录 */
- $_data = [
- 'order_id' => $_aor_class->getOrderId(),
- 'parent_id' => $_aor_class->getParentId(),
- 'agent_id' => $_aor_class->getAgentId(),
- 'app_id' => $_aor_class->getAppId(),
- 'mem_id' => $_aor_class->getMemId(),
- 'ip' => $_mem_data['reg_ip'],
- 'agent_cpa' => $_aor_class->getAgentGain(),
- 'parent_cpa' => $_aor_class->getParentGain()
- ];
- (new AgentCpaLogModel())->addData($_data);
- return SettleStatus::NO_ERROR;
- }
- }
|