jichaobo 3 år sedan
förälder
incheckning
3964998cdb

+ 17 - 3
fiveep-service/src/main/java/com/bizmatics/service/impl/DeviceAnalogVariableListServiceImpl.java

@@ -27,6 +27,8 @@ import com.bizmatics.service.util.SecurityUtils;
 import com.bizmatics.service.vo.*;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
@@ -154,28 +156,40 @@ public class DeviceAnalogVariableListServiceImpl extends AbstractCrudService<Dev
         return file.getName();
     }
 
+    @Transactional
     @Override
     public void variableListImport(MultipartFile multipartFile){
         SysUser user = SecurityUtils.getLoginUser().getUser();
         ImportParams params = new ImportParams();
         params.setHeadRows(1);
+        String err="文件导入失败";
         try {
             List<DeviceAnalogVariableListImport> deviceAnalogVariableListImportList = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
                     DeviceAnalogVariableListImport.class, params);
             if (CollectionUtils.isNotEmpty(deviceAnalogVariableListImportList)){
+                int rot=0;
                 for (DeviceAnalogVariableListImport deviceAnalogVariableListImportLists:deviceAnalogVariableListImportList) {
                     DeviceAnalogVariableList deviceAnalogVariableList = BeanMapperUtils.map(deviceAnalogVariableListImportLists, DeviceAnalogVariableList.class);
                     deviceAnalogVariableList.setVariableCoding(deviceAnalogVariableListImportLists.getDeviceCode()+"_"+deviceAnalogVariableListImportLists.getVariableCoding());
                     deviceAnalogVariableList.setStatus(1);
                     deviceAnalogVariableList.setCreator(user.getUserName());
                     deviceAnalogVariableList.setCreateTime(new Date());
-                    this.save(deviceAnalogVariableList);
+                    try{
+                        this.save(deviceAnalogVariableList);
+                    }catch (Exception e){
+                        int h=rot+2;
+                        err="文件导入失败,第"+h+"行数据导入失败";
+                        throw  new BusinessException(err);
+                    }
+                    rot++;
                 }
             }else {
-                throw new BusinessException("文件不能为空");
+                err="文件不能为空";
+                throw new BusinessException(err);
             }
         }catch (Exception e){
-            throw  new BusinessException("文件导入失败"+e.getMessage());
+            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+            throw  new BusinessException(err);
         }
     }
 

+ 17 - 4
fiveep-service/src/main/java/com/bizmatics/service/impl/DeviceAttributeServiceImpl.java

@@ -30,6 +30,8 @@ import org.apache.poi.ss.usermodel.Workbook;
 import org.jetbrains.annotations.NotNull;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
@@ -156,29 +158,40 @@ public class DeviceAttributeServiceImpl extends AbstractCrudService<DeviceAttrib
     }
 
 
-
+    @Transactional
     @Override
     public void deviceAttributeImport(MultipartFile multipartFile){
         SysUser user = SecurityUtils.getLoginUser().getUser();
         ImportParams params = new ImportParams();
         params.setHeadRows(1);
+        String err="文件导入失败";
         try {
             List<DeviceAttributeImportVo> deviceAttributeImportVoList = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
                     DeviceAttributeImportVo.class, params);
             if (CollectionUtils.isNotEmpty(deviceAttributeImportVoList)){
+                int rot=0;
                 for (DeviceAttributeImportVo deviceAttributeImportVos:deviceAttributeImportVoList) {
                     DeviceAttribute deviceAttribute = BeanMapperUtils.map(deviceAttributeImportVos, DeviceAttribute.class);
                     deviceAttribute.setVariableListId(0);
                     deviceAttribute.setStatus(1);
                     deviceAttribute.setCreator(user.getUserName());
                     deviceAttribute.setCreateTime(new Date());
-                    this.save(deviceAttribute);
+                    try{
+                        this.save(deviceAttribute);
+                    }catch (Exception e){
+                        int h=rot+2;
+                        err="文件导入失败,第"+h+"行数据导入失败";
+                        throw  new BusinessException(err);
+                    }
+                    rot++;
                 }
             }else {
-                throw new BusinessException("文件不能为空");
+                err="文件不能为空";
+                throw new BusinessException(err);
             }
         }catch (Exception e){
-            throw  new BusinessException("文件导入失败"+e.getMessage());
+            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+            throw  new BusinessException(err);
         }
     }
 }