1.0
longz
2026-06-24 e45e74e5ae207f7642be9a88ed99b593de89fb3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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;
import com.sinata.appointment.dto.MiniVisitorDetailVo;
import com.sinata.appointment.dto.MiniVisitorListVo;
import com.sinata.appointment.dto.MiniVisitorUpdateRequest;
import com.sinata.appointment.entity.BizFieldConfig;
import com.sinata.appointment.entity.MpUser;
import com.sinata.appointment.entity.MpVisitor;
import com.sinata.appointment.exception.BizException;
import com.sinata.appointment.mapper.BizFieldConfigMapper;
import com.sinata.appointment.mapper.MpVisitorMapper;
import com.sinata.appointment.util.IdCardUtil;
import com.sinata.appointment.util.SensitiveDataUtil;
import com.sinata.appointment.util.UuidAssert;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
@Service
@RequiredArgsConstructor
public class MiniVisitorApplicationService {
 
    private final MiniAuthApplicationService miniAuthApplicationService;
    private final MpVisitorMapper mpVisitorMapper;
    private final BizFieldConfigMapper bizFieldConfigMapper;
 
    @Transactional(rollbackFor = Exception.class)
    public void create(MiniVisitorCreateRequest req) {
        MpUser current = miniAuthApplicationService.currentMpUser();
        Set<String> requiredCodes = loadRequiredMpVisitorFieldCodes();
        VisitorNormalizedInput input =
                normalizeAndValidate(
                        req.getVisitorName(),
                        req.getVisitorMobile(),
                        req.getVisitorCompany(),
                        req.getIdCard(),
                        req.getGender(),
                        requiredCodes);
        assertIdCardUniqueUnderUser(current.getId(), input.idCard(), null);
 
        MpVisitor row = new MpVisitor();
        row.setMpUserId(current.getId());
        // 兼容当前表结构 NOT NULL:未填字段统一落空字符串
        row.setVisitorName(input.name() == null ? "" : input.name());
        row.setVisitorMobile(input.mobile() == null ? "" : input.mobile());
        row.setVisitorCompany(input.company() == null ? "" : input.company());
        row.setIdCardCipher(
                StringUtils.hasText(input.idCard())
                        ? SensitiveDataUtil.encryptIdCard(input.idCard())
                        : "");
        row.setGender(input.gender() == null ? "" : input.gender());
        row.setIsDefault(hasAnyActiveVisitor(current.getId()) ? 0 : 1);
        mpVisitorMapper.insert(row);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void update(Long id, String uuid, MiniVisitorUpdateRequest req) {
        MpUser current = miniAuthApplicationService.currentMpUser();
        MpVisitor row = loadOwnedVisitor(id, uuid, current.getId());
        Set<String> requiredCodes = loadRequiredMpVisitorFieldCodes();
        VisitorNormalizedInput input =
                normalizeAndValidate(
                        req.getVisitorName(),
                        req.getVisitorMobile(),
                        req.getVisitorCompany(),
                        req.getIdCard(),
                        req.getGender(),
                        requiredCodes);
        assertIdCardUniqueUnderUser(current.getId(), input.idCard(), row.getId());
        row.setVisitorName(input.name() == null ? "" : input.name());
        row.setVisitorMobile(input.mobile() == null ? "" : input.mobile());
        row.setVisitorCompany(input.company() == null ? "" : input.company());
        row.setIdCardCipher(
                StringUtils.hasText(input.idCard())
                        ? SensitiveDataUtil.encryptIdCard(input.idCard())
                        : "");
        row.setGender(input.gender() == null ? "" : input.gender());
        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());
        MiniVisitorDetailVo vo = new MiniVisitorDetailVo();
        vo.setId(row.getId());
        vo.setUuid(row.getUuid());
        vo.setVisitorName(row.getVisitorName());
        vo.setVisitorMobile(row.getVisitorMobile());
        vo.setVisitorCompany(row.getVisitorCompany());
        vo.setIdCard(
                StringUtils.hasText(row.getIdCardCipher())
                        ? SensitiveDataUtil.decryptIdCard(row.getIdCardCipher())
                        : "");
        vo.setGender(row.getGender());
        vo.setIsDefault(isDefault(row));
        return vo;
    }
 
    public List<MiniVisitorListVo> listMine() {
        MpUser current = miniAuthApplicationService.currentMpUser();
        Set<String> requiredCodes = loadRequiredMpVisitorFieldCodes();
        List<MpVisitor> rows =
                mpVisitorMapper.selectList(
                        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());
    }
 
    private MiniVisitorListVo toListVo(MpVisitor row, Set<String> requiredCodes) {
        MiniVisitorListVo vo = new MiniVisitorListVo();
        vo.setId(row.getId());
        vo.setUuid(row.getUuid());
        vo.setVisitorName(row.getVisitorName());
        vo.setGender(row.getGender());
        if (StringUtils.hasText(row.getIdCardCipher())) {
            String plain = SensitiveDataUtil.decryptIdCard(row.getIdCardCipher());
            vo.setIdCardMasked(SensitiveDataUtil.maskIdCard(plain));
        } else {
            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) {
            if (AppointmentFieldCodes.VISITOR_NAME.equals(code)
                    && !StringUtils.hasText(trim(row.getVisitorName()))) {
                missing.add(code);
            } else if (AppointmentFieldCodes.ID_CARD.equals(code)
                    && !StringUtils.hasText(trim(row.getIdCardCipher()))) {
                missing.add(code);
            } else if (AppointmentFieldCodes.GENDER.equals(code)
                    && !StringUtils.hasText(trim(row.getGender()))) {
                missing.add(code);
            } else if (AppointmentFieldCodes.COMPANY.equals(code)
                    && !StringUtils.hasText(trim(row.getVisitorCompany()))) {
                missing.add(code);
            } else if (AppointmentFieldCodes.MOBILE.equals(code)
                    && !StringUtils.hasText(trim(row.getVisitorMobile()))) {
                missing.add(code);
            }
        }
        return missing;
    }
 
    private Set<String> loadRequiredMpVisitorFieldCodes() {
        List<BizFieldConfig> rows =
                bizFieldConfigMapper.selectList(
                        new LambdaQueryWrapper<BizFieldConfig>()
                                .eq(BizFieldConfig::getDelFlag, 0)
                                .eq(BizFieldConfig::getVisible, 1)
                                .eq(BizFieldConfig::getRequiredFlag, 1));
        Set<String> supported =
                Set.of(
                        AppointmentFieldCodes.VISITOR_NAME,
                        AppointmentFieldCodes.ID_CARD,
                        AppointmentFieldCodes.GENDER,
                        AppointmentFieldCodes.COMPANY,
                        AppointmentFieldCodes.MOBILE);
        return rows.stream()
                .map(BizFieldConfig::getFieldCode)
                .filter(StringUtils::hasText)
                .map(String::trim)
                .filter(supported::contains)
                .collect(Collectors.toCollection(LinkedHashSet::new));
    }
 
    private static String trim(String s) {
        return s == null ? null : s.trim();
    }
 
    private static String trimToNull(String s) {
        String t = trim(s);
        return (t == null || t.isEmpty()) ? null : t;
    }
 
    private MpVisitor loadOwnedVisitor(Long visitorId, String uuid, Long mpUserId) {
        MpVisitor row = mpVisitorMapper.selectById(visitorId);
        if (row == null || (row.getDelFlag() != null && row.getDelFlag() == 1)) {
            throw new BizException(BizErrorCode.ORDER_NOT_FOUND, "访客不存在");
        }
        UuidAssert.match(row, uuid);
        if (!mpUserId.equals(row.getMpUserId())) {
            throw new BizException(BizErrorCode.AUTH_FORBIDDEN, "无权操作该访客");
        }
        return row;
    }
 
    private VisitorNormalizedInput normalizeAndValidate(
            String visitorName,
            String visitorMobile,
            String visitorCompany,
            String idCard,
            String gender,
            Set<String> requiredCodes) {
        String name = trimToNull(visitorName);
        String mobile = trimToNull(visitorMobile);
        String company = trimToNull(visitorCompany);
        String idCardPlain = trimToNull(idCard);
        String genderText = trimToNull(gender);
 
        if (requiredCodes.contains(AppointmentFieldCodes.VISITOR_NAME) && !StringUtils.hasText(name)) {
            throw new BizException(BizErrorCode.AUTH_INVALID_CREDENTIALS, "来访人姓名不能为空");
        }
        if (StringUtils.hasText(name) && name.length() > 30) {
            throw new BizException(BizErrorCode.AUTH_INVALID_CREDENTIALS, "来访人姓名不能超过30个字");
        }
 
        if (requiredCodes.contains(AppointmentFieldCodes.MOBILE) && !StringUtils.hasText(mobile)) {
            throw new BizException(BizErrorCode.AUTH_INVALID_CREDENTIALS, "来访人电话不能为空");
        }
        if (StringUtils.hasText(mobile) && !mobile.matches("^1[3-9]\\d{9}$")) {
            throw new BizException(BizErrorCode.AUTH_INVALID_CREDENTIALS, "来访人电话格式不正确");
        }
 
        if (requiredCodes.contains(AppointmentFieldCodes.COMPANY) && !StringUtils.hasText(company)) {
            throw new BizException(BizErrorCode.AUTH_INVALID_CREDENTIALS, "来访人单位不能为空");
        }
 
        if (requiredCodes.contains(AppointmentFieldCodes.ID_CARD) && !StringUtils.hasText(idCardPlain)) {
            throw new BizException(BizErrorCode.AUTH_INVALID_CREDENTIALS, "身份证号码不能为空");
        }
        if (StringUtils.hasText(idCardPlain) && !IdCardUtil.isValidMainland18(idCardPlain)) {
            throw new BizException(BizErrorCode.AUTH_INVALID_CREDENTIALS, "身份证号码格式不正确");
        }
 
        if (requiredCodes.contains(AppointmentFieldCodes.GENDER) && !StringUtils.hasText(genderText)) {
            throw new BizException(BizErrorCode.AUTH_INVALID_CREDENTIALS, "性别不能为空");
        }
        if (StringUtils.hasText(genderText) && !"男".equals(genderText) && !"女".equals(genderText)) {
            throw new BizException(BizErrorCode.AUTH_INVALID_CREDENTIALS, "性别仅支持男/女");
        }
        return new VisitorNormalizedInput(name, mobile, company, idCardPlain, genderText);
    }
 
    private void assertIdCardUniqueUnderUser(Long mpUserId, String idCardPlain, Long excludeVisitorId) {
        String normalizedIdCard = normalizeIdCard(idCardPlain);
        if (!StringUtils.hasText(normalizedIdCard) || mpUserId == null) {
            return;
        }
        List<MpVisitor> rows =
                mpVisitorMapper.selectList(
                        new LambdaQueryWrapper<MpVisitor>()
                                .eq(MpVisitor::getMpUserId, mpUserId)
                                .eq(MpVisitor::getDelFlag, 0));
        for (MpVisitor row : rows) {
            if (excludeVisitorId != null && excludeVisitorId.equals(row.getId())) {
                continue;
            }
            if (!StringUtils.hasText(row.getIdCardCipher())) {
                continue;
            }
            String existing = normalizeIdCard(SensitiveDataUtil.decryptIdCard(row.getIdCardCipher()));
            if (normalizedIdCard.equals(existing)) {
                throw new BizException(BizErrorCode.AUTH_INVALID_CREDENTIALS, "同一账号下身份证号码不可重复");
            }
        }
    }
 
    private String normalizeIdCard(String idCard) {
        String value = trimToNull(idCard);
        return value == null ? null : value.toUpperCase();
    }
 
    private record VisitorNormalizedInput(
            String name, String mobile, String company, String idCard, String gender) {}
}