2026-05-29 19:54:56 +08:00

137 lines
4.4 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: zn
* Date: 2017/7/31
* Time: 14:59
*/
namespace common\modules\rebate;
use common\modules\systemlog\SystemLogUtil;
use common\services\GameConfigServices;
use common\services\ResultStatusServices;
use common\services\StatusException;
use common\services\UserService;
use common\models\Farm;
use common\services\FarmService;
use common\models\Land;
use common\services\LandService;
class RebateUtil
{
//返点类型
const TYPE_SELL = 0;//卖
const TYPE_BUY = 1;//买
const TYPE_LIST = [
self::TYPE_SELL,
self::TYPE_BUY,
];
//等级
const MIN_LEVEL = 1;
const MAX_LEVEL = 8;
/**
* 分销返点
* @param $userID
* @param $gold
* @param $type
* @throws StatusException
*/
public static function addRebate($userID, $gold, $type)
{
$list = UserRebate::findAll(['child_user_id' => $userID]);
foreach ($list as $item) {
//获取配置
$rebatePercent = floatval(GameConfigServices::getCommonNumByID(GameConfigServices::COMMON_REBATE_LIST[$item->level]));
if ($rebatePercent == 0) continue;
$rate = GameConfigServices::getCommonNumByID(GameConfigServices::COMMON_GEM_EXCHANGE_GOLD_RATE);
//计算返点金额
$rebateGold = intval($gold * ($rebatePercent / 100));
if ($rebateGold == 0) continue;
$farmDbData = Farm::findOne(['user_id' => $item->parent_user_id]);
//查找索引最大的农田
$maxLandPosition = Land::find()
->where(['farm_id' => $farmDbData->id])
->max('position');
if($maxLandPosition<11){
continue;
}
//修改接受返点的用户金币
$log = SystemLogUtil::get($item->parent_user_id, SystemLogUtil::TYPE_GOLD, $rebateGold, 0, SystemLogUtil::FROM_TYPE_REBATE, []);
$rs = UserService::checkAndModifyMoney($item->parent_user_id, true,$rebateGold, 0, $log);
if ($rs != ResultStatusServices::RS_OPERATION_SUCCESS) throw new StatusException($rs);
//保存返点记录
$rebateDbData = new Rebate();
$rebateDbData->from_user_id = $item->child_user_id;
$rebateDbData->be_user_id = $item->parent_user_id;
$rebateDbData->rebate_gold = $rebateGold;
$rebateDbData->level = $item->level;
$rebateDbData->type = $type;
if (!$rebateDbData->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
}
}
/**
* 创建推广关系
* @param $childUserID
* @param $parentUserID
* @throws StatusException
*/
public static function createUserRebates($childUserID, $parentUserID)
{
$childList = UserRebate::findAll(['child_user_id' => $parentUserID]);
foreach ($childList as $item) {
$level = $item->level + 1;
if ($level > RebateUtil::MAX_LEVEL) continue;
$userRebate = new UserRebate();
$userRebate->parent_user_id = $item->parent_user_id;
$userRebate->child_user_id = $childUserID;
$userRebate->level = $level;
if (!$userRebate->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
}
$userRebate = new UserRebate();
$userRebate->parent_user_id = $parentUserID;
$userRebate->child_user_id = $childUserID;
$userRebate->level = 1;
if (!$userRebate->save()) throw new StatusException(ResultStatusServices::RS_SAVE_DB_ERROR);
}
// /**
// * 迁移数据
// * @param $userID
// */
// public static function createUserRebate($userID)
// {
// $findUserID = $userID;
//
// $userIDList = [];
//
// while (true) {
// if (count($userIDList) >= self::MAX_LEVEL) break;
//
// $user = User::findById($findUserID, false);
// if (empty($user)) break;
//
// $findUserID = $user->introduce_user_id;
// if ($findUserID <= 0) break;
//
// array_push($userIDList, $findUserID);
// }
//
// for ($i=0;$i<count($userIDList);$i++)
// {
// $userRebate=new UserRebate();
// $userRebate->parent_user_id=$userIDList[$i];
// $userRebate->child_user_id=$userID;
// $userRebate->level=$i+1;
// $userRebate->save();
// }
// }
}