Browse Source

增加了典表保存的重复校验功能

LeeXin 3 years ago
parent
commit
7eca6f68bf

+ 30 - 50
src/main/java/cn/fastfun/controller/api/ProductController.java

@@ -4,6 +4,7 @@ package cn.fastfun.controller.api;
 import cn.fastfun.service.ProductService;
 import cn.fastfun.service.entity.Product;
 import cn.fastfun.service.entity.QueryParamForProduct;
+import cn.fastfun.util.ResolveUtil;
 import cn.fastfun.util.VerifyUtil;
 import com.bridge.dto.*;
 import io.swagger.annotations.Api;
@@ -28,64 +29,43 @@ public class ProductController {
 
         // 针对产品规格这一项进行操作
         if(product.getSnType().equals("产品规格")){
-
-            String[] SnTypeDescList = product.getSnTypeDesc().split(" ");
-            List<Product> productSpecification = new ArrayList<>();
-
-            String chemicalSystemCode = product.getSnTypeCode().substring(0,1);
-            String chemicalSystemDesc = SnTypeDescList[0];
-            String batteryPlatformCode = product.getSnTypeCode().substring(1,2);
-            String batteryPlatformDesc = SnTypeDescList[1];
-            String capacityCode = product.getSnTypeCode().substring(2,4);
-            String capacityDesc = SnTypeDescList[2];
-
-            Product chemicalSystem = new Product();
-            chemicalSystem.setSnType("化学体系");
-            chemicalSystem.setSnTypeCode(chemicalSystemCode);
-            chemicalSystem.setSnTypeDesc(chemicalSystemDesc);
-            chemicalSystem.setOperator(product.getOperator());
-            String checkSnTypeCodeResultOnChemicalSystem = VerifyUtil.checkSnTypeCode(chemicalSystem);
-            if(checkSnTypeCodeResultOnChemicalSystem.equals("passed")){
-                Product tmp_1 = productService.save(chemicalSystem);
-                productSpecification.add(tmp_1);}
-            else{
-                return ApiDTO.error(500,checkSnTypeCodeResultOnChemicalSystem);
+            String checkSnTypeCodeResult = VerifyUtil.checkSnTypeCode(product);
+            if(!(checkSnTypeCodeResult.equals("passed"))){
+                return ApiDTO.error(500,checkSnTypeCodeResult);
             }
-
-            Product batteryPlatform = new Product();
-            batteryPlatform.setSnType("电池平台");
-            batteryPlatform.setSnTypeCode(batteryPlatformCode);
-            batteryPlatform.setSnTypeDesc(batteryPlatformDesc);
-            batteryPlatform.setOperator(product.getOperator());
-            String checkSnTypeCodeResultOnBatteryPlatform = VerifyUtil.checkSnTypeCode(batteryPlatform);
-            if(checkSnTypeCodeResultOnBatteryPlatform.equals("passed")){
-                Product tmp_2 = productService.save(batteryPlatform);
-                productSpecification.add(tmp_2);}
-            else{
-                return ApiDTO.error(500,checkSnTypeCodeResultOnChemicalSystem);
+            List<Product> productSpecificationResults = ResolveUtil.resolveProductSpecification(product);
+            List<List<Product>> checkDuplicatedResults = new ArrayList<>();
+
+            for(Product productSpecificationResult:productSpecificationResults) {
+                List<Product> checkDuplicatedResult = productService.findAll(
+                        Arrays.asList(QueryParamExp.eq("snTypeCode", productSpecificationResult.getSnTypeCode()),
+                                QueryParamExp.eq("snType", productSpecificationResult.getSnType())));
+
+                checkDuplicatedResults.add(checkDuplicatedResult);
+
+                String checkSnTypeCodeResultForEach = VerifyUtil.checkSnTypeCode(productSpecificationResult);
+                if((checkSnTypeCodeResultForEach.equals("passed"))&(checkDuplicatedResult.isEmpty())){
+                        productService.save(productSpecificationResult);
+                    }
+                else if(!(checkSnTypeCodeResultForEach.equals("passed"))){
+                    return ApiDTO.error(500,checkSnTypeCodeResultForEach);
+                }
             }
-
-            Product capacity = new Product();
-            capacity.setSnType("容量");
-            capacity.setSnTypeCode(capacityCode);
-            capacity.setSnTypeDesc(capacityDesc);
-            capacity.setOperator(product.getOperator());
-            String checkSnTypeCodeResultOnCapacity = VerifyUtil.checkSnTypeCode(capacity);
-            if(checkSnTypeCodeResultOnCapacity.equals("passed")){
-                Product tmp_3 = productService.save(capacity);
-                productSpecification.add(tmp_3);}
-            else{
-                return ApiDTO.error(500,checkSnTypeCodeResultOnChemicalSystem);
+            if((!(checkDuplicatedResults.get(0).isEmpty()))&(!(checkDuplicatedResults.get(1).isEmpty()))&(!(checkDuplicatedResults.get(2).isEmpty()))){
+                return ApiDTO.error(500,"该产品规格对应的三个规则行已存在,未进行任何保存",checkDuplicatedResults);
             }
-
-
-            return ApiDTO.ok("保存成功",productSpecification);
+            return ApiDTO.ok("保存成功,未保存的规则行id为null",productSpecificationResults);
         }
 
 
         // 校验输入的SN类型对应的编号
         String checkSnTypeCodeResult = VerifyUtil.checkSnTypeCode(product);
-
+        List<Product> checkDuplicatedResult = productService.findAll(
+                Arrays.asList(QueryParamExp.eq("snTypeCode",product.getSnTypeCode()),
+                        QueryParamExp.eq("snType",product.getSnType())));
+        if(!checkDuplicatedResult.isEmpty()){
+            return ApiDTO.error(500,"该规则行已存在",checkDuplicatedResult);
+        }
         if(checkSnTypeCodeResult.equals("passed")){
         Product tmp = productService.save(product);
         return ApiDTO.ok("保存成功",tmp);}

+ 41 - 0
src/main/java/cn/fastfun/util/ResolveUtil.java

@@ -1,7 +1,11 @@
 package cn.fastfun.util;
 
 
+import cn.fastfun.service.entity.Product;
+
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 public class ResolveUtil {
@@ -21,6 +25,43 @@ public class ResolveUtil {
         return snMap;
     }
 
+    // 将输入的产品规格代码拆分成三个项
+    public static List<Product> resolveProductSpecification(Product product){
+
+        String[] SnTypeDescList = product.getSnTypeDesc().split(" ");
+        List<Product> productSpecification = new ArrayList<>();
+
+        String chemicalSystemCode = product.getSnTypeCode().substring(0,1);
+        String chemicalSystemDesc = SnTypeDescList[0];
+        String batteryPlatformCode = product.getSnTypeCode().substring(1,2);
+        String batteryPlatformDesc = SnTypeDescList[1];
+        String capacityCode = product.getSnTypeCode().substring(2,4);
+        String capacityDesc = SnTypeDescList[2];
+
+        Product chemicalSystem = new Product();
+        chemicalSystem.setSnType("化学体系");
+        chemicalSystem.setSnTypeCode(chemicalSystemCode);
+        chemicalSystem.setSnTypeDesc(chemicalSystemDesc);
+        chemicalSystem.setOperator(product.getOperator());
+        productSpecification.add(chemicalSystem);
+
+        Product batteryPlatform = new Product();
+        batteryPlatform.setSnType("电池平台");
+        batteryPlatform.setSnTypeCode(batteryPlatformCode);
+        batteryPlatform.setSnTypeDesc(batteryPlatformDesc);
+        batteryPlatform.setOperator(product.getOperator());
+        productSpecification.add(batteryPlatform);
+
+        Product capacity = new Product();
+        capacity.setSnType("容量");
+        capacity.setSnTypeCode(capacityCode);
+        capacity.setSnTypeDesc(capacityDesc);
+        capacity.setOperator(product.getOperator());
+        productSpecification.add(capacity);
+
+        return productSpecification;
+    }
+
     // 对SN中的日期进行解释
     public static String resolveDate(String productionDate){
         String year = "20" + productionDate.substring(0,2) + "年";

+ 10 - 5
src/main/java/cn/fastfun/util/VerifyUtil.java

@@ -1,10 +1,14 @@
 package cn.fastfun.util;
 
+import cn.fastfun.service.ProductService;
 import cn.fastfun.service.entity.Product;
 
+import javax.annotation.Resource;
 import java.util.regex.Pattern;
 
 public class VerifyUtil {
+    @Resource
+    ProductService productService;
 
     // 校验输入的sn切片编号,用于典表的校验
     public static String checkSnTypeCode(Product product) {
@@ -42,6 +46,11 @@ public class VerifyUtil {
                     return "电芯厂家的编号不符合规则,不能包含I或者O";
                 }
                 break;
+            case "产品规格":
+                if(sntypecode.length() != 4) {
+                    return "产品规格的编号长度必须为4";
+                }
+                break;
             case "化学体系":
                 if(sntypecode.length() != 1) {
                     return "化学体系的编号长度必须为1";
@@ -99,7 +108,7 @@ public class VerifyUtil {
 //                break;
             default:
                 return "sn的切片类型指明不正确,必须为" +
-                        "产品类型,PACK制造商,电芯厂家,化学体系,电池平台,容量,产品扩展,生产日期(未开放),序列号(未开放)" +
+                        "产品类型,PACK制造商,电芯厂家,产品规格,产品扩展,生产日期(未开放),序列号(未开放)" +
                         "中的一种";
 
         }
@@ -107,10 +116,6 @@ public class VerifyUtil {
         return "passed";
     }
 
-    // 校验输入的数据行是否与数据库重复(比对sn_type和sn_type_desc是否同时相同)
-    public static String checkDuplicatedData(Product product) {
-        return null;
-    }
 
 
 }