|
|
@@ -15,6 +15,9 @@ import com.huimv.employment.dao.mapper.FeUserMapper;
|
|
15
|
15
|
import com.huimv.employment.service.config.SecurityProperties;
|
|
16
|
16
|
import com.huimv.employment.service.enterprise.dto.EnterpriseRegisterRequest;
|
|
17
|
17
|
import com.huimv.employment.service.enterprise.dto.EnterpriseRegisterResponse;
|
|
|
18
|
+import org.slf4j.Logger;
|
|
|
19
|
+import org.slf4j.LoggerFactory;
|
|
|
20
|
+import org.springframework.dao.DuplicateKeyException;
|
|
18
|
21
|
import org.springframework.stereotype.Service;
|
|
19
|
22
|
import org.springframework.transaction.annotation.Transactional;
|
|
20
|
23
|
|
|
|
@@ -26,6 +29,8 @@ import java.time.LocalDateTime;
|
|
26
|
29
|
@Service
|
|
27
|
30
|
public class EnterpriseService {
|
|
28
|
31
|
|
|
|
32
|
+ private static final Logger log = LoggerFactory.getLogger(EnterpriseService.class);
|
|
|
33
|
+
|
|
29
|
34
|
private static final String USER_TYPE_ENTERPRISE = "enterprise";
|
|
30
|
35
|
private static final String AUTH_STATUS_PENDING = "pending";
|
|
31
|
36
|
private static final String ROLE_OWNER = "owner";
|
|
|
@@ -66,7 +71,7 @@ public class EnterpriseService {
|
|
66
|
71
|
|
|
67
|
72
|
@Transactional(rollbackFor = Exception.class)
|
|
68
|
73
|
public EnterpriseRegisterResponse register(Long userId, EnterpriseRegisterRequest request) {
|
|
69
|
|
- requireEnterpriseUser(userId);
|
|
|
74
|
+ lockEnterpriseUserForUpdate(userId);
|
|
70
|
75
|
if (findBindingByUserId(userId) != null) {
|
|
71
|
76
|
throw new BizException(ErrorCode.ENTERPRISE_ALREADY_REGISTERED);
|
|
72
|
77
|
}
|
|
|
@@ -90,14 +95,21 @@ public class EnterpriseService {
|
|
90
|
95
|
enterprise.setContactMobileEnc(contactMobileEnc);
|
|
91
|
96
|
enterprise.setContactMobileMask(contactMobileMask);
|
|
92
|
97
|
enterprise.setAuthStatus(AUTH_STATUS_PENDING);
|
|
93
|
|
- enterprise.setEnterpriseCode("ENT_TMP_" + System.currentTimeMillis());
|
|
94
|
98
|
enterprise.setCreateTime(now);
|
|
95
|
99
|
enterprise.setUpdateTime(now);
|
|
96
|
100
|
enterprise.setDelFlag(0);
|
|
97
|
|
- feEnterpriseMapper.insert(enterprise);
|
|
|
101
|
+ try {
|
|
|
102
|
+ feEnterpriseMapper.insert(enterprise);
|
|
|
103
|
+ } catch (DuplicateKeyException ex) {
|
|
|
104
|
+ throw handleEnterpriseDuplicate(ex, "insert");
|
|
|
105
|
+ }
|
|
98
|
106
|
|
|
99
|
107
|
enterprise.setEnterpriseCode(generateEnterpriseCode(enterprise.getId()));
|
|
100
|
|
- feEnterpriseMapper.updateById(enterprise);
|
|
|
108
|
+ try {
|
|
|
109
|
+ feEnterpriseMapper.updateById(enterprise);
|
|
|
110
|
+ } catch (DuplicateKeyException ex) {
|
|
|
111
|
+ throw handleEnterpriseDuplicate(ex, "assign enterpriseCode");
|
|
|
112
|
+ }
|
|
101
|
113
|
|
|
102
|
114
|
FeEnterpriseUser binding = new FeEnterpriseUser();
|
|
103
|
115
|
binding.setEnterpriseId(enterprise.getId());
|
|
|
@@ -108,11 +120,45 @@ public class EnterpriseService {
|
|
108
|
120
|
binding.setCreateTime(now);
|
|
109
|
121
|
binding.setUpdateTime(now);
|
|
110
|
122
|
binding.setDelFlag(0);
|
|
111
|
|
- feEnterpriseUserMapper.insert(binding);
|
|
|
123
|
+ try {
|
|
|
124
|
+ feEnterpriseUserMapper.insert(binding);
|
|
|
125
|
+ } catch (DuplicateKeyException ex) {
|
|
|
126
|
+ throw handleEnterpriseDuplicate(ex, "bind user");
|
|
|
127
|
+ }
|
|
112
|
128
|
|
|
113
|
129
|
return toRegisterResponse(enterprise);
|
|
114
|
130
|
}
|
|
115
|
131
|
|
|
|
132
|
+ /**
|
|
|
133
|
+ * 事务内锁定 fe_user 行,串行化同一用户的并发登记请求。
|
|
|
134
|
+ */
|
|
|
135
|
+ private FeUser lockEnterpriseUserForUpdate(Long userId) {
|
|
|
136
|
+ FeUser user = feUserMapper.selectByIdForUpdate(userId);
|
|
|
137
|
+ if (user == null) {
|
|
|
138
|
+ throw new BizException(ErrorCode.UNAUTHORIZED);
|
|
|
139
|
+ }
|
|
|
140
|
+ if (!USER_TYPE_ENTERPRISE.equals(user.getUserType())) {
|
|
|
141
|
+ throw new BizException(ErrorCode.ENTERPRISE_ONLY);
|
|
|
142
|
+ }
|
|
|
143
|
+ return user;
|
|
|
144
|
+ }
|
|
|
145
|
+
|
|
|
146
|
+ private BizException handleEnterpriseDuplicate(DuplicateKeyException ex, String phase) {
|
|
|
147
|
+ String message = ex.getMessage();
|
|
|
148
|
+ if (message != null && (message.contains("uk_fe_enterprise_credit_code") || message.contains("credit_code"))) {
|
|
|
149
|
+ return new BizException(ErrorCode.BAD_REQUEST, "该统一社会信用代码已被登记");
|
|
|
150
|
+ }
|
|
|
151
|
+ if (message != null && (message.contains("uk_fe_enterprise_code") || message.contains("enterprise_code"))) {
|
|
|
152
|
+ log.error("Duplicate enterprise_code during {}, should not happen", phase, ex);
|
|
|
153
|
+ return new BizException(ErrorCode.INTERNAL_ERROR);
|
|
|
154
|
+ }
|
|
|
155
|
+ if (message != null && message.contains("uk_fe_enterprise_user")) {
|
|
|
156
|
+ return new BizException(ErrorCode.ENTERPRISE_ALREADY_REGISTERED);
|
|
|
157
|
+ }
|
|
|
158
|
+ log.warn("Unexpected duplicate key during enterprise register {}: {}", phase, message);
|
|
|
159
|
+ return new BizException(ErrorCode.ENTERPRISE_ALREADY_REGISTERED);
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
116
|
162
|
private FeEnterpriseUser findBindingByUserId(Long userId) {
|
|
117
|
163
|
return feEnterpriseUserMapper.selectOne(new LambdaQueryWrapper<FeEnterpriseUser>()
|
|
118
|
164
|
.eq(FeEnterpriseUser::getUserId, userId)
|