|
@@ -1,5 +1,6 @@
|
|
|
package com.qs.mp.web.controller.api.admin;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
@@ -26,6 +27,7 @@ import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
@@ -160,6 +162,7 @@ public class GoodsMgrController extends BaseApiController {
|
|
|
}
|
|
|
// 3.插入数据
|
|
|
try {
|
|
|
+ goods.setSkuProp(getSkuProp(skuList));
|
|
|
goodsService.saveGoods(goods, skuList);
|
|
|
} catch (Exception e) {
|
|
|
return AjaxResult.error("商品'" + goods.getTitle() + "'新增失败" + e.getMessage());
|
|
@@ -217,6 +220,7 @@ public class GoodsMgrController extends BaseApiController {
|
|
|
}
|
|
|
// 3.插入数据
|
|
|
try {
|
|
|
+ goods.setSkuProp(getSkuProp(skuList));
|
|
|
goodsService.updateGoods(goods, skuList);
|
|
|
} catch (Exception e) {
|
|
|
return AjaxResult.error("商品'" + goods.getTitle() + "'新增失败" + e.getMessage());
|
|
@@ -225,8 +229,7 @@ public class GoodsMgrController extends BaseApiController {
|
|
|
}
|
|
|
|
|
|
// 获取商品的sku
|
|
|
- public String getGoodsSku(List<GoodsSku> skuList) {
|
|
|
- String goosSku= "";
|
|
|
+ public String getSkuProp(List<GoodsSku> skuList) {
|
|
|
List<Map<String,Object>> skuMapList = new ArrayList<Map<String,Object>>();
|
|
|
if(null != skuList && skuList.size() > 0) {
|
|
|
for(GoodsSku sku : skuList) {
|
|
@@ -250,9 +253,47 @@ public class GoodsMgrController extends BaseApiController {
|
|
|
}
|
|
|
if(null != skuMapList && skuMapList.size() > 0) {
|
|
|
// 合并key相同的Map
|
|
|
+ Map<String,Object> map = skuMapList.stream().flatMap(m -> m.entrySet().stream())
|
|
|
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> getObjValue(a,b)));
|
|
|
|
|
|
+ List<Map<String,Object>> mapList=new ArrayList<>();
|
|
|
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ Object value = entry.getValue();
|
|
|
+ Map<String, Object> mapObj = new HashMap<String, Object>();
|
|
|
+ mapObj.put("name", key);
|
|
|
+ if (value instanceof List<?>) {
|
|
|
+ mapObj.put("value", value);
|
|
|
+ } else {
|
|
|
+ List<Object> list = new ArrayList<>();
|
|
|
+ list.add(value + "");
|
|
|
+ mapObj.put("value", list);
|
|
|
+ }
|
|
|
+ mapList.add(mapObj);
|
|
|
+ }
|
|
|
+ return JSONArray.toJSONString(mapList);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private Object getObjValue(Object a, Object b){
|
|
|
+ boolean isa = (a instanceof List<?>);
|
|
|
+ boolean isb = (b instanceof List<?>);
|
|
|
+ System.out.println("isa == " + isa);
|
|
|
+ System.out.println("isb == " + isb);
|
|
|
+ List<Object> list = new ArrayList<>();
|
|
|
+ if (isa) {
|
|
|
+ list.addAll((List<?>) a);
|
|
|
+ } else {
|
|
|
+ list.add(a+"");
|
|
|
+ }
|
|
|
+ if (isb) {
|
|
|
+ list.addAll((List<?>) b);
|
|
|
+ } else {
|
|
|
+ list.add(b+"");
|
|
|
}
|
|
|
- return goosSku;
|
|
|
+ return list.stream().distinct().collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
|