'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); } }