提交 3d70b703 authored 作者: 吴德鹏's avatar 吴德鹏

update activity

上级 16a2ebec
......@@ -195,7 +195,7 @@ public class ActivityController {
double value = con.getValue();
discount = String.valueOf(value);
if (itemCount < key) {
map.put("message", "Buy " + (key-itemCount) + " more item to Enjoy " + discount + "% off !");
map.put("message", "Buy " + (key - itemCount) + " more item to Enjoy " + discount + "% off !");
break;
} else {
map.put("message", "Congrats, you've got " + discount + "% off !");
......@@ -255,4 +255,91 @@ public class ActivityController {
return activity;
}
@PostMapping("/caculateCartDiscount")
public Result caculateCartDiscount1(@RequestBody String[] cartIds) {
BigDecimal itemPrice = BigDecimal.ZERO;
Map<String, Object> map = new HashMap<>();
List<Activity> fullList = activityRepository.findAllByUseType(1);
if (fullList.size() == 0 || fullList == null)
return new Result(ResultCodeEnum.SERVICE_ERROR.getCode(), "There is no online activity");
Activity activity = fullList.get(0);
String type = activity.getType().toString();
List<Condition> conList = JSONObject.parseArray(activity.getCondition(), Condition.class);
Collections.sort(conList, Comparator.comparing(Condition::getKey));
StringBuffer msg = new StringBuffer();
map.put("activity", activity);
if (cartIds == null) {
msg.append(activity.getContent());
}
int itemCount = 0;
for (String cartId : cartIds) {
Optional<TbCfCartRecordR> byId = cartRecordRRepository.findById(cartId);
if (byId.isPresent()) {
TbCfCartRecordR cart = byId.get();
itemCount += cart.getItemNum();
itemPrice = itemPrice.add(cart.getItemPrice()
.multiply(new BigDecimal(cart.getItemNum())));
}
}
BigDecimal lessAmount;
String discount;
for (Condition con : conList) {
if ("1".equals(type) || "2".equals(type)) {
double key = con.getKey();
BigDecimal threshold = new BigDecimal(key);
double value = con.getValue();
discount = String.valueOf(value);
if ("1".equals(type)) {
discount = "$" + discount;
} else {
discount = discount + "%";
}
while (itemPrice.compareTo(threshold) >= 0) {
msg.append("Congrats, you've got " + discount + " off ");
}
if (itemPrice.compareTo(threshold) < 0) {
lessAmount = threshold.subtract(itemPrice);
msg.append("Spend " + lessAmount + " more to Enjoy " + discount + " off !");
break;
} /*else {
msg.delete(0, msg.length());
msg.append("Congrats, you've got " + discount + " off !");
}*/
} else if ("3".equals(type)) {
int key = (int) con.getKey();
double value = con.getValue();
/**
* 2
* 2==9
* 3==8
*/
if (itemCount >= key) {
msg.append("Congrats, you've got " + value + "% off !");
} else {
msg.append("Buy " + (key - itemCount) + " more item to Enjoy " + value + "% off !");
}
}
}
map.put("message", msg);
return new Result(map);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论