MesDeviceVulnerableService.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.jeesite.modules.mes.service;
  2. import java.util.List;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import com.jeesite.common.entity.Page;
  6. import com.jeesite.common.service.CrudService;
  7. import com.jeesite.modules.mes.entity.MesDeviceVulnerable;
  8. import com.jeesite.modules.mes.dao.MesDeviceVulnerableDao;
  9. import com.jeesite.common.service.ServiceException;
  10. import com.jeesite.common.config.Global;
  11. import com.jeesite.common.validator.ValidatorUtils;
  12. import com.jeesite.common.utils.excel.ExcelImport;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import javax.validation.ConstraintViolation;
  15. import javax.validation.ConstraintViolationException;
  16. /**
  17. * mes_device_vulnerable_listService
  18. * @author mes
  19. * @version 2024-08-05
  20. */
  21. @Service
  22. public class MesDeviceVulnerableService extends CrudService<MesDeviceVulnerableDao, MesDeviceVulnerable> {
  23. /**
  24. * 获取单条数据
  25. * @param mesDeviceVulnerable
  26. * @return
  27. */
  28. @Override
  29. public MesDeviceVulnerable get(MesDeviceVulnerable mesDeviceVulnerable) {
  30. return super.get(mesDeviceVulnerable);
  31. }
  32. /**
  33. * 查询分页数据
  34. * @param mesDeviceVulnerable 查询条件
  35. * @param mesDeviceVulnerable.page 分页对象
  36. * @return
  37. */
  38. @Override
  39. public Page<MesDeviceVulnerable> findPage(MesDeviceVulnerable mesDeviceVulnerable) {
  40. return super.findPage(mesDeviceVulnerable);
  41. }
  42. /**
  43. * 查询列表数据
  44. * @param mesDeviceVulnerable
  45. * @return
  46. */
  47. @Override
  48. public List<MesDeviceVulnerable> findList(MesDeviceVulnerable mesDeviceVulnerable) {
  49. return super.findList(mesDeviceVulnerable);
  50. }
  51. /**
  52. * 保存数据(插入或更新)
  53. * @param mesDeviceVulnerable
  54. */
  55. @Override
  56. @Transactional
  57. public void save(MesDeviceVulnerable mesDeviceVulnerable) {
  58. super.save(mesDeviceVulnerable);
  59. }
  60. /**
  61. * 导入数据
  62. * @param file 导入的数据文件
  63. */
  64. @Transactional
  65. public String importData(MultipartFile file) {
  66. if (file == null){
  67. throw new ServiceException(text("请选择导入的数据文件!"));
  68. }
  69. int successNum = 0; int failureNum = 0;
  70. StringBuilder successMsg = new StringBuilder();
  71. StringBuilder failureMsg = new StringBuilder();
  72. try(ExcelImport ei = new ExcelImport(file, 2, 0)){
  73. List<MesDeviceVulnerable> list = ei.getDataList(MesDeviceVulnerable.class);
  74. for (MesDeviceVulnerable mesDeviceVulnerable : list) {
  75. try{
  76. ValidatorUtils.validateWithException(mesDeviceVulnerable);
  77. this.save(mesDeviceVulnerable);
  78. successNum++;
  79. successMsg.append("<br/>" + successNum + "、编号 " + mesDeviceVulnerable.getId() + " 导入成功");
  80. } catch (Exception e) {
  81. failureNum++;
  82. String msg = "<br/>" + failureNum + "、编号 " + mesDeviceVulnerable.getId() + " 导入失败:";
  83. if (e instanceof ConstraintViolationException){
  84. ConstraintViolationException cve = (ConstraintViolationException)e;
  85. for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
  86. msg += Global.getText(violation.getMessage()) + " ("+violation.getPropertyPath()+")";
  87. }
  88. }else{
  89. msg += e.getMessage();
  90. }
  91. failureMsg.append(msg);
  92. logger.error(msg, e);
  93. }
  94. }
  95. } catch (Exception e) {
  96. logger.error(e.getMessage(), e);
  97. failureMsg.append(e.getMessage());
  98. return failureMsg.toString();
  99. }
  100. if (failureNum > 0) {
  101. failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
  102. throw new ServiceException(failureMsg.toString());
  103. }else{
  104. successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
  105. }
  106. return successMsg.toString();
  107. }
  108. /**
  109. * 更新状态
  110. * @param mesDeviceVulnerable
  111. */
  112. @Override
  113. @Transactional
  114. public void updateStatus(MesDeviceVulnerable mesDeviceVulnerable) {
  115. super.updateStatus(mesDeviceVulnerable);
  116. }
  117. /**
  118. * 删除数据
  119. * @param mesDeviceVulnerable
  120. */
  121. @Override
  122. @Transactional
  123. public void delete(MesDeviceVulnerable mesDeviceVulnerable) {
  124. super.delete(mesDeviceVulnerable);
  125. }
  126. }