package com.oms.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.oms.entity.PricingRule; import com.oms.mapper.PricingRuleMapper; import com.oms.dto.PricingRuleDTO; import com.oms.enums.GenericStatus; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDateTime; import java.util.List; @Service @RequiredArgsConstructor public class PricingRuleService { private final PricingRuleMapper mapper; public List listRules(String ruleType, String status) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); if (ruleType != null) wrapper.eq(PricingRule::getRuleType, ruleType); if (status != null) wrapper.eq(PricingRule::getStatus, status); wrapper.orderByDesc(PricingRule::getCreatedAt); return mapper.selectList(wrapper); } public PricingRule getRule(Long id) { return mapper.selectById(id); } public PricingRule createRule(PricingRuleDTO dto) { PricingRule rule = new PricingRule(); rule.setRuleNo("PR" + System.currentTimeMillis()); rule.setRuleName(dto.getRuleName()); rule.setRuleType(dto.getRuleType()); rule.setProductSkuId(dto.getProductSkuId()); rule.setChannelId(dto.getChannelId()); rule.setBasePrice(dto.getBasePrice()); rule.setDiscountRate(dto.getDiscountRate()); rule.setFixedPrice(dto.getFixedPrice()); rule.setPriority(dto.getPriority()); rule.setStatus(GenericStatus.ACTIVE.getCode()); rule.setStartDate(dto.getStartDate()); rule.setEndDate(dto.getEndDate()); rule.setCreatedAt(LocalDateTime.now()); mapper.insert(rule); return rule; } public PricingRule updateRule(Long id, PricingRuleDTO dto) { PricingRule rule = mapper.selectById(id); if (rule == null) return null; rule.setRuleName(dto.getRuleName()); rule.setRuleType(dto.getRuleType()); rule.setProductSkuId(dto.getProductSkuId()); rule.setChannelId(dto.getChannelId()); rule.setBasePrice(dto.getBasePrice()); rule.setDiscountRate(dto.getDiscountRate()); rule.setFixedPrice(dto.getFixedPrice()); rule.setPriority(dto.getPriority()); rule.setStartDate(dto.getStartDate()); rule.setEndDate(dto.getEndDate()); rule.setUpdatedAt(LocalDateTime.now()); mapper.updateById(rule); return rule; } public void deleteRule(Long id) { mapper.deleteById(id); } public BigDecimal calculatePrice(Long skuId, String channelId, BigDecimal basePrice) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(PricingRule::getProductSkuId, skuId.toString()) .eq(PricingRule::getChannelId, channelId) .eq(PricingRule::getStatus, GenericStatus.ACTIVE.getCode()); PricingRule rule = mapper.selectOne(wrapper); if (rule == null) return basePrice; // 校验规则有效期 LocalDateTime now = LocalDateTime.now(); if (rule.getStartDate() != null && now.isBefore(rule.getStartDate())) return basePrice; if (rule.getEndDate() != null && now.isAfter(rule.getEndDate())) return basePrice; if (rule.getFixedPrice() != null) { return rule.getFixedPrice(); } if (rule.getDiscountRate() != null) { return basePrice.multiply(rule.getDiscountRate()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP); } return basePrice; } }