From e45e74e5ae207f7642be9a88ed99b593de89fb3b Mon Sep 17 00:00:00 2001
From: longz <longzhaoz@qq.com>
Date: 星期三, 24 六月 2026 16:54:46 +0800
Subject: [PATCH] 1.0

---
 backend/src/main/java/com/sinata/appointment/service/MiniVisitorApplicationService.java |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/backend/src/main/java/com/sinata/appointment/service/MiniVisitorApplicationService.java b/backend/src/main/java/com/sinata/appointment/service/MiniVisitorApplicationService.java
index e4e4848..2f1e864 100644
--- a/backend/src/main/java/com/sinata/appointment/service/MiniVisitorApplicationService.java
+++ b/backend/src/main/java/com/sinata/appointment/service/MiniVisitorApplicationService.java
@@ -1,6 +1,7 @@
 package com.sinata.appointment.service;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.sinata.appointment.constants.AppointmentFieldCodes;
 import com.sinata.appointment.constants.BizErrorCode;
 import com.sinata.appointment.dto.MiniVisitorCreateRequest;
@@ -59,6 +60,7 @@
                         ? SensitiveDataUtil.encryptIdCard(input.idCard())
                         : "");
         row.setGender(input.gender() == null ? "" : input.gender());
+        row.setIsDefault(hasAnyActiveVisitor(current.getId()) ? 0 : 1);
         mpVisitorMapper.insert(row);
     }
 
@@ -87,6 +89,31 @@
         mpVisitorMapper.updateById(row);
     }
 
+    @Transactional(rollbackFor = Exception.class)
+    public void setDefault(Long id, String uuid) {
+        MpUser current = miniAuthApplicationService.currentMpUser();
+        MpVisitor row = loadOwnedVisitor(id, uuid, current.getId());
+        mpVisitorMapper.update(
+                null,
+                new LambdaUpdateWrapper<MpVisitor>()
+                        .eq(MpVisitor::getMpUserId, current.getId())
+                        .eq(MpVisitor::getDelFlag, 0)
+                        .set(MpVisitor::getIsDefault, 0));
+        row.setIsDefault(1);
+        mpVisitorMapper.updateById(row);
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    public void delete(Long id, String uuid) {
+        MpUser current = miniAuthApplicationService.currentMpUser();
+        MpVisitor row = loadOwnedVisitor(id, uuid, current.getId());
+        boolean wasDefault = isDefault(row);
+        mpVisitorMapper.deleteById(row.getId());
+        if (wasDefault) {
+            resetEarliestVisitorAsDefault(current.getId());
+        }
+    }
+
     public MiniVisitorDetailVo detail(Long id, String uuid) {
         MpUser current = miniAuthApplicationService.currentMpUser();
         MpVisitor row = loadOwnedVisitor(id, uuid, current.getId());
@@ -101,6 +128,7 @@
                         ? SensitiveDataUtil.decryptIdCard(row.getIdCardCipher())
                         : "");
         vo.setGender(row.getGender());
+        vo.setIsDefault(isDefault(row));
         return vo;
     }
 
@@ -112,6 +140,7 @@
                         new LambdaQueryWrapper<MpVisitor>()
                                 .eq(MpVisitor::getMpUserId, current.getId())
                                 .eq(MpVisitor::getDelFlag, 0)
+                                .orderByDesc(MpVisitor::getIsDefault)
                                 .orderByDesc(MpVisitor::getCreateTime)
                                 .orderByDesc(MpVisitor::getId));
         return rows.stream().map(row -> toListVo(row, requiredCodes)).collect(Collectors.toList());
@@ -130,10 +159,38 @@
             vo.setIdCardMasked(null);
         }
         vo.setVisitorCompany(row.getVisitorCompany());
+        vo.setIsDefault(isDefault(row));
         vo.setMissingFieldCodes(resolveMissingFieldCodes(row, requiredCodes));
         return vo;
     }
 
+    private boolean isDefault(MpVisitor row) {
+        return row != null && row.getIsDefault() != null && row.getIsDefault() == 1;
+    }
+
+    private boolean hasAnyActiveVisitor(Long mpUserId) {
+        return mpVisitorMapper.selectCount(
+                        new LambdaQueryWrapper<MpVisitor>()
+                                .eq(MpVisitor::getMpUserId, mpUserId)
+                                .eq(MpVisitor::getDelFlag, 0))
+                > 0;
+    }
+
+    private void resetEarliestVisitorAsDefault(Long mpUserId) {
+        MpVisitor next =
+                mpVisitorMapper.selectOne(
+                        new LambdaQueryWrapper<MpVisitor>()
+                                .eq(MpVisitor::getMpUserId, mpUserId)
+                                .eq(MpVisitor::getDelFlag, 0)
+                                .orderByAsc(MpVisitor::getCreateTime)
+                                .orderByAsc(MpVisitor::getId)
+                                .last("limit 1"));
+        if (next != null) {
+            next.setIsDefault(1);
+            mpVisitorMapper.updateById(next);
+        }
+    }
+
     private List<String> resolveMissingFieldCodes(MpVisitor row, Set<String> requiredCodes) {
         List<String> missing = new ArrayList<>();
         for (String code : requiredCodes) {

--
Gitblit v1.7.1