Skip to content

Commit

Permalink
shipping free/price rule
Browse files Browse the repository at this point in the history
  • Loading branch information
DeminAlexey committed Dec 2, 2020
1 parent 98e8b28 commit e8b8bdd
Show file tree
Hide file tree
Showing 11 changed files with 596 additions and 11 deletions.
56 changes: 53 additions & 3 deletions src/main/java/alex/cache/ExpressCache.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,66 @@
package alex.cache;

import alex.Application;
import alex.entity.ExpressCompanyEntity;
import alex.lib.express.FreeRule;
import alex.lib.express.PriceRule;
import alex.lib.express.ProvincePrice;
import alex.repository.ExpressCompanyRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component
public class ExpressCache {
private static List<ExpressCompanyEntity> companies;
private static ExpressCompanyRepository expressCompanyRepository;
private static FreeRule freeRule;
private static PriceRule priceRule;

@Autowired
private void autowire(ExpressCompanyRepository expressCompanyRepository) {
ExpressCache.expressCompanyRepository = expressCompanyRepository;
}

@PostConstruct
public static synchronized void init() {
companies = expressCompanyRepository.findAll();
var list = Application.JDBC_TEMPLATE.queryForList("select * from system where entity='shipping'");
String json = null;
ObjectMapper objectMapper = new ObjectMapper();
for (var map : list) {
json = (String) map.get("value");
if (json.length() < 10) {
continue;
}
switch ((String)map.get("attribute")) {
case "freeRule":
try {
freeRule = objectMapper.readValue(json, FreeRule.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
break;
case "priceRule":
try {
priceRule = objectMapper.readValue(json, PriceRule.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
break;
}
}
if (freeRule == null) {
freeRule = new FreeRule();
}
if (priceRule == null) {
priceRule = new PriceRule();
}
}

public static List<ExpressCompanyEntity> getCompanies() {
Expand All @@ -37,8 +82,13 @@ public static String getNameById(long id) {
return null;
}

@Autowired
private void autowire(ExpressCompanyRepository expressCompanyRepository) {
ExpressCache.expressCompanyRepository = expressCompanyRepository;
public static FreeRule getFreeRule() {
return freeRule;
}

public static PriceRule getPriceRule() {
return priceRule;
}


}
4 changes: 4 additions & 0 deletions src/main/java/alex/cache/RegionCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ public static Region getRegion(long code) {
return area;
}

public static List<Region> getProvinces() {
return provinces;
}

public static String getRegionJson() {
return regionJson;
}
Expand Down
155 changes: 155 additions & 0 deletions src/main/java/alex/controller/admin/sys/Shipping.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package alex.controller.admin.sys;

import alex.Application;
import alex.cache.ExpressCache;
import alex.cache.RegionCache;
import alex.entity.ExpressCompanyEntity;
import alex.lib.AdminHelper;
import alex.lib.Helper;
import alex.lib.JsonResult;
import alex.lib.express.FreeRule;
import alex.lib.express.PriceRule;
import alex.lib.express.ProvincePrice;
import alex.repository.ExpressCompanyRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.ini4j.Reg;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -15,6 +23,7 @@

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@Controller(value = "admin/sys/shipping")
@RequestMapping(path = "/admin/sys/shipping")
Expand Down Expand Up @@ -86,4 +95,150 @@ public String postDelete(HttpServletRequest request) {
jsonResult.setMsg("已删除");
return AdminHelper.msgPage(jsonResult, request);
}

@GetMapping(value = "freeRule")
public ModelAndView getFreeRule(HttpServletRequest request) {
ModelAndView modelAndView = Helper.newModelAndView("admin/sys/shipping/freeRule", request);
StringBuilder provincesHtml = new StringBuilder();
for (var p : RegionCache.getProvinces()) {
provincesHtml.append("<option value=\"")
.append(p.getCode())
.append("\">")
.append(p.getName())
.append("</option>");
}
modelAndView.addObject("provincesHtml", provincesHtml);
modelAndView.addObject("title", "包邮规则");
ObjectMapper objectMapper = new ObjectMapper();
try {
modelAndView.addObject("freeRule",
objectMapper.writeValueAsString(ExpressCache.getFreeRule()));
} catch (JsonProcessingException e) {
e.printStackTrace();

}
return modelAndView;
}

@PostMapping(value = "freeRule", produces = "application/json;charset=UTF-8")
@ResponseBody
public String postFreeRule(HttpServletRequest request) {
JsonResult jsonResult = new JsonResult();
FreeRule freeRule = new FreeRule();
freeRule.setEnable(Helper.longValue(request.getParameter("enable")) > 0);
freeRule.setPrice(Helper.bigDecimalValue(request.getParameter("price")).multiply(new BigDecimal(100)).longValue());
String exclude = request.getParameter("exclude");
if (exclude != null && exclude.length() > 1) {
for(var code : exclude.split(",")){
var l = Helper.longValue(code);
if (RegionCache.getRegion(l) != null) {
freeRule.getExclude().add(l);
}
}
}

ObjectMapper objectMapper = new ObjectMapper();
try {
String json = objectMapper.writeValueAsString(freeRule);
Application.JDBC_TEMPLATE.update("update system set value = ? where entity='shipping' and attribute = 'freeRule'",
json);
} catch (JsonProcessingException e) {
e.printStackTrace();
jsonResult.setMsg("未知错误,请联系管理员");
return jsonResult.toString();
}
ExpressCache.init();
jsonResult.setMsg("包邮规则更新完成");
return AdminHelper.msgPage(jsonResult, request);

}

@GetMapping(value = "priceRule")
public ModelAndView getPriceRule(HttpServletRequest request) {
ModelAndView modelAndView = Helper.newModelAndView("admin/sys/shipping/priceRule", request);
StringBuilder provincesHtml = new StringBuilder();
for (var p : RegionCache.getProvinces()) {
provincesHtml.append("<option value=\"")
.append(p.getCode())
.append("\">")
.append(p.getName())
.append("</option>");
}

ObjectMapper objectMapper = new ObjectMapper();
try {
modelAndView.addObject("priceRule",
objectMapper.writeValueAsString(ExpressCache.getPriceRule()));
} catch (JsonProcessingException e) {
e.printStackTrace();

}
modelAndView.addObject("title", "运费规则");
modelAndView.addObject("provincesHtml", provincesHtml);
return modelAndView;
}

@PostMapping(value = "priceRule", produces = "application/json;charset=UTF-8")
@ResponseBody
public String postPriceRule(HttpServletRequest request) {
PriceRule priceRule = new PriceRule();
JsonResult jsonResult = new JsonResult();
priceRule.setFirstPrice(Helper.bigDecimalValue(request.getParameter("firstPrice")).multiply(new BigDecimal(100)).longValue());
priceRule.setFirstWeight(Helper.longValue(request.getParameter("firstWeight")));
priceRule.setAdditionalPrice(Helper.bigDecimalValue(request.getParameter("additionalPrice")).multiply(new BigDecimal(100)).longValue());
priceRule.setAdditionalWeight(Helper.longValue(request.getParameter("additionalWeight")));
if (priceRule.getFirstWeight() < 1 || priceRule.getFirstPrice() < 1) {
jsonResult.setMsg("首重重量或价格不得为空");
return jsonResult.toString();
}
if (priceRule.getAdditionalWeight() < 1 || priceRule.getAdditionalPrice() < 1) {
jsonResult.setMsg("续重重量或价格不得为空");
return jsonResult.toString();
}
priceRule.setOtherDefault(Helper.longValue(request.getParameter("otherDefault")) > 0);
// 自定义地区规则
String[] provinces = request.getParameterValues("provinces");
String[] price1 = request.getParameterValues("price1");
String[] price2 = request.getParameterValues("price2");
if (provinces != null && price1 != null && price2 != null) {
if (provinces.length != price1.length || provinces.length != price2.length) {
jsonResult.setMsg("自定义地区数据格式错误");
return jsonResult.toString();
}
for (int i = 0; i < provinces.length; i++) {
ProvincePrice provincePrice = new ProvincePrice();
for (var code : provinces[i].split(",")) {
long l = Helper.longValue(code);
if (l % 10000 > 0 || RegionCache.getRegion(l) == null) {
continue;
}
provincePrice.getProvinces().add(l);
}
provincePrice.setFirstPrice(Helper.bigDecimalValue(price1[i]).multiply(new BigDecimal(100)).longValue());
provincePrice.setAdditionalPrice(Helper.bigDecimalValue(price2[i]).multiply(new BigDecimal(100)).longValue());
if (provincePrice.getFirstPrice() <= 0 || provincePrice.getAdditionalPrice() <= 0) {
jsonResult.setMsg("价格不得为空或零");
return jsonResult.toString();
}
if (provincePrice.getProvinces().size() > 0) {
priceRule.getProvincePrices().add(provincePrice);
}
}
}


ObjectMapper objectMapper = new ObjectMapper();
try {
String json = objectMapper.writeValueAsString(priceRule);
Application.JDBC_TEMPLATE.update("update system set value = ? where entity='shipping' and attribute = 'priceRule'",
json);
} catch (JsonProcessingException e) {
e.printStackTrace();
jsonResult.setMsg("未知错误,请联系管理员");
return jsonResult.toString();
}
ExpressCache.init();
jsonResult.setMsg("运费规则更新完成");
return AdminHelper.msgPage(jsonResult, request);
}
}
18 changes: 15 additions & 3 deletions src/main/java/alex/lib/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
* helper class
*/
public class Helper {

public static BigDecimal bigDecimalValue(String str) {
try {
return new BigDecimal(str);
} catch (Exception e) {
return new BigDecimal(0);
}
}

public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (Byte b : bytes) {
Expand Down Expand Up @@ -199,6 +208,7 @@ public static ModelAndView newModelAndView(String viewName, HttpServletRequest r
public static String priceFormat(BigDecimal bigDecimal) {
return priceFormat(bigDecimal.multiply(new BigDecimal(100)).longValue());
}

public static String priceFormat(long l) {
String prefix = l < 0 ? "-" : "";
l = Math.abs(l);
Expand Down Expand Up @@ -231,6 +241,7 @@ public static String randomString(int len) {

/**
* remove specify strings
*
* @param str
* @param args
* @return
Expand All @@ -244,10 +255,11 @@ public static String stringRemove(String str, String... args) {
}
return str;
}

public static String stringValue(Object obj) {
if (obj == null) {
return null;
}
if (obj == null) {
return null;
}
return obj.toString();
}

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/alex/lib/express/FreeRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package alex.lib.express;

import java.util.ArrayList;
import java.util.List;

/**
* 包邮规则
*/
public class FreeRule {
// 是否启用包邮规则
private boolean enable = false;

// 包邮价
private long price;

//排除省份
private final List<Long> exclude = new ArrayList<>();

public boolean isEnable() {
return enable;
}

public void setEnable(boolean enable) {
this.enable = enable;
}

public List<Long> getExclude() {
return exclude;
}

public long getPrice() {
return price;
}

public void setPrice(long price) {
this.price = price;
}
}
Loading

0 comments on commit e8b8bdd

Please sign in to comment.