后台shop++购物车请求的数据是一个Map结构的数据,业务需要要在类似的购物车中加一个套餐。
那么套餐里面就包含商品信息了,觉得不用他的Map了于是封装了两个类:
套餐信息显示类,商品信息显示类
请求返回数据都正常,但是js就是解析不了后台返回来的数据。也不报错。
主要原因是SetMealView 类的setMeal原来是一个SetMeal对象,后来改成了String就可以了。
/**
* @Auther: lanhaifeng
* @Date: 2018/12/3 0003 09:10
* @Description:套餐信息显示类
*/
public class SetMealView {
private String setMeal;//套餐名称
private List<ProductDateilView> viewList=new ArrayList<ProductDateilView>();//套餐商品信息
public SetMealView() {
}
public SetMealView(String setMeal, List<ProductDateilView> viewList) {
this.setMeal = setMeal;
this.viewList = viewList;
}
public String getSetMeal() {
return setMeal;
}
public void setSetMeal(String setMeal) {
this.setMeal = setMeal;
}
public List<ProductDateilView> getViewList() {
return viewList;
}
public void setViewList(List<ProductDateilView> viewList) {
this.viewList = viewList;
}
}
/**
* @Auther: lanhaifeng
* @Date: 2018/12/3 0003 08:58
* @Description:商品信息显示类,不想搞那么多Map
* 所以封装一下
*/
public class ProductDateilView {
private long skuId;//商品详细编号
private String skuName;//商品详细名称
private String skuPath;//连接地址
private int stock;//库存数量
private String specificationValues;////型号
private String brand;//品牌名称
private long productCategory_id;//商品类别编号
private String skuThumbnail;//商品图标
private BigDecimal price;//价格
private int quantity;//数量
private BigDecimal subtotal;//价格小计
public long getSkuId() {
return skuId;
}
public void setSkuId(long skuId) {
this.skuId = skuId;
}
public String getSkuName() {
return skuName;
}
public void setSkuName(String skuName) {
this.skuName = skuName;
}
public String getSkuPath() {
return skuPath;
}
public void setSkuPath(String skuPath) {
this.skuPath = skuPath;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getSpecificationValues() {
return specificationValues;
}
public void setSpecificationValues(String specificationValues) {
this.specificationValues = specificationValues;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public long getProductCategory_id() {
return productCategory_id;
}
public void setProductCategory_id(long productCategory_id) {
this.productCategory_id = productCategory_id;
}
public String getSkuThumbnail() {
return skuThumbnail;
}
public void setSkuThumbnail(String skuThumbnail) {
this.skuThumbnail = skuThumbnail;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public BigDecimal getSubtotal() {
return subtotal;
}
public void setSubtotal(BigDecimal subtotal) {
this.subtotal = subtotal;
}
}
那么请求数据的时候在原来获取购物车的基础上进行了改造。
/**
* 查询详细的购物清单
* @param currentCart
* @return
*/
public Map<String, Object> getSkuDetail( @CurrentCart Cart currentCart){
Map<String, Object> data = new HashMap<>();
//查询购物车所有数据信息
Setting setting = SystemUtils.getSetting();
if (currentCart != null && !currentCart.isEmpty()) {
data.put("quantity", currentCart.getQuantity(false));
data.put("rewardPoint", currentCart.getRewardPoint());
data.put("effectivePrice", currentCart.getEffectivePrice());
data.put("discount", currentCart.getDiscount());
//List<Map<String, Object>> cartItems = new ArrayList<>();//存套餐外的商品
List<ProductDateilView> cartItems = new ArrayList<>();//存套餐外的商品
List<SetMealView> setmealist = new ArrayList<>();//setmealist保存套餐内的商品
//这里做一个小小的改动,要对套餐进行一个分组
for (CartItem cartItem : currentCart) {
ProductDateilView productDateilView=new ProductDateilView();
//Map<String, Object> item = new HashMap<>();
Sku sku = cartItem.getSku();
Product product=sku.getProduct();
//item.put("skuId", sku.getId());
productDateilView.setSkuId(sku.getId());
//item.put("skuName", sku.getName());
productDateilView.setSkuName(sku.getName());
//item.put("skuPath", sku.getPath());
productDateilView.setSkuPath(sku.getPath());
//item.put("stock",sku.getStock());//库存
productDateilView.setStock(sku.getStock());
//item.put("specificationValues",sku.getSpecificationValues().get(0).getValue());//型号
productDateilView.setSpecificationValues(sku.getSpecificationValues().get(0).getValue());
if(null!=product.getBrand()){
String brand=(product.getBrand().getName()!=null ||product.getBrand().getName().equals(""))?product.getBrand().getName():"自营";
//item.put("brand",brand);//品牌
productDateilView.setBrand(brand);
}else {
//item.put("brand","自营");//品牌
productDateilView.setBrand("自营");
}
//item.put("productCategory_id",product.getProductCategory().getId());//产品类别
productDateilView.setProductCategory_id(product.getProductCategory().getId());
//item.put("skuThumbnail", sku.getThumbnail() != null ? sku.getThumbnail() : setting.getDefaultThumbnailProductImage());
productDateilView.setSkuThumbnail(sku.getThumbnail() != null ? sku.getThumbnail() : setting.getDefaultThumbnailProductImage());
//item.put("price", cartItem.getPrice());
productDateilView.setPrice(cartItem.getPrice());
//item.put("quantity", cartItem.getQuantity());
productDateilView.setQuantity(cartItem.getQuantity());
//item.put("subtotal", cartItem.getSubtotal());
productDateilView.setSubtotal(cartItem.getSubtotal());
if(cartItem.getSetmeal_id()>0){ //判断如果是套餐
//cartItems.add(item);
SetMeal setMeal=setMealService.find(cartItem.getSetmeal_id());
//item.put("setMeal", setMeal);
boolean isExits=false;
for (SetMealView se: setmealist) {
if(se.getSetMeal().equals(setMeal.getsName())){
se.getViewList().add(productDateilView);
isExits=true;
break;
}
}
if(isExits==false){
List<ProductDateilView> list=new ArrayList<>();
list.add(productDateilView);
setmealist.add(new SetMealView(setMeal.getsName(),list));
}
}else {
cartItems.add(productDateilView);
}
}
data.put("cartItems", cartItems);
data.put("setmealist", setmealist);
}
return data;
}