自学内容网 自学内容网

一种主子表的update方法

更新

方法

这段代码展示了一个较为复杂的更新方法,主要用于更新一个主表 `OnsiteExamination` 及其关联的子表。整体流程可以分为以下几个步骤:

1. 获取更新参数
   - `Long id = updateParam.getId();`
   - `Long tenantId = updateParam.getTenantId();`
     
     这两个变量分别对应主表记录的 ID 和租户 ID。

 2. 将更新参数转换为数据对象
   - `OnsiteExaminationDO onsiteExaminationDO = OnsiteExaminationConverter.updateParamToDO(updateParam);`
     
     这里将传入的更新参数 `updateParam` 转换为数据对象 `OnsiteExaminationDO`,方便后续操作。

 3. 查询旧数据
   - `List<ExaminationTeamModel> examinationTeamModels = examinationTeamService.listByExaminationId(id, tenantId);`
   - `List<ExaminationProjectModel> examinationProjectModels = examinationProjectService.listByExaminationId(id, tenantId);`
   - `List<ExaminationFileModel> examinationFileModels = examinationFileService.listByExaminationId(id, tenantId);`
     
     分别查询出与主表关联的子表(`ExaminationTeamModel`, `ExaminationProjectModel`, `ExaminationFileModel`)的旧数据。这些数据将在后续步骤中用于比较,以确定哪些数据需要删除或更新。

4. 获取新数据并转换为 Map
   - `List<ExaminationTeamUpdateParam> examinationTeamUpdateParams = updateParam.getExaminationTeams();`
   - `Map<Long, ExaminationTeamUpdateParam> examinationTeamUpdateParamMap = CollectionHelper.collectMap(examinationTeamUpdateParams, ExaminationTeamUpdateParam::getId);`

   - `List<ExaminationProjectUpdateParam> examinationProjectUpdateParams = updateParam.getExaminationProjects();`
   - `Map<Long, ExaminationProjectUpdateParam> examinationProjectUpdateParamMap = CollectionHelper.collectMap(examinationProjectUpdateParams, ExaminationProjectUpdateParam::getId);`

   - `List<ExaminationFileUpdateParam> examinationFileUpdateParams = updateParam.getExaminationFiles();`
   - `Map<Long, ExaminationFileUpdateParam> examinationFileUpdateParamMap = CollectionHelper.collectMap(examinationFileUpdateParams, ExaminationFileUpdateParam::getId);`
     
     这里将传入的子表更新参数列表转换为 Map,Map 的 Key 是子表记录的 ID,Value 是对应的更新参数。这种结构便于后续进行数据的对比和更新。

5. 比较旧数据与新数据,确定需要删除的记录
   - `List<Long> examinationTeamDeleteIds = OnsiteExaminationConverter.getTeamDeleteIds(examinationTeamModels, examinationTeamUpdateParamMap);`
   - `List<Long> examinationProjectDeleteIds = OnsiteExaminationConverter.getProjectDeleteIds(examinationProjectModels, examinationProjectUpdateParamMap);`
   - `List<Long> examinationFileDeleteIds = OnsiteExaminationConverter.getFileDeleteIds(examinationFileModels, examinationFileUpdateParamMap);`
     
     通过比较旧数据和新数据的 Map,确定哪些子表记录需要从数据库中删除。`getTeamDeleteIds`, `getProjectDeleteIds`, `getFileDeleteIds` 这些方法可能实现了一种逻辑,即如果新数据中不包含某个旧数据的 ID,则认为该旧数据需要删除。

6. 记录操作日志
   - `OperationLogSaveParam operationLogSaveParam = OnsiteExaminationConverter.toOperationLogSaveParam(onsiteExaminationDO, LogOperateTypeEnum.UPDATE);`
   - `operationLogService.save(operationLogSaveParam);`
     
     记录本次操作的日志,通常用于审计或追踪目的。`LogOperateTypeEnum.UPDATE` 表示这是一次更新操作。

 7. 执行更新操作
   - `onsiteExaminationRepository.update(onsiteExaminationDO, examinationTeamUpdateParams, examinationProjectUpdateParams, examinationFileUpdateParams, examinationTeamDeleteIds, examinationProjectDeleteIds, examinationFileDeleteIds);`
     
     最终调用 `onsiteExaminationRepository` 的 `update` 方法,执行数据库的更新操作。该方法会根据传入的主表数据对象、子表更新参数、以及需要删除的子表记录 ID 列表,来完成数据库的更新操作。

 总结
这个方法的核心思想是**先查询旧数据**,然后**比较旧数据与新数据**,确定哪些数据需要更新,哪些数据需要删除,最后**批量执行数据库操作**。这种方法在处理包含多个关联子表的复杂更新操作时非常有效,可以确保数据的一致性和完整性。

@Override
    public void update(OnsiteExaminationUpdateParam updateParam) {
        Long id = updateParam.getId();
        Long tenantId = updateParam.getTenantId();
        OnsiteExaminationDO onsiteExaminationDO = OnsiteExaminationConverter.updateParamToDO(updateParam);
        // 查出附属表旧数据
        List<ExaminationTeamModel> examinationTeamModels = examinationTeamService.listByExaminationId(id, tenantId);
        List<ExaminationProjectModel> examinationProjectModels = examinationProjectService.listByExaminationId(id, tenantId);
        List<ExaminationFileModel> examinationFileModels = examinationFileService.listByExaminationId(id, tenantId);

        // 获取附属表新数据
        List<ExaminationTeamUpdateParam> examinationTeamUpdateParams = updateParam.getExaminationTeams();
        Map<Long, ExaminationTeamUpdateParam> examinationTeamUpdateParamMap = CollectionHelper.collectMap(examinationTeamUpdateParams, ExaminationTeamUpdateParam::getId);

        List<ExaminationProjectUpdateParam> examinationProjectUpdateParams = updateParam.getExaminationProjects();
        Map<Long, ExaminationProjectUpdateParam> examinationProjectUpdateParamMap = CollectionHelper.collectMap(examinationProjectUpdateParams, ExaminationProjectUpdateParam::getId);

        List<ExaminationFileUpdateParam> examinationFileUpdateParams = updateParam.getExaminationFiles();
        Map<Long, ExaminationFileUpdateParam> examinationFileUpdateParamMap = CollectionHelper.collectMap(examinationFileUpdateParams, ExaminationFileUpdateParam::getId);

        // 比较得出要删除的id
        List<Long> examinationTeamDeleteIds = OnsiteExaminationConverter.getTeamDeleteIds(examinationTeamModels, examinationTeamUpdateParamMap);
        List<Long> examinationProjectDeleteIds = OnsiteExaminationConverter.getProjectDeleteIds(examinationProjectModels, examinationProjectUpdateParamMap);
        List<Long> examinationFileDeleteIds = OnsiteExaminationConverter.getFileDeleteIds(examinationFileModels, examinationFileUpdateParamMap);

        OperationLogSaveParam operationLogSaveParam = OnsiteExaminationConverter.toOperationLogSaveParam(onsiteExaminationDO, LogOperateTypeEnum.UPDATE);
        operationLogService.save(operationLogSaveParam);

        onsiteExaminationRepository.update(onsiteExaminationDO, examinationTeamUpdateParams, examinationProjectUpdateParams,examinationFileUpdateParams, examinationTeamDeleteIds, examinationProjectDeleteIds,examinationFileDeleteIds);
    }

比较出要删除ids的方法如下

   public static List<Long> getTeamDeleteIds(List<ExaminationTeamModel> examinationTeamModels, Map<Long, ExaminationTeamUpdateParam> examinationTeamUpdateParamMap) {
        // 对比出删除的现场考察数据
        return CollectionHelper.collectNotNullList(examinationTeamModels, param -> {
            Long teamId = param.getId();
            ExaminationTeamUpdateParam examinationTeamUpdateParam = MapHelper.getValue(examinationTeamUpdateParamMap, teamId);
            if (examinationTeamUpdateParam == null) {
                return teamId;
            }
            return null;
        });
    }

仓储层update逻辑

  @HussarTransactional
    public void update(OnsiteExaminationDO onsiteExaminationDO, List<ExaminationTeamUpdateParam> examinationTeamUpdateParams, List<ExaminationProjectUpdateParam> examinationProjectUpdateParams,List<ExaminationFileUpdateParam> examinationFileUpdateParams, List<Long> examinationTeamDeleteIds, List<Long> examinationProjectDeleteIds, List<Long> examinationFileDeleteIds) {
        Long examinationId = onsiteExaminationDO.getId();
        Long tenantId = onsiteExaminationDO.getTenantId();
        onsiteExaminationMapper.updateById(onsiteExaminationDO);
        examinationTeamUpdateParams = CollectionHelper.collectNotNullList(examinationTeamUpdateParams, param -> {
            param.setExaminationId(examinationId);
            param.setTenantId(tenantId);
            return param;
        });
        if (CollectionUtils.isNotEmpty(examinationTeamUpdateParams)) {
            examinationTeamService.saveOrUpdateBatch(examinationTeamUpdateParams);
        }
        if (CollectionUtils.isNotEmpty(examinationTeamDeleteIds)) {
            examinationTeamService.deleteBatchByIds(examinationTeamDeleteIds, tenantId);
        }


        examinationProjectUpdateParams = CollectionHelper.collectNotNullList(examinationProjectUpdateParams, param -> {
            param.setExaminationId(examinationId);
            param.setTenantId(tenantId);
            return param;
        });
        if (CollectionUtils.isNotEmpty(examinationProjectUpdateParams)) {
            examinationProjectService.saveOrUpdateBatch(examinationProjectUpdateParams);
        }
        if (CollectionUtils.isNotEmpty(examinationProjectDeleteIds)) {
            examinationProjectService.deleteBatchByIds(examinationProjectDeleteIds, tenantId);
        }

        examinationFileUpdateParams = CollectionHelper.collectNotNullList(examinationFileUpdateParams, param -> {
            param.setExaminationId(examinationId);
            param.setTenantId(tenantId);
            return param;
        });
        if (CollectionUtils.isNotEmpty(examinationFileUpdateParams)) {
            examinationFileService.saveOrUpdateBatch(examinationFileUpdateParams);
        }
        if (CollectionUtils.isNotEmpty(examinationFileDeleteIds)) {
            examinationFileService.deleteBatchByIds(examinationFileDeleteIds, tenantId);
        }
    }


原文地址:https://blog.csdn.net/u014755700/article/details/143750358

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!