feat:修复细节问题

This commit is contained in:
lulz1 2024-07-04 15:29:04 +08:00
parent 9ef85d82e7
commit d45e270c00
12 changed files with 148 additions and 21 deletions

View File

@ -134,7 +134,7 @@ public class OrderApplicationService {
.reloadStatus(); .reloadStatus();
Leg oldLeg = leg.deepClone(); Leg oldLeg = leg.deepClone();
oldLeg.setRouteId(null);
// leg 进行更新 // leg 进行更新
leg.update(legData); leg.update(legData);
@ -149,7 +149,7 @@ public class OrderApplicationService {
//这里加入一个待审批事件, 变更加入旧行程数据 用于回滚还原 //这里加入一个待审批事件, 变更加入旧行程数据 用于回滚还原
leg.addEvent(LegApprovalEvent.prepare(oldLeg)); leg.addEvent(LegApprovalEvent.prepare(oldLeg));
} }
if (!routeOrder.getOrderStatus().equals(ORDER_STATUS_PREPARE) && !approveOrderNo.getSysCode().equals("ANFSSC")){ if (!routeOrder.getOrderStatus().equals(ORDER_STATUS_PREPARE) && !approveOrderNo.getSysCode().equals("ANFSSC")) {
sendLegChangeCommand(oldLeg, leg, sendLegChangeCommand(oldLeg, leg,
routeOrder, routeOrder,
addLegData.getLegData().getChangeReason(), addLegData.getLegData().getChangeReason(),

View File

@ -19,6 +19,11 @@ public class ApprovalLegAdd extends ApprovalProcess {
return this; return this;
} }
@Override
protected ApprovalData recoverApproveData(RouteOrder routeOrder, ApprovalRecord approvalRecord) {
return null;
}
@Override @Override
public ApprovalProcess approveData(RouteOrder routeOrder, OrderDetail orderDetail) { public ApprovalProcess approveData(RouteOrder routeOrder, OrderDetail orderDetail) {
return null; return null;

View File

@ -10,6 +10,7 @@ import org.springframework.data.util.Pair;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.chint.domain.aggregates.approval.ApprovalType.LEG_ADD_CHANGE_BATCH; import static com.chint.domain.aggregates.approval.ApprovalType.LEG_ADD_CHANGE_BATCH;
@ -23,27 +24,79 @@ public class ApprovalLegAddAndChangeBatch extends ApprovalProcess {
this.approvalType = LEG_ADD_CHANGE_BATCH; this.approvalType = LEG_ADD_CHANGE_BATCH;
} }
@Override
protected ApprovalData recoverApproveData(RouteOrder routeOrder, ApprovalRecord approvalRecord) {
ApprovalData approvalData = new ApprovalData();
Map<Boolean, List<Leg>> partitionedLegs = partitionLegsByExtension(
routeOrder.getLegItems(),
LEG_APPROVAL_STATUS_SUBMIT,
approvalRecord.getApprovalRecordNo()
);
List<Leg> changeLegs = partitionedLegs.get(true);
List<Leg> addLegs = partitionedLegs.get(false);
List<Pair<Leg, Leg>> pairList = createLegPairs(changeLegs);
approvalData.setRouteOrder(routeOrder);
approvalData.setAddLegList(addLegs);
approvalData.setChangeLegList(pairList);
return approvalData;
}
@Override @Override
public ApprovalProcess approveData(RouteOrder routeOrder) { public ApprovalProcess approveData(RouteOrder routeOrder) {
Map<Boolean, List<Leg>> legMap = routeOrder.getLegItems() Map<Boolean, List<Leg>> legMap = routeOrder.getLegItems()
.stream() .stream()
.filter(it -> it.getLegApprovalStatus().equals(LEG_APPROVAL_STATUS_PREPARE)) .filter(it -> it.getLegApprovalStatus().equals(LEG_APPROVAL_STATUS_PREPARE))
.collect(Collectors.partitioningBy(it -> it.getLastEvent().getExtension() == null .collect(Collectors.partitioningBy(this::isExtensionNullOrEmpty));
|| it.getLastEvent().getExtension().isEmpty()));
ApprovalData approvalData = new ApprovalData(); ApprovalData approvalData = new ApprovalData();
approvalData.setRouteOrder(routeOrder); approvalData.setRouteOrder(routeOrder);
approvalData.setAddLegList(legMap.get(true)); approvalData.setAddLegList(legMap.get(true));
List<Pair<Leg, Leg>> pairList = legMap.get(false).stream() List<Pair<Leg, Leg>> pairList = createLegPairs(legMap.get(false));
.map(leg -> {
String extension = leg.getLastEvent().getExtension();
Leg oldLeg = leg.deepClone().restoreFromLegString(extension);
return Pair.of(leg, oldLeg);
}).toList();
approvalData.setChangeLegList(pairList); approvalData.setChangeLegList(pairList);
this.approvalData = approvalData; this.approvalData = approvalData;
return this; return this;
} }
private Map<Boolean, List<Leg>> partitionLegsByExtension(List<Leg> legs, Integer approvalStatus, String approvalRecordNo) {
return legs.stream()
.peek(Leg::reloadStatus)
.filter(leg -> isLegApprovedAndMatchesRecord(leg, approvalStatus, approvalRecordNo))
.collect(Collectors.partitioningBy(this::isExtensionNotNullOrEmpty));
}
private boolean isLegApprovedAndMatchesRecord(Leg leg, Integer approvalStatus, String approvalRecordNo) {
return leg.getLegApprovalStatus().equals(approvalStatus) &&
leg.getLastApprovalSubmitEvent().isPresent() &&
leg.getLastApprovalSubmitEvent().get().getExtension().equals(approvalRecordNo) &&
leg.getLastApprovalPrepareEvent().isPresent();
}
private boolean isExtensionNotNullOrEmpty(Leg leg) {
Optional<LegApprovalEvent> lastApprovalPrepareEvent = leg.getLastApprovalPrepareEvent();
if(lastApprovalPrepareEvent.isEmpty()){
return false;
}
String extension = lastApprovalPrepareEvent.get().getExtension();
return extension != null && !extension.isEmpty();
}
private boolean isExtensionNullOrEmpty(Leg leg) {
String extension = leg.getLastEvent().getExtension();
return extension == null || extension.isEmpty();
}
private List<Pair<Leg, Leg>> createLegPairs(List<Leg> legs) {
return legs.stream()
.filter(leg -> leg.getLastApprovalPrepareEvent().isPresent())
.map(leg -> {
String extension = leg.getLastApprovalPrepareEvent().get().getExtension();
Leg oldLeg = leg.deepClone().restoreFromLegString(extension);
return Pair.of(leg, oldLeg);
})
.toList();
}
@Override @Override
public boolean approve() { public boolean approve() {
@ -78,7 +131,7 @@ public class ApprovalLegAddAndChangeBatch extends ApprovalProcess {
return false; return false;
} }
BaseEvent lastEvent = leg.getLastEvent(); BaseEvent lastEvent = leg.getLastEvent();
if(lastEvent == null){ if (lastEvent == null) {
return false; return false;
} }
return lastEvent.getEventType().equals(LEG_APPROVAL_STATUS_SUBMIT) && return lastEvent.getEventType().equals(LEG_APPROVAL_STATUS_SUBMIT) &&

View File

@ -14,6 +14,11 @@ public class ApprovalLegChange extends ApprovalProcess{
this.approvalType = LEG_CHANGE; this.approvalType = LEG_CHANGE;
} }
@Override
protected ApprovalData recoverApproveData(RouteOrder routeOrder, ApprovalRecord approvalRecord) {
return null;
}
@Override @Override
public ApprovalProcess approveData(RouteOrder routeOrder) { public ApprovalProcess approveData(RouteOrder routeOrder) {
return this; return this;

View File

@ -13,6 +13,11 @@ public class ApprovalOrderChange extends ApprovalProcess{
this.approvalType = LEG_CHANGE; this.approvalType = LEG_CHANGE;
} }
@Override
protected ApprovalData recoverApproveData(RouteOrder routeOrder, ApprovalRecord approvalRecord) {
return null;
}
@Override @Override
public ApprovalProcess approveData(RouteOrder routeOrder) { public ApprovalProcess approveData(RouteOrder routeOrder) {
return this; return this;

View File

@ -28,6 +28,11 @@ public class ApprovalOrderExpense extends ApprovalProcess{
} }
@Override
protected ApprovalData recoverApproveData(RouteOrder routeOrder, ApprovalRecord approvalRecord) {
return null;
}
@Override @Override
public ApprovalProcess doSubmitToBpm() { public ApprovalProcess doSubmitToBpm() {
bpmPlatform.submitOrderExpenseApproval(approvalData); bpmPlatform.submitOrderExpenseApproval(approvalData);

View File

@ -13,6 +13,11 @@ public class ApprovalOrderRefund extends ApprovalProcess {
this.approvalType = ORDER_REFUND; this.approvalType = ORDER_REFUND;
} }
@Override
protected ApprovalData recoverApproveData(RouteOrder routeOrder, ApprovalRecord approvalRecord) {
return null;
}
@Override @Override
public ApprovalProcess approveData(RouteOrder routeOrder) { public ApprovalProcess approveData(RouteOrder routeOrder) {
return this; return this;

View File

@ -34,6 +34,19 @@ public abstract class ApprovalProcess {
return this; return this;
} }
public ApprovalProcess approveData(RouteOrder routeOrder, ApprovalRecord approvalRecord) {
validateApprovalStatus(approvalRecord);
this.approvalData = recoverApproveData(routeOrder, approvalRecord);
return this;
}
private void validateApprovalStatus(ApprovalRecord approvalRecord) {
if (!approvalRecord.getApprovalStatus().equals(ApprovalRecord.prepareApprovalStatus())) {
throw new CommandException("该审批单已经审批完毕,无法再次发起审批。");
}
}
protected abstract ApprovalData recoverApproveData(RouteOrder routeOrder, ApprovalRecord approvalRecord);
public ApprovalProcess approveData(RouteOrder routeOrder, OrderDetail orderDetail) { public ApprovalProcess approveData(RouteOrder routeOrder, OrderDetail orderDetail) {
ApprovalData approvalData = new ApprovalData(); ApprovalData approvalData = new ApprovalData();

View File

@ -455,4 +455,21 @@ public class Leg implements Serializable, EventManageable {
.filter(legEvent -> legEvent.getEventType().equals(LEG_EVENT_CHANGE)) .filter(legEvent -> legEvent.getEventType().equals(LEG_EVENT_CHANGE))
.max(Comparator.comparing(LegEvent::getHappenTime)); .max(Comparator.comparing(LegEvent::getHappenTime));
} }
public Optional<LegApprovalEvent> getLastApprovalSubmitEvent() {
return getLastApprovalEventByType(LEG_APPROVAL_STATUS_SUBMIT);
}
public Optional<LegApprovalEvent> getLastApprovalPrepareEvent() {
return getLastApprovalEventByType(LEG_APPROVAL_STATUS_PREPARE);
}
private Optional<LegApprovalEvent> getLastApprovalEventByType(Integer eventType) {
if (this.legApprovalEventList == null) {
return Optional.empty();
}
return this.legApprovalEventList.stream()
.filter(legEvent -> legEvent.getEventType().equals(eventType))
.max(Comparator.comparing(LegApprovalEvent::getHappenTime));
}
} }

View File

@ -8,10 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.*;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
@Repository @Repository
@ -34,7 +31,10 @@ public class PermissionConfigRepositoryImpl implements PermissionConfigRepositor
@Override @Override
public List<PermissionConfig> findByPermissionNameIn(Set<String> approvalTypes) { public List<PermissionConfig> findByPermissionNameIn(Set<String> approvalTypes) {
List<PermissionConfig> permissionConfigs = cachePermissionConfigRepository List<PermissionConfig> permissionConfigs = cachePermissionConfigRepository
.cachePermissionConfigBatch(approvalTypes); .cachePermissionConfigBatch(approvalTypes)
.stream()
.filter(Objects::nonNull)
.toList();
List<String> cachedNames = permissionConfigs.stream() List<String> cachedNames = permissionConfigs.stream()
.filter(Objects::nonNull) .filter(Objects::nonNull)
.map(PermissionConfig::getPermissionName) .map(PermissionConfig::getPermissionName)
@ -45,7 +45,10 @@ public class PermissionConfigRepositoryImpl implements PermissionConfigRepositor
.toList(); .toList();
if (!missingNames.isEmpty()) { if (!missingNames.isEmpty()) {
List<PermissionConfig> fetchedPermissionConfigs = jdbcPermissionConfigRepository List<PermissionConfig> fetchedPermissionConfigs = jdbcPermissionConfigRepository
.findByPermissionNameIn(missingNames); .findByPermissionNameIn(missingNames)
.stream()
.filter(Objects::nonNull).toList();
permissionConfigs = new ArrayList<>(permissionConfigs);
permissionConfigs.addAll(fetchedPermissionConfigs); permissionConfigs.addAll(fetchedPermissionConfigs);
CompletableFuture.runAsync(() -> fetchedPermissionConfigs.forEach(this::updateCache)); CompletableFuture.runAsync(() -> fetchedPermissionConfigs.forEach(this::updateCache));
} }

View File

@ -6,21 +6,33 @@ import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
@Repository @Repository
public class CachePermissionConfigRepository { public class CachePermissionConfigRepository {
@Autowired @Autowired
private RedisTemplate<String, PermissionConfig> redisTemplate; private RedisTemplate<String, Object> redisTemplate;
public List<PermissionConfig> cachePermissionConfigBatch(Set<String> permissionNameList) { public List<PermissionConfig> cachePermissionConfigBatch(Set<String> permissionNameList) {
List<String> keys = permissionNameList.stream() List<String> keys = permissionNameList.stream()
.filter(Objects::nonNull) .filter(Objects::nonNull)
.map(name -> "PermissionConfig::" + name).toList(); .map(name -> "PermissionConfig::" + name).toList();
return redisTemplate.opsForValue().multiGet(keys); List<Object> rawConfigs = redisTemplate.opsForValue().multiGet(keys);
if (rawConfigs == null) {
return Collections.emptyList();
}
List<PermissionConfig> permissionConfigs = rawConfigs.stream()
.filter(config -> config instanceof PermissionConfig)
.map(config -> (PermissionConfig) config)
.collect(Collectors.toList());
return Objects.requireNonNull(permissionConfigs);
} }
@Cacheable(value = "PermissionConfig", key = "#permissionConfig.permissionName") @Cacheable(value = "PermissionConfig", key = "#permissionConfig.permissionName")

View File

@ -11,7 +11,11 @@ public class DelayDispatch {
if (attempt >= 5) { if (attempt >= 5) {
return; return;
} }
boolean success = requestSupplier != null && requestSupplier.get() != null && requestSupplier.get(); // 使用Supplier<Boolean>获取请求成功与否的状态 boolean success = false;
if (requestSupplier != null) {
Boolean result = requestSupplier.get();
success = result != null && result;
}
if (!success) { if (!success) {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.schedule(() -> { scheduler.schedule(() -> {
@ -23,4 +27,4 @@ public class DelayDispatch {
}, 5, TimeUnit.SECONDS); }, 5, TimeUnit.SECONDS);
} }
} }
} }