zhangherong
8 天以前 2d3ded65b3c30b4f5cba6080d9c28e16a0b296cc
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamMaintenanceStandardServiceImpl.java
@@ -20,10 +20,6 @@
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.BaseFactory;
import org.jeecg.modules.system.entity.BaseFactoryUser;
import org.jeecg.modules.system.service.IBaseFactoryService;
import org.jeecg.modules.system.service.IBaseFactoryUserService;
import org.jeecg.modules.eam.constant.BusinessCodeConst;
import org.jeecg.modules.eam.constant.EamMaintenanceStandardDetailCategory;
import org.jeecg.modules.eam.constant.MaintenanceStandardStatusEnum;
@@ -38,15 +34,17 @@
import org.jeecg.modules.eam.service.IEamEquipmentService;
import org.jeecg.modules.eam.service.IEamMaintenanceStandardDetailService;
import org.jeecg.modules.eam.service.IEamMaintenanceStandardService;
import org.jeecg.modules.eam.vo.*;
import org.jeecg.modules.eam.vo.EamMaintenanceStandardVo;
import org.jeecg.modules.flowable.apithird.business.entity.FlowMyBusiness;
import org.jeecg.modules.flowable.apithird.business.service.IFlowMyBusinessService;
import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI;
import org.jeecg.modules.flowable.apithird.service.FlowCommonService;
import org.jeecg.modules.flowable.service.IFlowDefinitionService;
import org.jeecg.modules.flowable.service.IFlowTaskService;
import org.jeecg.modules.system.service.ISysBusinessCodeRuleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.entity.BaseFactory;
import org.jeecg.modules.system.entity.BaseFactoryUser;
import org.jeecg.modules.system.entity.SysParams;
import org.jeecg.modules.system.service.*;
import org.jeecg.modules.system.vo.UserSelector;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -65,11 +63,11 @@
/**
 * @Description: 保养标准
 * @Author: jeecg-boot
 * @Date:   2025-03-26
 * @Date: 2025-03-26
 * @Version: V1.0
 */
@Service("IEamMaintenanceStandardService")
public class EamMaintenanceStandardServiceImpl extends ServiceImpl<EamMaintenanceStandardMapper, EamMaintenanceStandard> implements IEamMaintenanceStandardService , FlowCallBackServiceI {
public class EamMaintenanceStandardServiceImpl extends ServiceImpl<EamMaintenanceStandardMapper, EamMaintenanceStandard> implements IEamMaintenanceStandardService, FlowCallBackServiceI {
    @Resource
    private EamMaintenanceStandardMapper eamMaintenanceStandardMapper;
@@ -97,6 +95,8 @@
    private IEamEquipmentExtendService eamEquipmentExtendService;
    @Autowired
    private ISysBusinessCodeRuleService businessCodeRuleService;
    @Autowired
    private ISysParamsService sysParamsService;
    // 常量定义
    private static final String REPAIR_TITLE = "维修人员保养内容";
@@ -124,45 +124,45 @@
            queryWrapper.in("e.equipment_code", equipArr);
        } else {
            //没有选择设备,根据中心过滤设备
            List<BaseFactoryUser> baseFactoryUserList=baseFactoryUserService.
                    list(new LambdaQueryWrapper<BaseFactoryUser>().eq(BaseFactoryUser::getUserId,sysUser.getId()));
            if(!CollectionUtils.isEmpty(baseFactoryUserList)){
            List<BaseFactoryUser> baseFactoryUserList = baseFactoryUserService.
                    list(new LambdaQueryWrapper<BaseFactoryUser>().eq(BaseFactoryUser::getUserId, sysUser.getId()));
            if (!CollectionUtils.isEmpty(baseFactoryUserList)) {
                Set<String> factoryIds = baseFactoryUserList.stream().map(BaseFactoryUser::getFactoryId).collect(Collectors.toSet());
                Set<String> factoryCode= baseFactoryService.listByIds(factoryIds).stream().map(BaseFactory::getOrgCode).collect(Collectors.toSet());
                Set<String> factoryCode = baseFactoryService.listByIds(factoryIds).stream().map(BaseFactory::getOrgCode).collect(Collectors.toSet());
                queryWrapper.in("e.factory_org_code", factoryCode);
            } else {
                return page;
            }
        }
        if(eamMaintenanceStandard != null) {
        if (eamMaintenanceStandard != null) {
            //编码 模糊查询
            if(StringUtils.isNotBlank(eamMaintenanceStandard.getStandardCode())) {
            if (StringUtils.isNotBlank(eamMaintenanceStandard.getStandardCode())) {
                queryWrapper.like("ems.standard_code", eamMaintenanceStandard.getStandardCode());
            }
            //名称 模糊查询
            if(StringUtils.isNotBlank(eamMaintenanceStandard.getStandardName())) {
            if (StringUtils.isNotBlank(eamMaintenanceStandard.getStandardName())) {
                queryWrapper.like("ems.standard_name", eamMaintenanceStandard.getStandardName());
            }
            //设备
            if(StringUtils.isNotBlank(eamMaintenanceStandard.getEquipmentId())) {
            if (StringUtils.isNotBlank(eamMaintenanceStandard.getEquipmentId())) {
                queryWrapper.eq("ems.equipment_id", eamMaintenanceStandard.getEquipmentId());
            }
            //保养分类
            if(StringUtils.isNotBlank(eamMaintenanceStandard.getMaintenanceCategory())) {
            if (StringUtils.isNotBlank(eamMaintenanceStandard.getMaintenanceCategory())) {
                queryWrapper.eq("ems.maintenance_category", eamMaintenanceStandard.getMaintenanceCategory());
            }
            //保养分类
            if(StringUtils.isNotBlank(eamMaintenanceStandard.getStandardStatus())) {
            if (StringUtils.isNotBlank(eamMaintenanceStandard.getStandardStatus())) {
                queryWrapper.eq("ems.standard_status", eamMaintenanceStandard.getStandardStatus());
            }
            //设备编码
            if(StringUtils.isNotBlank(eamMaintenanceStandard.getKeyword())) {
            if (StringUtils.isNotBlank(eamMaintenanceStandard.getKeyword())) {
                queryWrapper.and(wrapper -> wrapper.like("e.equipment_name", eamMaintenanceStandard.getKeyword())
                        .or()
                        .like("e.equipment_code", eamMaintenanceStandard.getKeyword()));
            }
            //id
            if(StringUtils.isNotBlank(eamMaintenanceStandard.getId())) {
            if (StringUtils.isNotBlank(eamMaintenanceStandard.getId())) {
                queryWrapper.eq("ems.id", eamMaintenanceStandard.getId());
            }
        }
@@ -177,19 +177,19 @@
        BeanUtils.copyProperties(standardRequest, entity);
        entity.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
        //版本递增
        entity.setStandardVersion("v"+CommonConstant.OPERATE_TYPE_1);
        entity.setStandardVersion("v" + CommonConstant.OPERATE_TYPE_1);
        //设备处理
        entity.setEquipmentId(standardRequest.getEquipmentId());
        //删除标记
        entity.setDelFlag(CommonConstant.DEL_FLAG_0);
        //重复性校验
        EamMaintenanceStandard exist = checkDuplicate(entity.getEquipmentId(), entity.getMaintenanceCategory(),MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
        if(exist != null){
        EamMaintenanceStandard exist = checkDuplicate(entity.getEquipmentId(), entity.getMaintenanceCategory(), MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
        if (exist != null) {
            throw new JeecgBootException("设备标准已存在,不能重复添加!");
        }
        eamMaintenanceStandardMapper.insert(entity);
        //处理明细数据
        if(CollectionUtil.isNotEmpty(standardRequest.getTableDetailList())) {
        if (CollectionUtil.isNotEmpty(standardRequest.getTableDetailList())) {
            standardRequest.getTableDetailList().forEach(tableDetail -> {
                tableDetail.setStandardId(entity.getId());
            });
@@ -202,7 +202,7 @@
    @Transactional(rollbackFor = Exception.class)
    public boolean editMaintenanceStandard(EamMaintenanceStandardRequest standardRequest) {
        EamMaintenanceStandard entity = eamMaintenanceStandardMapper.selectById(standardRequest.getId());
        if(entity == null){
        if (entity == null) {
            throw new JeecgBootException("编辑的数据已删除,请刷新重试!");
        }
        entity.setStandardName(standardRequest.getStandardName());
@@ -211,25 +211,17 @@
        //处理详情
        if(CollectionUtil.isNotEmpty(standardRequest.getTableDetailList())) {
            List<EamMaintenanceStandardDetail> addList = new ArrayList<>();
            List<EamMaintenanceStandardDetail> updateList = new ArrayList<>();
            //先删除原有
            LambdaQueryWrapper<EamMaintenanceStandardDetail> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(EamMaintenanceStandardDetail::getStandardId, standardRequest.getId());
            eamMaintenanceStandardDetailService.remove(queryWrapper);
            standardRequest.getTableDetailList().forEach(tableDetail -> {
                tableDetail.setStandardId(entity.getId());
                if(tableDetail.getId() == null){
                    addList.add(tableDetail);
                }else {
                    updateList.add(tableDetail);
                }
                EamMaintenanceStandardDetail eamMaintenanceStandardDetail=new EamMaintenanceStandardDetail();
                BeanUtils.copyProperties(tableDetail, eamMaintenanceStandardDetail);
                eamMaintenanceStandardDetail.setStandardId(entity.getId());
                addList.add(eamMaintenanceStandardDetail);
            });
            if(CollectionUtil.isNotEmpty(addList)){
                eamMaintenanceStandardDetailService.saveBatch(addList);
            }
            if(CollectionUtil.isNotEmpty(updateList)){
                eamMaintenanceStandardDetailService.updateBatchById(updateList);
            }
        }
        if(CollectionUtil.isNotEmpty(standardRequest.getRemoveDetailList())) {
            List<String> ids = standardRequest.getRemoveDetailList().stream().map(EamMaintenanceStandardDetail::getId).collect(Collectors.toList());
            eamMaintenanceStandardDetailService.removeBatchByIds(ids);
            eamMaintenanceStandardDetailService.saveBatch(addList);
        }
        return true;
    }
@@ -238,7 +230,7 @@
    @Transactional(rollbackFor = Exception.class)
    public boolean upgradeMaintenanceStandard(EamMaintenanceStandardRequest standardRequest) {
        EamMaintenanceStandard entity = eamMaintenanceStandardMapper.selectById(standardRequest.getId());
        if(entity == null){
        if (entity == null) {
            throw new JeecgBootException("编辑的数据已删除,请刷新重试!");
        }
@@ -258,7 +250,7 @@
        if (matcher.find()) {
            try {
                int mainVersion = Integer.parseInt(matcher.group(1));
                newEntity.setStandardVersion("v"+(mainVersion+1));
                newEntity.setStandardVersion("v" + (mainVersion + 1));
            } catch (NumberFormatException ignored) {
            }
        }
@@ -267,13 +259,13 @@
        //删除标记
        newEntity.setDelFlag(CommonConstant.DEL_FLAG_0);
        //重复性校验
        EamMaintenanceStandard exist = checkDuplicate(newEntity.getEquipmentId(), newEntity.getMaintenanceCategory(),MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
        if(exist != null){
        EamMaintenanceStandard exist = checkDuplicate(newEntity.getEquipmentId(), newEntity.getMaintenanceCategory(), MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
        if (exist != null) {
            throw new JeecgBootException("设备标准已存在,不能重复添加!");
        }
        eamMaintenanceStandardMapper.insert(newEntity);
        //处理明细数据
        if(CollectionUtil.isNotEmpty(standardRequest.getTableDetailList())) {
        if (CollectionUtil.isNotEmpty(standardRequest.getTableDetailList())) {
            standardRequest.getTableDetailList().forEach(tableDetail -> {
                tableDetail.setId(null);
                tableDetail.setCreateBy(null);
@@ -288,7 +280,7 @@
    }
    @Override
    public EamMaintenanceStandard checkDuplicate(String equipmentId, String maintenanceCategory,String standardStatus) {
    public EamMaintenanceStandard checkDuplicate(String equipmentId, String maintenanceCategory, String standardStatus) {
        LambdaQueryWrapper<EamMaintenanceStandard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EamMaintenanceStandard::getEquipmentId, equipmentId);
        queryWrapper.eq(EamMaintenanceStandard::getMaintenanceCategory, maintenanceCategory);
@@ -297,7 +289,7 @@
        queryWrapper.orderByDesc(EamMaintenanceStandard::getStandardVersion);
        List<EamMaintenanceStandard> list = eamMaintenanceStandardMapper.selectList(queryWrapper);
        if(CollectionUtil.isEmpty(list)) {
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return list.get(0);
@@ -305,12 +297,13 @@
    /**
     * 查询标准列表-前端展示该用户拥有的标准
     * @param keyword 设备编号
     *
     * @param keyword             设备编号
     * @param maintenanceCategory 保养类型
     * @return
     */
    @Override
    public List<EamMaintenanceStandard> queryListByKeywordAndCategory(String keyword, String equipmentId, Integer pageSize, String maintenanceCategory){
    public List<EamMaintenanceStandard> queryListByKeywordAndCategory(String keyword, String equipmentId, Integer pageSize, String maintenanceCategory) {
        Page<EamMaintenanceStandard> page = new Page<EamMaintenanceStandard>(1, pageSize);
        EamMaintenanceStandard query = new EamMaintenanceStandard();
        query.setEquipmentId(equipmentId);
@@ -335,28 +328,29 @@
    /**
     * 流程启动,保存对应的数据
     *
     * @param id
     * @return
     */
    @Override
    public Result<?> saveEamMaintenanceStandardProcess(String id){
    public Result<?> saveEamMaintenanceStandardProcess(String id) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        EamMaintenanceStandard maintenanceStandard=this.getById(id);
        if (maintenanceStandard==null){
        EamMaintenanceStandard maintenanceStandard = this.getById(id);
        if (maintenanceStandard == null) {
            return Result.error("未找到对应保养标准");
        }
        System.out.println("保养规范流程:" + maintenanceStandard.getId());
        flowCommonService.initActBusiness(maintenanceStandard.getStandardName()+"规范进行流程审核",
        flowCommonService.initActBusiness(maintenanceStandard.getStandardName() + "规范进行流程审核",
                maintenanceStandard.getId(), "IEamMaintenanceStandardService", "eam_maintenance_standard", null);
        Map<String, Object> variables = new HashMap<>();
        variables.put("dataId", maintenanceStandard.getId());
        variables.put("organization", "保养规范启动流程");
        variables.put("comment", "保养规范启动流程");
        variables.put("proofreading",true);
        Result result= flowDefinitionService.startProcessInstanceByKey("eam_maintenance_standard", variables);
        variables.put("proofreading", true);
        Result result = flowDefinitionService.startProcessInstanceByKey("eam_maintenance_standard", variables);
        if (!result.isSuccess()) {
            super.removeById(maintenanceStandard.getId());
        }else {
        } else {
            maintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_REPAIR_DIRECTOR.name());
            maintenanceStandard.setDesigner(user.getUsername());
            maintenanceStandard.setDesignTime(new Date());
@@ -370,11 +364,10 @@
                return Result.error("流程记录不存在");
            }
            FlowMyBusiness flowMyBusiness = businessList.get(0);
            EamEquipment eamEquipment=eamEquipmentService.getById(maintenanceStandard.getEquipmentId());
            BaseFactory baseFactory=baseFactoryService.getOne(new QueryWrapper<BaseFactory>().eq("org_code", eamEquipment.getFactoryOrgCode()));
            List<UserSelector> userSelectorList=sysUserService.selectOperatorFactoryList(eamEquipment.getEquipmentCode(),baseFactory.getId(), BusinessCodeConst.PCR0008);
            EamEquipment eamEquipment = eamEquipmentService.getById(maintenanceStandard.getEquipmentId());
            List<UserSelector> userSelectorList = sysUserService.selectOperatorList(eamEquipment.getEquipmentCode(), eamEquipment.getFactoryOrgCode(), BusinessCodeConst.PCR0008);
            if (!CollectionUtils.isEmpty(userSelectorList)) {
                List<String> usernameList=userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.toList());
                List<String> usernameList = userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.toList());
                flowMyBusiness.setTodoUsers(JSON.toJSONString(usernameList));
                flowMyBusinessService.updateById(flowMyBusiness);
            }
@@ -384,11 +377,12 @@
    /**
     * 审批操作
     *
     * @param eamMaintenanceStandardVo
     * @return
     */
    @Override
    public Result<?> auditEamMaintenanceStandard(EamMaintenanceStandardVo eamMaintenanceStandardVo){
    public Result<?> auditEamMaintenanceStandard(EamMaintenanceStandardVo eamMaintenanceStandardVo) {
        try {
            // 参数校验
            if (StrUtil.isEmpty(eamMaintenanceStandardVo.getTaskId()) || StrUtil.isEmpty(eamMaintenanceStandardVo.getDataId())) {
@@ -442,23 +436,22 @@
            Result result = flowTaskService.complete(eamMaintenanceStandardVo);
            if (result.isSuccess()) {
                if (eamMaintenanceStandardVo.getRepairManagerApproveResult() != null) {
                    if (eamMaintenanceStandardVo.getRepairManagerApproveResult().equals("1")){
                    if (eamMaintenanceStandardVo.getRepairManagerApproveResult().equals("1")) {
                        List<FlowMyBusiness> newbusinessList = flowMyBusinessService.list(
                                new QueryWrapper<FlowMyBusiness>()
                                        .eq("process_instance_id", eamMaintenanceStandardVo.getInstanceId()));
                        FlowMyBusiness newflowMyBusiness = newbusinessList.get(0);
                        eamMaintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_TECHNICAL_DIRECTOR.name());
                        //获取flow的流程实例id,设置下一步的处理人员
                        EamMaintenanceStandard maintenanceStandard=this.getById(eamMaintenanceStandardVo.getDataId());
                        EamEquipment eamEquipment=eamEquipmentService.getById(maintenanceStandard.getEquipmentId());
                        BaseFactory baseFactory=baseFactoryService.getOne(new QueryWrapper<BaseFactory>().eq("org_code", eamEquipment.getFactoryOrgCode()));
                        List<UserSelector> userSelectorList=sysUserService.selectOperatorFactoryList(eamEquipment.getEquipmentCode(),baseFactory.getId(), BusinessCodeConst.PCR0009);
                        EamMaintenanceStandard maintenanceStandard = this.getById(eamMaintenanceStandardVo.getDataId());
                        EamEquipment eamEquipment = eamEquipmentService.getById(maintenanceStandard.getEquipmentId());
                        List<UserSelector> userSelectorList = sysUserService.selectOperatorList(eamEquipment.getEquipmentCode(), eamEquipment.getFactoryOrgCode(), BusinessCodeConst.PCR0009);
                        if (!CollectionUtils.isEmpty(userSelectorList)) {
                            List<String> usernameList=userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.toList());
                            List<String> usernameList = userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.toList());
                            newflowMyBusiness.setTodoUsers(JSON.toJSONString(usernameList));
                            flowMyBusinessService.updateById(newflowMyBusiness);
                        }
                    }else {
                    } else {
                        eamMaintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
                    }
                    eamMaintenanceStandard.setRepairManager(user.getUsername());
@@ -467,15 +460,15 @@
                    eamMaintenanceStandard.setRepairManagerApproveComment(eamMaintenanceStandardVo.getComment());
                }
                if (eamMaintenanceStandardVo.getTechnicalManagerApproveResult() != null) {
                    if (eamMaintenanceStandardVo.getTechnicalManagerApproveResult().equals("1")){
                    if (eamMaintenanceStandardVo.getTechnicalManagerApproveResult().equals("1")) {
                        eamMaintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.START.name());
                        //判断是否进行升版,通过设备编号、保养分类、状态进行筛选
                        EamMaintenanceStandard maintenanceStandard=this.getById(eamMaintenanceStandardVo.getDataId());
                        QueryWrapper<EamMaintenanceStandard> queryWrapper=new QueryWrapper<>();
                        EamMaintenanceStandard maintenanceStandard = this.getById(eamMaintenanceStandardVo.getDataId());
                        QueryWrapper<EamMaintenanceStandard> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("equipment_id", maintenanceStandard.getEquipmentId());
                        queryWrapper.eq("maintenance_category", maintenanceStandard.getMaintenanceCategory());
                        queryWrapper.eq("standard_status", MaintenanceStandardStatusEnum.START.name());
                        List<EamMaintenanceStandard> list=eamMaintenanceStandardMapper.selectList(queryWrapper);
                        List<EamMaintenanceStandard> list = eamMaintenanceStandardMapper.selectList(queryWrapper);
                        if (!CollectionUtils.isEmpty(list)) {
                            //作废原有
                            for (EamMaintenanceStandard eamMaintenanceStandard1 : list) {
@@ -483,7 +476,7 @@
                                this.updateById(eamMaintenanceStandard1);
                            }
                        }
                    }else {
                    } else {
                        eamMaintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
                    }
                    eamMaintenanceStandard.setTechnicalManager(user.getUsername());
@@ -546,9 +539,10 @@
    /*导入点检文件Excel--------------------------开始*/
    /**
        * 点检表导入入口
    */
     * 点检表导入入口
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> importPointInspectionExcel(MultipartFile file) {
@@ -560,32 +554,32 @@
            // 1. 提取设备信息
            EamMaintenanceStandard eamMaintenanceStandard = extractDeviceInfo(sheet);
            if (eamMaintenanceStandard == null) {
            if (eamMaintenanceStandard == null || eamMaintenanceStandard.getEquipmentId() == null) {
                return Result.error("设备信息提取失败");
            }else {
                if (eamMaintenanceStandard.getEquipmentId() == null) {
                    return Result.error("设备信息提取失败");
                }
            }
            eamMaintenanceStandard.setStandardName(name);
            EamMaintenanceStandard exist = checkDuplicate(eamMaintenanceStandard.getEquipmentId(), eamMaintenanceStandard.getMaintenanceCategory(),MaintenanceStandardStatusEnum.START.name());
            if(exist != null){
            // 检查重复
            EamMaintenanceStandard exist = checkDuplicate(eamMaintenanceStandard.getEquipmentId(),
                    eamMaintenanceStandard.getMaintenanceCategory(), MaintenanceStandardStatusEnum.START.name());
            if (exist != null) {
                return Result.error(name + ": 设备标准已存在,不能重复添加");
            }
            eamMaintenanceStandardMapper.insert(eamMaintenanceStandard);
            // 2. 提取每日点检项目
            List<EamMaintenanceStandardDetail> dailyDetails = extractDailyItems(sheet, eamMaintenanceStandard);
            if(dailyDetails.isEmpty()){
            Map<Integer, String> rowErrors = new HashMap<>();
            // 2. 提取每日点检项目(优化空行和结束标记处理)
            List<EamMaintenanceStandardDetail> dailyDetails = extractDailyItems(sheet, eamMaintenanceStandard, rowErrors);
            if (dailyDetails.isEmpty()) {
                return Result.error("未找到每日点检项目");
            }
            // 3. 提取周保养项目
            List<EamMaintenanceStandardDetail> weeklyDetails = extractWeeklyItems(sheet, eamMaintenanceStandard);
            if(weeklyDetails.isEmpty()){
            List<EamMaintenanceStandardDetail> weeklyDetails = extractWeeklyItems(sheet, eamMaintenanceStandard, rowErrors);
            if (weeklyDetails.isEmpty()) {
                return Result.error("未找到周保养项目");
            }
@@ -598,6 +592,17 @@
                eamMaintenanceStandardDetailService.saveBatch(allDetails);
            }
            // 触发保养流程
            SysParams sysParams = sysParamsService.getSysPramBySettingKey("maintenance_import_type");
            if (sysParams != null) {
                if (sysParams.getSettingValue().equals("1")){
                    eamMaintenanceStandard.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
                    eamMaintenanceStandardMapper.updateById(eamMaintenanceStandard);
                }
            } else {
                return Result.error("未找到保养流程导入相关配置,请联系管理员");
            }
            return Result.OK("点检表导入成功");
        } catch (Exception e) {
@@ -606,8 +611,8 @@
    }
    /**
        * 提取点检表标题
    */
     * 提取点检表标题
     */
    private String extractInspectionTitle(MultipartFile file) {
        try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) {
            Sheet sheet = workbook.getSheetAt(0);
@@ -631,7 +636,7 @@
    }
    /**
        * 提取点检表之前的文字
     * 提取点检表之前的文字
     */
    private String extractTextBeforeInspection(String title) {
        if (StringUtils.isBlank(title)) {
@@ -643,8 +648,8 @@
    }
    /**
        * 提取设备信息
    */
     * 提取设备信息
     */
    private EamMaintenanceStandard extractDeviceInfo(Sheet sheet) {
        Row headerRow = sheet.getRow(0);
        if (headerRow == null) {
@@ -679,7 +684,7 @@
            EamEquipment equipments = eamEquipmentService.selectByEquipmentCode(standard.getEquipmentCode());
            if (equipments == null) {
                return null;
            }else {
            } else {
                standard.setEquipmentId(equipments.getId());
            }
        }
@@ -696,7 +701,7 @@
    }
    /**
        * 解析日期字符串
     * 解析日期字符串
     */
    private Date parseDate(String dateStr) throws ParseException {
        // 尝试多种日期格式
@@ -722,172 +727,8 @@
    }
    /**
        * 提取每日点检项目
    */
    private List<EamMaintenanceStandardDetail> extractDailyItems(Sheet sheet, EamMaintenanceStandard standard) {
        return extractItems(sheet, standard, "点检项目", "完成数据/要求", "DAY_INSPECTION");
    }
    /**
        * 提取周保养项目
    */
    private List<EamMaintenanceStandardDetail> extractWeeklyItems(Sheet sheet, EamMaintenanceStandard standard) {
        return extractItems(sheet, standard, "周保养项目", "检查标准", "WEEK_INSPECTION");
    }
    /**
        * 通用项目提取方法
    */
    private List<EamMaintenanceStandardDetail> extractItems(Sheet sheet, EamMaintenanceStandard standard,
                                                            String primaryHeader, String secondaryHeader,
                                                            String itemCategory) {
        int[] section = findTableSection(sheet, primaryHeader, secondaryHeader);
        if (section == null) {
            return Collections.emptyList();
        }
        List<EamMaintenanceStandardDetail> details = new ArrayList<>();
        for (int rowIdx = section[0]; rowIdx <= section[1]; rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            if (row == null || isEmptyRow(row)) {
                continue;
            }
            // 确保第一列是序号(数字)
            Cell seqCell = row.getCell(0);
            if (seqCell == null || seqCell.getCellType() != CellType.NUMERIC) {
                continue;
            }
            // 创建项目详情
            EamMaintenanceStandardDetail detail = new EamMaintenanceStandardDetail();
            detail.setStandardId(standard.getId());
            detail.setItemName(getCellStringValue(row.getCell(1)));
            detail.setItemCategory(itemCategory);
            // 根据项目类型设置需求字段
            if ("DAY_INSPECTION".equals(itemCategory)) {
                detail.setItemDemand(getCellStringValue(row.getCell(2)));
            } else if ("WEEK_INSPECTION".equals(itemCategory)) {
                detail.setItemDemand(getCellStringValue(row.getCell(2)));
            }
            details.add(detail);
        }
        return details;
    }
    /**
        * 查找表格区域
     * 使用正则提取字段
     */
    private int[] findTableSection(Sheet sheet, String primaryHeader, String secondaryHeader) {
        for (int rowIdx = 0; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            if (row == null) continue;
            if (isHeaderRow(row, primaryHeader, secondaryHeader)) {
                int startRow = rowIdx + 1;
                int endRow = findDataEnd(sheet, startRow);
                return new int[]{startRow, endRow};
            }
        }
        return null;
    }
    /**
        * 检查是否为表头行
    */
    private boolean isHeaderRow(Row row, String header1, String header2) {
        boolean foundHeader1 = false;
        boolean foundHeader2 = false;
        for (int colIdx = 0; colIdx < row.getLastCellNum(); colIdx++) {
            Cell cell = row.getCell(colIdx);
            if (cell == null) continue;
            String cellValue = getCellStringValue(cell);
            if (cellValue.contains(header1)) foundHeader1 = true;
            if (cellValue.contains(header2)) foundHeader2 = true;
        }
        return foundHeader1 && foundHeader2;
    }
    /**
        * 查找数据结束位置
     */
    private int findDataEnd(Sheet sheet, int startRow) {
        for (int rowIdx = startRow; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            if (row == null) return rowIdx - 1;
            // 检查是否结束标志行(如签字行)
            if (isSignatureRow(row)) {
                return rowIdx - 1;
            }
            // 检查是否新的表头开始
            if (isNewHeaderStart(row)) {
                return rowIdx - 1;
            }
        }
        return sheet.getLastRowNum();
    }
    /**
        * 识别签字行特征
    */
    private boolean isSignatureRow(Row row) {
        for (int colIdx = 0; colIdx < row.getLastCellNum(); colIdx++) {
            Cell cell = row.getCell(colIdx);
            if (cell == null) continue;
            String value = getCellStringValue(cell);
            if (value.contains("签字") || value.contains("责任人") ||
                    value.contains("执行") || value.contains("确认")) {
                return true;
            }
        }
        return false;
    }
    /**
     * 识别新表头开始
    */
    private boolean isNewHeaderStart(Row row) {
        for (int colIdx = 0; colIdx < row.getLastCellNum(); colIdx++) {
            Cell cell = row.getCell(colIdx);
            if (cell == null) continue;
            String value = getCellStringValue(cell);
            if ("序号".equals(value) || "点检项目".equals(value) || "周保养项目".equals(value)) {
                return true;
            }
        }
        return false;
    }
    /**
        * 检查行是否为空
    */
    private boolean isEmptyRow(Row row) {
        if (row == null) return true;
        for (int colIdx = 0; colIdx < row.getLastCellNum(); colIdx++) {
            Cell cell = row.getCell(colIdx);
            if (cell != null && cell.getCellType() != CellType.BLANK) {
                String value = getCellStringValue(cell);
                if (StringUtils.isNotBlank(value)) {
                    return false;
                }
            }
        }
        return true;
    }
    /**
        * 使用正则提取字段
    */
    private String extractField(String text, String regex) {
        if (StringUtils.isBlank(text)) return "";
        Pattern pattern = Pattern.compile(regex);
@@ -896,10 +737,139 @@
    }
    /**
        * 获取单元格字符串值
            * 提取每日点检项目
     */
    private List<EamMaintenanceStandardDetail> extractDailyItems(Sheet sheet, EamMaintenanceStandard standard, Map<Integer, String> rowErrors) {
        // 使用多关键词匹配
        String[] primaryHeaders = {"点检项目", "日常点检", "每日检查"};
        String[] secondaryHeaders = {"完成数据/要求", "检查标准", "要求"};
        return extractItems(sheet, standard, primaryHeaders, secondaryHeaders, "DAY_INSPECTION", rowErrors);
    }
    /**
            * 提取周保养项目
     */
    private List<EamMaintenanceStandardDetail> extractWeeklyItems(Sheet sheet, EamMaintenanceStandard standard, Map<Integer, String> rowErrors) {
        // 使用多关键词匹配
        String[] primaryHeaders = {"周保养项目", "周保养", "每周保养"};
        String[] secondaryHeaders = {"检查标准", "保养要求", "标准"};
        return extractItems(sheet, standard, primaryHeaders, secondaryHeaders, "WEEK_INSPECTION", rowErrors);
    }
    /**
     * 核心改进:优化表格区域识别和数据提取
    */
    private List<EamMaintenanceStandardDetail> extractItems(Sheet sheet,
                                                            EamMaintenanceStandard standard,
                                                            String[] primaryHeaders,
                                                            String[] secondaryHeaders,
                                                            String itemCategory,
                                                            Map<Integer, String> rowErrors) {
        // 1. 定位表格区域(精确匹配表头)
        int startRow = findHeaderRow(sheet, primaryHeaders, secondaryHeaders);
        if (startRow == -1) {
            rowErrors.put(-1, "未找到" + Arrays.toString(primaryHeaders) + "表头区域");
            return Collections.emptyList();
        }
        int endRow = findDataEnd(sheet, startRow + 1, "周保养项目");
        // 提取日志(实际使用时可以去掉)
        System.out.println("提取区域: " + (startRow + 1) + "行到" + (endRow + 1) + "行");
        // 2. 提取数据行
        List<EamMaintenanceStandardDetail> details = new ArrayList<>();
        for (int rowIdx = startRow + 1; rowIdx <= endRow; rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            if (row == null) continue;
            try {
                // 序号列处理
                Cell seqCell = row.getCell(0);
                if (seqCell == null || seqCell.getCellType() == CellType.BLANK) {
                    continue;
                }
                // 获取序号值(支持数字和文本格式)
                int seqValue = 0;
                try {
                    if (seqCell.getCellType() == CellType.NUMERIC) {
                        seqValue = (int) seqCell.getNumericCellValue();
                    } else if (seqCell.getCellType() == CellType.STRING) {
                        seqValue = Integer.parseInt(seqCell.getStringCellValue().trim());
                    }
                } catch (NumberFormatException e) {
                    rowErrors.put(rowIdx + 1, "序号格式错误");
                    continue;
                }
                // 项目名称列(第二列)
                Cell nameCell = row.getCell(1);
                if (nameCell == null || nameCell.getCellType() == CellType.BLANK) {
                    continue;
                }
                String itemName = getCellStringValue(nameCell).trim();
                // 要求/标准列(第三列)
                String demand = "";
                if (row.getLastCellNum() >= 3) {
                    Cell demandCell = row.getCell(2);
                    if (demandCell != null) {
                        demand = getCellStringValue(demandCell).trim();
                    }
                }
                // 创建详情对象
                EamMaintenanceStandardDetail detail = new EamMaintenanceStandardDetail();
                detail.setStandardId(standard.getId());
                detail.setItemCode(seqValue);
                detail.setItemName(itemName);
                detail.setItemDemand(demand);
                detail.setItemCategory(itemCategory);
                details.add(detail);
            } catch (Exception e) {
                rowErrors.put(rowIdx + 1, "解析错误: " + e.getMessage());
            }
        }
        return details;
    }
    /**
     * 获取行中所有单元格的字符串值
     */
    private List<String> getRowStringValues(Row row) {
        List<String> values = new ArrayList<>();
        if (row == null) return values;
        for (int cellIdx = 0; cellIdx < row.getLastCellNum(); cellIdx++) {
            Cell cell = row.getCell(cellIdx);
            if (cell != null) {
                values.add(getCellStringValue(cell));
            } else {
                values.add(""); // 对于空单元格添加空字符串
            }
        }
        return values;
    }
    /**
     * 获取单元格字符串值(增强公式处理)
     */
    private String getCellStringValue(Cell cell) {
        if (cell == null) return "";
        if (cell == null) {
            return "";
        }
        // 保持不变,但添加数字类型处理
        if (cell.getCellType() == CellType.NUMERIC) {
            // 整数处理
            double num = cell.getNumericCellValue();
            if (num == (int) num) {
                return String.valueOf((int) num);
            }
            return String.valueOf(num);
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue().trim();
@@ -911,31 +881,188 @@
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return handleFormulaCell(cell);
                return getFormulaCellValue(cell);
            default:
                return "";
        }
    }
    /**
        * 处理公式单元格
     * 精确查找表头行
    */
    private String handleFormulaCell(Cell cell) {
    private int findHeaderRow(Sheet sheet, String[] primaryHeaders, String[] secondaryHeaders) {
        for (int rowIdx = 0; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            if (row == null) continue;
            String rowText = getRowStringValues(row).stream().collect(Collectors.joining());
            // 检查主标题和副标题
            boolean hasPrimary = false;
            boolean hasSecondary = false;
            for (String header : primaryHeaders) {
                if (rowText.contains(header)) {
                    hasPrimary = true;
                    break;
                }
            }
            for (String header : secondaryHeaders) {
                if (rowText.contains(header)) {
                    hasSecondary = true;
                    break;
                }
            }
            if (hasPrimary && hasSecondary) {
                return rowIdx;
            }
        }
        return -1;
    }
    /**
     * 查找数据结束位置(根据您的Excel结构优化)
     */
    private int findDataEnd(Sheet sheet, int startRow, String nextSectionKeyword) {
        int consecutiveEmptyRows = 0;
        final int MAX_EMPTY_ROWS = 3;
        for (int rowIdx = startRow; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            // 关键改进1:空行处理
            if (isEssentiallyEmpty(row)) {
                consecutiveEmptyRows++;
                if (consecutiveEmptyRows >= MAX_EMPTY_ROWS) {
                    return rowIdx - consecutiveEmptyRows;
                }
                continue;
            } else {
                consecutiveEmptyRows = 0;
            }
            // 关键改进2:结束标记检测
            String rowText = getRowStringValues(row).stream().collect(Collectors.joining());
            // 根据您的Excel结构,签字行有特定格式
            if (rowText.contains("维护责任人签字")) {
                return rowIdx - 1;
            }
            // 检测下一个区域的开始(如周保养项目)
            if (StringUtils.isNotBlank(nextSectionKeyword) &&
                    rowText.contains(nextSectionKeyword)) {
                return rowIdx - 1;
            }
        }
        return sheet.getLastRowNum();
    }
    /**
     * 判断条件:前两列为空即视为空行
     */
    private boolean isEssentiallyEmpty(Row row) {
        if (row == null) return true;
        // 检查序号列
        Cell indexCell = row.getCell(0);
        if (indexCell != null && indexCell.getCellType() != CellType.BLANK) {
            return false;
        }
        // 检查项目名称列
        Cell nameCell = row.getCell(1);
        if (nameCell != null && nameCell.getCellType() != CellType.BLANK) {
            return false;
        }
        return true;
    }
    /**
            * 获取公式单元格值
     */
    private String getFormulaCellValue(Cell cell) {
        try {
            FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
            Workbook workbook = cell.getSheet().getWorkbook();
            FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
            CellValue cellValue = evaluator.evaluate(cell);
            if (cellValue == null) return "";
            if (cellValue == null) {
                return "";
            }
            switch (cellValue.getCellType()) {
                case STRING: return cellValue.getStringValue();
                case NUMERIC: return String.valueOf((int) cellValue.getNumberValue());
                case BOOLEAN: return String.valueOf(cellValue.getBooleanValue());
                default: return "";
                case STRING:
                    return cellValue.getStringValue();
                case NUMERIC:
                    return String.valueOf((int) cellValue.getNumberValue());
                case BOOLEAN:
                    return String.valueOf(cellValue.getBooleanValue());
                default:
                    return "";
            }
        } catch (Exception e) {
            log.warn("解析公式单元格失败");
            return "";
        }
    }
    private int findDataEnd(Sheet sheet, int startRow) {
        int consecutiveEmptyRows = 0;
        final int MAX_EMPTY_ROWS = 2;
        boolean foundData = false;
        for (int rowIdx = startRow; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            // 关键优化1:先检查是否为空行
            if (isEmptyRow(row)) {
                consecutiveEmptyRows++;
                if (consecutiveEmptyRows >= MAX_EMPTY_ROWS) {
                    return foundData ? rowIdx - consecutiveEmptyRows : startRow;
                }
                continue;
            } else {
                consecutiveEmptyRows = 0;
            }
            // 关键优化2:严格限定结束标记的识别条件
            if (isStrongEndMarker(row)) {
                return foundData ? rowIdx - 1 : startRow;
            }
            // 关键优化3:标记已找到有效数据
            foundData = true;
        }
        return sheet.getLastRowNum();
    }
    // 增强版结束标记识别
    private boolean isStrongEndMarker(Row row) {
        String rowText = String.join("", getRowStringValues(row)).toLowerCase();
        return
                // 精确匹配签字特征(出现在行首)
                (rowText.startsWith("维护责任人签字") ||
                        rowText.startsWith("执行人签字")) ||
                        // 精确匹配新板块标题
                        rowText.matches("^\\s*周保养项目\\s*$") ||
                        rowText.contains("年度保养项目");
    }
    // 空行检测优化(允许部分列为空)
    private boolean isEmptyRow(Row row) {
        if (row == null) return true;
        // 只检查前3列(序号列+项目名+要求)
        for (int cellIdx = 0; cellIdx < Math.min(3, row.getLastCellNum()); cellIdx++) {
            Cell cell = row.getCell(cellIdx);
            if (cell != null && cell.getCellType() != CellType.BLANK) {
                return false;
            }
        }
        return true;
    }
    /*导入点检文件Excel--------------------------结束*/
@@ -943,6 +1070,7 @@
    /**
     * 统一保养规范导入入口
     *
     * @param file 上传文件
     * @param type 保养类型 (SECOND/THIRD)
     * @return 导入结果(包含成功/失败信息)
@@ -975,15 +1103,15 @@
                    }
                    // 提取设备信息
                    standard = extractDeviceInfo(table,type);
                    standard = extractDeviceInfo(table, type);
                    if (standard == null) {
                        return Result.error(fileName + ": 设备信息提取失败");
                    }
                    // 配置类型相关参数
                    configureStandard(standard, type, file);
                    EamMaintenanceStandard exist = checkDuplicate(standard.getEquipmentId(), standard.getMaintenanceCategory(),MaintenanceStandardStatusEnum.START.name());
                    if(exist != null){
                    EamMaintenanceStandard exist = checkDuplicate(standard.getEquipmentId(), standard.getMaintenanceCategory(), MaintenanceStandardStatusEnum.START.name());
                    if (exist != null) {
                        return Result.error(fileName + ": 设备标准已存在,不能重复添加");
                    }
                    eamMaintenanceStandardMapper.insert(standard);
@@ -1022,6 +1150,18 @@
            // 4. 保存项目
            eamMaintenanceStandardDetailService.saveBatch(items);
            SysParams sysParams = sysParamsService.getSysPramBySettingKey("maintenance_import_type");
            if (sysParams != null) {
                if (sysParams.getSettingValue().equals("1")) {
                    standard.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_SUBMIT.name());
                    eamMaintenanceStandardMapper.updateById(standard);
                }
            } else {
                return Result.error("未找到保养流程导入相关配置,请联系管理员");
            }
            return Result.ok(fileName + ": 导入成功, 项目数: " + items.size());
        } catch (ImportException e) {
@@ -1033,7 +1173,7 @@
    /**
     * 后处理方法:根据不同类型进行处理
    */
     */
    private void processItemsAfterExtraction(List<EamMaintenanceStandardDetail> items, String type) {
        if ("SECOND".equals(type)) {
            // 二级保养: 删除没有序号的数据
@@ -1044,18 +1184,21 @@
            if (!items.isEmpty()) {
                items.remove(0);
            }
            // 2. 为缺失部位的数据填充前一条的保养部位
            String lastPart = "";
            int i = 1;
            for (EamMaintenanceStandardDetail item : items) {
                item.setItemCode(i);
                if (item.getItemPart() != null && !item.getItemPart().isEmpty()) {
                    lastPart = item.getItemPart();
                } else if (!lastPart.isEmpty()) {
                    item.setItemPart(lastPart);
                }
                i++;
            }
        }
    }
    /**
     * 提取二级保养项目(区分第一页和后续页面)
     */
@@ -1094,7 +1237,7 @@
                if (row.getTableCells().size() > 1) {
                    String seqText = getCellText(row.getCell(1));
                    try {
                        if (!seqText.equals("序号")){
                        if (!seqText.equals("序号")) {
                            item.setItemCode(Integer.parseInt(seqText.trim()));
                        }
                    } catch (NumberFormatException e) {
@@ -1141,7 +1284,6 @@
            // 创建保养项目
            EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail();
            item.setItemCategory("THIRD_MAINTENANCE");
            item.setStandardId(standardId);
            int colCount = row.getTableCells().size();
@@ -1221,7 +1363,7 @@
    /**
     * 文档类型校验 - 防止二保传入三保或反之
    */
     */
    private boolean isWrongDocumentType(XWPFTable table, String requestedType) {
        boolean hasRepairTitle = false;
        boolean hasOperatorTitle = false;
@@ -1268,7 +1410,7 @@
    /**
     * 提取设备基本信息
     */
    private EamMaintenanceStandard extractDeviceInfo(XWPFTable table,String type) {
    private EamMaintenanceStandard extractDeviceInfo(XWPFTable table, String type) {
        if (table.getNumberOfRows() < 2) return null;
        // 提取前两行数据
@@ -1287,11 +1429,11 @@
            EamEquipment equipments = eamEquipmentService.selectByEquipmentCode(standard.getEquipmentCode());
            if (equipments == null) {
                return null;
            }else {
            } else {
                standard.setEquipmentId(equipments.getId());
            }
            if (type.equals("THIRD")){
                EamEquipmentExtend eamEquipmentExtend=eamEquipmentExtendService.getById(standard.getEquipmentId());
            if (type.equals("THIRD")) {
                EamEquipmentExtend eamEquipmentExtend = eamEquipmentExtendService.getById(standard.getEquipmentId());
                standard.setMaintenancePeriod(eamEquipmentExtend.getThirdMaintenancePeriod());
            }
        }