DinerServiceImpl.java
11 KB
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
package com.infoloop.tianting.service.impl;
import com.infoloop.tianting.clientinventoryservice.SingleClientRpcResponse;
import com.infoloop.tianting.context.LoginContextHolder;
import com.infoloop.tianting.exception.ClientEndExceptions;
import com.infoloop.tianting.exception.ErrorCodeEnum;
import com.infoloop.tianting.mealorderservice.DinerModification;
import com.infoloop.tianting.mealorderservice.MpAccountDinerRefCreation;
import com.infoloop.tianting.mealorderservice.SingleDinerRpcResponse;
import com.infoloop.tianting.mealorderservice.SingleGradeClassRpcResponse;
import com.infoloop.tianting.mealorderservice.SingleMealOrderRpcResponse;
import com.infoloop.tianting.mealorderservice.SingleMpAccountDinerRefRpcResponse;
import com.infoloop.tianting.model.common.CreatedResult;
import com.infoloop.tianting.model.dto.DinerDTO;
import com.infoloop.tianting.model.vo.DinerVO;
import com.infoloop.tianting.model.vo.GradeClassVO;
import com.infoloop.tianting.service.DinerService;
import com.infoloop.tianting.service.client.ClientInventoryServiceRpcClient;
import com.infoloop.tianting.service.client.MealOrderServiceRpcClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DinerServiceImpl implements DinerService {
private final MealOrderServiceRpcClient mealOrderServiceRpcClient;
private final ClientInventoryServiceRpcClient clientInventoryServiceRpcClient;
@Override
public List<DinerVO> getMpAccountDiners() {
final var loginInfo = LoginContextHolder.getLoginInfo();
final var mpAccountDinerRefRpcResponseList = mealOrderServiceRpcClient.queryMpAccountDinerRefsByCondition(loginInfo.getEnterpriseId(), List.of(loginInfo.getId()), Collections.emptyList());
final var dinerIds = mpAccountDinerRefRpcResponseList.stream().map(SingleMpAccountDinerRefRpcResponse::getDinerId).toList();
if (dinerIds.isEmpty()) {
return Collections.emptyList();
}
final var dinersByIds = mealOrderServiceRpcClient.getDinersByIds(loginInfo.getEnterpriseId(), dinerIds);
final var clientIds = dinersByIds.stream().map(SingleDinerRpcResponse::getClientId).distinct().toList();
final var gradeClassIds = dinersByIds.stream().map(SingleDinerRpcResponse::getClassId).distinct().toList();
final var clientMap = clientInventoryServiceRpcClient.getClientsByIds(loginInfo.getEnterpriseId(), clientIds).getClientsList().stream().collect(Collectors.toMap(SingleClientRpcResponse::getId, Function.identity()));
final var gradeClassMap = mealOrderServiceRpcClient.getGradeClassesByIds(loginInfo.getEnterpriseId(), gradeClassIds).stream().collect(Collectors.toMap(SingleGradeClassRpcResponse::getId, Function.identity()));
final var reserveDinerMealOrderMap = mealOrderServiceRpcClient.queryReserveMealOrdersByDinerIds(loginInfo.getEnterpriseId(), dinerIds).stream().collect(Collectors.toMap(SingleMealOrderRpcResponse::getDinerId, Function.identity()));
return dinersByIds.stream().map(e -> DinerVO.builder()
.id(e.getId())
.enterpriseId(e.getEnterpriseId())
.clientId(e.getClientId())
.clientCode(clientMap.get(e.getClientId()).getCode())
.clientName(clientMap.get(e.getClientId()).getName())
.classId(e.getClassId())
.gradeName(gradeClassMap.get(e.getClassId()).getGradeName())
.className(gradeClassMap.get(e.getClassId()).getClassName())
.isReserve(reserveDinerMealOrderMap.get(e.getId()) != null)
.studentNo(e.getStudentNo())
.name(e.getName())
.build()).toList();
}
@Override
public CreatedResult<DinerVO> createMpAccountDiner(DinerDTO.CreateDinerDTO createDinerDTO) {
final var loginInfo = LoginContextHolder.getLoginInfo();
final var clientsByCodes = clientInventoryServiceRpcClient.getClientsByCodes(loginInfo.getEnterpriseId(), List.of(createDinerDTO.getClientCode()));
if (clientsByCodes.size() != 1) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.CLIENT_CODE_NOT_EXISTS);
}
final var singleClientRpcResponse = clientsByCodes.get(0);
final var diners = mealOrderServiceRpcClient.queryDinersByCondition(loginInfo.getEnterpriseId(), List.of(singleClientRpcResponse.getId()), List.of(createDinerDTO.getClassId()), createDinerDTO.getStudentNo());
if (diners.isEmpty()) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.DINER_NOT_FOUND);
}
if (diners.size() != 1) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.DINER_NOT_FOUND);
}
final var diner = diners.get(0);
if (!diner.getName().equals(createDinerDTO.getName())) {
log.info("createMpAccountDiner dinerId:{} name:{}, createName:{}", diner.getId(), diner.getName(), createDinerDTO.getName());
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.DINER_NAME_STUDENT_NO_NOT_MISMATCH);
}
final int dinerId = diner.getId();
final var mpAccountDinerRefs = mealOrderServiceRpcClient.queryMpAccountDinerRefsByCondition(loginInfo.getEnterpriseId(), List.of(loginInfo.getId()), List.of(dinerId));
if (!mpAccountDinerRefs.isEmpty()) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.DINER_ALREADY_EXISTS);
} else {
final var mpAccountDinerRefCreation = MpAccountDinerRefCreation.newBuilder()
.setDinerId(dinerId)
.setAccountId(loginInfo.getId())
.build();
final var mpAccountDinerRef = mealOrderServiceRpcClient.createMpAccountDinerRef(loginInfo, mpAccountDinerRefCreation);
log.info("createMpAccountDiner dinerId:{} mpAccountDinerRefId:{}", dinerId, mpAccountDinerRef.getId());
return CreatedResult.<DinerVO>builder().id(dinerId).isCreated(mpAccountDinerRef.getIsCreated()).build();
}
}
@Override
public List<GradeClassVO> getClientGradeClasses(SingleClientRpcResponse client) {
final var loginInfo = LoginContextHolder.getLoginInfo();
final var gradeClasses = mealOrderServiceRpcClient.queryGradeClassesByCondition(loginInfo.getEnterpriseId(), List.of(client.getId()));
return gradeClasses.stream().map(e -> GradeClassVO.builder()
.id(e.getId())
.enterpriseId(e.getEnterpriseId())
.gradeName(e.getGradeName())
.className(e.getClassName())
.build()).toList();
}
@Override
public void updateMpAccountDiner(SingleDinerRpcResponse diner, DinerDTO.UpdateDinerDTO updateDinerDTO) {
final var loginInfo = LoginContextHolder.getLoginInfo();
final var clientsByCodes = clientInventoryServiceRpcClient.getClientsByCodes(loginInfo.getEnterpriseId(), List.of(updateDinerDTO.getClientCode()));
if (clientsByCodes.size() != 1) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.CLIENT_CODE_NOT_EXISTS);
}
final var singleClientRpcResponse = clientsByCodes.get(0);
if (diner.getClientId() == singleClientRpcResponse.getId() && diner.getClassId() == updateDinerDTO.getClassId() && diner.getStudentNo().equals(updateDinerDTO.getStudentNo()) && !diner.getName().equals(updateDinerDTO.getName())) {
mealOrderServiceRpcClient.updateDiner(loginInfo, DinerModification.newBuilder().setId(diner.getId()).setShouldUpdateName(true).setName(updateDinerDTO.getName()).build());
return;
}
final var diners = mealOrderServiceRpcClient.queryDinersByCondition(loginInfo.getEnterpriseId(), List.of(singleClientRpcResponse.getId()), List.of(updateDinerDTO.getClassId()), updateDinerDTO.getStudentNo());
if (!diners.isEmpty()) {
throw ClientEndExceptions.IncorrectRequestValue.build(ErrorCodeEnum.DINER_ALREADY_EXISTS);
}
mealOrderServiceRpcClient.updateDiner(loginInfo, DinerModification.newBuilder()
.setId(diner.getId())
.setShouldUpdateClientId(true).setClientId(singleClientRpcResponse.getId())
.setShouldUpdateClassId(updateDinerDTO.getClassId() != null).setClassId(updateDinerDTO.getClassId() == null ? diner.getClassId() : updateDinerDTO.getClassId())
.setShouldUpdateName(updateDinerDTO.getName() != null).setName(updateDinerDTO.getName() == null ? diner.getName() : updateDinerDTO.getName())
.setShouldUpdateStudentNo(updateDinerDTO.getStudentNo() != null).setStudentNo(updateDinerDTO.getStudentNo() == null ? diner.getStudentNo() : updateDinerDTO.getStudentNo())
.build());
}
@Override
public void deleteMpAccountDiner(SingleDinerRpcResponse diner) {
final var loginInfo = LoginContextHolder.getLoginInfo();
final var mpAccountDinerRefRpcResponseList = mealOrderServiceRpcClient.queryMpAccountDinerRefsByCondition(loginInfo.getEnterpriseId(), List.of(loginInfo.getId()), List.of(diner.getId()));
// mealOrderServiceRpcClient.deleteDinersByIds(loginInfo, List.of(diner.getId()));
if (!mpAccountDinerRefRpcResponseList.isEmpty()) {
mealOrderServiceRpcClient.deleteMpAccountDinerRefsByIds(loginInfo.getEnterpriseId(), mpAccountDinerRefRpcResponseList.stream().map(SingleMpAccountDinerRefRpcResponse::getId).toList());
}
}
@Override
public List<DinerVO> getClientClassDiners(SingleClientRpcResponse client, SingleGradeClassRpcResponse gradeClass, String studentNo) {
final var loginInfo = LoginContextHolder.getLoginInfo();
final var diners = mealOrderServiceRpcClient.queryDinersByCondition(loginInfo.getEnterpriseId(), List.of(client.getId()), List.of(gradeClass.getId()), studentNo);
final var mpAccountDinerRefs = mealOrderServiceRpcClient.queryMpAccountDinerRefsByCondition(loginInfo.getEnterpriseId(), List.of(loginInfo.getId()), Collections.emptyList());
final var existDinerIds = mpAccountDinerRefs.stream().map(SingleMpAccountDinerRefRpcResponse::getDinerId).collect(Collectors.toSet());
return diners.stream().filter(diner -> !existDinerIds.contains(diner.getId())).map(diner -> DinerVO.builder()
.id(diner.getId())
.enterpriseId(diner.getEnterpriseId())
.clientId(diner.getClientId())
.clientCode(client.getCode())
.clientName(client.getName())
.classId(diner.getClassId())
.gradeName(gradeClass.getGradeName())
.className(gradeClass.getClassName())
.studentNo(diner.getStudentNo())
.name(diner.getName())
.build()).toList();
}
}