Authored by chenchao

use cache

... ... @@ -64,7 +64,14 @@ public class CacheKeyBuilder {
SELLER_LEVEL_FUNC_CONFIG("ufo:order:seller:stageLevel:func:Config", ""),
ORDER_DELIVER_NOTICE_TIMES("ufo:order:deliver:","orderCode:{}:date:{}:times");
ORDER_DELIVER_NOTICE_TIMES("ufo:order:deliver:","orderCode:{}:date:{}:times"),
SELLER_PENALTY_CONFIG("ufo:order:seller:penalty:config", ""),
SELLER_EARNEST_MONEY_CONFIG("ufo:order:seller:earnestMoney:config",""),
PRESALE_THRESHOLD("ufo:order:presale:threshold", "")
;
private String fix;
... ...
... ... @@ -16,4 +16,10 @@ public interface ExpiredTime {
int BUYER_ORDER_NUMS = 300;
int BUYER_CANCEL_PANELTY_CONFIG = 300;
/**
* 基础数据统一使用5分钟
* TODO 以后有需要 细化各个key
*/
int ORDER_BASE_CONFIG = 300;
}
... ...
package com.yohoufo.order.service.handler;
import com.yohobuy.ufo.model.order.constants.SkupType;
import com.yohoufo.dal.order.model.MetaConfig;
import com.yohoufo.order.model.dto.BuyerPenalty;
import com.yohoufo.order.model.dto.EarnestMoney;
import com.yohoufo.order.model.dto.ServiceFeeRate;
... ... @@ -31,10 +30,11 @@ public class SellerOrderComputeHandler extends AbsSellerOrderComputeHandler impl
protected EarnestMoney calEarnestMoney(BigDecimal prdPrice){
BigDecimal[] EARNEST_MONEY_RANGE = orderDynamicConfig.getEMR();
EarnestMoney earnestMoney = new EarnestMoney();
// earnestMoney.rate 动态可配
EarnestMoney emc = metaConfigService.getSellerEarnestMoney(SkupType.IN_STOCK);
BigDecimal rate = (emc==null || Objects.isNull(rate = emc.getRate())) ? new BigDecimal(0.08D) : rate;
earnestMoney.setRate(rate);
//TODO earnestMoney.rate 需要调整为动态可配
//
BigDecimal real = halfUp(prdPrice.multiply(rate));
final BigDecimal min = EARNEST_MONEY_RANGE[0] == null ? earnestMoney.getMin() : EARNEST_MONEY_RANGE[0];
final BigDecimal max = EARNEST_MONEY_RANGE[1] == null ? earnestMoney.getMax() : EARNEST_MONEY_RANGE[1];
... ...
... ... @@ -63,7 +63,7 @@ public class MetaConfigService {
bp = JSONObject.parseObject(value, BuyerPenalty.class);
}catch (Exception ex){
bp = new BuyerPenalty();
logger.warn("in getBuyerPenalty parse config value fail, key {} value {}", key, value);
logger.warn("in getBuyerPenalty parse config value fail, key {} value {}", key, value, ex);
}
return bp;
}
... ... @@ -95,7 +95,7 @@ public class MetaConfigService {
logger.info("in getEntryThreshold from DB {}", metaConfig);
configVal=metaConfig.getValue();
if (StringUtils.isNotBlank(configVal)){
cacheClient.setEx(rkb, configVal, 300);
cacheClient.setEx(rkb, configVal, ExpiredTime.ORDER_BASE_CONFIG);
}
}
... ... @@ -108,6 +108,7 @@ public class MetaConfigService {
return map;
}
/**
* {
... ... @@ -116,12 +117,20 @@ public class MetaConfigService {
* }
*/
public EarnestMoney getSellerEarnestMoney(SkupType skupType){
final String key = MetaConfigKey.SELLER_EARNEST_MONEY;
CacheKeyBuilder.KeyTemp kt = CacheKeyBuilder.KeyTemp.SELLER_EARNEST_MONEY_CONFIG;
RedisKeyBuilder rkb = kt.builderKeyOnlyFixed();
String configVal = cacheClient.get(rkb, String.class);
EarnestMoney em = null;
MetaConfig metaConfig = metaConfigMapper.selectByCode(key);
String metaVal = metaConfig.getValue();
if (StringUtils.isBlank(configVal)) {
final String key = MetaConfigKey.SELLER_EARNEST_MONEY;
MetaConfig metaConfig = metaConfigMapper.selectByCode(key);
configVal = metaConfig.getValue();
if (StringUtils.isNotBlank(configVal)){
cacheClient.setEx(rkb, configVal, ExpiredTime.ORDER_BASE_CONFIG);
}
}
try{
JSONObject emjo = JSONObject.parseObject(metaVal);
JSONObject emjo = JSONObject.parseObject(configVal);
String advanceSaleKey = "advanceSale", inStockKey = "inStock";
JSONObject asJO = emjo.getJSONObject(advanceSaleKey),
insJO = emjo.getJSONObject(inStockKey);
... ... @@ -140,7 +149,7 @@ public class MetaConfigService {
}catch (Exception ex){
logger.warn("in getSellerEarnestMoney parseObject fail metaVal {}", metaVal);
logger.warn("in getSellerEarnestMoney parseObject fail metaVal {}", configVal, ex);
}
return em;
}
... ... @@ -192,14 +201,25 @@ public class MetaConfigService {
}
}
* */
final String key = MetaConfigKey.SELLER_PENALTY;
MetaConfig metaConfig = metaConfigMapper.selectByCode(key);
String metaVal = metaConfig.getValue();
CacheKeyBuilder.KeyTemp kt = CacheKeyBuilder.KeyTemp.SELLER_PENALTY_CONFIG;
RedisKeyBuilder rkb = kt.builderKeyOnlyFixed();
String configVal = cacheClient.get(rkb, String.class);
if (StringUtils.isBlank(configVal)) {
final String key = MetaConfigKey.SELLER_PENALTY;
MetaConfig metaConfig = metaConfigMapper.selectByCode(key);
configVal = metaConfig.getValue();
if (StringUtils.isNotBlank(configVal)){
cacheClient.setEx(rkb, configVal, ExpiredTime.ORDER_BASE_CONFIG);
}
}
BuyerPenalty sellerPenalty = new BuyerPenalty();
try{
sellerPenalty = JSONObject.parseObject(metaVal, BuyerPenalty.class);
sellerPenalty = JSONObject.parseObject(configVal, BuyerPenalty.class);
}catch (Exception ex){
logger.warn("in getSellerPenalty parseObject fail, metaVal {}", metaVal);
logger.warn("in getSellerPenalty parseObject fail, metaVal {}", configVal, ex);
}
return sellerPenalty;
}
... ... @@ -210,12 +230,21 @@ public class MetaConfigService {
* @return
*/
public PreSaleOrderConfig getPreSaleOrderConfig(){
String key = MetaConfigKey.PRESALE_THRESHOLD;
MetaConfig metaConfig = metaConfigMapper.selectByCode(key);
String metaVal = metaConfig.getValue();
CacheKeyBuilder.KeyTemp kt = CacheKeyBuilder.KeyTemp.PRESALE_THRESHOLD;
RedisKeyBuilder rkb = kt.builderKeyOnlyFixed();
String configVal = cacheClient.get(rkb, String.class);
if (StringUtils.isBlank(configVal)) {
String key = MetaConfigKey.PRESALE_THRESHOLD;
MetaConfig metaConfig = metaConfigMapper.selectByCode(key);
configVal = metaConfig.getValue();
if (StringUtils.isNotBlank(configVal)){
cacheClient.setEx(rkb, configVal, ExpiredTime.ORDER_BASE_CONFIG);
}
}
PreSaleOrderConfig psoc;
try {
JSONObject psocJO = JSONObject.parseObject(metaVal);
JSONObject psocJO = JSONObject.parseObject(configVal);
JSONObject ntrJO = psocJO.getJSONObject("notice").getJSONObject("timeRange");
TimeUnit ntrtu = "h".equalsIgnoreCase(ntrJO.getString("timeUnit")) ? TimeUnit.HOURS : TimeUnit.DAYS;
PreSaleOrderConfig.Range ntr = PreSaleOrderConfig.Range.builder()
... ... @@ -232,7 +261,7 @@ public class MetaConfigService {
psoc = PreSaleOrderConfig.builder().autoCancelRange(actr)
.noticeRange(ntr).build();
}catch (Exception ex){
logger.warn("getPreSaleOrderConfig fail ,use default in Process self");
logger.warn("getPreSaleOrderConfig fail ,use default in Process self, configVal {}", configVal, ex);
psoc = PreSaleOrderConfig.getDefault();
}
return psoc;
... ...