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

full activity

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