MesDeviceService.java 4.4 KB

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