123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace app\common\Service\Commission;
- use app\common\model\commission\Level as LevelModel;
- use app\common\model\commission\CommissionGoods as CommissionGoodsModel;
- /**
- * 分销商品服务(全新设计)
- */
- class Goods
- {
- public $commissionGoods; // 分销商品模型
- protected $commissionConfig = null; // 独立分销设置
- protected $commissionRules; // 分销规则
- protected $skuPriceId; // 商品规格ID
- /**
- * 构造函数
- *
- * @param int|object $goods 商品ID或商品对象
- * @param int $skuPriceId 商品规格ID
- */
- public function __construct($goods, $skuPriceId = 0)
- {
- $this->skuPriceId = $skuPriceId;
-
- // 获取分销商品信息
- if (is_numeric($goods)) {
- $this->commissionGoods = CommissionGoodsModel::where([
- 'goods_id' => $goods,
- 'status' => 1 // 参与分销
- ])->find();
- } else {
- $this->commissionGoods = $goods;
- }
- if ($this->commissionGoods) {
- // 获取独立配置
- $this->commissionConfig = $this->commissionGoods->getCommissionConfig();
-
- // 获取佣金规则
- $this->commissionRules = $this->loadCommissionRules();
- } else {
- $this->commissionRules = false;
- }
- }
- /**
- * 加载佣金规则
- * @return array|false
- */
- private function loadCommissionRules()
- {
- if (!$this->commissionGoods) {
- return false;
- }
- switch ($this->commissionGoods->rule_type) {
- case 0: // 默认规则
- return $this->getDefaultCommissionRules();
-
- case 1: // 独立规则
- return $this->commissionGoods->getCommissionRules($this->skuPriceId);
-
- case 2: // 批量规则
- return $this->commissionGoods->getCommissionRules(0);
-
- default:
- return false;
- }
- }
- public function getCommissionConfig()
- {
- return $this->commissionConfig;
- }
- public function getCommissionRules()
- {
- return $this->commissionRules;
- }
- /**
- * 获取对应分销商等级、对应层级的商品佣金规则
- *
- * @param int $agentLevel 分销商等级
- * @param int $commissionLevel 分销层级
- * @return array|false
- */
- public function getCommissionLevelRule($agentLevel, $commissionLevel = 1)
- {
- if (empty($this->commissionRules)) {
- return false;
- }
- if (isset($this->commissionRules[$agentLevel][$commissionLevel])) {
- return $this->commissionRules[$agentLevel][$commissionLevel];
- }
-
- return false;
- }
- /**
- * 计算分销佣金
- *
- * @param array $commissionRule 分销规则
- * @param string $amount 结算价格
- * @param int $goodsNum 购买数量
- * @return string
- */
- public function caculateGoodsCommission($commissionRule, $amount, $goodsNum = 1)
- {
- if (empty($commissionRule)) {
- return '0.00';
- }
- // 使用新的统一计算方法
- if ($this->commissionGoods) {
- return $this->commissionGoods->calculateCommission($commissionRule, $amount, $goodsNum);
- }
- return '0.00';
- }
- /**
- * 获取分销商等级默认规则
- * @return array
- */
- private function getDefaultCommissionRules()
- {
- $agentLevelRules = LevelModel::order('level asc')->column('commission_rules', 'level');
- $commissionRules = [];
-
- foreach ($agentLevelRules as $agentLevel => $rule) {
- $rule = json_decode($rule, true);
- if (!is_array($rule)) {
- continue;
- }
-
- foreach ($rule as $commission_level => $percent) {
- $commission_level = explode('_', $commission_level);
- if (count($commission_level) >= 2) {
- $levelName = $commission_level[1];
- // 将英文级别名映射为数字级别
- $levelMap = [
- 'first' => 1, // 一级
- 'second' => 2, // 二级
- 'third' => 3, // 三级
- ];
- $level = isset($levelMap[$levelName]) ? $levelMap[$levelName] : intval($levelName);
- $commissionRules[$agentLevel][$level] = [
- 'type' => 'rate',
- 'value' => $percent,
- 'rate' => $percent,
- 'money' => ''
- ];
- }
- }
- }
-
- return $commissionRules;
- }
- }
|