MesDeviceStateService.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.jeesite.modules.mes.service;
  2. import java.util.List;
  3. import com.jeesite.common.lang.ObjectUtils;
  4. import com.jeesite.modules.mes.entity.MesDevice;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Transactional;
  8. import com.jeesite.common.entity.Page;
  9. import com.jeesite.common.service.CrudService;
  10. import com.jeesite.modules.mes.entity.MesDeviceState;
  11. import com.jeesite.modules.mes.dao.MesDeviceStateDao;
  12. import com.jeesite.common.service.ServiceException;
  13. import com.jeesite.common.config.Global;
  14. import com.jeesite.common.validator.ValidatorUtils;
  15. import com.jeesite.common.utils.excel.ExcelImport;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import javax.validation.ConstraintViolation;
  18. import javax.validation.ConstraintViolationException;
  19. /**
  20. * 设备运行状态Service
  21. * @author hzd
  22. * @version 2023-08-25
  23. */
  24. @Service
  25. public class MesDeviceStateService extends CrudService<MesDeviceStateDao, MesDeviceState> {
  26. @Autowired
  27. private MesDeviceStateDao mesDeviceStateDao;
  28. /**
  29. * 获取单条数据
  30. * @param mesDeviceState
  31. * @return
  32. */
  33. @Override
  34. public MesDeviceState get(MesDeviceState mesDeviceState) {
  35. return super.get(mesDeviceState);
  36. }
  37. /**
  38. * 查询分页数据
  39. * @param mesDeviceState 查询条件
  40. * @param mesDeviceState.page 分页对象
  41. * @return
  42. */
  43. @Override
  44. public Page<MesDeviceState> findPage(MesDeviceState mesDeviceState) {
  45. return super.findPage(mesDeviceState);
  46. }
  47. /**
  48. * 查询列表数据
  49. * @param mesDeviceState
  50. * @return
  51. */
  52. @Override
  53. public List<MesDeviceState> findList(MesDeviceState mesDeviceState) {
  54. return super.findList(mesDeviceState);
  55. }
  56. public MesDeviceState findInfo(MesDeviceState mesDeviceState) {
  57. return mesDeviceStateDao.findInfo(mesDeviceState);
  58. }
  59. /**
  60. * 保存数据(插入或更新)
  61. * @param mesDeviceState
  62. */
  63. @Override
  64. @Transactional
  65. public void save(MesDeviceState mesDeviceState) {
  66. super.save(mesDeviceState);
  67. }
  68. @Transactional
  69. public void addsave(MesDeviceState mesDeviceState) {
  70. // 获取最新一条数据
  71. MesDeviceState mesDeviceState1 = new MesDeviceState();
  72. mesDeviceState1.setDeviceSn(mesDeviceState.getDeviceSn());
  73. MesDeviceState last = mesDeviceStateDao.findInfo(mesDeviceState1);
  74. if(!ObjectUtils.isEmpty(last)){
  75. if(!last.getState().equals(mesDeviceState.getState())){ // 状态相同说明没有改变,不需要更新
  76. if(mesDeviceState.getStartDate().getTime() > last.getStartDate().getTime()){
  77. Integer interval = Math.round((mesDeviceState.getStartDate().getTime() - last.getStartDate().getTime())/1000);
  78. last.setInterval(interval);
  79. last.setEndDate(mesDeviceState.getStartDate());
  80. super.update(last);
  81. }
  82. super.save(mesDeviceState);
  83. }
  84. }else{
  85. super.save(mesDeviceState);
  86. }
  87. }
  88. /**
  89. * 导入数据
  90. * @param file 导入的数据文件
  91. */
  92. @Transactional
  93. public String importData(MultipartFile file) {
  94. if (file == null){
  95. throw new ServiceException(text("请选择导入的数据文件!"));
  96. }
  97. int successNum = 0; int failureNum = 0;
  98. StringBuilder successMsg = new StringBuilder();
  99. StringBuilder failureMsg = new StringBuilder();
  100. try(ExcelImport ei = new ExcelImport(file, 2, 0)){
  101. List<MesDeviceState> list = ei.getDataList(MesDeviceState.class);
  102. for (MesDeviceState mesDeviceState : list) {
  103. try{
  104. ValidatorUtils.validateWithException(mesDeviceState);
  105. this.save(mesDeviceState);
  106. successNum++;
  107. successMsg.append("<br/>" + successNum + "、编号 " + mesDeviceState.getId() + " 导入成功");
  108. } catch (Exception e) {
  109. failureNum++;
  110. String msg = "<br/>" + failureNum + "、编号 " + mesDeviceState.getId() + " 导入失败:";
  111. if (e instanceof ConstraintViolationException){
  112. ConstraintViolationException cve = (ConstraintViolationException)e;
  113. for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
  114. msg += Global.getText(violation.getMessage()) + " ("+violation.getPropertyPath()+")";
  115. }
  116. }else{
  117. msg += e.getMessage();
  118. }
  119. failureMsg.append(msg);
  120. logger.error(msg, e);
  121. }
  122. }
  123. } catch (Exception e) {
  124. logger.error(e.getMessage(), e);
  125. failureMsg.append(e.getMessage());
  126. return failureMsg.toString();
  127. }
  128. if (failureNum > 0) {
  129. failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
  130. throw new ServiceException(failureMsg.toString());
  131. }else{
  132. successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
  133. }
  134. return successMsg.toString();
  135. }
  136. /**
  137. * 更新状态
  138. * @param mesDeviceState
  139. */
  140. @Override
  141. @Transactional
  142. public void updateStatus(MesDeviceState mesDeviceState) {
  143. super.updateStatus(mesDeviceState);
  144. }
  145. /**
  146. * 删除数据
  147. * @param mesDeviceState
  148. */
  149. @Override
  150. @Transactional
  151. public void delete(MesDeviceState mesDeviceState) {
  152. super.delete(mesDeviceState);
  153. }
  154. /**
  155. * 更新设备状态
  156. * @param mesDeviceState
  157. */
  158. @Transactional
  159. public void updateState(MesDeviceState mesDeviceState) {
  160. // 获取上条数据,更新结束日期及持续时长
  161. if(mesDeviceState.getStartDate() != null){
  162. MesDeviceState mesDeviceState1 = new MesDeviceState();
  163. mesDeviceState1.setDeviceSn(mesDeviceState.getDeviceSn());
  164. MesDeviceState last = mesDeviceStateDao.findInfo(mesDeviceState1);
  165. if(!ObjectUtils.isEmpty(last)){
  166. if(mesDeviceState.getStartDate().getTime() > last.getStartDate().getTime()){
  167. Integer interval = Math.round((mesDeviceState.getStartDate().getTime() - last.getStartDate().getTime())/1000);
  168. last.setInterval(interval);
  169. last.setEndDate(mesDeviceState.getStartDate());
  170. super.update(last);
  171. }
  172. }
  173. }
  174. super.save(mesDeviceState);
  175. }
  176. }