MesInboundService.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.common.mybatis.mapper.query.QueryType;
  8. import com.jeesite.modules.mes.entity.MesInbound;
  9. import com.jeesite.modules.mes.dao.MesInboundDao;
  10. import com.jeesite.common.service.ServiceException;
  11. import com.jeesite.common.config.Global;
  12. import com.jeesite.common.validator.ValidatorUtils;
  13. import com.jeesite.common.utils.excel.ExcelImport;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import javax.validation.ConstraintViolation;
  16. import javax.validation.ConstraintViolationException;
  17. /**
  18. * 入库表Service
  19. * @author system
  20. * @version 2026-05-09
  21. */
  22. @Service
  23. public class MesInboundService extends CrudService<MesInboundDao, MesInbound> {
  24. /**
  25. * 获取单条数据
  26. * @param mesInbound
  27. * @return
  28. */
  29. @Override
  30. public MesInbound get(MesInbound mesInbound) {
  31. return super.get(mesInbound);
  32. }
  33. /**
  34. * 查询分页数据
  35. * @param mesInbound 查询条件
  36. * @param mesInbound.page 分页对象
  37. * @return
  38. */
  39. @Override
  40. public Page<MesInbound> findPage(MesInbound mesInbound) {
  41. return super.findPage(mesInbound);
  42. }
  43. /**
  44. * 查询列表数据
  45. * @param mesInbound
  46. * @return
  47. */
  48. @Override
  49. public List<MesInbound> findList(MesInbound mesInbound) {
  50. return super.findList(mesInbound);
  51. }
  52. /**
  53. * 保存数据(插入或更新)
  54. * @param mesInbound
  55. */
  56. @Override
  57. @Transactional
  58. public void save(MesInbound mesInbound) {
  59. super.save(mesInbound);
  60. }
  61. /**
  62. * 导入数据
  63. * @param file 导入的数据文件
  64. */
  65. @Transactional
  66. public String importData(MultipartFile file) {
  67. if (file == null){
  68. throw new ServiceException(text("请选择导入的数据文件!"));
  69. }
  70. int successNum = 0; int failureNum = 0;
  71. StringBuilder successMsg = new StringBuilder();
  72. StringBuilder failureMsg = new StringBuilder();
  73. try(ExcelImport ei = new ExcelImport(file, 2, 0)){
  74. List<MesInbound> list = ei.getDataList(MesInbound.class);
  75. for (MesInbound mesInbound : list) {
  76. try{
  77. ValidatorUtils.validateWithException(mesInbound);
  78. this.save(mesInbound);
  79. successNum++;
  80. successMsg.append("<br/>" + successNum + "、编号 " + mesInbound.getId() + " 导入成功");
  81. } catch (Exception e) {
  82. failureNum++;
  83. String msg = "<br/>" + failureNum + "、编号 " + mesInbound.getId() + " 导入失败:";
  84. if (e instanceof ConstraintViolationException){
  85. ConstraintViolationException cve = (ConstraintViolationException)e;
  86. for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
  87. msg += Global.getText(violation.getMessage()) + " ("+violation.getPropertyPath()+")";
  88. }
  89. }else{
  90. msg += e.getMessage();
  91. }
  92. failureMsg.append(msg);
  93. logger.error(msg, e);
  94. }
  95. }
  96. } catch (Exception e) {
  97. logger.error(e.getMessage(), e);
  98. failureMsg.append(e.getMessage());
  99. return failureMsg.toString();
  100. }
  101. if (failureNum > 0) {
  102. failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
  103. throw new ServiceException(failureMsg.toString());
  104. }else{
  105. successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
  106. }
  107. return successMsg.toString();
  108. }
  109. /**
  110. * 更新状态
  111. * @param mesInbound
  112. */
  113. @Override
  114. @Transactional
  115. public void updateStatus(MesInbound mesInbound) {
  116. super.updateStatus(mesInbound);
  117. }
  118. /**
  119. * 删除数据
  120. * @param mesInbound
  121. */
  122. @Override
  123. @Transactional
  124. public void delete(MesInbound mesInbound) {
  125. super.delete(mesInbound);
  126. }
  127. /**
  128. * 统计指定产线、指定时间段内的入库数量(用于大屏“近七日产品质检”的产出统计)
  129. * @param lineSn 产线编码(MP/XT/ZP)
  130. * @param sdate 开始时间(含)yyyy-MM-dd HH:mm:ss
  131. * @param edate 结束时间(不含)yyyy-MM-dd HH:mm:ss
  132. */
  133. public Long countByLineAndDate(String lineSn, String sdate, String edate){
  134. MesInbound q = new MesInbound();
  135. q.getSqlMap().getWhere()
  136. .and("line_sn", QueryType.EQ, lineSn)
  137. .and("create_date", QueryType.GTE, sdate)
  138. .and("create_date", QueryType.LT, edate);
  139. return super.findCount(q);
  140. }
  141. }