B2B2C mall system based on script engine freight structure sharing

script generation process

When adding a template, a script is generated, and when a merchant adds a product, the corresponding script is copied to the sku.

When updating the shipping template, send a message to consumers to update the template information of related SKUs in batches.

calculation process

cache data structure

The script and template ids are stored as part of the sku in order to calculate the total price of the sku using the same template.

Script example

function getShipPrice(){
  var firstWeight=1;            //首重
  var firstPrice=1;            //首重
  var continuedWeight=1;    //续重
  var continuedPrice=1;        //续重续费
  var shipPrice=firstPrice;
  //校验是否超出首重
  if(firstWeight < $goodsWeight){
    //续重 = 总重/续重
     var count = ($goodsWeight - firstWeight)/ continuedWeight;
    //总价 = 首重 + (续重*价格)
     shipPrice = firstPrice + count*continuedPrice;
  }
  return shipPrice; 

}
parameter Types of detailed
$goodsWeight double total product weight
$goodsNum Integer Total number of items

Return value example 2

domain model

Fake code:


 public Map<Integer, Double> setShippingPrice() {
        //获取购物车信息
        Cart cart = cache.get();
        //获取购物车中的所有的sku
        List<Sku> skuList = cart.getSkuList();
        //获取所有sku中的key集合,用来批量的从缓存中读取脚本信息
        List<String> keyList = getKeyList(skuList);
        //批量从缓存中读取所有的运费模板
        List<TemplateScript> templateScripts = getCache(keyList);
        //根据模板id进行分组,查询出所有用到相同模板的sku
        Map<Integer, List<Sku>> maps = group(templateScripts);
        Iterator iter = maps.entrySet().iterator();
        //用来存储每一个商家的运费
        Map<Integer, Double> shipPriceMap = new HashMap<>();
        Integer sellerId = 0;
        Double price = 0.0;
        while (iter.hasNext()) {
            List<Sku> sList = (List<Sku>) iter.next();
            TemplateScript script = sList.get(0).getScript();
            //计算总重量
            Double goodsWeigth = countWeight(sList);
            //计算总数量
            int goodsNum = countNum(sList);
            //调用脚本引擎计算价格
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("javascript");
            engine.put("$weight", goodsWeigth);
            engine.put("$num", goodsNum);
            String jsFunc = script;
            engine.eval(jsFunc);
            Invocable invocable = (Invocable) engine;
            double shipPrice = (Double) invocable.invokeFunction("price");
            price += shipPrice;
            Integer newselelrid = getSellerId(sList);
            if (!sellerId.equals(newselelrid)) {
                //记录这个sellerid的运费价格
                shipPriceMap.put(newselelrid, price);
                sellerId = newselelrid;
                price = 0.0;
            }
        }
        return shipPriceMap;


    }

Original article by javashop

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324041845&siteId=291194637