StaffOutputValueService.java
20.3 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
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
339
340
341
342
package cn.com.glsx.busassistant.service;
import cn.com.glsx.busassistant.converter.SellerServeMerchantStatDTOConvert;
import cn.com.glsx.busassistant.converter.StaffOutputValueStatDTOConvert;
import cn.com.glsx.busassistant.converter.StaffPerformanceDTOConvert;
import cn.com.glsx.busassistant.converter.TechnicianServeMerchantStatDTOConvert;
import cn.com.glsx.busassistant.enums.StaffMerchantStatTypeEnum;
import cn.com.glsx.busassistant.request.outputValue.ListStaffOutputValuesRequest;
import cn.com.glsx.busassistant.request.outputValue.ListStaffRequest;
import cn.com.glsx.busassistant.response.outputValue.*;
import cn.com.glsx.dj.smartcarlife.dto.MerchantSubRefDTO;
import cn.com.glsx.dj.smartcarlife.dto.staffStat.SellerServeMerchantStatDTO;
import cn.com.glsx.dj.smartcarlife.dto.staffStat.StaffOutputValueStatDTO;
import cn.com.glsx.dj.smartcarlife.dto.staffStat.StaffPerformanceDTO;
import cn.com.glsx.dj.smartcarlife.dto.staffStat.TechnicianServeMerchantStatDTO;
import cn.com.glsx.dj.smartcarlife.enums.EmployeePerformanceEnum;
import cn.com.glsx.dj.smartcarlife.remote.MerchantSubRefRemote;
import cn.com.glsx.dj.smartcarlife.remote.StaffStatRemote;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.util.RpcAssert;
import cn.com.glsx.framework.rop.response.RopResponse;
import cn.com.glsx.merchants.enums.StaffBusinessTypeEnum;
import cn.com.glsx.merchants.model.response.MerchantStaffResponse;
import cn.com.glsx.scrm.system.model.wechatapplets.LoginUserInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.oreframework.boot.autoconfigure.rop.util.RopThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Service
public class StaffOutputValueService {
@Autowired
private MerchantServiceCenterService merchantServiceCenterService;
@Autowired
private MerchantStaffService merchantStaffService;
@Autowired
private Environment env;
@Autowired
private StaffStatRemote staffStatRemote;
@Autowired
private MerchantSubRefRemote merchantSubRefRemote;
private List<Byte> UPGRADEGOODS_TYPE_LIST;
@PostConstruct
public void init(){
UPGRADEGOODS_TYPE_LIST = Arrays.stream(env.getProperty("outputvalue.sceneOne.goodsType", "1").split(","))
.map(Byte::parseByte).collect(Collectors.toList());;
}
public RopResponse<RpcPagination<ListStaffOutputValuesItemResponse>> listStaffOutputValues(ListStaffOutputValuesRequest request){
LoginUserInfoResponse loginUser = RopThreadLocal.getLoginUser(LoginUserInfoResponse.class);
List<Integer> partnerMerchantIds;
if (!loginUser.getIsSuper()) {
partnerMerchantIds = new ArrayList<>();
partnerMerchantIds.add(loginUser.getMerchantId());
}else{
partnerMerchantIds = request.getPartnerMerchantIds();
}
partnerMerchantIds = getContainSubMerchantIds(partnerMerchantIds);
log.debug("当前服务商及数字化服务商,partnerMerchantIds:{}", partnerMerchantIds);
List<Integer> yqMerchantIds = merchantServiceCenterService.listYqMerchantIdsByPartnerMerchantIds(partnerMerchantIds);
if (CollectionUtils.isEmpty(yqMerchantIds)) {
log.info("当前服务商及数字化服务商-关联园区店为空,partnerMerchantIds:{}", partnerMerchantIds);
return RopResponse.success(new RpcPagination<>());
}
List<MerchantStaffResponse> merchantStaffs = merchantStaffService.listMerchantStaffsByMerchantIds(yqMerchantIds);
if (CollectionUtils.isEmpty(merchantStaffs)) {
log.info("园区店下无员工,yqMerchantIds:{}", yqMerchantIds);
return RopResponse.success(new RpcPagination<>());
}
return listStaffOutputValues(request, merchantStaffs, yqMerchantIds);
}
public List<MerchantStaffResponse> listStaff(ListStaffRequest request){
LoginUserInfoResponse loginUser = RopThreadLocal.getLoginUser(LoginUserInfoResponse.class);
List<Integer> partnerMerchantIds;
if (!loginUser.getIsSuper()) {
partnerMerchantIds = new ArrayList<>();
partnerMerchantIds.add(loginUser.getMerchantId());
}else{
partnerMerchantIds = request.getPartnerMerchantIds();
}
partnerMerchantIds = getContainSubMerchantIds(partnerMerchantIds);
log.debug("当前服务商及数字化服务商,partnerMerchantIds:{}", partnerMerchantIds);
List<Integer> yqMerchantIds = merchantServiceCenterService.listYqMerchantIdsByPartnerMerchantIds(partnerMerchantIds);
if (CollectionUtils.isEmpty(yqMerchantIds)) {
log.info("当前服务商及数字化服务商-关联园区店为空,partnerMerchantIds:{}", partnerMerchantIds);
return null;
}
return merchantStaffService.listMerchantStaffsByMerchantIds(yqMerchantIds);
}
private List<Integer> getContainSubMerchantIds(List<Integer> partnerMerchantIds) {
if (CollectionUtils.isNotEmpty(partnerMerchantIds)) {
MerchantSubRefDTO condition = MerchantSubRefDTO.builder().merchantIds(partnerMerchantIds).build();
RpcResponse<List<MerchantSubRefDTO>> res = merchantSubRefRemote.list(condition);
if(res.hasResult()){
res.getResult().stream().map(MerchantSubRefDTO::getSubMerchantId).forEach(partnerMerchantIds::add);
}else{
log.error("查询服务商下架关系失败,res:{}", res);
}
}
return partnerMerchantIds;
}
public RopResponse<RpcPagination<ListStaffOutputValuesItemResponse>> listStaffOutputValues(ListStaffOutputValuesRequest request, List<MerchantStaffResponse> merchantStaffs, List<Integer> yqMerchantIds){
Map<String, MerchantStaffResponse> outputValueStaffsMap = new HashMap<>();
Map<String, MerchantStaffResponse> constructionStaffsMap = new HashMap<>();
initStaffsMap(request, merchantStaffs, outputValueStaffsMap, constructionStaffsMap);
RpcPagination<ListStaffOutputValuesItemResponse> staffOutputValuesPage = pageStaffOutputValue(outputValueStaffsMap,
constructionStaffsMap, yqMerchantIds, request);
if(staffOutputValuesPage != null){
compileStaffMerchantStatDetail(request, staffOutputValuesPage.getList(), outputValueStaffsMap, constructionStaffsMap);
return RopResponse.success(staffOutputValuesPage);
}
return RopResponse.error("获取员工产值统计失败");
}
private void compileStaffMerchantStatDetail(ListStaffOutputValuesRequest request, List<ListStaffOutputValuesItemResponse> list, Map<String, MerchantStaffResponse> outputValueStaffsMap, Map<String, MerchantStaffResponse> constructionStaffsMap) {
if (CollectionUtils.isNotEmpty(list)) {
for (ListStaffOutputValuesItemResponse staffOutputValueItemResp : list) {
MerchantStaffResponse outputValueStaff = outputValueStaffsMap.get(staffOutputValueItemResp.getStaffUUID());
if (outputValueStaff != null) {
compileOutputValueStaffMerchantStatDetail(staffOutputValueItemResp, request, outputValueStaff);
}else{
MerchantStaffResponse constructionStaff = constructionStaffsMap.get(staffOutputValueItemResp.getStaffUUID());
compileConstructionStaffMerchantStatDetail(staffOutputValueItemResp, request, constructionStaff);
}
}
compileStaffPerformanceDetail(list);
}
}
private void compileStaffPerformanceDetail(List<ListStaffOutputValuesItemResponse> list) {
if (CollectionUtils.isNotEmpty(list)) {
for (ListStaffOutputValuesItemResponse staffOutputValuesItem : list) {
Byte staffType = null;
if (Objects.equals(StaffMerchantStatTypeEnum.OUTPUT_VALUE.getValue(), staffOutputValuesItem.getOutputValueType())) {
staffType = EmployeePerformanceEnum.StaffType.RESIDENT.getValue();
}else if (Objects.equals(StaffMerchantStatTypeEnum.CONSTRUCTION.getValue(), staffOutputValuesItem.getOutputValueType())) {
staffType = EmployeePerformanceEnum.StaffType.TECHNICIAN.getValue();
}
String performanceMonthStart = env.getProperty("staff.performanceMonth.start", "2023-09");
if (!performanceMonthStart.matches("^\\d{4}(-)(0[1-9]|1[0-2])$")) {
log.warn("staff.performanceMonth.start配置错误, performanceMonthStart:{}", performanceMonthStart);
performanceMonthStart = "2023-09";
}
StaffPerformanceDTO condition = StaffPerformanceDTO.builder().staffUUID(staffOutputValuesItem.getStaffUUID()).staffType(staffType).performanceMonthStart(performanceMonthStart).build();
RpcResponse<StaffPerformanceDTO> res = staffStatRemote.getStaffPerformance(condition);
if(res.hasResult()){
StaffPerformanceResponse staffPerformanceResponse = StaffPerformanceDTOConvert.INSTANCE.toStaffPerformanceResponse(res.getResult());
compileStaffPerformanceResult(staffPerformanceResponse, performanceMonthStart);
staffOutputValuesItem.setPerformance(staffPerformanceResponse);
}else{
log.error("获取员工绩效配置及结果失败, condition:{},res:{}", condition, res);
}
}
}
}
private void compileStaffPerformanceResult(StaffPerformanceResponse staffPerformanceResponse, String performanceMonthStart) {
if (staffPerformanceResponse != null) {
Map<String, StaffPerformanceResultResponse> performanceResultMap;
if (CollectionUtils.isNotEmpty(staffPerformanceResponse.getPerformanceResults())) {
performanceResultMap = staffPerformanceResponse.getPerformanceResults().stream().collect(Collectors.toMap(StaffPerformanceResultResponse::getPerformanceMonth, o -> o, (o1, o2) -> o2));
}else{
performanceResultMap = new HashMap<>();
}
LocalDate lastPerformanceMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
// 从performanceMonthStart开始,到当前月份,每个月都要有绩效结果(内有计算结果则结果值为null)
LocalDate performanceMonth = LocalDate.parse(performanceMonthStart + "-01");
List<StaffPerformanceResultResponse> list = new ArrayList<>();
while (lastPerformanceMonth.isAfter(performanceMonth)
|| lastPerformanceMonth.isEqual(performanceMonth)) {
String lastPerformanceMonthStr = lastPerformanceMonth.format(DateTimeFormatter.ofPattern("yyyy-MM"));
StaffPerformanceResultResponse performanceResult = performanceResultMap.get(lastPerformanceMonthStr);
if (performanceResult == null) {
performanceResult = new StaffPerformanceResultResponse();
performanceResult.setPerformanceMonth(lastPerformanceMonthStr);
}
list.add(performanceResult);
lastPerformanceMonth = lastPerformanceMonth.minusMonths(1);
}
staffPerformanceResponse.setPerformanceResults(list);
}
}
private void compileConstructionStaffMerchantStatDetail(ListStaffOutputValuesItemResponse staffOutputValueItemResp, ListStaffOutputValuesRequest request, MerchantStaffResponse constructionStaff) {
RpcAssert.assertNotNull(constructionStaff, DefaultErrorEnum.DATA_NULL,"constructionStaff not be null");
TechnicianServeMerchantStatDTO technicianServeMerchantStatDTO = TechnicianServeMerchantStatDTO.builder()
.technicianStaffUUID(constructionStaff.getUuid()).shopMerchantIds(request.getShopMerchantIds())
.startTime(request.getStartTime()).endTime(request.getEndTime()).upgradeGoodsTypes(UPGRADEGOODS_TYPE_LIST).build();
RpcPagination<TechnicianServeMerchantStatDTO> pagination = new RpcPagination<>();
pagination.setPages(request.getMerchantOutputValuesPageSize());
pagination.setCondition(technicianServeMerchantStatDTO);
RpcPagination<TechnicianServeMerchantStatDTO> technicianServeMerchantStatPage = pageTechnicianServeMerchantStat(pagination);
if (technicianServeMerchantStatPage != null) {
List<StaffConstructionStatItemResponse> constructionStats = technicianServeMerchantStatPage.getList().stream()
.map(TechnicianServeMerchantStatDTOConvert.INSTANCE::toStaffConstructionStatItemResponse).collect(Collectors.toList());
staffOutputValueItemResp.setConstructionStats(constructionStats);
}
staffOutputValueItemResp.setStaffName(constructionStaff.getStaffName());
staffOutputValueItemResp.setStaffRoleName(constructionStaff.getRoleName());
staffOutputValueItemResp.setStaffBusinessType(constructionStaff.getStaffBusinessType());
staffOutputValueItemResp.setYqMerchantId(constructionStaff.getMerchantNo());
staffOutputValueItemResp.setYqMerchantName(constructionStaff.getMerchantName());
staffOutputValueItemResp.setOutputValueType(StaffMerchantStatTypeEnum.CONSTRUCTION.getValue());
}
public RpcPagination<TechnicianServeMerchantStatDTO> pageTechnicianServeMerchantStat(RpcPagination<TechnicianServeMerchantStatDTO> pagination){
RpcResponse<RpcPagination<TechnicianServeMerchantStatDTO>> res = staffStatRemote.pageTechnicianServeMerchantStat(pagination);
if(res.hasResult()){
return res.getResult();
}else{
log.error("分页获取技师的4s店订单台次统计失败,res:{}", res);
}
return null;
}
private void compileOutputValueStaffMerchantStatDetail(ListStaffOutputValuesItemResponse staffOutputValueItemResp, ListStaffOutputValuesRequest request, MerchantStaffResponse outputValueStaff) {
RpcAssert.assertNotNull(outputValueStaff, DefaultErrorEnum.DATA_NULL,"constructionStaff not be null");
SellerServeMerchantStatDTO sellerServeMerchantStatDTO = SellerServeMerchantStatDTO.builder()
.sellerStaffUUID(outputValueStaff.getUuid()).shopMerchantIds(request.getShopMerchantIds())
.startTime(request.getStartTime()).endTime(request.getEndTime()).upgradeGoodsTypes(UPGRADEGOODS_TYPE_LIST).build();
RpcPagination<SellerServeMerchantStatDTO> pagination = new RpcPagination<>();
pagination.setPageSize(request.getMerchantOutputValuesPageSize());
pagination.setCondition(sellerServeMerchantStatDTO);
RpcPagination<SellerServeMerchantStatDTO> sellerServeMerchantStatPage = pageSellerServeMerchantStat(pagination);
if (sellerServeMerchantStatPage != null) {
List<StaffOutputValueStatItemResponse> sellerStats = sellerServeMerchantStatPage.getList().stream()
.map(SellerServeMerchantStatDTOConvert.INSTANCE::toStaffOutputValueStatItemResponse).collect(Collectors.toList());
staffOutputValueItemResp.setOutputValueStats(sellerStats);
}
staffOutputValueItemResp.setStaffName(outputValueStaff.getStaffName());
staffOutputValueItemResp.setStaffRoleName(outputValueStaff.getRoleName());
staffOutputValueItemResp.setStaffBusinessType(outputValueStaff.getStaffBusinessType());
staffOutputValueItemResp.setYqMerchantId(outputValueStaff.getMerchantNo());
staffOutputValueItemResp.setYqMerchantName(outputValueStaff.getMerchantName());
staffOutputValueItemResp.setOutputValueType(StaffMerchantStatTypeEnum.OUTPUT_VALUE.getValue());
}
public RpcPagination<SellerServeMerchantStatDTO> pageSellerServeMerchantStat(RpcPagination<SellerServeMerchantStatDTO> pagination){
RpcResponse<RpcPagination<SellerServeMerchantStatDTO>> res = staffStatRemote.pageSellerServeMerchantStat(pagination);
if(res.hasResult()){
return res.getResult();
}else{
log.error("分页获取驻店人员的4s店产值统计失败,res:{}", res);
}
return null;
}
private RpcPagination<ListStaffOutputValuesItemResponse> pageStaffOutputValue(Map<String, MerchantStaffResponse> outputValueStaffsMap,
Map<String, MerchantStaffResponse> constructionStaffsMap,
List<Integer> yqMerchantIds, ListStaffOutputValuesRequest request){
StaffOutputValueStatDTO staffOutputValueStatDTO = StaffOutputValueStatDTO.builder()
.sellerStaffUUIDs(new ArrayList<>(outputValueStaffsMap.keySet()))
.technicianStaffUUIDs(new ArrayList<>(constructionStaffsMap.keySet()))
.yqMerchantIds(yqMerchantIds).startTime(request.getStartTime()).endTime(request.getEndTime())
.shopMerchantIds(request.getShopMerchantIds()).upgradeGoodsTypes(UPGRADEGOODS_TYPE_LIST).build();
if (CollectionUtils.isEmpty(staffOutputValueStatDTO.getSellerStaffUUIDs())) {
staffOutputValueStatDTO.setSellerStaffUUIDs(Arrays.asList("-1"));
}
if (CollectionUtils.isEmpty(staffOutputValueStatDTO.getTechnicianStaffUUIDs())) {
staffOutputValueStatDTO.setTechnicianStaffUUIDs(Arrays.asList("-1"));
}
RpcPagination<StaffOutputValueStatDTO> pagination = new RpcPagination<>();
pagination.setPageNum(request.getPageNum());
pagination.setPageSize(request.getPageSize());
pagination.setCondition(staffOutputValueStatDTO);
RpcResponse<RpcPagination<StaffOutputValueStatDTO>> res = staffStatRemote.pageStaffOutputValue(pagination);
if(res.hasResult()){
pagination = res.getResult();
RpcPagination<ListStaffOutputValuesItemResponse> resp = new RpcPagination<>();
resp.setPageSize(pagination.getPageSize());
resp.setPageNum(pagination.getPageNum());
resp.setTotal(pagination.getTotal());
resp.setPages(pagination.getPages());
resp.setStartRow(pagination.getStartRow());
resp.setEndRow(pagination.getEndRow());
resp.setList(new ArrayList<>());
if (CollectionUtils.isNotEmpty(pagination.getList())) {
resp.setList(pagination.getList().parallelStream().map(StaffOutputValueStatDTOConvert.INSTANCE::toListStaffOutputValuesItemResponse)
.collect(Collectors.toList()));
}
return resp;
}else{
log.error("分页获取员工产值统计失败,msg:{}", res.getMessage());
}
return null;
}
private void initStaffsMap(ListStaffOutputValuesRequest request, List<MerchantStaffResponse> merchantStaffs, Map<String, MerchantStaffResponse> outputValueStaffsMap, Map<String, MerchantStaffResponse> constructionStaffsMap) {
boolean specificStaff = CollectionUtils.isNotEmpty(request.getStaffUUIDs());
List<MerchantStaffResponse> outputValueStaffs = new ArrayList<>();
List<MerchantStaffResponse> constructionStaffs = new ArrayList<>();
for (MerchantStaffResponse staff : merchantStaffs) {
if (!specificStaff || request.getStaffUUIDs().contains(staff.getUuid())) {
if (isTechnician(staff)) {
constructionStaffs.add(staff);
} else {
outputValueStaffs.add(staff);
}
}
}
outputValueStaffsMap.putAll(
outputValueStaffs.stream().collect(Collectors.toMap(MerchantStaffResponse::getUuid, o -> o, (o1, o2) -> o2))
);
constructionStaffsMap.putAll(
constructionStaffs.stream().collect(Collectors.toMap(MerchantStaffResponse::getUuid, o -> o, (o1, o2) -> o2))
);
}
public Boolean isTechnician(MerchantStaffResponse staff){
return Objects.equals(StaffBusinessTypeEnum.TECHNICIAN.getCode(), staff.getStaffBusinessType());
}
}