- 框架初始化
 - 安装插件
 - 修复PHP8.4报错
This commit is contained in:
2025-04-19 17:21:20 +08:00
commit c6a4e1f5f6
5306 changed files with 967782 additions and 0 deletions

View File

@@ -0,0 +1,541 @@
<?php
namespace addons\shopro\service\commission;
use addons\shopro\service\commission\Config;
use app\admin\model\shopro\user\User as UserModel;
use app\admin\model\shopro\commission\Agent as AgentModel;
use app\admin\model\shopro\commission\Level as LevelModel;
use app\admin\model\shopro\commission\Log as LogModel;
use app\admin\model\shopro\Share as ShareModel;
/**
* 分销商业务
*/
class Agent
{
public $user; // 商城用户
public $agent; // 分销商
public $config; // 分销设置
public $parentUserId;
public $nextAgentTeam;
public $nextUserTeam;
/**
* 构造函数
*
* @param mixed $user 用户ID/用户对象
*/
public function __construct($user)
{
if (is_numeric($user)) {
$this->user = UserModel::get($user);
} else {
$this->user = UserModel::get($user->id);
}
if (!empty($this->user->id)) {
$this->agent = AgentModel::with(['level_info'])->find($this->user->id);
}
$this->config = new Config();
}
/**
* 获取分销商实时状态
*/
public function getAgentStatus($autoCreate = false)
{
if (empty($this->agent)) {
// 自动创建分销商
if ($autoCreate) {
return $this->createNewAgent();
}
return NULL;
}
return $this->agent->status;
}
/**
* 获取分销商可参与状态 正常和冻结都可正常浏览并统计业绩
*/
public function isAgentAvaliable()
{
$status = $this->getAgentStatus();
if (in_array($status, [AgentModel::AGENT_STATUS_NORMAL, AgentModel::AGENT_STATUS_FREEZE])) {
return true;
}
return false;
}
/**
* 获取分销商等级
*/
public function getAgentLevel()
{
if (empty($this->agent)) {
return 0;
}
if (empty($this->agent->level_info)) {
return 1;
}
return $this->agent->level_info->level;
}
/**
* 分销商升级是否锁定
*/
public function getAgentUpgradeLock()
{
if (empty($this->agent)) {
return true;
}
if ($this->agent->upgrade_lock == AgentModel::UPGRADE_LOCK_OPEN) {
return true;
}
return false;
}
/**
* 实时获取上级推荐人
*/
public function getParentUserId()
{
if (empty($this->parentUserId)) {
$this->parentUserId = 0;
$parent_user_id = $this->user->parent_user_id;
// 未直接绑定分销商,从分享记录查找最近的分销商
if ($parent_user_id === NULL) {
$shareLog = ShareModel::hasWhere(
'agent',
function ($query) {
return $query->where('status', 'in', [AgentModel::AGENT_STATUS_NORMAL, AgentModel::AGENT_STATUS_FREEZE]);
}
)->where('Share.user_id', $this->user->id)->order('id desc')->find();
if ($shareLog) {
$parent_user_id = $shareLog['share_id'];
}
}
// 再次检查上级分销商是否可用
if ($parent_user_id > 0) {
$parentUser = UserModel::where('id', $parent_user_id)->find();
$parentAgent = AgentModel::avaliable()->where(['user_id' => $parent_user_id])->find();
if ($parentUser && $parentAgent) {
$this->parentUserId = $parentAgent->user_id;
}
}
}
return $this->parentUserId;
}
/**
* 创建分销商
*/
public function createNewAgent($event = '', $applyInfo = [])
{
// 已经是分销商
if (!empty($this->agent)) {
return $this->getAgentStatus();
}
$agentStatus = AgentModel::AGENT_STATUS_NULL;
$condition = $this->config->getBecomeAgentEvent();
$check = false; // 是否满足条件
$needAgentApplyForm = $this->config->isAgentApplyForm();
if ($event !== '' && $condition['type'] !== $event) {
return $agentStatus;
}
switch ($condition['type']) {
case 'apply': // 直接自助申请
$check = true;
$needAgentApplyForm = true;
break;
case 'goods': // 需购买指定产品
$isBuy = \app\admin\model\shopro\order\Order::hasWhere('items', function ($query) use ($condition) {
return $query->where('goods_id', 'in', $condition['value'])->where('refund_status', 0);
})->where('Order.user_id', $this->user->id)->paid()->find();
if ($isBuy) $check = true;
break;
case 'consume': // 消费累计
if ($this->user->total_consume >= $condition['value']) {
$check = true;
}
break;
case 'user': // 新会员注册
$check = true;
$needAgentApplyForm = false;
break;
}
// 可以成为分销商 检查系统设置
if ($check) {
// 需后台审核
if ($this->config->isAgentCheck()) {
$agentStatus = AgentModel::AGENT_STATUS_PENDING;
} else {
$agentStatus = AgentModel::AGENT_STATUS_NORMAL;
}
// 需要提交资料
if ($needAgentApplyForm && empty($applyInfo)) {
$agentStatus = AgentModel::AGENT_STATUS_NEEDINFO; // 需要主动提交资料,暂时不加分销商信息
}
}
// 可以直接添加分销商信息
if ($agentStatus === AgentModel::AGENT_STATUS_NORMAL || $agentStatus === AgentModel::AGENT_STATUS_PENDING) {
AgentModel::create([
'user_id' => $this->user->id,
'level' => 1, // 默认分销商等级
'status' => $agentStatus,
'apply_info' => $applyInfo,
'apply_num' => 1,
'become_time' => time()
]);
// 绑定上级推荐人
if ($this->user->parent_user_id === NULL) {
if ($this->bindUserRelation('agent') && $agentStatus !== AgentModel::AGENT_STATUS_NORMAL) {
$this->createAsyncAgentUpgrade($this->user->id); // 防止真正成为分销商时重新触发升级任务造成冗余
}
}
// 绑定为平台直推
if ($this->user->parent_user_id === NULL) {
$this->user->parent_user_id = 0;
$this->user->save();
}
$this->agent = AgentModel::with(['level_info'])->find($this->user->id);
// 添加分销商状态记录
LogModel::add($this->user->id, 'agent', ['type' => 'status', 'value' => $agentStatus]);
// 统计分销层级单链业绩
if ($agentStatus === AgentModel::AGENT_STATUS_NORMAL) {
$this->createAsyncAgentUpgrade($this->user->id);
}
}
return $agentStatus;
}
/**
* 绑定用户关系
*
* @param string $event 事件标识(share=点击分享链接, pay=首次支付, agent=成为子分销商)
* @param int $bindAgentId 可指定需绑定的分销商用户ID 默认从分享记录中去查
*/
public function bindUserRelation($event, $bindAgentId = NULL)
{
$bindCheck = false; // 默认不绑定
// 该用户已经有上级
if ($this->user->parent_user_id !== NULL) {
return false;
}
// 不满足绑定下级事件
if ($this->config->getInviteLockEvent() !== $event) {
return false;
}
switch ($this->config->getInviteLockEvent()) {
case 'share':
$bindCheck = true;
break;
case 'pay':
if ($this->user->total_consume > 0) {
$bindCheck = true;
}
break;
case 'agent':
$bindCheck = true;
break;
}
if (!$bindCheck) {
return false;
}
if ($bindAgentId === NULL) {
$bindAgentId = $this->getParentUserId();
}
if (!$bindAgentId) {
return false;
}
$bindAgent = new Agent($bindAgentId);
if (!$bindAgent->isAgentAvaliable()) {
return false;
}
// 允许绑定用户
$this->user->parent_user_id = $bindAgent->user->id;
$this->user->save();
// 添加推广记录
LogModel::add($bindAgent->user->id, 'share', ['user' => $this->user]);
return true;
}
/**
* 创建[分销商升级&统计业绩]异步队列任务
* 为了防止计算量大而引起阻塞,使用异步递归
*/
public function createAsyncAgentUpgrade($user_id = 0)
{
if ($user_id === 0) {
$user_id = $this->user->id;
}
\think\Queue::push('\addons\shopro\job\Commission@agentUpgrade', [
'user_id' => $user_id
], 'shopro');
}
/**
* 执行用户统计、分销商信息统计、分销商等级升级计划 (递归往上升级)
*
* @param bool $upgrade 执行分销商等级升级
*/
public function runAgentUpgradePlan($upgrade = true)
{
if ($this->isAgentAvaliable()) {
// 获取下级直推团队用户信息
$nextUserTeam = $this->getNextUserTeam();
$nextAgentTeam = $this->getNextAgentTeam();
// 一级用户人数
$this->agent->child_user_count_1 = count($nextUserTeam);
// 二级用户人数 = 一级分销商的一级用户人数
$this->agent->child_user_count_2 = array_sum(array_column($nextAgentTeam, 'child_user_count_1'));
// 团队用户人数 = 一级用户人数 + 一级用户的团队用户人数
$this->agent->child_user_count_all = $this->agent->child_user_count_1 + array_sum(array_column($nextAgentTeam, 'child_user_count_all'));
// 一级分销商人数
$this->agent->child_agent_count_1 = count($nextAgentTeam);
// 二级分销商人数 = 一级分销商的一级分销商人数
$this->agent->child_agent_count_2 = array_sum(array_column($nextAgentTeam, 'child_agent_count_1'));
// 团队分销商人数 = 一级分销商人数 + 一级分销商的团队分销商人数
$this->agent->child_agent_count_all = $this->agent->child_agent_count_1 + array_sum(array_column($nextAgentTeam, 'child_agent_count_all'));
// 二级分销订单金额 = 一级分销商的一级分销订单金额 + 一级分销商的自购订单金额
$this->agent->child_order_money_2 = array_sum(array_column($nextAgentTeam, 'child_order_money_1')) + array_sum(array_column($nextAgentTeam, 'child_order_money_0'));
// 团队分销订单金额 = 自购分销订单金额 + 一级分销订单金额 + 一级所有分销商的团队分销订单总金额
$this->agent->child_order_money_all = $this->agent->child_order_money_0 + $this->agent->child_order_money_1 + array_sum(array_column($nextAgentTeam, 'child_order_money_all'));
// 二级分销订单数量 = 一级分销商的一级分销订单数量 + 一级分销商的自购订单数量
$this->agent->child_order_count_2 = array_sum(array_column($nextAgentTeam, 'child_order_count_1')) + array_sum(array_column($nextAgentTeam, 'child_order_count_0'));
// 团队分销订单数量 = 自购分销订单数量 + 一级分销订单数量 + 一级所有分销商的团队分销订单总数量
$this->agent->child_order_count_all = $this->agent->child_order_count_0 + $this->agent->child_order_count_1 + array_sum(array_column($nextAgentTeam, 'child_order_count_all'));
// 一级分销商等级统计
$child_agent_level_1 = array_count_values(array_column($nextAgentTeam, 'level'));
ksort($child_agent_level_1);
$this->agent->child_agent_level_1 = $child_agent_level_1;
// 团队分销商等级统计 = 一级分销商等级 + 一级分销商的团队分销商等级
$child_agent_level_all = $this->childAgentLevelCount(array_column($nextAgentTeam, 'child_agent_level_all'), $this->agent->child_agent_level_1);
ksort($child_agent_level_all);
$this->agent->child_agent_level_all = $child_agent_level_all;
$this->agent->save();
// 分销商自动升级
if (!$this->getAgentUpgradeLock() && $upgrade) {
$canUpgradeLevel = $this->checkAgentUpgradeLevel();
if ($canUpgradeLevel) {
if ($this->config->isUpgradeCheck()) {
$this->agent->level_status = $canUpgradeLevel;
} else {
$this->agent->level = $canUpgradeLevel;
LogModel::add($this->user->id, 'agent', ['type' => 'level', 'level' => LevelModel::find($canUpgradeLevel)]);
}
$this->agent->save();
}
}
\think\Log::info('统计分销商业绩[ID=' . $this->user->id . '&nickname=' . $this->user->nickname . '] ---Ended');
}
$parentUserId = $this->getParentUserId();
if ($parentUserId) {
$this->createAsyncAgentUpgrade($parentUserId);
}
}
/**
* 统计团队分销商等级排布
*/
private function childAgentLevelCount($childAgentLevelArray, $childAgentLevel1Array)
{
$childAgentLevelCount = [];
foreach ($childAgentLevelArray as &$agentLevel) {
if (!empty($agentLevel)) {
$agentLevel = json_decode($agentLevel, true);
array_walk($agentLevel, function ($count, $level) use (&$childAgentLevelCount) {
if (isset($childAgentLevelCount[$level])) {
$childAgentLevelCount[$level] += $count;
} else {
$childAgentLevelCount[$level] = $count;
}
});
}
}
array_walk($childAgentLevel1Array, function ($count, $level) use (&$childAgentLevelCount) {
if (isset($childAgentLevelCount[$level])) {
$childAgentLevelCount[$level] += $count;
} else {
$childAgentLevelCount[$level] = $count;
}
});
return $childAgentLevelCount;
}
/**
* 获取下级分销商团队
*/
public function getNextAgentTeam()
{
if (!$this->isAgentAvaliable()) {
return [];
}
if (empty($this->nextAgentTeam)) {
$this->nextAgentTeam = AgentModel::hasWhere('user', function ($query) {
return $query->where('parent_user_id', $this->user->id);
})->column('user_id, Agent.level, child_user_count_1, child_user_count_all,child_agent_count_1, child_agent_count_all, child_order_money_0, child_order_money_1, child_order_money_all, child_order_count_0, child_order_count_1, child_order_count_all, child_agent_level_1, child_agent_level_all');
}
return $this->nextAgentTeam;
}
/**
* 获取下级直推团队用户
*/
public function getNextUserTeam()
{
if (!$this->isAgentAvaliable()) {
return [];
}
if (empty($this->nextUserTeam)) {
$this->nextUserTeam = UserModel::where(['parent_user_id' => $this->user->id, 'status' => 'normal'])->column('id');
}
return $this->nextUserTeam;
}
/**
* 获取可升级的分销商等级
*/
private function getNextAgentLevel()
{
$nextAgentLevel = [];
$agentLevel = $this->getAgentLevel();
if ($agentLevel) {
$nextAgentLevel = LevelModel::where('level', '>', $agentLevel)->order('level asc')->select();
}
return $nextAgentLevel;
}
/**
* 比对当前分销商条件是否满足升级规则
*/
private function checkAgentUpgradeLevel()
{
$nextAgentLevel = $this->getNextAgentLevel();
if (count($nextAgentLevel)) {
foreach ($nextAgentLevel as $level) {
$checkLevel[$level->level] = $this->isMatchUpgradeLevelRule($level);
// 不允许越级升级
if (!$this->config->isUpgradeJump()) break;
}
$checkLevel = array_reverse($checkLevel, true);
$canUpgradeLevel = array_search(true, $checkLevel);
if ($canUpgradeLevel) {
return $canUpgradeLevel;
}
}
return 0;
}
/**
* 分销商升级规则检查
*/
public function isMatchUpgradeLevelRule($level)
{
foreach ($level->upgrade_rules as $name => $value) {
$match[$name] = false;
switch ($name) {
case 'total_consume': // 用户消费金额
$match[$name] = $this->user->$name >= $value;
break;
case 'child_user_count_all': // 团队用户人数
case 'child_user_count_1': // 一级用户人数
case 'child_user_count_2': // 二级用户人数
case 'child_order_money_0': // 自购分销订单金额
case 'child_order_money_1': // 一级分销订单金额
case 'child_order_money_2': // 二级分销订单金额
case 'child_order_money_all': // 团队分销订单金额
case 'child_order_count_0': // 自购分销订单数量
case 'child_order_count_1': // 一级分销订单数量
case 'child_order_count_2': // 二级分销订单数量
case 'child_order_count_all': // 团队分销订单数量
case 'child_agent_count_1': // 一级分销商人数
case 'child_agent_count_2': // 二级分销商人数
case 'child_agent_count_all': // 团队分销商人数
$match[$name] = $this->agent->$name >= $value;
break;
case 'child_agent_level_1': // 一级分销商等级统计
case 'child_agent_level_all': // 团队分销商等级统计
$match[$name] = true;
if (count($value) > 0) {
if (empty($this->agent->$name)) {
$match[$name] = false;
} else {
foreach ($value as $k => $row) {
if (!isset(($this->agent->$name)[$row['level']]) || ($this->agent->$name)[$row['level']] < $row['count']) {
$match[$name] = false;
break;
}
}
}
}
break;
}
// ①满足任意一种条件:只要有一种符合立即返回可以升级状态
if (!$level->upgrade_type && $match[$name]) {
return true;
break;
}
// ②满足所有条件:不满足任意一种条件立即返回不可升级状态
if ($level->upgrade_type && !$match[$name]) {
return false;
break;
}
}
// 循环完所有的 如果是①的情况则代表都不符合条件,如果是②则代表都符合条件 返回对应状态即可
return boolval($level->upgrade_type);
}
}

View File

@@ -0,0 +1,116 @@
<?php
namespace addons\shopro\service\commission;
class Config
{
protected $config;
public function __construct()
{
$this->config = sheep_config('shop.commission');
}
// 覆盖默认分销设置
public function setConfig($config)
{
foreach ($config as $name => $value) {
$this->config[$name] = $value;
}
}
// 获取绑定推广关系的事件节点
public function getInviteLockEvent()
{
$becomeAgentEvent = $this->getBecomeAgentEvent();
if($becomeAgentEvent === 'user') {
return 'agent';
}
return $this->config['invite_lock'];
}
// 获取成为分销商的条件
public function getBecomeAgentEvent()
{
return $this->config['become_agent'];
}
// 分销商是否需要审核
public function isAgentCheck()
{
return boolval($this->config['agent_check']);
}
// 分销商升级审核
public function isUpgradeCheck()
{
return boolval($this->config['upgrade_check']);
}
// 分销商允许越级升级
public function isUpgradeJump()
{
return boolval($this->config['upgrade_jump']);
}
// 是否需要完善分销商表单信息
public function isAgentApplyForm()
{
return boolval($this->config['agent_form']['status']);
}
// 获取申请资料表单信息
public function getAgentForm()
{
return $this->config['agent_form'];
}
// 申请协议
public function getApplyProtocol()
{
return $this->config['apply_protocol'];
}
// 分销层级
public function getCommissionLevel()
{
return intval($this->config['level']);
}
// 是否允许分销自购
public function isSelfBuy()
{
return boolval($this->config['self_buy']);
}
// 是否显示升级条件
public function isUpgradeDisplay()
{
return boolval($this->config['upgrade_display']);
}
// 佣金结算价格类型 pay_price=支付金额 goods_price=商品价格 (都不含运费 因为运费对于平台和用户没有实际价值)
public function getRewardType()
{
return $this->config['reward_type'];
}
// 佣金结算节点 payed=支付后
public function getRewardEvent()
{
return $this->config['reward_event'];
}
// 退款是否扣除分销业绩
public function getRefundCommissionOrder()
{
return boolval($this->config['refund_commission_order']);
}
// 退款是否扣除佣金
public function getRefundCommissionReward()
{
return boolval($this->config['refund_commission_reward']);
}
}

View File

@@ -0,0 +1,141 @@
<?php
namespace addons\shopro\service\commission;
use app\admin\model\shopro\commission\Level as LevelModel;
use app\admin\model\shopro\commission\CommissionGoods as CommissionGoodsModel;
/**
* 分销商品
*/
class Goods
{
public $commissionGoods; // 分销商品
protected $commissionConfig = NULL; // 独立分销设置 默认无
protected $commissionRules; // 分销规则
protected $skuPriceId; // 商品规格ID
/**
* 获取商品实时分佣规则
*
* @param int $goods 商品
* @param int $skuId 商品规格ID
*/
public function __construct($goods, $skuPriceId = 0)
{
$commissionRules = CommissionGoodsModel::GOODS_COMMISSION_STATUS_OFF;
if(is_numeric($goods)) {
$this->commissionGoods = CommissionGoodsModel::where(['goods_id' => $goods, 'status' => CommissionGoodsModel::GOODS_COMMISSION_STATUS_ON])->find();
}else {
$this->commissionGoods = $goods;
}
$this->skuPriceId = $skuPriceId;
if ($this->commissionGoods) {
$commission_config = $this->commissionGoods->commission_config;
if ($commission_config['status']) {
$this->commissionConfig = $commission_config;
}
switch ($this->commissionGoods->self_rules) {
// 默认分销规则
case CommissionGoodsModel::GOODS_COMMISSION_RULES_DEFAULT:
$commissionRules = $this->getDefaultCommissionRules();
break;
// 独立分销规则
case CommissionGoodsModel::GOODS_COMMISSION_RULES_SELF:
$commissionRules = $this->getSelfCommissionRules();
break;
// 批量分销规则
case CommissionGoodsModel::GOODS_COMMISSION_RULES_BATCH:
$commissionRules = $this->getBatchCommissionRules();
break;
}
}
$this->commissionRules = $commissionRules;
}
public function getCommissionConfig()
{
return $this->commissionConfig;
}
public function getCommissionRules()
{
return $this->commissionRules;
}
/**
* 获取对应分销商等级、对应层级的商品佣金规则
*
* @param int $agentLevel 分销商等级(不是id)
* @param int $commissionLevel 分销商层级(默认一级)
*/
public function getCommissionLevelRule($agentLevel, $commissionLevel = 1)
{
if (isset($this->commissionRules[$agentLevel]) && isset($this->commissionRules[$agentLevel][$commissionLevel])) {
$commissionRule = $this->commissionRules[$agentLevel][$commissionLevel];
return $commissionRule;
}
return false;
}
/**
* 计算对应规则分销佣金
*
* @param int $commissionRule 分销规则
* @param int $amount 结算价格
* @param int $goodsNum 购买数量
*/
public function caculateGoodsCommission($commissionRule, $amount, $goodsNum = 1)
{
$commission = 0;
if (!empty($commissionRule['rate']) && $commissionRule['rate'] > 0) {
$commission = round($amount * $commissionRule['rate'] * 0.01, 2);
}
if (!empty($commissionRule['money']) && $commissionRule['money'] > 0) {
$commission = $commissionRule['money'] * $goodsNum;
}
return number_format($commission, 2, '.', '');
}
// 获取分销商等级默认规则
private function getDefaultCommissionRules()
{
$agentLevelRules = LevelModel::order('level asc')->column('commission_rules', 'level');
$commissionRules = [];
foreach ($agentLevelRules as $agentLevel => $rule) {
$rule = json_decode($rule, true);
foreach ($rule as $commission_level => $percent) {
$commission_level = explode('_', $commission_level);
$level = intval($commission_level[1]);
$commissionRules[$agentLevel][$level] = ['rate' => $percent];
}
}
return $commissionRules;
}
// 获取分销商品独立分佣规则
private function getSelfCommissionRules()
{
$commissionRules = [];
if (isset($this->commissionGoods->commission_rules[$this->skuPriceId])) {
$commissionRules = $this->commissionGoods->commission_rules[$this->skuPriceId];
}
return $commissionRules;
}
// 获取商品批量分佣规则
private function getBatchCommissionRules()
{
return $this->commissionGoods->commission_rules;
}
}

View File

@@ -0,0 +1,235 @@
<?php
namespace addons\shopro\service\commission;
use addons\shopro\service\commission\Config;
use app\admin\model\shopro\commission\Order as OrderModel;
use app\admin\model\shopro\commission\Reward as RewardModel;
use app\admin\model\shopro\commission\Log as LogModel;
/**
* 分佣业务
*/
class Order
{
public $config; // 分销设置
public $item; // 订单商品
public $buyer; // 购买人
public $goods; // 分销商品
public $amount = 0; // 订单商品核算价格
public $commissionLevel = 0; // 分销层级
public $selfBuy; // 是否自购
// 分销状态
const COMMISSION_CLOSE = 0; // 分销功能已关闭
/**
* 构造函数
*
* @param array $item 分销商品
*/
public function __construct($item)
{
$this->buyer = new Agent($item['user_id']);
$this->item = $item;
$this->config = new Config();
}
/**
* 检测并设置分销商品规则
*
* @return void
*/
public function checkAndSetCommission()
{
// 未找到订单商品或购买人
if (!$this->item || !$this->buyer->user) {
return false;
}
// 获取商品分销佣金规则
$this->goods = new Goods($this->item['goods_id'], $this->item['goods_sku_price_id']);
// 商品有独立分销设置,覆盖默认系统配置
if ($commissionConfig = $this->goods->getCommissionConfig()) {
$this->config->setConfig($commissionConfig);
}
// 获取系统设置分销层级
$this->commissionLevel = $this->config->getCommissionLevel();
// 分销中心已关闭
if (self::COMMISSION_CLOSE === $this->commissionLevel) {
return false;
}
// 商品不参与分销
if (!$this->goods->getCommissionRules()) {
return false;
}
// 是否自购分销订单
$this->selfBuy = $this->buyer->isAgentAvaliable() && $this->config->isSelfBuy();
// 未找到上级分销商且不是自购
if (!$this->buyer->getParentUserId() && !$this->selfBuy) {
return false;
}
// 获取商品结算价格(四舍五入精确到分)
$this->amount = $this->getGoodsCommissionAmount();
// 没有分佣的必要了
if ($this->amount <= 0) {
return false;
}
return true;
}
// 获取商品核算总金额
public function getGoodsCommissionAmount()
{
$commissionType = $this->config->getRewardType();
$amount = round(0, 2);
switch ($commissionType) {
case 'pay_price':
$amount = $this->item['pay_fee'];
break;
case 'goods_price':
$amount = round($this->item['goods_price'] * $this->item['goods_num'], 2);
break;
}
return $amount;
}
/**
* 创建分销订单
*/
public function createCommissionOrder()
{
$agentId = $this->selfBuy ? $this->buyer->user->id : $this->buyer->getParentUserId();
if (!$agentId) {
return false;
}
$commissionOrder = OrderModel::where('order_item_id', $this->item['id'])->find();
// 已添加过分销订单
if ($commissionOrder) {
return $commissionOrder;
}
$commissionOrderParams = [
'self_buy' => intval($this->selfBuy),
'order_id' => $this->item['order_id'],
'order_item_id' => $this->item['id'],
'buyer_id' => $this->buyer->user->id,
'goods_id' => $this->item['goods_id'],
'agent_id' => $agentId,
'amount' => $this->amount,
'reward_type' => $this->config->getRewardType(),
'commission_rules' => $this->goods->getCommissionRules(), // 记录当前设置的分佣规则,防止以后系统或者分销商设置有变导致更改历史规则
'reward_event' => $this->config->getRewardEvent(),
'commission_order_status' => $this->goods->commissionGoods->commission_order_status, // 是否计入分销业绩
'commission_reward_status' => RewardModel::COMMISSION_REWARD_STATUS_PENDING, // 佣金状态
];
$commissionOrder = OrderModel::create($commissionOrderParams);
// 添加分销商推广订单业绩
$orderAgent = new Agent($commissionOrder->agent_id);
if ($orderAgent->isAgentAvaliable() && $commissionOrder->commission_order_status) {
if($this->selfBuy) {
// 添加自购业绩
$orderAgent->agent->setInc('child_order_money_0', $commissionOrder->amount);
$orderAgent->agent->setInc('child_order_count_0', 1);
}else {
// 添加一级业绩
$orderAgent->agent->setInc('child_order_money_1', $commissionOrder->amount);
$orderAgent->agent->setInc('child_order_count_1', 1);
}
LogModel::add($commissionOrder['agent_id'], 'order', [
'type' => 'paid',
'order' => $commissionOrder,
'item' => $this->item,
'buyer' => $this->buyer->user
], $this->buyer->user);
}
return $commissionOrder;
}
/**
* 执行分销计划,递归往上分佣
*
* @param object $commissionOrder 分销订单
* @param object $currentAgent 当前待分佣的分销商 默认自购开始算
* @param int $currentCommissionLevel 当前分佣层级 一级(自购)开始算
*/
public function runCommissionPlan($commissionOrder, $currentAgent = null, $currentCommissionLevel = 1)
{
// 超出分佣层级
if ($this->commissionLevel < $currentCommissionLevel) {
return true;
}
// 当前层级为1且分销订单是自购订单 则当前分销商为购买人自己
if ($currentCommissionLevel === 1) {
$currentAgent = new Agent($commissionOrder->agent_id);
}
if ($currentAgent && !empty($currentAgent->user)) {
// 防止重复添加佣金
$commissionReward = RewardModel::where([
'commission_order_id' => $commissionOrder->id,
'agent_id' => $currentAgent->user->id,
'commission_level' => $currentCommissionLevel, // 分佣层级
])->find();
if (!$commissionReward) {
$currentAgentLevel = $currentAgent->getAgentLevel();
$currentCommissionLevelRule = $this->goods->getCommissionLevelRule($currentAgentLevel, $currentCommissionLevel);
if ($currentCommissionLevelRule) {
$commission = $this->goods->caculateGoodsCommission($currentCommissionLevelRule, $this->amount, $this->item['goods_num']);
if ($commission > 0) {
$commissionRewardParams = [
'order_id' => $commissionOrder->order_id,
'order_item_id' => $commissionOrder->order_item_id,
'buyer_id' => $commissionOrder->buyer_id, // 购买人
'commission_order_id' => $commissionOrder->id, // 分销订单ID
'agent_id' => $currentAgent->user->id, // 待分佣分销商ID
'type' => 'commission', // 奖金类型
'commission' => $commission, // 佣金
'status' => 0, // 佣金状态
'original_commission' => $commission, // 原始佣金
'commission_level' => $currentCommissionLevel, // 分佣层级
'commission_rules' => $currentCommissionLevelRule, // 分佣层级
'agent_level' => $currentAgentLevel // 分佣时分销商等级
];
$commissionReward = RewardModel::create($commissionRewardParams);
LogModel::add($commissionReward['agent_id'], 'reward', [
'type' => 'paid',
'reward' => $commissionReward,
], $this->buyer->user);
}
}
}
// 递归执行下一次
$currentCommissionLevel++;
// 超出分销层级
if ($this->commissionLevel < $currentCommissionLevel) {
return true;
}
$parentUserId = $currentAgent->getParentUserId();
// 执行下一级分销任务
if ($parentUserId) {
$parentAgent = new Agent($parentUserId);
$this->runCommissionPlan($commissionOrder, $parentAgent, $currentCommissionLevel);
} else {
return true;
}
}
}
}

View File

@@ -0,0 +1,274 @@
<?php
namespace addons\shopro\service\commission;
// use addons\shopro\library\Oper;
use app\admin\model\shopro\commission\Log as LogModel;
use app\admin\model\shopro\commission\Order as OrderModel;
use app\admin\model\shopro\commission\Reward as RewardModel;
use app\admin\model\shopro\user\User as UserModel;
use addons\shopro\service\Wallet as WalletService;
/**
* 结算奖金
*/
class Reward
{
protected $event = '';
/**
* 执行奖金计划,直接派发佣金
*
* @param string $event 分佣的事件
*/
public function __construct($event)
{
$this->event = $event;
}
/**
* 执行奖金计划, 派发整单佣金
*
* @param mixed $commissionOrder|$commissionOrderId 分销订单
*/
public function runCommissionRewardByOrder($commissionOrder)
{
if (is_numeric($commissionOrder)) {
$commissionOrder = OrderModel::find($commissionOrder);
}
// 未找到分销订单
if (!$commissionOrder) {
return false;
}
// 已经操作过了
if ($commissionOrder['commission_reward_status'] !== RewardModel::COMMISSION_REWARD_STATUS_PENDING) {
return false;
}
$rewardEvent = $commissionOrder['reward_event'];
// 不满足分佣事件
if ($rewardEvent !== $this->event && $this->event !== 'admin') {
return false;
}
// 更新分销订单结算状态
$commissionOrder->commission_reward_status = RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED;
$commissionOrder->commission_time = time();
$commissionOrder->save();
// 防止重复添加佣金
$commissionRewards = RewardModel::where([
'commission_order_id' => $commissionOrder['id'],
'status' => RewardModel::COMMISSION_REWARD_STATUS_PENDING,
])->select();
// 添加分销商收益、余额添加钱包、更新分销佣金结算状态、提醒分销商到账
if (count($commissionRewards) > 0) {
foreach ($commissionRewards as $commissionReward) {
$this->runCommissionReward($commissionReward);
}
}
return true;
}
/**
* 执行奖金计划,直接派发佣金
*
* @param mixed $commissionReward|$commissionRewardId 奖金记录
*/
public function runCommissionReward($commissionReward)
{
if (is_numeric($commissionReward)) {
$commissionReward = RewardModel::find($commissionReward);
}
// 未找到奖金记录
if (!$commissionReward) {
return false;
}
if ($commissionReward->status == RewardModel::COMMISSION_REWARD_STATUS_PENDING) {
$rewardAgent = new Agent($commissionReward->agent_id);
if ($rewardAgent->isAgentAvaliable()) {
$rewardAgent->agent->setInc('total_income', $commissionReward->commission);
$commissionReward->status = RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED;
$commissionReward->commission_time = time();
$commissionReward->save();
WalletService::change($rewardAgent->user, $commissionReward->type, $commissionReward->commission, 'reward_income', $commissionReward);
LogModel::add($rewardAgent->user->id, 'reward', [
'type' => $this->event,
'reward' => $commissionReward
]);
}
}
return true;
}
/**
* 扣除/取消 分销订单
*
* @param mixed $commissionOrder|$commissionOrderId 分销订单
* @param array $deductOrderMoney 扣除分销商的订单业绩 默认扣除
* @param array $deleteReward 扣除分销订单奖金 默认扣除
*/
public function backCommissionRewardByOrder($commissionOrder, $deductOrderMoney = true, $deleteReward = true)
{
if ($this->event !== 'admin' && $this->event !== 'refund') {
return false;
}
if ($this->event === 'refund') {
$config = new Config();
$deductOrderMoney = $config->getRefundCommissionOrder();
$deleteReward = $config->getRefundCommissionReward();
}
if (!$deductOrderMoney && !$deleteReward) {
return false;
}
if (is_numeric($commissionOrder)) {
$commissionOrder = OrderModel::find($commissionOrder);
}
// 未找到分销订单
if (!$commissionOrder) {
return false;
}
// 扣除分销商的订单业绩
if ($deductOrderMoney) {
// 变更分销订单状态
if ($commissionOrder->commission_order_status == OrderModel::COMMISSION_ORDER_STATUS_YES) { // 扣除
$commissionOrder->commission_order_status = OrderModel::COMMISSION_ORDER_STATUS_BACK;
$commissionOrder->save();
$orderAgent = new Agent($commissionOrder->agent_id);
// 扣除分销订单业绩
if($commissionOrder->self_buy) {
$orderAgent->agent->setDec('child_order_money_0', $commissionOrder->amount);
$orderAgent->agent->setDec('child_order_count_0', 1);
}else {
$orderAgent->agent->setDec('child_order_money_1', $commissionOrder->amount);
$orderAgent->agent->setDec('child_order_count_1', 1);
}
// 重新计算分销链条业绩
$orderAgent->createAsyncAgentUpgrade();
LogModel::add($orderAgent->user->id, 'order', [
'type' => $this->event,
'order' => $commissionOrder,
'buyer' => UserModel::find($commissionOrder->buyer_id)
]);
}
if ($commissionOrder->commission_order_status == OrderModel::COMMISSION_ORDER_STATUS_NO) { // 取消
$commissionOrder->commission_order_status = OrderModel::COMMISSION_ORDER_STATUS_CANCEL;
$commissionOrder->save();
}
}
// 变更分销订单佣金执行状态
if ($deleteReward) {
if ($commissionOrder->commission_reward_status == RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED) { // 扣除
$commissionOrder->commission_reward_status = RewardModel::COMMISSION_REWARD_STATUS_BACK;
$commissionOrder->save();
// 防止重复扣除佣金
$commissionRewards = RewardModel::where([
'commission_order_id' => $commissionOrder->id,
'status' => RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED,
])->select();
if (count($commissionRewards) > 0) {
// 扣除分销佣金
foreach ($commissionRewards as $commissionReward) {
$this->backCommissionReward($commissionReward);
}
}
}
}
if ($commissionOrder->commission_reward_status == RewardModel::COMMISSION_REWARD_STATUS_PENDING) { // 取消
$commissionOrder->commission_reward_status = RewardModel::COMMISSION_REWARD_STATUS_CANCEL;
$commissionOrder->save();
$commissionRewards = RewardModel::where([
'commission_order_id' => $commissionOrder->id,
'status' => RewardModel::COMMISSION_REWARD_STATUS_PENDING
])->select();
// 取消分销佣金
if (count($commissionRewards) > 0) {
foreach ($commissionRewards as $commissionReward) {
$this->cancelCommissionReward($commissionReward);
}
}
}
return true;
}
/**
* 扣除单笔分销佣金
*
* @param mixed $commissionReward|$commissionRewardId 奖金记录
*/
public function backCommissionReward($commissionReward)
{
if (is_numeric($commissionReward)) {
$commissionReward = RewardModel::find($commissionReward);
}
// 未找到奖金记录
if (!$commissionReward) {
return false;
}
if ($commissionReward->status == RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED) {
$rewardAgent = new Agent($commissionReward->agent_id);
if ($rewardAgent->agent->total_income < $commissionReward->commission) {
$rewardAgent->agent->total_income = 0;
$rewardAgent->agent->save();
} else {
$rewardAgent->agent->setDec('total_income', $commissionReward->commission);
}
WalletService::change($rewardAgent->user, $commissionReward->type, -$commissionReward->commission, 'reward_back', $commissionReward);
$commissionReward->status = RewardModel::COMMISSION_REWARD_STATUS_BACK;
$commissionReward->save();
LogModel::add($rewardAgent->user->id, 'reward', [
'type' => $this->event,
'reward' => $commissionReward,
]);
}
return true;
}
/**
* 取消单笔分销佣金
*
* @param mixed $commissionReward|$commissionRewardId 奖金记录
*/
public function cancelCommissionReward($commissionReward)
{
if (is_numeric($commissionReward)) {
$commissionReward = RewardModel::find($commissionReward);
}
// 未找到奖金记录
if (!$commissionReward) {
return false;
}
if ($commissionReward->status == RewardModel::COMMISSION_REWARD_STATUS_PENDING) {
$commissionReward->status = RewardModel::COMMISSION_REWARD_STATUS_CANCEL;
$commissionReward->save();
$rewardAgent = new Agent($commissionReward->agent_id);
LogModel::add($rewardAgent->user->id, 'reward', [
'type' => $this->event,
'reward' => $commissionReward,
]);
}
return true;
}
}