提交 27a516a0 authored 作者: wudepeng's avatar wudepeng

full activity

上级 3d70b703
...@@ -15,10 +15,10 @@ import org.springframework.web.bind.annotation.*; ...@@ -15,10 +15,10 @@ import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.stream.Stream;
/** /**
* @Auther: wudepeng * @Auther: wudepeng
...@@ -137,78 +137,8 @@ public class ActivityController { ...@@ -137,78 +137,8 @@ public class ActivityController {
return t; return t;
} }
@PostMapping("/caculateCartDiscount")
public Result caculateCartDiscount(@RequestBody String[] cartIds) {
// Activity activity = null;
// List<Activity> activityList = new ArrayList<>();
BigDecimal itemPrice = BigDecimal.ZERO;
BigDecimal lessAmount = BigDecimal.ZERO;
String discount = null;
int itemCount = 0;
Map<String, Object> map = new HashMap<>();
for (String cartId : cartIds) {
Optional<TbCfCartRecordR> byId = cartRecordRRepository.findById(cartId);
if (byId.isPresent()) {
TbCfCartRecordR cart = byId.get();
itemPrice = itemPrice.add(cart.getItemPrice().multiply(new BigDecimal(cart.getItemNum())));
itemCount += cart.getItemNum();
// String itemId = cart.getItemId();
// activity = checkActivity(itemId);
// activityList.add(activity);
}
}
if (activityRepository.existsByUseType(1)) {
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));
map.put("activity", activity);
if ("1".equals(type) || "2".equals(type)) {
for (Condition con : conList) {
double key = con.getKey();
BigDecimal threshold = new BigDecimal(key);
double value = con.getValue();
discount = String.valueOf(value);
String msg = "";
if ("1".equals(type)) {
msg = "$" + discount;
} else {
msg = discount + "%";
}
if (itemPrice.compareTo(threshold) < 0) {
lessAmount = threshold.subtract(itemPrice);
map.put("message", "Spend " + lessAmount + " more to Enjoy " + msg + " off !");
break;
} else {
map.put("message", "Congrats, you've got " + msg + " off !");
}
}
} else if ("3".equals(type)) {
for (Condition con : conList) {
int key = (int) con.getKey();
double value = con.getValue();
discount = String.valueOf(value);
if (itemCount < key) {
map.put("message", "Buy " + (key - itemCount) + " more item to Enjoy " + discount + "% off !");
break;
} else {
map.put("message", "Congrats, you've got " + discount + "% off !");
}
}
}
}
return new Result(map);
}
public Activity checkActivity(String itemId) { public Activity checkActivity(String itemId) {
Result result = new Result(); Result result = new Result();
Activity activity = null; Activity activity = null;
...@@ -256,12 +186,14 @@ public class ActivityController { ...@@ -256,12 +186,14 @@ public class ActivityController {
} }
@PostMapping("/caculateCartDiscount") @PostMapping("/caculateCartDiscount")
public Result caculateCartDiscount1(@RequestBody String[] cartIds) { public Result caculateCartDiscount(@RequestBody String[] cartIds) {
BigDecimal itemPrice = BigDecimal.ZERO; BigDecimal itemPrice = BigDecimal.ZERO;
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
NumberFormat nf = NumberFormat.getInstance();
List<Activity> fullList = activityRepository.findAllByUseType(1); List<Activity> fullList = activityRepository.findAllByUseType(1);
if (fullList.size() == 0 || fullList == null) if (fullList.size() == 0 || fullList == null)
...@@ -275,14 +207,12 @@ public class ActivityController { ...@@ -275,14 +207,12 @@ public class ActivityController {
StringBuffer msg = new StringBuffer(); StringBuffer msg = new StringBuffer();
map.put("activity", activity); map.put("activity", activity);
if (cartIds == null) { boolean exist=false;
msg.append(activity.getContent());
}
int itemCount = 0; int itemCount = 0;
for (String cartId : cartIds) { for (String cartId : cartIds) {
Optional<TbCfCartRecordR> byId = cartRecordRRepository.findById(cartId); Optional<TbCfCartRecordR> byId = cartRecordRRepository.findById(cartId);
if (byId.isPresent()) { if (byId.isPresent()) {
exist=true;
TbCfCartRecordR cart = byId.get(); TbCfCartRecordR cart = byId.get();
itemCount += cart.getItemNum(); itemCount += cart.getItemNum();
itemPrice = itemPrice.add(cart.getItemPrice() itemPrice = itemPrice.add(cart.getItemPrice()
...@@ -293,46 +223,63 @@ public class ActivityController { ...@@ -293,46 +223,63 @@ public class ActivityController {
BigDecimal lessAmount; BigDecimal lessAmount;
String discount; String discount;
for (Condition con : conList) { for (Condition con : conList) {
if ("1".equals(type) || "2".equals(type)) {
double key = con.getKey();
BigDecimal threshold = new BigDecimal(key);
double value = con.getValue(); double value = con.getValue();
discount = String.valueOf(value); double key = con.getKey();
discount = nf.format(value);
if ("1".equals(type)) { if ("1".equals(type)) {
discount = "$" + discount; discount = "$" + discount;
} else { } else {
discount = discount + "%"; discount = discount + "%";
} }
while (itemPrice.compareTo(threshold) >= 0) {
msg.append("Congrats, you've got " + discount + " off ");
}
if (itemPrice.compareTo(threshold) < 0) { if ("1".equals(type) || "2".equals(type)) {
BigDecimal threshold = new BigDecimal(key);
if (itemPrice.compareTo(threshold) >= 0) {
msg.delete(0,msg.length());
msg.append("Congrats, you've got " + discount + " off! ");
}
else {
lessAmount = threshold.subtract(itemPrice); lessAmount = threshold.subtract(itemPrice);
msg.append("Spend " + lessAmount + " more to Enjoy " + discount + " off !"); String less = nf.format(lessAmount);
msg.append("Spend " + less + " more to Enjoy " + discount + " off !");
break; break;
} /*else { }
msg.delete(0, msg.length());
msg.append("Congrats, you've got " + discount + " off !");
}*/
} else if ("3".equals(type)) { } else if ("3".equals(type)) {
int key = (int) con.getKey();
double value = con.getValue();
/** /**
* 2 *
* 2==9 * 2件9折
* 3==8 * 3件8折
* 5件7折
*
* 如果用户购买1件商品,提示:Buy 1 more item to Enjoy 10% off !
* 如果用户购买2件商品,提示:Congrats, you've got 30% off ! Buy 1 more item to Enjoy 20% off !
* .
* .
* .
* 如果用户购大于或者等于5件商品,提示:Congrats, you've got 30% off !
*
*/ */
int num = (int) con.getKey();
if (itemCount >= key) { if (itemCount >= num) {
msg.append("Congrats, you've got " + value + "% off !"); msg.delete(0,msg.length());
} else { msg.append("Congrats, you've got " + discount + " off ! ");
msg.append("Buy " + (key - itemCount) + " more item to Enjoy " + value + "% off !");
} }
else {
msg.append("Buy " + (num - itemCount) + " more item to Enjoy " + discount + " off !");
break;
} }
} }
}
if (!exist){
msg.delete(0,msg.length());
msg.append(activity.getContent());
}
map.put("message", msg); map.put("message", msg);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论