MesSawService.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.jeesite.modules.mes.service;
  2. import java.util.List;
  3. import javax.validation.ConstraintViolation;
  4. import javax.validation.ConstraintViolationException;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Transactional;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import com.jeesite.common.config.Global;
  9. import com.jeesite.common.entity.Page;
  10. import com.jeesite.common.service.CrudService;
  11. import com.jeesite.common.service.ServiceException;
  12. import com.jeesite.common.utils.excel.ExcelImport;
  13. import com.jeesite.common.validator.ValidatorUtils;
  14. import com.jeesite.modules.mes.dao.MesSawDao;
  15. import com.jeesite.modules.mes.entity.MesSaw;
  16. /**
  17. * 锯切数据Service
  18. * @author system
  19. * @version 2026-06-11
  20. */
  21. @Service
  22. public class MesSawService extends CrudService<MesSawDao, MesSaw> {
  23. @Override
  24. public MesSaw get(MesSaw e) { return super.get(e); }
  25. @Override
  26. public Page<MesSaw> findPage(MesSaw e) { return super.findPage(e); }
  27. @Override
  28. public List<MesSaw> findList(MesSaw e) { return super.findList(e); }
  29. @Override
  30. @Transactional
  31. public void save(MesSaw e) { super.save(e); }
  32. @Override
  33. @Transactional
  34. public void updateStatus(MesSaw e) { super.updateStatus(e); }
  35. @Override
  36. @Transactional
  37. public void delete(MesSaw e) { super.delete(e); }
  38. @Transactional
  39. public String importData(MultipartFile file) {
  40. if (file == null) throw new ServiceException(text("请选择导入的数据文件!"));
  41. int successNum = 0; int failureNum = 0;
  42. StringBuilder successMsg = new StringBuilder();
  43. StringBuilder failureMsg = new StringBuilder();
  44. try (ExcelImport ei = new ExcelImport(file, 2, 0)) {
  45. List<MesSaw> list = ei.getDataList(MesSaw.class);
  46. for (MesSaw item : list) {
  47. try {
  48. ValidatorUtils.validateWithException(item);
  49. this.save(item);
  50. successNum++;
  51. successMsg.append("<br/>").append(successNum).append("、编号 ").append(item.getId()).append(" 导入成功");
  52. } catch (Exception ex) {
  53. failureNum++;
  54. String msg = "<br/>" + failureNum + "、编号 " + item.getId() + " 导入失败:";
  55. if (ex instanceof ConstraintViolationException) {
  56. for (ConstraintViolation<?> v : ((ConstraintViolationException) ex).getConstraintViolations()) {
  57. msg += Global.getText(v.getMessage()) + " (" + v.getPropertyPath() + ")";
  58. }
  59. } else {
  60. msg += ex.getMessage();
  61. }
  62. failureMsg.append(msg);
  63. logger.error(msg, ex);
  64. }
  65. }
  66. } catch (Exception ex) {
  67. logger.error(ex.getMessage(), ex);
  68. failureMsg.append(ex.getMessage());
  69. return failureMsg.toString();
  70. }
  71. if (failureNum > 0) {
  72. failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
  73. throw new ServiceException(failureMsg.toString());
  74. } else {
  75. successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
  76. }
  77. return successMsg.toString();
  78. }
  79. }