1.查询商品返回规格上下架信息字段
This commit is contained in:
@@ -9,6 +9,7 @@ import com.chaozhanggui.system.cashierservice.dao.*;
|
||||
import com.chaozhanggui.system.cashierservice.entity.*;
|
||||
import com.chaozhanggui.system.cashierservice.entity.dto.ProductStatusDTO;
|
||||
import com.chaozhanggui.system.cashierservice.entity.dto.ProductStockDTO;
|
||||
import com.chaozhanggui.system.cashierservice.entity.dto.QuerySpecDTO;
|
||||
import com.chaozhanggui.system.cashierservice.entity.vo.ShopCategoryVo;
|
||||
import com.chaozhanggui.system.cashierservice.exception.MsgException;
|
||||
import com.chaozhanggui.system.cashierservice.interceptor.LimitSubmitAspect;
|
||||
@@ -134,69 +135,7 @@ public class ProductService {
|
||||
it.setTbProductSpec(tbProductSpec);
|
||||
|
||||
TbProductSkuResult skuResult=tbProductSkuResultMapper.selectByPrimaryKey(it.getId());
|
||||
|
||||
|
||||
// 上下架对应的sku
|
||||
String selectSpec = it.getSelectSpec();
|
||||
|
||||
List<TbProductSku> tbProductSkus = tbProductSkuMapper.selectGroundingByProId(it.getId());
|
||||
HashSet<String> specSet = new HashSet<>();
|
||||
tbProductSkus.forEach(item -> {
|
||||
String specSnap = item.getSpecSnap();
|
||||
if (specSnap != null) {
|
||||
specSet.addAll(Arrays.asList(specSnap.split(",")));
|
||||
}
|
||||
});
|
||||
String tagSnap = skuResult != null ? skuResult.getTagSnap() : null;
|
||||
if (tagSnap != null) {
|
||||
JSONArray tagSnaps = JSONObject.parseArray(tagSnap);
|
||||
JSONObject snapJSON;
|
||||
|
||||
JSONArray finalSnap = new JSONArray();
|
||||
|
||||
HashMap<String, String> snapMap = new HashMap<>();
|
||||
for (Object snap : tagSnaps) {
|
||||
StringBuilder finalValues = new StringBuilder();
|
||||
snapJSON = (JSONObject) snap;
|
||||
String values = snapJSON.getString("value");
|
||||
if (StrUtil.isNotBlank(values)) {
|
||||
String[] valueList = values.split(",");
|
||||
for (String value : valueList) {
|
||||
if (specSet.contains(value)) {
|
||||
finalValues.append(value).append(",");
|
||||
}
|
||||
}
|
||||
|
||||
if (StrUtil.isNotBlank(finalValues.toString())) {
|
||||
finalValues = new StringBuilder(StrUtil.removeSuffix(finalValues.toString(), ","));
|
||||
snapJSON.put("value", finalValues.toString());
|
||||
finalSnap.add(snapJSON);
|
||||
snapMap.put(snapJSON.getString("name"), snapJSON.getString("value"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (selectSpec != null) {
|
||||
JSONArray selectSpecJSON = JSONObject.parseArray(selectSpec);
|
||||
for (Object selectSpecInfo : selectSpecJSON) {
|
||||
JSONObject specInfo = (JSONObject) selectSpecInfo;
|
||||
String name = snapMap.get(specInfo.getString("name"));
|
||||
if (name != null) {
|
||||
specInfo.put("value", name.split(","));
|
||||
specInfo.put("selectSpecResult", name.split(","));
|
||||
}
|
||||
}
|
||||
|
||||
it.setSelectSpec(selectSpecJSON.toJSONString());
|
||||
}
|
||||
|
||||
skuResult.setTagSnap(finalSnap.toJSONString());
|
||||
}
|
||||
it.setProductSkuResult(skuResult);
|
||||
|
||||
|
||||
|
||||
// 查询sku信息
|
||||
List<TbProductSku> skuWithBLOBs = tbProductSkuMapper.selectByProductCheckGrounding(it.getId());
|
||||
List<TbProductSku> skus = tbProductSkuMapper.selectByProductId(it.getId());
|
||||
@@ -209,7 +148,7 @@ public class ProductService {
|
||||
}
|
||||
it.setSkuList(skuWithBLOBs);
|
||||
|
||||
|
||||
it.setGroundingSpecInfo(querySpec(Integer.valueOf(shopId), it.getId()));
|
||||
});
|
||||
}
|
||||
PageInfo pageInfo=new PageInfo(tbProductWithBLOBs);
|
||||
@@ -280,4 +219,80 @@ public class ProductService {
|
||||
producer.sendStockRecordMsg(data);
|
||||
}
|
||||
}
|
||||
|
||||
private static void generateCombinations(List<List<String>> valuesList, int index, List<String> current, List<String> result) {
|
||||
if (index == valuesList.size()) {
|
||||
result.add(String.join(",", current));
|
||||
return;
|
||||
}
|
||||
|
||||
for (String value : valuesList.get(index)) {
|
||||
current.add(value);
|
||||
generateCombinations(valuesList, index + 1, current, result);
|
||||
current.remove(current.size() - 1); // 回溯
|
||||
}
|
||||
}
|
||||
|
||||
public Object querySpec(Integer shopId, Integer productId) {
|
||||
TbProduct tbProduct = tbProductMapper.selectByPrimaryKey(productId);
|
||||
if (tbProduct == null) {
|
||||
throw new MsgException("商品不存在");
|
||||
}
|
||||
TbProductSkuResult skuResult = tbProductSkuResultMapper.selectByPrimaryKey(productId);
|
||||
|
||||
// 重组有效规格数据
|
||||
String tagSnap = skuResult != null ? skuResult.getTagSnap() : null;
|
||||
List<TbProductSku> tbProductSkus = tbProductSkuMapper.selectGroundingByProId(productId);
|
||||
|
||||
JSONArray finalSnap = new JSONArray();
|
||||
|
||||
List<String> result = new ArrayList<>();
|
||||
if (tagSnap != null) {
|
||||
JSONArray tagSnaps = JSONObject.parseArray(tagSnap);
|
||||
List<List<String>> valuesList = new ArrayList<>();
|
||||
|
||||
// 提取所有 value 的列表
|
||||
for (int i = 0; i < tagSnaps.size(); i++) {
|
||||
JSONObject jsonObject = tagSnaps.getJSONObject(i);
|
||||
if (jsonObject.containsKey("value")) {
|
||||
String[] values = jsonObject.getString("value").split(",");
|
||||
valuesList.add(Arrays.asList(values));
|
||||
}
|
||||
}
|
||||
// 生成所有可能的排列组合
|
||||
generateCombinations(valuesList, 0, new ArrayList<>(), result);
|
||||
|
||||
}
|
||||
|
||||
ArrayList<HashMap<String, Object>> specList = new ArrayList<>();
|
||||
tbProductSkus.forEach(item -> {
|
||||
HashMap<String, Object> itemMap = new HashMap<>();
|
||||
itemMap.put("specSnap", item.getSpecSnap());
|
||||
itemMap.put("skuId", item.getId());
|
||||
specList.add(itemMap);
|
||||
});
|
||||
|
||||
|
||||
ArrayList<HashMap<String, Object>> otherVal = new ArrayList<>();
|
||||
for (String res : result) {
|
||||
boolean found = false;
|
||||
for (HashMap<String, Object> spec : specList) {
|
||||
if (res.equals(spec.get("specSnap").toString())) {
|
||||
spec.put("isGrounding", true);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
HashMap<String, Object> itemMap = new HashMap<>();
|
||||
itemMap.put("specSnap", res);
|
||||
itemMap.put("skuId", null);
|
||||
itemMap.put("isGrounding", false);
|
||||
otherVal.add(itemMap);
|
||||
}
|
||||
}
|
||||
specList.addAll(otherVal);
|
||||
return specList;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user