【开发】特种设备(二):自动更新下次检验时间

特种设备新增是在PC端,维护人员不确定下次检验时间是否需要填写,实际上,大量的特种设备都是固定时间检验,比如30天,60天或者180天等。

因此,下次检验时间可以由程序自动生成。

该步骤的触发有四个机制:

  1. 新增特种设备
  2. 更新特种设备
  3. 特种设备延期验收。
  4. 新增特种设备验收
    5. 更新特种设备验收(修改验收,不能修改上次检验时间和有效期。)

虽然听起来很多,但是代码量并不多,很多方法都是可以复用的。

这里面最主要的难点,是业务比较复杂,因为每次确定下次检验时间,那么三次推送机制:提前30天推送,提前7天推送,当天推送,就需要实时进行变更。

变更时,还要判断变更后的下次推送时间,是否满足三次推送,比如,提前30天推送,是否大于当前时间,如果不大于,那么这项推送可以取消。

距离,某设备的10月1号是下次检验时间,那么9月1号需要推送一次,如果他在9月25号,将该设备的下次检验时间,更改为10月7号,那么提前30天推送,也就是9月7号明显早于9月25号,此时就不需要提前30天推送,只需要推送2次,也就是提前7天推送和当天推送。

1. 新增特种设备

	/**
     * 新增特种设备
     */
    @Override
    @Transactional
    public int insertSpecEquipment(SpecEquipment specEquipment){
        // 获取下次检验时间
        specEquipment = checkNextInspectionDate(specEquipment);
        specEquipment.setCreateTime(DateUtils.getNowDate());
        specEquipment.setCreateBy(SecurityUtils.getRealName());
        return specEquipmentMapper.insertSpecEquipment(specEquipment);
    }
    /**
     * 若下次检验时间为空,则自动填充
     */
    public SpecEquipment checkNextInspectionDate(SpecEquipment specEquipment){
        if(specEquipment.getNextInspectionDate() == null){
            if(specEquipment.getValidPeriod() == null || specEquipment.getValidPeriod() == 0){
                throw new ServiceException("该特种设备验收有效期为0,请联系管理员!");
            }
            Date nextInspectionDate = DateUtils.plusDay(specEquipment.getLastInspectionDate(), specEquipment.getValidPeriod());
            specEquipment.setNextInspectionDate(nextInspectionDate);
        }
        return specEquipment;
    }

2. 更新特种设备

	/**
     * 修改特种设备
     */
    @Override
    @Transactional
    public int updateSpecEquipment(SpecEquipment specEquipment){
        // 获取下次检验时间
        specEquipment = checkNextInspectionDate(specEquipment);
        specEquipment.setUpdateBy(SecurityUtils.getRealName());
        specEquipment.setUpdateTime(DateUtils.getNowDate());
        return specEquipmentMapper.updateSpecEquipment(specEquipment);
    }
    /**
     * 若下次检验时间为空,则自动填充
     */
    public SpecEquipment checkNextInspectionDate(SpecEquipment specEquipment){
        if(specEquipment.getNextInspectionDate() == null){
            if(specEquipment.getValidPeriod() == null || specEquipment.getValidPeriod() == 0){
                throw new ServiceException("该特种设备验收有效期为0,请联系管理员!");
            }
            Date nextInspectionDate = DateUtils.plusDay(specEquipment.getLastInspectionDate(), specEquipment.getValidPeriod());
            specEquipment.setNextInspectionDate(nextInspectionDate);
        }
        return specEquipment;
    }

3. 特种设备延期验收

特种设备延期验收,本质上就是更新特种设备表中的下次检验时间,只不过这是手动修改,它的逻辑和 第二种机制 更新特种设备是一样的。

只不过在 checkLastInspectionDate时。

因为specEquipment.getNextInspectionDate()不为空,直接返回对象,没有计算它的下次检验时间。

4. 新增特种设备验收

	/**
     * 新增特种设备检验填报
     */
    @Override
    @Transactional
    public int insertSpecInspect(SpecInspect specInspect){
        // 上次填报时间为当前系统时间
        specInspect.setLastInspectionDate(DateUtils.getNowDate());
        specInspect.setCreateTime(DateUtils.getNowDate());
        specInspect.setDeptId(0L);
        // 获取当前填报人所属的部门
        if (StringUtils.isNotEmpty(specInspect.getInspectMan())) {
            SysUser sysUser = userMapper.selectUserByUserName(specInspect.getInspectMan());
            if (StringUtils.isNotNull(sysUser)){
                specInspect.setDeptId(sysUser.getDeptId());
            }
        }
        // 验证下次检验日期
        specInspect = checkNextInspectionDate(specInspect);
        specInspectMapper.insertSpecInspect(specInspect);
        // 更新特种设备表中的上次检验日期和下次检验日期
        return autoUpdateSpecEquipment(specInspect);
    }
    /**
     * 若下次检验时间为空,则自动填充
     */
    public SpecInspect checkNextInspectionDate(SpecInspect specInspect){
        if(specInspect.getNextInspectionDate() == null ){
            // 获取有效期
            SpecEquipment s = specEquipmentMapper.selectSpecEquipmentById(specInspect.getSpecId());
            if(s != null){
                if(s.getValidPeriod() == null || s.getValidPeriod() == 0){
                    throw new ServiceException("该特种设备验收有效期为0,请联系管理员!");
                }
                // 下次填报时间为空,则根据上次填报时间和有效期,自动计算下次填报时间
                Date nextInspectionDate = DateUtils.plusDay(specInspect.getLastInspectionDate(), s.getValidPeriod());
                specInspect.setNextInspectionDate(nextInspectionDate);
            }
        }
        return specInspect;
    }
    /**
     * 更新特种设备表中的下次检验时间
     */
    public int autoUpdateSpecEquipment(SpecInspect specInspect){
        SpecEquipment se = new SpecEquipment();
        se.setId(specInspect.getSpecId());
        se.setLastInspectionDate(specInspect.getLastInspectionDate());
        se.setNextInspectionDate(specInspect.getNextInspectionDate());
        return specEquipmentMapper.updateSpecEquipment(se);
    }
posted @ 2022-04-06 08:02  layman~  阅读(136)  评论(0编辑  收藏  举报