Goods.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\common\Service\Commission;
  3. use app\common\model\commission\Level as LevelModel;
  4. use app\common\model\commission\CommissionGoods as CommissionGoodsModel;
  5. /**
  6. * 分销商品服务(全新设计)
  7. */
  8. class Goods
  9. {
  10. public $commissionGoods; // 分销商品模型
  11. protected $commissionConfig = null; // 独立分销设置
  12. protected $commissionRules; // 分销规则
  13. protected $skuPriceId; // 商品规格ID
  14. /**
  15. * 构造函数
  16. *
  17. * @param int|object $goods 商品ID或商品对象
  18. * @param int $skuPriceId 商品规格ID
  19. */
  20. public function __construct($goods, $skuPriceId = 0)
  21. {
  22. $this->skuPriceId = $skuPriceId;
  23. // 获取分销商品信息
  24. if (is_numeric($goods)) {
  25. $this->commissionGoods = CommissionGoodsModel::where([
  26. 'goods_id' => $goods,
  27. 'status' => 1 // 参与分销
  28. ])->find();
  29. } else {
  30. $this->commissionGoods = $goods;
  31. }
  32. if ($this->commissionGoods) {
  33. // 获取独立配置
  34. $this->commissionConfig = $this->commissionGoods->getCommissionConfig();
  35. // 获取佣金规则
  36. $this->commissionRules = $this->loadCommissionRules();
  37. } else {
  38. $this->commissionRules = false;
  39. }
  40. }
  41. /**
  42. * 加载佣金规则
  43. * @return array|false
  44. */
  45. private function loadCommissionRules()
  46. {
  47. if (!$this->commissionGoods) {
  48. return false;
  49. }
  50. switch ($this->commissionGoods->rule_type) {
  51. case 0: // 默认规则
  52. return $this->getDefaultCommissionRules();
  53. case 1: // 独立规则
  54. return $this->commissionGoods->getCommissionRules($this->skuPriceId);
  55. case 2: // 批量规则
  56. return $this->commissionGoods->getCommissionRules(0);
  57. default:
  58. return false;
  59. }
  60. }
  61. public function getCommissionConfig()
  62. {
  63. return $this->commissionConfig;
  64. }
  65. public function getCommissionRules()
  66. {
  67. return $this->commissionRules;
  68. }
  69. /**
  70. * 获取对应分销商等级、对应层级的商品佣金规则
  71. *
  72. * @param int $agentLevel 分销商等级
  73. * @param int $commissionLevel 分销层级
  74. * @return array|false
  75. */
  76. public function getCommissionLevelRule($agentLevel, $commissionLevel = 1)
  77. {
  78. if (empty($this->commissionRules)) {
  79. return false;
  80. }
  81. if (isset($this->commissionRules[$agentLevel][$commissionLevel])) {
  82. return $this->commissionRules[$agentLevel][$commissionLevel];
  83. }
  84. return false;
  85. }
  86. /**
  87. * 计算分销佣金
  88. *
  89. * @param array $commissionRule 分销规则
  90. * @param string $amount 结算价格
  91. * @param int $goodsNum 购买数量
  92. * @return string
  93. */
  94. public function caculateGoodsCommission($commissionRule, $amount, $goodsNum = 1)
  95. {
  96. if (empty($commissionRule)) {
  97. return '0.00';
  98. }
  99. // 使用新的统一计算方法
  100. if ($this->commissionGoods) {
  101. return $this->commissionGoods->calculateCommission($commissionRule, $amount, $goodsNum);
  102. }
  103. return '0.00';
  104. }
  105. /**
  106. * 获取分销商等级默认规则
  107. * @return array
  108. */
  109. private function getDefaultCommissionRules()
  110. {
  111. $agentLevelRules = LevelModel::order('level asc')->column('commission_rules', 'level');
  112. $commissionRules = [];
  113. foreach ($agentLevelRules as $agentLevel => $rule) {
  114. $rule = json_decode($rule, true);
  115. if (!is_array($rule)) {
  116. continue;
  117. }
  118. foreach ($rule as $commission_level => $percent) {
  119. $commission_level = explode('_', $commission_level);
  120. if (count($commission_level) >= 2) {
  121. $levelName = $commission_level[1];
  122. // 将英文级别名映射为数字级别
  123. $levelMap = [
  124. 'first' => 1, // 一级
  125. 'second' => 2, // 二级
  126. 'third' => 3, // 三级
  127. ];
  128. $level = isset($levelMap[$levelName]) ? $levelMap[$levelName] : intval($levelName);
  129. $commissionRules[$agentLevel][$level] = [
  130. 'type' => 'rate',
  131. 'value' => $percent,
  132. 'rate' => $percent,
  133. 'money' => ''
  134. ];
  135. }
  136. }
  137. }
  138. return $commissionRules;
  139. }
  140. }