CommissionRules.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\common\model\commission;
  3. use think\Model;
  4. /**
  5. * 分销佣金规则模型
  6. */
  7. class CommissionRules extends Model
  8. {
  9. protected $name = 'shop_commission_rules';
  10. // 佣金类型常量
  11. const TYPE_RATE = 'rate'; // 比例佣金
  12. const TYPE_MONEY = 'money'; // 固定金额
  13. // 状态常量
  14. const STATUS_DISABLED = 0; // 禁用
  15. const STATUS_ENABLED = 1; // 启用
  16. protected $type = [
  17. 'value' => 'float',
  18. 'created_at' => 'timestamp',
  19. 'updated_at' => 'timestamp'
  20. ];
  21. /**
  22. * 关联商品
  23. */
  24. public function goods()
  25. {
  26. return $this->belongsTo('CommissionGoods', 'goods_id', 'goods_id');
  27. }
  28. /**
  29. * 计算佣金
  30. * @param string $amount 结算金额
  31. * @param int $quantity 商品数量
  32. * @return string
  33. */
  34. public function calculateCommission($amount, $quantity = 1)
  35. {
  36. if ($this->type === self::TYPE_RATE) {
  37. // 比例佣金:金额 * 比例 / 100
  38. return bcmul(
  39. bcmul($amount, $this->value, 4),
  40. '0.01',
  41. 2
  42. );
  43. } else {
  44. // 固定金额:金额 * 数量
  45. return bcmul($this->value, $quantity, 2);
  46. }
  47. }
  48. /**
  49. * 获取特定规则
  50. * @param int $goodsId 商品ID
  51. * @param int $skuId SKU ID
  52. * @param int $agentLevel 分销商等级
  53. * @param int $level 分销层级
  54. * @return CommissionRules|null
  55. */
  56. public static function getRule($goodsId, $skuId, $agentLevel, $level)
  57. {
  58. return self::where([
  59. 'goods_id' => $goodsId,
  60. 'sku_id' => $skuId,
  61. 'agent_level' => $agentLevel,
  62. 'level' => $level,
  63. 'status' => self::STATUS_ENABLED
  64. ])->find();
  65. }
  66. /**
  67. * 批量获取商品规则
  68. * @param int $goodsId 商品ID
  69. * @param int $skuId SKU ID,0表示批量规则
  70. * @return array
  71. */
  72. public static function getGoodsRules($goodsId, $skuId = 0)
  73. {
  74. $rules = self::where([
  75. 'goods_id' => $goodsId,
  76. 'sku_id' => $skuId,
  77. 'status' => self::STATUS_ENABLED
  78. ])->order('agent_level asc, level asc')->select();
  79. $result = [];
  80. foreach ($rules as $rule) {
  81. $result[$rule['agent_level']][$rule['level']] = [
  82. 'type' => $rule['type'],
  83. 'value' => $rule['value'],
  84. 'rate' => $rule['type'] === self::TYPE_RATE ? $rule['value'] : '',
  85. 'money' => $rule['type'] === self::TYPE_MONEY ? $rule['value'] : ''
  86. ];
  87. }
  88. return $result;
  89. }
  90. /**
  91. * 批量设置规则
  92. * @param int $goodsId 商品ID
  93. * @param int $skuId SKU ID
  94. * @param array $rulesData 规则数据
  95. * @return bool
  96. */
  97. public static function setGoodsRules($goodsId, $skuId, $rulesData)
  98. {
  99. // 删除现有规则
  100. self::where([
  101. 'goods_id' => $goodsId,
  102. 'sku_id' => $skuId
  103. ])->delete();
  104. // 批量插入新规则
  105. $insertData = [];
  106. foreach ($rulesData as $agentLevel => $levelRules) {
  107. foreach ($levelRules as $level => $rule) {
  108. if (isset($rule['type'], $rule['value']) && bccomp($rule['value'], '0', 4) > 0) {
  109. $insertData[] = [
  110. 'goods_id' => $goodsId,
  111. 'sku_id' => $skuId,
  112. 'agent_level' => $agentLevel,
  113. 'level' => $level,
  114. 'type' => $rule['type'],
  115. 'value' => $rule['value'],
  116. 'status' => self::STATUS_ENABLED
  117. ];
  118. }
  119. }
  120. }
  121. if (!empty($insertData)) {
  122. return self::insertAll($insertData);
  123. }
  124. return true;
  125. }
  126. /**
  127. * 删除商品的所有规则
  128. * @param int $goodsId 商品ID
  129. * @return bool
  130. */
  131. public static function deleteGoodsRules($goodsId)
  132. {
  133. return self::where('goods_id', $goodsId)->delete();
  134. }
  135. /**
  136. * 复制规则到新商品
  137. * @param int $fromGoodsId 源商品ID
  138. * @param int $toGoodsId 目标商品ID
  139. * @return bool
  140. */
  141. public static function copyRules($fromGoodsId, $toGoodsId)
  142. {
  143. $sourceRules = self::where('goods_id', $fromGoodsId)->select();
  144. if ($sourceRules->isEmpty()) {
  145. return true;
  146. }
  147. $insertData = [];
  148. foreach ($sourceRules as $rule) {
  149. $insertData[] = [
  150. 'goods_id' => $toGoodsId,
  151. 'sku_id' => $rule['sku_id'],
  152. 'agent_level' => $rule['agent_level'],
  153. 'level' => $rule['level'],
  154. 'type' => $rule['type'],
  155. 'value' => $rule['value'],
  156. 'status' => $rule['status']
  157. ];
  158. }
  159. return self::insertAll($insertData);
  160. }
  161. }