123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace app\common\model\commission;
- use think\Model;
- /**
- * 分销佣金规则模型
- */
- class CommissionRules extends Model
- {
- protected $name = 'shop_commission_rules';
- // 佣金类型常量
- const TYPE_RATE = 'rate'; // 比例佣金
- const TYPE_MONEY = 'money'; // 固定金额
- // 状态常量
- const STATUS_DISABLED = 0; // 禁用
- const STATUS_ENABLED = 1; // 启用
- protected $type = [
- 'value' => 'float',
- 'created_at' => 'timestamp',
- 'updated_at' => 'timestamp'
- ];
- /**
- * 关联商品
- */
- public function goods()
- {
- return $this->belongsTo('CommissionGoods', 'goods_id', 'goods_id');
- }
- /**
- * 计算佣金
- * @param string $amount 结算金额
- * @param int $quantity 商品数量
- * @return string
- */
- public function calculateCommission($amount, $quantity = 1)
- {
- if ($this->type === self::TYPE_RATE) {
- // 比例佣金:金额 * 比例 / 100
- return bcmul(
- bcmul($amount, $this->value, 4),
- '0.01',
- 2
- );
- } else {
- // 固定金额:金额 * 数量
- return bcmul($this->value, $quantity, 2);
- }
- }
- /**
- * 获取特定规则
- * @param int $goodsId 商品ID
- * @param int $skuId SKU ID
- * @param int $agentLevel 分销商等级
- * @param int $level 分销层级
- * @return CommissionRules|null
- */
- public static function getRule($goodsId, $skuId, $agentLevel, $level)
- {
- return self::where([
- 'goods_id' => $goodsId,
- 'sku_id' => $skuId,
- 'agent_level' => $agentLevel,
- 'level' => $level,
- 'status' => self::STATUS_ENABLED
- ])->find();
- }
- /**
- * 批量获取商品规则
- * @param int $goodsId 商品ID
- * @param int $skuId SKU ID,0表示批量规则
- * @return array
- */
- public static function getGoodsRules($goodsId, $skuId = 0)
- {
- $rules = self::where([
- 'goods_id' => $goodsId,
- 'sku_id' => $skuId,
- 'status' => self::STATUS_ENABLED
- ])->order('agent_level asc, level asc')->select();
- $result = [];
- foreach ($rules as $rule) {
- $result[$rule['agent_level']][$rule['level']] = [
- 'type' => $rule['type'],
- 'value' => $rule['value'],
- 'rate' => $rule['type'] === self::TYPE_RATE ? $rule['value'] : '',
- 'money' => $rule['type'] === self::TYPE_MONEY ? $rule['value'] : ''
- ];
- }
- return $result;
- }
- /**
- * 批量设置规则
- * @param int $goodsId 商品ID
- * @param int $skuId SKU ID
- * @param array $rulesData 规则数据
- * @return bool
- */
- public static function setGoodsRules($goodsId, $skuId, $rulesData)
- {
- // 删除现有规则
- self::where([
- 'goods_id' => $goodsId,
- 'sku_id' => $skuId
- ])->delete();
- // 批量插入新规则
- $insertData = [];
- foreach ($rulesData as $agentLevel => $levelRules) {
- foreach ($levelRules as $level => $rule) {
- if (isset($rule['type'], $rule['value']) && bccomp($rule['value'], '0', 4) > 0) {
- $insertData[] = [
- 'goods_id' => $goodsId,
- 'sku_id' => $skuId,
- 'agent_level' => $agentLevel,
- 'level' => $level,
- 'type' => $rule['type'],
- 'value' => $rule['value'],
- 'status' => self::STATUS_ENABLED
- ];
- }
- }
- }
- if (!empty($insertData)) {
- return self::insertAll($insertData);
- }
- return true;
- }
- /**
- * 删除商品的所有规则
- * @param int $goodsId 商品ID
- * @return bool
- */
- public static function deleteGoodsRules($goodsId)
- {
- return self::where('goods_id', $goodsId)->delete();
- }
- /**
- * 复制规则到新商品
- * @param int $fromGoodsId 源商品ID
- * @param int $toGoodsId 目标商品ID
- * @return bool
- */
- public static function copyRules($fromGoodsId, $toGoodsId)
- {
- $sourceRules = self::where('goods_id', $fromGoodsId)->select();
-
- if ($sourceRules->isEmpty()) {
- return true;
- }
- $insertData = [];
- foreach ($sourceRules as $rule) {
- $insertData[] = [
- 'goods_id' => $toGoodsId,
- 'sku_id' => $rule['sku_id'],
- 'agent_level' => $rule['agent_level'],
- 'level' => $rule['level'],
- 'type' => $rule['type'],
- 'value' => $rule['value'],
- 'status' => $rule['status']
- ];
- }
- return self::insertAll($insertData);
- }
- }
|