shinnosuke
2026-08-13 3cfa835d19909d81b0ca03f29e003b397a93241e
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
package com.talentcard.server.service;
 
import java.util.List;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.talentcard.server.business.BusinessIdGenerator;
import com.talentcard.server.common.BusinessException;
import com.talentcard.server.common.ErrorCode;
import com.talentcard.server.entity.DependentCardApply;
import com.talentcard.server.entity.DependentCardApplyStatusLog;
import com.talentcard.server.mapper.DependentCardApplyMapper;
import com.talentcard.server.mapper.DependentCardApplyStatusLogMapper;
import com.talentcard.server.request.DependentCardApplyRequest;
import com.talentcard.server.request.DependentCardUnbindRequest;
import com.talentcard.server.response.DependentCardApplyDetailResponse;
import com.talentcard.server.response.DependentCardApplyItemResponse;
import com.talentcard.server.response.DependentCardApplyResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
public class DependentCardApplyService {
 
    private static final String ELITE_CARD_LEVEL = "卓越人才卡";
    private static final int MAX_APPLICATION_COUNT = 3;
    private static final int MAX_FAMILY_CARD_COUNT = 2;
 
    private final DependentCardApplyMapper mapper;
    private final DependentCardApplyStatusLogMapper statusLogMapper;
 
    public DependentCardApplyService(
            DependentCardApplyMapper mapper,
            DependentCardApplyStatusLogMapper statusLogMapper
    ) {
        this.mapper = mapper;
        this.statusLogMapper = statusLogMapper;
    }
 
    @Transactional
    public DependentCardApplyResponse submit(DependentCardApplyRequest request) {
        DependentCardApply ownerCard = ownerCard(request.talentCardId());
        int familyCardLimit = familyCardLimit(ownerCard);
        long applicationCount = mapper.selectCount(
                Wrappers.<DependentCardApply>lambdaQuery()
                        .eq(DependentCardApply::getTalentCardId, request.talentCardId())
                        .in(
                                DependentCardApply::getStatus,
                                DependentCardApply.STATUS_PENDING_REVIEW,
                                DependentCardApply.STATUS_APPROVED,
                                DependentCardApply.STATUS_REJECTED
                        )
        );
        if (applicationCount >= MAX_APPLICATION_COUNT) {
            throw new BusinessException(ErrorCode.CONFLICT, "同一张主卡最多提交3个家属附属卡申请");
        }
        long occupiedCount = mapper.selectCount(
                Wrappers.<DependentCardApply>lambdaQuery()
                        .eq(DependentCardApply::getTalentCardId, request.talentCardId())
                        .in(
                                DependentCardApply::getStatus,
                                DependentCardApply.STATUS_PENDING_REVIEW,
                                DependentCardApply.STATUS_APPROVED
                        )
        );
        if (occupiedCount >= familyCardLimit) {
            throw new BusinessException(
                    ErrorCode.CONFLICT,
                    "该主卡家属附属卡名额已满,最多绑定" + familyCardLimit + "个"
            );
        }
        DependentCardApply apply = new DependentCardApply(
                BusinessIdGenerator.next("DCA"),
                number(ownerCard.getTalentId()),
                number(ownerCard.getTalentCardId()),
                text(ownerCard.getTalentName()),
                text(ownerCard.getTalentMobile()),
                text(ownerCard.getTalentCardNo()),
                text(ownerCard.getCardLevel()),
                request.dependentName().trim(),
                request.dependentMobile().trim(),
                request.dependentIdCardNo().trim().toUpperCase(),
                request.relation().trim()
        );
        if (mapper.insert(apply) != 1) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "家属附属卡申请提交失败");
        }
        recordStatus(apply, null, DependentCardApply.STATUS_PENDING_REVIEW, "TALENT", apply.getTalentId(), "小程序提交申请");
        return DependentCardApplyResponse.from(apply);
    }
 
    @Transactional(readOnly = true)
    public List<DependentCardApplyItemResponse> list(Long talentCardId) {
        requireTalentCardId(talentCardId);
        return mapper.selectList(
                        Wrappers.<DependentCardApply>lambdaQuery()
                                .eq(DependentCardApply::getTalentCardId, talentCardId)
                                .ne(DependentCardApply::getStatus, DependentCardApply.STATUS_CANCELLED)
                                .orderByDesc(DependentCardApply::getApplyTime)
                                .orderByDesc(DependentCardApply::getId)
                ).stream()
                .map(DependentCardApplyItemResponse::from)
                .toList();
    }
 
    @Transactional
    public DependentCardApplyItemResponse unbind(Long id, DependentCardUnbindRequest request) {
        requireTalentCardId(request.talentCardId());
        DependentCardApply apply = mapper.selectByIdForUpdate(id);
        if (apply == null || !request.talentCardId().equals(apply.getTalentCardId())) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "家属附属卡申请不存在");
        }
//        if (!Integer.valueOf(DependentCardApply.STATUS_APPROVED).equals(apply.getStatus())) {
//            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "仅已通过审核的家属附属卡可以解绑");
//        }
        int updated = mapper.update(
                null,
                Wrappers.<DependentCardApply>lambdaUpdate()
                        .eq(DependentCardApply::getId, id)
                        .eq(DependentCardApply::getTalentCardId, request.talentCardId())
                        .eq(DependentCardApply::getStatus, DependentCardApply.STATUS_APPROVED)
                        .set(DependentCardApply::getStatus, DependentCardApply.STATUS_CANCELLED)
        );
        if (updated != 1) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "家属附属卡状态已变化,请刷新后重试");
        }
        recordStatus(
                apply,
                DependentCardApply.STATUS_APPROVED,
                DependentCardApply.STATUS_CANCELLED,
                "TALENT",
                apply.getTalentId(),
                "小程序用户主动解绑"
        );
        return DependentCardApplyItemResponse.from(mapper.selectById(id));
    }
 
 
    @Transactional(readOnly = true)
    public DependentCardApplyDetailResponse detail(Long id, Long talentCardId) {
        Long applyId = requirePositive(id, "申请ID不能为空");
        requireTalentCardId(talentCardId);
        DependentCardApply apply = mapper.selectById(applyId);
        if (apply == null || !talentCardId.equals(apply.getTalentCardId())) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "家属附属卡申请不存在");
        }
        return DependentCardApplyDetailResponse.from(apply);
    }
 
    @Transactional
    public DependentCardApplyItemResponse resubmit(Long id, DependentCardApplyRequest request) {
        Long applyId = requirePositive(id, "申请ID不能为空");
        requireTalentCardId(request.talentCardId());
        DependentCardApply apply = mapper.selectByIdForUpdate(applyId);
        if (apply == null || !request.talentCardId().equals(apply.getTalentCardId())) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "家属附属卡申请不存在");
        }
        if (!Integer.valueOf(DependentCardApply.STATUS_REJECTED).equals(apply.getStatus())) {
            throw new BusinessException(ErrorCode.CONFLICT, "仅已驳回的申请可以修改重新提交");
        }
        DependentCardApply resubmitted = new DependentCardApply(
                BusinessIdGenerator.next("DCA"),
                apply.getTalentId(),
                apply.getTalentCardId(),
                apply.getTalentName(),
                apply.getTalentMobile(),
                apply.getTalentCardNo(),
                apply.getCardLevel(),
                request.dependentName().trim(),
                request.dependentMobile().trim(),
                request.dependentIdCardNo().trim().toUpperCase(),
                request.relation().trim()
        );
        if (mapper.insert(resubmitted) != 1) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "家属附属卡申请重新提交失败");
        }
        recordStatus(
                resubmitted,
                null,
                DependentCardApply.STATUS_PENDING_REVIEW,
                "TALENT",
                resubmitted.getTalentId(),
                "用户基于已驳回申请重新提交,原申请ID:" + applyId
        );
        return DependentCardApplyItemResponse.from(resubmitted);
    }
    private DependentCardApply ownerCard(Long talentCardId) {
        requireTalentCardId(talentCardId);
        DependentCardApply card = mapper.selectOwnerCard(talentCardId);
        if (card == null) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "主卡不存在或已失效");
        }
        return card;
    }
 
    private int familyCardLimit(DependentCardApply card) {
        if (!ELITE_CARD_LEVEL.equals(card.getCardLevel())) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "仅卓越人才卡可申请家属附属卡");
        }
//        if (!Integer.valueOf(1).equals(card.getIsFamilyCardEnabled())) {
//            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "该主卡未开通家属附属卡申请权限");
//        }
//        Integer configuredLimit = card.getFamilyCardLimit();
//        if (configuredLimit == null || configuredLimit <= 0) {
//            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "该主卡未配置家属附属卡名额");
//        }
        return Math.min(2, MAX_FAMILY_CARD_COUNT);
    }
 
    private void recordStatus(
            DependentCardApply apply,
            Integer fromStatus,
            Integer toStatus,
            String operatorType,
            Long operatorId,
            String remark
    ) {
        if (statusLogMapper.insert(new DependentCardApplyStatusLog(
                apply.getId(),
                apply.getApplyNo(),
                fromStatus,
                toStatus,
                operatorType,
                operatorId,
                remark
        )) != 1) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "家属附属卡状态记录保存失败");
        }
    }
 
    private Long requirePositive(Long id, String message) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.BAD_REQUEST, message);
        }
        return id;
    }
 
    private void requireTalentCardId(Long talentCardId) {
        if (talentCardId == null || talentCardId <= 0) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "主卡ID不能为空");
        }
    }
 
    private Long number(Object value) {
        return value instanceof Number number ? number.longValue() : null;
    }
 
    private String text(Object value) {
        return value == null ? null : String.valueOf(value);
    }
}