车间能级提升-智能设备管理系统
baoshiwei
2025-04-12 beaed6d077e7c3e9abfad68acb8c587835b5a406
eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/service/impl/EimsMaintPlanServiceImpl.java
@@ -1,8 +1,8 @@
package org.dromara.eims.service.impl;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import org.dromara.common.core.constant.DictConstants;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
@@ -12,7 +12,12 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.eims.domain.vo.EimsRepairResVo;
import org.dromara.eims.domain.EimsEqu;
import org.dromara.eims.domain.vo.EimsEquVo;
import org.dromara.eims.domain.vo.MaintCheckItemVo;
import org.dromara.eims.listener.EasyExcelCellListener;
import org.dromara.eims.listener.MaintCheckItemImportListener;
import org.dromara.eims.mapper.EimsEquMapper;
import org.dromara.system.domain.SysDept;
import org.dromara.system.domain.vo.SysDeptVo;
import org.dromara.system.mapper.SysDeptMapper;
@@ -22,7 +27,9 @@
import org.dromara.eims.domain.EimsMaintPlan;
import org.dromara.eims.mapper.EimsMaintPlanMapper;
import org.dromara.eims.service.IEimsMaintPlanService;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.*;
/**
@@ -37,6 +44,7 @@
    private final EimsMaintPlanMapper baseMapper;
    private final SysDeptMapper sysDeptMapper;
    private final EimsEquMapper equMapper;
    /**
     * 查询保养计划
@@ -106,6 +114,7 @@
        qw.eq(bo.getMaintUser() != null, "mp.maint_user", bo.getMaintUser());
        qw.in(bo.getMaintDept() != null, "mp.maint_dept", getAllDescendantIds(bo.getMaintDept()));
        qw.eq(bo.getStatus() != null, "mp.status", bo.getStatus());
        qw.orderByDesc("mp.create_time");
        return qw;
    }
@@ -144,7 +153,7 @@
     */
    @Override
    public Boolean insertByBo(EimsMaintPlanBo bo) {
        setMaintNextTime(bo);
        //setMaintNextTime(bo);
        EimsMaintPlan add = MapstructUtils.convert(bo, EimsMaintPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
@@ -154,46 +163,7 @@
        return flag;
    }
    /**
     * 计算保养计划下次执行时间
     */
    @SneakyThrows
    private void setMaintNextTime(EimsMaintPlanBo bo) {
        Date maintFirstTime = bo.getMaintFirstTime();
        Date maintLastTime = bo.getMaintLastTime();
        //下次保养时间计算规则 0-按固定周期  1-按上次保养时间
        Date nextDate = (maintLastTime != null && bo.getMaintRule().equals(DictConstants.MAINT_TIME_RULE_DETAIL.LAST)) ? maintLastTime :maintFirstTime;
        //首次执行时间为空抛出异常
        if (maintFirstTime == null) {
            throw new Exception("首次执行时间不能为空!");
        }
        //周期
        Long maintCycle = bo.getMaintCycle();
        //单位 1-天 2-周 3-月 4-季 5-年
        String maintCycleUnit = bo.getMaintCycleUnit();
        switch (maintCycleUnit) {
            case "1":
                nextDate = DateUtils.addDays(nextDate, maintCycle.intValue());
                break;
            case "2":
                nextDate = DateUtils.addWeeks(nextDate, maintCycle.intValue());
                break;
            case "3":
                nextDate = DateUtils.addMonths(nextDate, maintCycle.intValue());
                break;
            case "4":
                nextDate = DateUtils.addMonths(nextDate, maintCycle.intValue() * 3);
                break;
            case "5":
                nextDate = DateUtils.addYears(nextDate, maintCycle.intValue());
                break;
        }
        bo.setMaintNextTime(nextDate);
    }
    /**
     * 修改保养计划
@@ -232,4 +202,165 @@
    }
    public String importData(MultipartFile is, boolean updateSupport) throws IOException {
    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
        // 保养项目列表
        MaintCheckItemImportListener checkItemImportListener = new MaintCheckItemImportListener(updateSupport);
        EasyExcel.read(is.getInputStream(), MaintCheckItemVo.class, checkItemImportListener).headRowNumber(4).sheet().doRead();
        List<MaintCheckItemVo> successList = checkItemImportListener.getSuccessList();
        // 读取固定资产编号
        EasyExcelCellListener readListener = new EasyExcelCellListener(3, 1);
        EasyExcel.read(is.getInputStream(), readListener).headRowNumber(0).sheet().doReadSync();
        String assetNo = Optional.ofNullable(readListener.getCellValue())
            .map(value -> {
                int colonIndex = Math.max(value.indexOf(":"), value.indexOf(":")); // 合并冒号处理
                return colonIndex != -1 ? value.substring(colonIndex + 1) : value;
            })
            .map(String::trim)
            .orElseThrow(() -> new ServiceException("导入失败,无法读取固定资产编号"));
        // 读取保养计划年份
        EasyExcelCellListener readYearListener = new EasyExcelCellListener(2, 3);
        EasyExcel.read(is.getInputStream(), readYearListener).headRowNumber(0).sheet().doReadSync();
        String yearStr = readYearListener.getCellValue();
        String year = yearStr.replaceAll("[^\\d]", ""); // 去除非数字字符
        year = (year.length() == 4) ? year : DateUtils.getDate().substring(0,4);
        QueryWrapper<EimsEqu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_no", assetNo);
        EimsEquVo eimsEquVo = equMapper.selectVoOne(queryWrapper);
        if (eimsEquVo == null) throw new ServiceException("导入失败,设备未找到请在设备台帐中添加");
        // 月份字段处理优化
        String[] monthFields = {"january","february","march","april","may","june",
                              "july","august","september","october","november","december"};
        for (MaintCheckItemVo itemVo : successList) {
            if ("执行人签名".equals(itemVo.getItemName())) break;
            EimsMaintPlanBo maintPlanBo = new EimsMaintPlanBo();
            maintPlanBo.setEquId(eimsEquVo.getEquId());
            maintPlanBo.setMaintName(itemVo.getItemName());
            maintPlanBo.setStatus("0");
            maintPlanBo.setMaintType("1");
            maintPlanBo.setMaintRule("0");
            // 添加period校验
            String period = itemVo.getPeriod();
            if (StringUtils.isBlank(period)) {
                failureNum++;
                failureMsg.append(failureNum).append("、周期字段为空<br>");
                continue;
            }
            try {
                if (period.length() > 1) {
                    maintPlanBo.setMaintCycle(Long.parseLong(period.substring(0, period.length() - 1)));
                    String substring = period.substring(period.length() - 1);
                    // 转换周期单位,M转换为3,D转换为1,Y转换为5,W转换为2,Q转换为4
                    switch (substring) {
                        case "M":
                            substring = "3";
                            break;
                        case "D":
                            substring = "1";
                            break;
                        case "Y":
                            substring = "5";
                            break;
                        case "W":
                            substring = "2";
                            break;
                        case "Q":
                            substring = "4";
                            break;
                        default:
                    }
                    maintPlanBo.setMaintCycleUnit(substring);
                } else {
                    maintPlanBo.setMaintCycle(Long.parseLong(period));
                    maintPlanBo.setMaintCycleUnit("");
                }
            } catch (NumberFormatException e) {
                failureNum++;
                failureMsg.append(failureNum).append("、无效的周期格式:").append(period).append("<br>");
                continue;
            }
            // 月份判断优化
            for (int i = 0; i < monthFields.length; i++) {
                try {
                    String monthValue = (String) MaintCheckItemVo.class
                        .getMethod("get" + StringUtils.capitalize(monthFields[i]))
                        .invoke(itemVo);
                    if (StringUtils.isNotBlank(monthValue)) {
                        String month = String.format("%02d", i+1); // 保证两位月份
                        maintPlanBo.setMaintFirstTime(DateUtils.parseDate(year + "-" + month + "-01"));
                        break;
                    }
                } catch (Exception e) {
                    // 反射异常处理
                    failureNum++;
                    failureMsg.append(failureNum).append("、月份字段访问异常<br>");
                    continue;
                }
            }
            if (maintPlanBo.getMaintFirstTime() != null) {
                Date firstTime = maintPlanBo.getMaintFirstTime();
                Date nextTime = calcNextTime(firstTime, maintPlanBo.getMaintCycle().intValue(), 1);
                maintPlanBo.setMaintNextTime(nextTime);
            }
            if (!insertByBo(maintPlanBo)) {
                failureNum++;
                failureMsg.append(failureNum).append("、设备:").append(eimsEquVo.getEquName()).append(",导入失败<br>");
            } else {
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、设备:").append(eimsEquVo.getEquName()).append(",导入成功");
            }
        }
    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
        return failureMsg.toString();
    } else {
        successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        return successMsg.toString();
    }
}
private Date calcNextTime(Date firstTime, int intervalMonths, int initialOffset) {
    if (intervalMonths <= 0) {
        throw new IllegalArgumentException("Interval months must be positive");
    }
    if (firstTime == null) {
        throw new IllegalArgumentException("First time cannot be null");
    }
    Date current = new Date();
    int adjustmentCount = initialOffset;
    Date workingDate = (Date) firstTime.clone();
    while (workingDate.before(current)) {
        adjustmentCount++;
        workingDate = DateUtils.addMonths(workingDate, intervalMonths);
    }
    return (Date) workingDate.clone();
}
}