Parcourir la source

Merge branch 'fyc-vpp' of uskycloud/usky-modules into feature/service-vpp-20260701

fuyuchuan il y a 4 jours
Parent
commit
b96297cf6e

+ 23 - 0
service-vpp/service-vpp-biz/src/main/java/com/usky/vpp/service/impl/VppDrInvitationServiceImpl.java

@@ -138,6 +138,9 @@ public class VppDrInvitationServiceImpl implements VppDrInvitationService {
         invitation.setSmsContactId(resolveSmsContactId(request.getSmsContactId(), customer.getId()));
         invitation.setRemark(request.getRemark());
         VppAuditHelper.fillCreate(invitation);
+        if (invitation.getTenantId() == null && event.getTenantId() != null) {
+            invitation.setTenantId(event.getTenantId());
+        }
         invitationMapper.insert(invitation);
         return toVo(invitation);
     }
@@ -292,6 +295,7 @@ public class VppDrInvitationServiceImpl implements VppDrInvitationService {
             invitation.setDeclaredCapacityKw(declared);
             invitation.setReplyStatus(REPLY_ACCEPT);
             invitation.setResponseStatus(RESPONSE_DECLARED);
+            syncEventDeclared(invitation.getDrEventId());
             syncParticipationToUn(invitation.getDrEventId(), true);
         } else {
             if (!StringUtils.hasText(request.getReason())) {
@@ -388,10 +392,29 @@ public class VppDrInvitationServiceImpl implements VppDrInvitationService {
         participation.setDeclaredAt(LocalDateTime.now());
         VppDrParticipationHelper.refreshCompletionRate(participation);
         VppAuditHelper.fillCreate(participation);
+        if (participation.getTenantId() == null && invitation.getTenantId() != null) {
+            participation.setTenantId(invitation.getTenantId());
+        }
         participationMapper.insert(participation);
         return participation;
     }
 
+    private void syncEventDeclared(Long eventId) {
+        if (eventId == null) {
+            return;
+        }
+        VppDrEvent event = eventMapper.selectById(eventId);
+        if (event == null || VppAuditHelper.isDeleted(event.getDeleteFlag())) {
+            return;
+        }
+        if (event.getEventStatus() != null && event.getEventStatus() != RESPONSE_PENDING) {
+            return;
+        }
+        event.setEventStatus(RESPONSE_DECLARED);
+        VppAuditHelper.fillUpdate(event);
+        eventMapper.updateById(event);
+    }
+
     private void syncParticipationToUn(Long eventId, boolean participate) {
         try {
             unIntegrationService.submitParticipation(eventId, participate);

+ 61 - 0
service-vpp/service-vpp-biz/src/main/java/com/usky/vpp/service/impl/VppDrServiceImpl.java

@@ -69,6 +69,8 @@ public class VppDrServiceImpl implements VppDrService {
     private static final int EVENT_STATUS_SPLITTING = VppDrEventStatus.LAST_PLACE_SPLITTING;
 
     private static final int PARTICIPATE_STATUS_ACCEPT = 1;
+    private static final int REPLY_ACCEPT = 1;
+    private static final int REPLY_REJECT = 2;
 
     private static final int EXECUTE_STATUS_PENDING = 0;
     private static final int EXECUTE_STATUS_RUNNING = 1;
@@ -232,6 +234,14 @@ public class VppDrServiceImpl implements VppDrService {
         event.setEventStatus(EVENT_STATUS_DECLARED);
         VppAuditHelper.fillUpdate(event);
         eventMapper.updateById(event);
+        for (VppDrInvitation invitation : loadEventInvitations(event.getId())) {
+            invitation.setResponseStatus(EVENT_STATUS_DECLARED);
+            if (invitation.getTenantId() == null) {
+                invitation.setTenantId(event.getTenantId() != null ? event.getTenantId() : tenantId);
+            }
+            VppAuditHelper.fillUpdate(invitation);
+            invitationMapper.updateById(invitation);
+        }
     }
 
     @Override
@@ -342,6 +352,7 @@ public class VppDrServiceImpl implements VppDrService {
                     .filter(inv -> inv.getSiteId() != null)
                     .collect(Collectors.toMap(VppDrInvitation::getSiteId, inv -> inv, (a, b) -> a));
 
+            Map<Long, VppDrInvitation> matchedInvitations = new LinkedHashMap<>();
             Map<Long, BigDecimal> declaredByResourceId = new HashMap<>();
             for (DrParticipateRequest.DrResourceDeclare resource : request.getResources()) {
                 if (resource.getResourceId() == null) {
@@ -368,8 +379,10 @@ public class VppDrServiceImpl implements VppDrService {
                     throw new BusinessException("邀约申报出清容量必须大于0");
                 }
                 declaredByResourceId.put(resource.getResourceId(), declaredCapacityKw);
+                matchedInvitations.put(invitation.getId(), invitation);
             }
 
+            List<VppDrParticipation> createdParticipations = new ArrayList<>();
             for (DrParticipateRequest.DrResourceDeclare resource : request.getResources()) {
                 VppResourcePoint rp = resourcePointMapper.selectById(resource.getResourceId());
                 VppDrParticipation participation = new VppDrParticipation();
@@ -381,16 +394,22 @@ public class VppDrServiceImpl implements VppDrService {
                 participation.setDeclaredAt(LocalDateTime.now());
                 VppDrParticipationHelper.refreshCompletionRate(participation);
                 VppAuditHelper.fillCreate(participation);
+                if (participation.getTenantId() == null && event.getTenantId() != null) {
+                    participation.setTenantId(event.getTenantId());
+                }
                 participationMapper.insert(participation);
+                createdParticipations.add(participation);
             }
 
             event.setEventStatus(EVENT_STATUS_DECLARED);
             VppAuditHelper.fillUpdate(event);
             eventMapper.updateById(event);
+            syncInvitationStatus(event, true, matchedInvitations.values(), createdParticipations);
         } else {
             event.setEventStatus(EVENT_STATUS_CANCELLED);
             VppAuditHelper.fillUpdate(event);
             eventMapper.updateById(event);
+            syncInvitationStatus(event, false, loadEventInvitations(eventId), Collections.emptyList());
         }
 
         syncParticipationToUn(eventId, request.getParticipate());
@@ -404,6 +423,48 @@ public class VppDrServiceImpl implements VppDrService {
         }
     }
 
+    private List<VppDrInvitation> loadEventInvitations(Long eventId) {
+        return invitationMapper.selectList(new LambdaQueryWrapper<VppDrInvitation>()
+                .eq(VppDrInvitation::getDrEventId, eventId)
+                .eq(VppDrInvitation::getDeleteFlag, VppAuditHelper.NOT_DELETED));
+    }
+
+    private void syncInvitationStatus(VppDrEvent event, boolean participate,
+                                      Collection<VppDrInvitation> invitations,
+                                      List<VppDrParticipation> createdParticipations) {
+        if (invitations == null || invitations.isEmpty()) {
+            return;
+        }
+        Map<Long, Long> participationIdByCustomer = createdParticipations == null
+                ? Collections.emptyMap()
+                : createdParticipations.stream()
+                .filter(p -> p.getCustomerId() != null && p.getId() != null)
+                .collect(Collectors.toMap(VppDrParticipation::getCustomerId, VppDrParticipation::getId, (a, b) -> a));
+        for (VppDrInvitation invitation : invitations) {
+            if (invitation == null) {
+                continue;
+            }
+            if (participate) {
+                invitation.setReplyStatus(REPLY_ACCEPT);
+                invitation.setResponseStatus(EVENT_STATUS_DECLARED);
+                if (invitation.getParticipationId() == null && invitation.getCustomerId() != null) {
+                    Long participationId = participationIdByCustomer.get(invitation.getCustomerId());
+                    if (participationId != null) {
+                        invitation.setParticipationId(participationId);
+                    }
+                }
+            } else {
+                invitation.setReplyStatus(REPLY_REJECT);
+                invitation.setResponseStatus(EVENT_STATUS_CANCELLED);
+            }
+            if (invitation.getTenantId() == null) {
+                invitation.setTenantId(event.getTenantId() != null ? event.getTenantId() : SecurityUtils.getTenantId());
+            }
+            VppAuditHelper.fillUpdate(invitation);
+            invitationMapper.updateById(invitation);
+        }
+    }
+
     private VppDrInvitation resolveInvitationForResource(Long customerId, Long siteId,
                                                          Map<Long, VppDrInvitation> invitationByCustomerId,
                                                          Map<Long, VppDrInvitation> invitationBySiteId) {

+ 11 - 0
service-vpp/service-vpp-biz/src/main/java/com/usky/vpp/util/VppAuditHelper.java

@@ -29,6 +29,7 @@ public final class VppAuditHelper {
             invokeSetter(entity, "setCreatedBy", auditUser);
             invokeSetter(entity, "setUpdatedBy", auditUser);
         }
+        fillTenantIdIfAbsent(entity);
     }
 
     public static void fillUpdate(Object entity) {
@@ -88,6 +89,16 @@ public final class VppAuditHelper {
         }
     }
 
+    private static void fillTenantIdIfAbsent(Object entity) {
+        if (StringUtils.hasText(invokeGetter(entity, "getTenantId"))) {
+            return;
+        }
+        Integer tenantId = currentTenantId();
+        if (tenantId != null) {
+            invokeSetter(entity, "setTenantId", tenantId);
+        }
+    }
+
     public static Integer resolveTenantId(String dnId) {
         if (StringUtils.hasText(dnId)) {
             Integer tenantId = VppSiteResourceHelper.resolveTenantIdByUnResourceId(dnId);