package com.example.northglasserpclient.service.sd.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.northglasserpclient.domain.po.sd.*; import com.example.northglasserpclient.mapper.sd.ProductMapper; import com.example.northglasserpclient.service.sd.*; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; import java.util.Map; @Service @RequiredArgsConstructor public class ProductServiceImpl extends ServiceImpl implements IProductService { private final IProductDetailService productDetailService; private final IGlassPriceBasicService glassPriceBasicService; @Override public List getProduct() { return list(new LambdaQueryWrapper()); } @Override public Map glassPriceComputed(String productId) { Map map = new HashMap<>(); List productDetails = productDetailService.list( new LambdaQueryWrapper() .eq(ProductDetail::getProdId, productId) ); final Double[] money = {0.0}; String mergedResult = productDetails.get(0).getProcess(); outerLoop: for (ProductDetail productDetail : productDetails){ Map separation = JSON.parseObject( productDetail.getSeparation(), new TypeReference>(){}); String name = ""; switch (productDetail.getDetailType()) { case "glass": mergedResult = mergeTwoProcesses(mergedResult, productDetail.getProcess()); name = separation.get("thickness") + separation.get("color"); String[] process = productDetail.getProcess().split("->"); for (String s : process) { GlassPriceBasic glassPriceBasic = glassPriceBasicService.getOne( new LambdaQueryWrapper() .eq(GlassPriceBasic::getName, name+s)); if(glassPriceBasic == null) { money[0] += 0.0; break outerLoop; }else{ money[0] += glassPriceBasic.getPrice(); } } break; case "hollow": name = separation.get("thickness") + separation.get("gasType") + separation.get("Type"); break; case "interlayer": name = separation.get("thickness") + separation.get("color") + separation.get("type"); break; } GlassPriceBasic glassPriceBasic = glassPriceBasicService .getOne(new LambdaQueryWrapper().eq(GlassPriceBasic::getName, name)); if(glassPriceBasic == null) { money[0]= 0.0; break outerLoop; }else{ money[0] += glassPriceBasic.getPrice(); } } map.put("money", money[0]); map.put("workmanship", mergedResult); return map; } //合并产品的工艺流程 private static String mergeTwoProcesses(String p1, String p2) { String[] arr1 = p1.split("->"); String[] arr2 = p2.split("->"); // 找到共同前缀长度 int minLen = Math.min(arr1.length, arr2.length); int commonLen = 0; while (commonLen < minLen && arr1[commonLen].equals(arr2[commonLen])) { commonLen++; } // 拼接共同前缀 StringBuilder commonPrefix = new StringBuilder(); for (int i = 0; i < commonLen; i++) { if (i > 0) commonPrefix.append("->"); commonPrefix.append(arr1[i]); } // 提取p1的差异部分 StringBuilder diff1 = new StringBuilder(); for (int i = commonLen; i < arr1.length; i++) { diff1.append("->").append(arr1[i]); } // 提取p2的差异部分 StringBuilder diff2 = new StringBuilder(); for (int i = commonLen; i < arr2.length; i++) { diff2.append("->").append(arr2[i]); } // 合并差异(用[]包裹,多个差异用|分隔) if (diff1.length() == 0 && diff2.length() == 0) { return commonPrefix.toString(); } else if (diff1.length() == 0) { return commonPrefix + "" + diff2 ; } else if (diff2.length() == 0) { return commonPrefix + "" + diff1 ; } else { return commonPrefix + "" + diff1 + diff2; } } }