fix: 优化逻辑

This commit is contained in:
tangwei 2026-07-07 16:15:49 +08:00
parent d9e15f9736
commit 645b97435c
2 changed files with 158 additions and 33 deletions

View File

@ -60,6 +60,8 @@ import java.util.*;
@Slf4j
public class FishDraftDataController {
private static final int IN_QUERY_BATCH_SIZE = 900;
@Resource
private IFishDraftDataService fishDraftDataService;
@ -368,11 +370,10 @@ public class FishDraftDataController {
@PostMapping("/batchApproveByApprovalId")
@Operation(summary = "根据审批批次全部审批通过")
public ResponseResult batchApproveByApprovalId(@RequestBody BatchApproveRequest request) {
if (request.getApprovalIds().isEmpty()) {
if (request.getApprovalIds() == null || request.getApprovalIds().isEmpty()) {
return ResponseResult.error("请选择审批批次");
}
List<FishDraftData> draft = fishDraftDataService.list(new LambdaQueryWrapper<FishDraftData>().eq(FishDraftData::getDeletedFlag, 0).in(FishDraftData::getApprovalId, request.getApprovalIds()).eq(FishDraftData::getStatus, "PENDING").select(FishDraftData::getId));
List<String> ids = draft.stream().map(FishDraftData::getId).toList();
List<String> ids = listDraftIdsByApprovalIdsAndStatus(request.getApprovalIds(), "PENDING");
boolean result = fishDraftDataService.batchApprove(ids, request.getApproveComment());
return result ? ResponseResult.success("审批通过") : ResponseResult.error("审批失败");
}
@ -388,8 +389,7 @@ public class FishDraftDataController {
@Operation(summary = "批量驳回")
public ResponseResult batchRejectFull(@RequestBody BatchRejectRequest request) {
if (request.getApprovalIds() != null && !request.getApprovalIds().isEmpty()) {
List<FishDraftData> draft = fishDraftDataService.list(new LambdaQueryWrapper<FishDraftData>().eq(FishDraftData::getDeletedFlag, 0).in(FishDraftData::getApprovalId, request.getApprovalIds()).eq(FishDraftData::getStatus, "PENDING").select(FishDraftData::getId));
List<String> ids = draft.stream().map(FishDraftData::getId).toList();
List<String> ids = listDraftIdsByApprovalIdsAndStatus(request.getApprovalIds(), "PENDING");
boolean result = fishDraftDataService.rejectBatch(ids, request.getRejectReason());
return result ? ResponseResult.success("驳回成功") : ResponseResult.error("驳回失败");
}
@ -429,6 +429,47 @@ public class FishDraftDataController {
return result ? ResponseResult.success("修改成功") : ResponseResult.error("修改失败");
}
private List<String> listDraftIdsByApprovalIdsAndStatus(List<String> approvalIds, String status) {
List<String> normalizedApprovalIds = normalizeBatchValues(approvalIds);
if (normalizedApprovalIds.isEmpty()) {
return Collections.emptyList();
}
List<String> ids = new ArrayList<>();
for (List<String> batchApprovalIds : splitList(normalizedApprovalIds, IN_QUERY_BATCH_SIZE)) {
List<FishDraftData> draft = fishDraftDataService.list(new LambdaQueryWrapper<FishDraftData>()
.eq(FishDraftData::getDeletedFlag, 0)
.in(FishDraftData::getApprovalId, batchApprovalIds)
.eq(FishDraftData::getStatus, status)
.select(FishDraftData::getId));
ids.addAll(draft.stream().map(FishDraftData::getId).filter(StrUtil::isNotBlank).toList());
}
return ids;
}
private List<String> normalizeBatchValues(List<String> values) {
if (values == null || values.isEmpty()) {
return Collections.emptyList();
}
LinkedHashSet<String> normalized = new LinkedHashSet<>();
for (String value : values) {
if (StrUtil.isNotBlank(value)) {
normalized.add(value.trim());
}
}
return new ArrayList<>(normalized);
}
private <T> List<List<T>> splitList(List<T> source, int batchSize) {
if (source == null || source.isEmpty()) {
return Collections.emptyList();
}
List<List<T>> result = new ArrayList<>();
for (int i = 0; i < source.size(); i += batchSize) {
result.add(source.subList(i, Math.min(source.size(), i + batchSize)));
}
return result;
}
@PostMapping("/delete")
@Operation(summary = "删除过鱼数据")
public ResponseResult delete(@RequestParam String id) {

View File

@ -2,7 +2,6 @@ package com.yfd.platform.qgc_data.service.impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfd.platform.common.DataSourceLoadOptionsBase;
@ -48,6 +47,8 @@ import java.util.*;
@Slf4j
public class FishDraftDataServiceImpl extends ServiceImpl<FishDraftDataMapper, FishDraftData> implements IFishDraftDataService {
private static final int IN_QUERY_BATCH_SIZE = 200;
@Resource
private FishDraftDataMapper fishDraftDataMapper;
@ -254,16 +255,15 @@ public class FishDraftDataServiceImpl extends ServiceImpl<FishDraftDataMapper, F
@Override
public boolean batchRemoveDraft(List<String> ids) {
long count = this.count(new LambdaQueryWrapper<FishDraftData>().in(FishDraftData::getId, ids).eq(FishDraftData::getLockFlag, 1));
long count = countByIdsAndLockFlag(ids, 1);
if(count > 0){
return false;
}
LambdaUpdateWrapper<FishDraftData> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.in(FishDraftData::getId, ids);
updateWrapper.set(FishDraftData::getDeletedFlag, 1);
String userId = SecurityUtils.getUserId();
updateWrapper.set(FishDraftData::getDeletedBy,userId );
return this.update(updateWrapper);
return updateByIdsInBatches(ids, entity -> {
entity.setDeletedFlag(1);
entity.setDeletedBy(userId);
});
}
@Override
@ -313,13 +313,13 @@ public class FishDraftDataServiceImpl extends ServiceImpl<FishDraftDataMapper, F
setUserDataScope(approvalMain);
approvalMainService.save(approvalMain);
LambdaUpdateWrapper<FishDraftData> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.in(FishDraftData::getId, ids);
updateWrapper.eq(FishDraftData::getLockFlag, 0);
updateWrapper.set(FishDraftData::getApprovalId, approvalMain.getId());
updateWrapper.set(FishDraftData::getStatus, "PENDING");
updateWrapper.set(FishDraftData::getSubmitTime, new Date());
this.update(updateWrapper);
List<String> unlockedIds = listIdsByLockFlag(ids, 0);
Date submitTime = new Date();
updateByIdsInBatches(unlockedIds, entity -> {
entity.setApprovalId(approvalMain.getId());
entity.setStatus("PENDING");
entity.setSubmitTime(submitTime);
});
approvalLogService.logSubmit(approvalMain.getId(), operatorId, "批量提交草稿,共" + ids.size() + "");
@ -421,7 +421,7 @@ public class FishDraftDataServiceImpl extends ServiceImpl<FishDraftDataMapper, F
Date now = new Date();
String operatorId = SecurityUtils.getUserId();
List<FishDraftData> dataList = this.listByIds(ids);
List<FishDraftData> dataList = listByIdsInBatches(ids);
Set<String> processedApprovalIds = new HashSet<>();
List<String> validIds = new ArrayList<>();
List<SdFpssR> fpssRList = new ArrayList<>();
@ -438,12 +438,11 @@ public class FishDraftDataServiceImpl extends ServiceImpl<FishDraftDataMapper, F
}
if (!validIds.isEmpty()) {
LambdaUpdateWrapper<FishDraftData> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.in(FishDraftData::getId, validIds);
updateWrapper.set(FishDraftData::getStatus, "APPROVED");
updateWrapper.set(FishDraftData::getApproveTime, now);
updateWrapper.set(FishDraftData::getUpdatedBy, operatorId);
this.update(updateWrapper);
updateByIdsInBatches(validIds, entity -> {
entity.setStatus("APPROVED");
entity.setApproveTime(now);
entity.setUpdatedBy(operatorId);
});
boolean b = sdFpssRService.saveOrUpdateBatch(fpssRList);
log.info("=======================插入数据条数======================="+fpssRList.size());
for (String approvalId : processedApprovalIds) {
@ -548,7 +547,7 @@ public class FishDraftDataServiceImpl extends ServiceImpl<FishDraftDataMapper, F
Date now = new Date();
String operatorId = SecurityUtils.getUserId();
List<FishDraftData> dataList = this.listByIds(ids);
List<FishDraftData> dataList = listByIdsInBatches(ids);
Set<String> processedApprovalIds = new HashSet<>();
List<String> validIds = new ArrayList<>();
@ -562,12 +561,11 @@ public class FishDraftDataServiceImpl extends ServiceImpl<FishDraftDataMapper, F
}
if (!validIds.isEmpty()) {
LambdaUpdateWrapper<FishDraftData> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.in(FishDraftData::getId, validIds);
updateWrapper.set(FishDraftData::getStatus, "REJECTED");
updateWrapper.set(FishDraftData::getApproveTime, now);
updateWrapper.set(FishDraftData::getUpdatedBy, operatorId);
this.update(updateWrapper);
updateByIdsInBatches(validIds, entity -> {
entity.setStatus("REJECTED");
entity.setApproveTime(now);
entity.setUpdatedBy(operatorId);
});
for (String approvalId : processedApprovalIds) {
approvalLogService.logReject(approvalId, operatorId, rejectReason);
@ -679,4 +677,90 @@ public class FishDraftDataServiceImpl extends ServiceImpl<FishDraftDataMapper, F
log.info("批量保存草稿完成, 共{}条, 耗时{}ms", fishDraftDataList.size(), System.currentTimeMillis() - start);
return result;
}
private List<FishDraftData> listByIdsInBatches(List<String> ids) {
List<String> normalizedIds = normalizeBatchValues(ids);
if (normalizedIds.isEmpty()) {
return new ArrayList<>();
}
List<FishDraftData> result = new ArrayList<>();
for (List<String> batchIds : splitList(normalizedIds, IN_QUERY_BATCH_SIZE)) {
result.addAll(this.list(new LambdaQueryWrapper<FishDraftData>().in(FishDraftData::getId, batchIds)));
}
return result;
}
private long countByIdsAndLockFlag(List<String> ids, Integer lockFlag) {
List<String> normalizedIds = normalizeBatchValues(ids);
if (normalizedIds.isEmpty()) {
return 0L;
}
long total = 0L;
for (List<String> batchIds : splitList(normalizedIds, IN_QUERY_BATCH_SIZE)) {
total += this.count(new LambdaQueryWrapper<FishDraftData>()
.in(FishDraftData::getId, batchIds)
.eq(FishDraftData::getLockFlag, lockFlag));
}
return total;
}
private List<String> listIdsByLockFlag(List<String> ids, Integer lockFlag) {
List<String> normalizedIds = normalizeBatchValues(ids);
if (normalizedIds.isEmpty()) {
return Collections.emptyList();
}
List<String> result = new ArrayList<>();
for (List<String> batchIds : splitList(normalizedIds, IN_QUERY_BATCH_SIZE)) {
List<FishDraftData> records = this.list(new LambdaQueryWrapper<FishDraftData>()
.select(FishDraftData::getId)
.in(FishDraftData::getId, batchIds)
.eq(FishDraftData::getLockFlag, lockFlag));
result.addAll(records.stream().map(FishDraftData::getId).filter(StrUtil::isNotBlank).toList());
}
return result;
}
private boolean updateByIdsInBatches(List<String> ids,
java.util.function.Consumer<FishDraftData> entityCustomizer) {
List<String> normalizedIds = normalizeBatchValues(ids);
if (normalizedIds.isEmpty()) {
return false;
}
boolean updated = false;
for (List<String> batchIds : splitList(normalizedIds, IN_QUERY_BATCH_SIZE)) {
List<FishDraftData> batchEntities = new ArrayList<>(batchIds.size());
for (String batchId : batchIds) {
FishDraftData entity = new FishDraftData();
entity.setId(batchId);
entityCustomizer.accept(entity);
batchEntities.add(entity);
}
updated = this.updateBatchById(batchEntities, IN_QUERY_BATCH_SIZE) || updated;
}
return updated;
}
private List<String> normalizeBatchValues(List<String> values) {
if (values == null || values.isEmpty()) {
return Collections.emptyList();
}
LinkedHashSet<String> normalized = new LinkedHashSet<>();
for (String value : values) {
if (StrUtil.isNotBlank(value)) {
normalized.add(value.trim());
}
}
return new ArrayList<>(normalized);
}
private <T> List<List<T>> splitList(List<T> source, int batchSize) {
if (source == null || source.isEmpty()) {
return Collections.emptyList();
}
List<List<T>> result = new ArrayList<>();
for (int i = 0; i < source.size(); i += batchSize) {
result.add(source.subList(i, Math.min(source.size(), i + batchSize)));
}
return result;
}
}