From e6ffa9fcebef022be1e8e8162c65f52754f9081e Mon Sep 17 00:00:00 2001
From: zss <zss@example.com>
Date: 星期六, 19 八月 2023 15:11:22 +0800
Subject: [PATCH] 基础数据

---
 standard-server/src/main/java/com/yuanchu/limslaboratory/service/impl/MaterialServiceImpl.java |   57 +++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/standard-server/src/main/java/com/yuanchu/limslaboratory/service/impl/MaterialServiceImpl.java b/standard-server/src/main/java/com/yuanchu/limslaboratory/service/impl/MaterialServiceImpl.java
index 4b75748..4e3721e 100644
--- a/standard-server/src/main/java/com/yuanchu/limslaboratory/service/impl/MaterialServiceImpl.java
+++ b/standard-server/src/main/java/com/yuanchu/limslaboratory/service/impl/MaterialServiceImpl.java
@@ -4,8 +4,11 @@
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.yuanchu.limslaboratory.mapper.ProductModelMapper;
 import com.yuanchu.limslaboratory.pojo.Material;
 import com.yuanchu.limslaboratory.mapper.MaterialMapper;
+import com.yuanchu.limslaboratory.pojo.Product;
 import com.yuanchu.limslaboratory.pojo.Specifications;
 import com.yuanchu.limslaboratory.pojo.Standard;
 import com.yuanchu.limslaboratory.pojo.dto.AddMaterialDto;
@@ -15,16 +18,20 @@
 import com.yuanchu.limslaboratory.service.SpecificationsService;
 import com.yuanchu.limslaboratory.service.StandardService;
 import com.yuanchu.limslaboratory.utils.MyUtil;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * <p>
- *  鏈嶅姟瀹炵幇绫�
+ * 鏈嶅姟瀹炵幇绫�
  * </p>
  *
  * @author 姹熻嫃榈烽洀缃戠粶绉戞妧鏈夐檺鍏徃
@@ -45,12 +52,14 @@
     @Autowired
     private ProductService productService;
 
+    @Resource
+    ProductModelMapper productModelMapper;
+
     //鏌ヨ鐗╂枡淇℃伅
     @Override
     public List<Map> selectMaterialLimit(int pageSize, int countSize, int type) {
-        return materialMapper.selectMaterialLimit((pageSize - 1) * countSize,pageSize * countSize, type);
+        return materialMapper.selectMaterialLimit((pageSize - 1) * countSize, pageSize * countSize, type);
     }
-
 
 
     @Override
@@ -59,11 +68,11 @@
         updateWrapper.eq(Material::getId, materialId);
         updateWrapper.set(Material::getState, 0);
         int isDeleteSuccess = materialMapper.update(new Material(), updateWrapper);
-        if (isDeleteSuccess == 1){
+        if (isDeleteSuccess == 1) {
             List<Integer> isDeleteStandard = standardService.deleteStandardsInformation(materialId);
-            if (!ObjectUtils.isEmpty(isDeleteStandard)){
+            if (!ObjectUtils.isEmpty(isDeleteStandard)) {
                 List<Integer> idDeleteSpecifications = specificationsService.StandardIdDeleteSpecifications(isDeleteStandard);
-                if (!ObjectUtils.isEmpty(idDeleteSpecifications)){
+                if (!ObjectUtils.isEmpty(idDeleteSpecifications)) {
                     productService.deleteProductInformation(idDeleteSpecifications);
                 }
             }
@@ -72,25 +81,37 @@
         return 0;
     }
 
+    //涓夌骇鏂板
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Integer addMaterialInformation(AddMaterialDto addMaterialDto) {
-        Material material = new Material()
-                .setCode("BZ" + IdWorker.getIdStr())
-                .setName(addMaterialDto.getMaterialName())
-                .setType(addMaterialDto.getType());
-        int isInsertSuccess = materialMapper.insert(material);
-        if (isInsertSuccess > 0){
+        Material material = materialMapper.selectOne(Wrappers.<Material>query().eq("name", addMaterialDto.getMaterialName()));
+        material.setType(addMaterialDto.getType());
+        int isInsertSuccess = materialMapper.updateById(material);
+        if (isInsertSuccess > 0) {
             Standard standard = new Standard()
                     .setMaterialId(material.getId())
                     .setName(addMaterialDto.getStandardName());
             boolean save = standardService.save(standard);
-            if (save){
+            if (save) {
                 Specifications specifications = new Specifications()
                         .setName(addMaterialDto.getSpecificationsName())
                         .setStandardId(standard.getId());
                 boolean save1 = specificationsService.save(specifications);
-                if (save1)
+                if (save1) {
+                    //娣诲姞鍨嬪彿涔嬪悗娣诲姞椤圭洰
+                    List<Map<String, Object>> productModel = productModelMapper.seleMode(addMaterialDto.getMaterialName());
+                    List<Product> productList = productModel.stream().map(stringObjectMap -> {
+                        Product product = new Product();
+                        product.setName(stringObjectMap.get("name").toString());
+                        product.setFather(stringObjectMap.get("father").toString());
+                        product.setUnit(stringObjectMap.get("unit").toString());
+                        product.setSpecifications_id(specifications.getId());
+                        return product;
+                    }).collect(Collectors.toList());
+                    productService.saveBatch(productList);
                     return 1;
+                }
             }
         }
         return 0;
@@ -100,4 +121,12 @@
     public List<Map<String, Object>> getFourLevelInformation(String specificationName) {
         return materialMapper.FourTree(specificationName);
     }
+
+    //娣诲姞鎸囨爣-->閫夋嫨鏍峰搧鍚嶇О
+    @Override
+    public List<String> selectmater() {
+        return materialMapper.selectmater();
+    }
+
+
 }

--
Gitblit v1.9.3