package com.jeesite.modules.mes.service; import java.util.List; import com.jeesite.common.lang.ObjectUtils; import com.jeesite.modules.mes.entity.MesDevice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.jeesite.common.entity.Page; import com.jeesite.common.service.CrudService; import com.jeesite.modules.mes.entity.MesDeviceState; import com.jeesite.modules.mes.dao.MesDeviceStateDao; import com.jeesite.common.service.ServiceException; import com.jeesite.common.config.Global; import com.jeesite.common.validator.ValidatorUtils; import com.jeesite.common.utils.excel.ExcelImport; import org.springframework.web.multipart.MultipartFile; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; /** * 设备运行状态Service * @author hzd * @version 2023-08-25 */ @Service public class MesDeviceStateService extends CrudService { @Autowired private MesDeviceStateDao mesDeviceStateDao; /** * 获取单条数据 * @param mesDeviceState * @return */ @Override public MesDeviceState get(MesDeviceState mesDeviceState) { return super.get(mesDeviceState); } /** * 查询分页数据 * @param mesDeviceState 查询条件 * @param mesDeviceState.page 分页对象 * @return */ @Override public Page findPage(MesDeviceState mesDeviceState) { return super.findPage(mesDeviceState); } /** * 查询列表数据 * @param mesDeviceState * @return */ @Override public List findList(MesDeviceState mesDeviceState) { return super.findList(mesDeviceState); } public MesDeviceState findInfo(MesDeviceState mesDeviceState) { return mesDeviceStateDao.findInfo(mesDeviceState); } /** * 保存数据(插入或更新) * @param mesDeviceState */ @Override @Transactional public void save(MesDeviceState mesDeviceState) { super.save(mesDeviceState); } @Transactional public void addsave(MesDeviceState mesDeviceState) { // 获取最新一条数据 MesDeviceState mesDeviceState1 = new MesDeviceState(); mesDeviceState1.setDeviceSn(mesDeviceState.getDeviceSn()); MesDeviceState last = mesDeviceStateDao.findInfo(mesDeviceState1); if(!ObjectUtils.isEmpty(last)){ if(!last.getState().equals(mesDeviceState.getState())){ // 状态相同说明没有改变,不需要更新 if(mesDeviceState.getStartDate().getTime() > last.getStartDate().getTime()){ Integer interval = Math.round((mesDeviceState.getStartDate().getTime() - last.getStartDate().getTime())/1000); last.setInterval(interval); last.setEndDate(mesDeviceState.getStartDate()); super.update(last); } super.save(mesDeviceState); } }else{ super.save(mesDeviceState); } } /** * 导入数据 * @param file 导入的数据文件 */ @Transactional public String importData(MultipartFile file) { if (file == null){ throw new ServiceException(text("请选择导入的数据文件!")); } int successNum = 0; int failureNum = 0; StringBuilder successMsg = new StringBuilder(); StringBuilder failureMsg = new StringBuilder(); try(ExcelImport ei = new ExcelImport(file, 2, 0)){ List list = ei.getDataList(MesDeviceState.class); for (MesDeviceState mesDeviceState : list) { try{ ValidatorUtils.validateWithException(mesDeviceState); this.save(mesDeviceState); successNum++; successMsg.append("
" + successNum + "、编号 " + mesDeviceState.getId() + " 导入成功"); } catch (Exception e) { failureNum++; String msg = "
" + failureNum + "、编号 " + mesDeviceState.getId() + " 导入失败:"; if (e instanceof ConstraintViolationException){ ConstraintViolationException cve = (ConstraintViolationException)e; for (ConstraintViolation violation : cve.getConstraintViolations()) { msg += Global.getText(violation.getMessage()) + " ("+violation.getPropertyPath()+")"; } }else{ msg += e.getMessage(); } failureMsg.append(msg); logger.error(msg, e); } } } catch (Exception e) { logger.error(e.getMessage(), e); failureMsg.append(e.getMessage()); return failureMsg.toString(); } if (failureNum > 0) { failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); throw new ServiceException(failureMsg.toString()); }else{ successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:"); } return successMsg.toString(); } /** * 更新状态 * @param mesDeviceState */ @Override @Transactional public void updateStatus(MesDeviceState mesDeviceState) { super.updateStatus(mesDeviceState); } /** * 删除数据 * @param mesDeviceState */ @Override @Transactional public void delete(MesDeviceState mesDeviceState) { super.delete(mesDeviceState); } /** * 更新设备状态 * @param mesDeviceState */ @Transactional public void updateState(MesDeviceState mesDeviceState) { // 获取上条数据,更新结束日期及持续时长 if(mesDeviceState.getStartDate() != null){ MesDeviceState mesDeviceState1 = new MesDeviceState(); mesDeviceState1.setDeviceSn(mesDeviceState.getDeviceSn()); MesDeviceState last = mesDeviceStateDao.findInfo(mesDeviceState1); if(!ObjectUtils.isEmpty(last)){ if(mesDeviceState.getStartDate().getTime() > last.getStartDate().getTime()){ Integer interval = Math.round((mesDeviceState.getStartDate().getTime() - last.getStartDate().getTime())/1000); last.setInterval(interval); last.setEndDate(mesDeviceState.getStartDate()); super.update(last); } } } super.save(mesDeviceState); } }