|
|
@@ -8,121 +8,134 @@ import com.fs.store.enums.ShippingTempEnum;
|
|
|
import com.fs.store.service.IFsShippingTemplatesFreeService;
|
|
|
import com.fs.store.service.IFsShippingTemplatesRegionService;
|
|
|
import com.fs.store.service.IFsShippingTemplatesService;
|
|
|
-import com.fs.store.strategy.impl.CountBasedStrategy;
|
|
|
-import com.fs.store.strategy.impl.WeightBasedStrategy;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.lang.ObjectUtils;
|
|
|
import org.apache.http.util.Asserts;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
/**
|
|
|
- * 运费模板管理器
|
|
|
+ * 运费模板管理器(懒加载,变更后 invalidate 即可热更新)
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class ShippingTemplateManager {
|
|
|
@Autowired
|
|
|
+ @Lazy
|
|
|
private IFsShippingTemplatesService fsShippingTemplatesService;
|
|
|
@Autowired
|
|
|
+ @Lazy
|
|
|
private IFsShippingTemplatesRegionService fsShippingTemplatesRegionService;
|
|
|
@Autowired
|
|
|
+ @Lazy
|
|
|
private IFsShippingTemplatesFreeService fsShippingTemplatesFreeService;
|
|
|
|
|
|
private final Map<Long, ShippingTemplate> TEMPLATES = new ConcurrentHashMap<>();
|
|
|
|
|
|
private static final Map<Integer, String> TEMPLATE_TYPE_MAP = new ConcurrentHashMap<>();
|
|
|
static {
|
|
|
- TEMPLATE_TYPE_MAP.put(1,"count");
|
|
|
- TEMPLATE_TYPE_MAP.put(2,"weight");
|
|
|
- TEMPLATE_TYPE_MAP.put(3,"volume");
|
|
|
+ TEMPLATE_TYPE_MAP.put(1, "count");
|
|
|
+ TEMPLATE_TYPE_MAP.put(2, "weight");
|
|
|
+ TEMPLATE_TYPE_MAP.put(3, "volume");
|
|
|
}
|
|
|
|
|
|
- @PostConstruct
|
|
|
- public void init(){
|
|
|
- log.info("正在初始化运费模板管理器...");
|
|
|
- // 查询运费模板
|
|
|
- List<FsShippingTemplates> fsShippingTemplates = fsShippingTemplatesService.listAllTemplate();
|
|
|
- for (FsShippingTemplates template : fsShippingTemplates) {
|
|
|
- String type = TEMPLATE_TYPE_MAP.get(template.getType());
|
|
|
- Asserts.notNull(type,"不支持的模板类型");
|
|
|
-
|
|
|
- ShippingTemplate shippingTemplate = new ShippingTemplate(template.getId(),type);
|
|
|
-
|
|
|
- log.info("正在加载模板配送区域及运费");
|
|
|
- List<FsShippingTemplatesRegion> fsShippingTemplatesRegions = fsShippingTemplatesRegionService.selectTempRegionByTempIdAndCityId(template.getId());
|
|
|
- for (FsShippingTemplatesRegion templatesRegion : fsShippingTemplatesRegions) {
|
|
|
- Region region = new Region();
|
|
|
- region.setCityId(templatesRegion.getCityId());
|
|
|
- // 如果是按重量
|
|
|
- if(ObjectUtil.equal(templatesRegion.getType(), ShippingTempEnum.TYPE_2.getValue())){
|
|
|
- region.setFirstWeight(templatesRegion.getFirst());
|
|
|
- region.setFirstWeightFee(templatesRegion.getFirstPrice());
|
|
|
- region.setAdditionalWeight(templatesRegion.getContinues());
|
|
|
- region.setAdditionalWeightFee(templatesRegion.getContinuePrice());
|
|
|
- } else if(ObjectUtil.equal(templatesRegion.getType(),ShippingTempEnum.TYPE_1.getValue())){
|
|
|
- region.setFirstItem(templatesRegion.getFirst());
|
|
|
- region.setFirstItemFee(templatesRegion.getFirstPrice());
|
|
|
- region.setAdditionalItemFee(templatesRegion.getContinuePrice());
|
|
|
- } else if(ObjectUtil.equal(templatesRegion.getType(), ShippingTempEnum.TYPE_3.getValue())){
|
|
|
- region.setFirstVolume(templatesRegion.getFirst());
|
|
|
- region.setFirstVolumeFee(templatesRegion.getFirstPrice());
|
|
|
- region.setAdditionalVolume(templatesRegion.getContinues());
|
|
|
- region.setAdditionalVolumeFee(templatesRegion.getContinuePrice());
|
|
|
- }
|
|
|
- shippingTemplate.addRegion(region);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- log.info("模板加载区域完成: {}个", fsShippingTemplatesRegions.size());
|
|
|
-
|
|
|
- log.info("正在加载模板包邮规则");
|
|
|
- List<FsShippingTemplatesFree> freeShippingRules = fsShippingTemplatesFreeService.selectFreeShippingRuleByTempId(template.getId());
|
|
|
- for (FsShippingTemplatesFree freeShippingRule : freeShippingRules) {
|
|
|
- FreeShippingRule rule = new FreeShippingRule();
|
|
|
- rule.setRegion(freeShippingRule.getCityId());
|
|
|
-
|
|
|
- if(ObjectUtil.equal(freeShippingRule.getType(), ShippingTempEnum.TYPE_2.getValue())){
|
|
|
- rule.setMinWeight(freeShippingRule.getNumber());
|
|
|
- rule.setMinAmount(freeShippingRule.getPrice());
|
|
|
- } else if(ObjectUtil.equal(freeShippingRule.getType(),ShippingTempEnum.TYPE_1.getValue())){
|
|
|
- rule.setMinCount(freeShippingRule.getNumber());
|
|
|
- rule.setMinAmount(freeShippingRule.getPrice());
|
|
|
- } else if(ObjectUtil.equal(freeShippingRule.getType(), ShippingTempEnum.TYPE_3.getValue())){
|
|
|
- rule.setMinVolume(freeShippingRule.getNumber());
|
|
|
- rule.setMinAmount(freeShippingRule.getPrice());
|
|
|
- }
|
|
|
-
|
|
|
- shippingTemplate.addFreeShippingRule(rule);
|
|
|
- }
|
|
|
- log.info("模板包邮规则加载完成: {} 个", freeShippingRules);
|
|
|
-
|
|
|
- this.addTemplate(shippingTemplate);
|
|
|
+ public void invalidate(Long templateId) {
|
|
|
+ if (templateId != null) {
|
|
|
+ TEMPLATES.remove(templateId);
|
|
|
}
|
|
|
- log.info("运费模板管理器初始化完成: {}个", fsShippingTemplates.size());
|
|
|
}
|
|
|
|
|
|
- public void addTemplate(ShippingTemplate template) {
|
|
|
- TEMPLATES.put(template.getId(), template);
|
|
|
+ public void remove(Long templateId) {
|
|
|
+ invalidate(templateId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void reload(Long templateId) {
|
|
|
+ invalidate(templateId);
|
|
|
+ getTemplate(templateId);
|
|
|
}
|
|
|
|
|
|
public ShippingTemplate getTemplate(Long templateId) {
|
|
|
- return TEMPLATES.get(templateId);
|
|
|
+ if (templateId == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ ShippingTemplate cached = TEMPLATES.get(templateId);
|
|
|
+ if (cached != null) {
|
|
|
+ return cached;
|
|
|
+ }
|
|
|
+ ShippingTemplate loaded = buildTemplateFromDb(templateId);
|
|
|
+ if (loaded != null) {
|
|
|
+ TEMPLATES.put(templateId, loaded);
|
|
|
+ }
|
|
|
+ return loaded;
|
|
|
}
|
|
|
|
|
|
public BigDecimal calculateShippingFee(Long templateId, ShippingOrder order) {
|
|
|
- ShippingTemplate template = TEMPLATES.get(templateId);
|
|
|
+ ShippingTemplate template = getTemplate(templateId);
|
|
|
if (template == null) {
|
|
|
throw new IllegalArgumentException("找不到运费模板: " + templateId);
|
|
|
}
|
|
|
return template.calculateShippingFee(order);
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
+ private ShippingTemplate buildTemplateFromDb(Long templateId) {
|
|
|
+ FsShippingTemplates template = fsShippingTemplatesService.selectFsShippingTemplatesById(templateId);
|
|
|
+ if (template == null || ObjectUtil.equal(template.getIsDel(), 1)) {
|
|
|
+ log.warn("运费模板不存在或已删除 templateId={}", templateId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String type = TEMPLATE_TYPE_MAP.get(template.getType());
|
|
|
+ Asserts.notNull(type, "不支持的模板类型");
|
|
|
+
|
|
|
+ ShippingTemplate shippingTemplate = new ShippingTemplate(template.getId(), type);
|
|
|
+
|
|
|
+ List<FsShippingTemplatesRegion> regions =
|
|
|
+ fsShippingTemplatesRegionService.selectTempRegionByTempIdAndCityId(template.getId());
|
|
|
+ for (FsShippingTemplatesRegion templatesRegion : regions) {
|
|
|
+ Region region = new Region();
|
|
|
+ region.setCityId(templatesRegion.getCityId());
|
|
|
+ if (ObjectUtil.equal(templatesRegion.getType(), ShippingTempEnum.TYPE_2.getValue())) {
|
|
|
+ region.setFirstWeight(templatesRegion.getFirst());
|
|
|
+ region.setFirstWeightFee(templatesRegion.getFirstPrice());
|
|
|
+ region.setAdditionalWeight(templatesRegion.getContinues());
|
|
|
+ region.setAdditionalWeightFee(templatesRegion.getContinuePrice());
|
|
|
+ } else if (ObjectUtil.equal(templatesRegion.getType(), ShippingTempEnum.TYPE_1.getValue())) {
|
|
|
+ region.setFirstItem(templatesRegion.getFirst());
|
|
|
+ region.setFirstItemFee(templatesRegion.getFirstPrice());
|
|
|
+ region.setAdditionalItemFee(templatesRegion.getContinuePrice());
|
|
|
+ } else if (ObjectUtil.equal(templatesRegion.getType(), ShippingTempEnum.TYPE_3.getValue())) {
|
|
|
+ region.setFirstVolume(templatesRegion.getFirst());
|
|
|
+ region.setFirstVolumeFee(templatesRegion.getFirstPrice());
|
|
|
+ region.setAdditionalVolume(templatesRegion.getContinues());
|
|
|
+ region.setAdditionalVolumeFee(templatesRegion.getContinuePrice());
|
|
|
+ }
|
|
|
+ shippingTemplate.addRegion(region);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<FsShippingTemplatesFree> freeShippingRules =
|
|
|
+ fsShippingTemplatesFreeService.selectFreeShippingRuleByTempId(template.getId());
|
|
|
+ for (FsShippingTemplatesFree freeShippingRule : freeShippingRules) {
|
|
|
+ FreeShippingRule rule = new FreeShippingRule();
|
|
|
+ rule.setRegion(freeShippingRule.getCityId());
|
|
|
+ if (ObjectUtil.equal(freeShippingRule.getType(), ShippingTempEnum.TYPE_2.getValue())) {
|
|
|
+ rule.setMinWeight(freeShippingRule.getNumber());
|
|
|
+ rule.setMinAmount(freeShippingRule.getPrice());
|
|
|
+ } else if (ObjectUtil.equal(freeShippingRule.getType(), ShippingTempEnum.TYPE_1.getValue())) {
|
|
|
+ rule.setMinCount(freeShippingRule.getNumber());
|
|
|
+ rule.setMinAmount(freeShippingRule.getPrice());
|
|
|
+ } else if (ObjectUtil.equal(freeShippingRule.getType(), ShippingTempEnum.TYPE_3.getValue())) {
|
|
|
+ rule.setMinVolume(freeShippingRule.getNumber());
|
|
|
+ rule.setMinAmount(freeShippingRule.getPrice());
|
|
|
+ }
|
|
|
+ shippingTemplate.addFreeShippingRule(rule);
|
|
|
+ }
|
|
|
+
|
|
|
+ log.debug("懒加载运费模板 templateId={}, regions={}, freeRules={}",
|
|
|
+ templateId, regions.size(), freeShippingRules.size());
|
|
|
+ return shippingTemplate;
|
|
|
+ }
|
|
|
+}
|