CommonService.java
14.6 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package cn.fw.morax.service.biz;
import cn.fw.common.exception.BusinessException;
import cn.fw.ehr.sdk.api.enums.RoleManagerEnum;
import cn.fw.morax.common.constant.Constant;
import cn.fw.morax.common.utils.ExcelDataUtil;
import cn.fw.morax.common.utils.PublicUtil;
import cn.fw.morax.domain.db.SettingDraft;
import cn.fw.morax.domain.db.eval.EvalGroupRewardRankLog;
import cn.fw.morax.domain.db.eval.EvalIndicatorValue;
import cn.fw.morax.domain.db.kpi.IndicatorUserValue;
import cn.fw.morax.domain.db.kpi.KpiGroup;
import cn.fw.morax.domain.dto.AbstractLaddersDto;
import cn.fw.morax.domain.enums.DataTypeEnum;
import cn.fw.morax.domain.enums.IndicatorValueTypeEnum;
import cn.fw.morax.domain.enums.SettingDraftStatusEnum;
import cn.fw.morax.domain.enums.SettingDraftTypeEnum;
import cn.fw.morax.domain.vo.kpi.IndicatorUserValueVO;
import cn.fw.morax.domain.vo.kpi.KpiIndicatorRankStaffVO;
import cn.fw.morax.rpc.ehr.EhrRpcService;
import cn.fw.morax.rpc.ehr.dto.StaffBaseInfoDTO;
import cn.fw.morax.service.data.SettingDraftService;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@RequiredArgsConstructor
@Service
public class CommonService {
private final EhrRpcService ehrRpcService;
private final SettingDraftService settingDraftService;
/**
* 检查阶梯
*
* @param dtos
*/
public static void checkLadders(List<? extends AbstractLaddersDto> dtos) {
if (PublicUtil.isEmpty(dtos)) {
return;
}
String errorPromptName = dtos.get(0).getBusinessName();
checkLadders(dtos, errorPromptName);
}
/**
* 检查阶梯
*
* @param dtos
*/
public static void checkLadders(List<? extends AbstractLaddersDto> dtos, String errorPromptName) {
if (PublicUtil.isEmpty(dtos)) {
return;
}
BigDecimal lastUpper = null;
//阶梯值校验 标准分不能超过指标基础分,阶梯值必须连续
for (AbstractLaddersDto laddersDto : dtos) {
BigDecimal lower = laddersDto.getLower();
BigDecimal upper = laddersDto.getUpper();
//上一条的下限要等于下一条的上限
if (PublicUtil.isNotEmpty(lastUpper) && (lastUpper.compareTo(lower) != 0) ) {
throw new BusinessException("【" + errorPromptName + "】的阶梯上限与下限不是连续,请编辑后重试");
}
//每条上限大于下限
if (lower.compareTo(upper) >= 0) {
throw new BusinessException("【" + errorPromptName + "】的阶梯下限大于或等于阶梯上限,请修改错误阶梯");
}
lastUpper = laddersDto.getUpper();
}
}
/**
* 导出文件
*
* @param response
* @param rows
* @param fileName
*/
public static <T> void downloadExcel(HttpServletResponse response, List<T> rows, String fileName, Class<?> source) {
ExcelWriter writer = ExcelUtil.getWriter();
ExcelDataUtil.setExcelHeadAndWidth(writer, source, Boolean.TRUE);
writer.setOnlyAlias(true);
writer.write(rows, true);
ExcelDataUtil.setAutoSizeColumn(writer.getSheet());
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename=" + fileName);
ServletOutputStream out= null;
try {
out = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
writer.flush(out, true);
writer.close();
IoUtil.close(out);
}
/**
* 导出文件
*
* @param response
* @param fileName
*/
public static <T> void downloadTemplateExcel(HttpServletResponse response, String fileName, Class<?> source) {
ExcelWriter writer = ExcelUtil.getWriter();
Map<String, String> headMap = ExcelDataUtil.setExcelHeadAndWidth(writer, source, Boolean.FALSE);
if (headMap.isEmpty()) {
return;
}
writer.writeHeadRow(headMap.values());
writer.setRowHeight(0, 25);
// ExcelDataUtil.setAutoSizeColumn(writer.getSheet(), headMap.size());
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename=" + fileName);
ServletOutputStream out= null;
try {
out = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
writer.flush(out, true);
writer.close();
IoUtil.close(out);
}
/**
* 获取编码后的文件名
*
* @param request
* @param fileName
* @return
*/
public static String getEncodeName(HttpServletRequest request, String fileName) {
String userAgent = request.getHeader("User-Agent");
if (userAgent.contains("MSIE") || userAgent.contains("Trident") || userAgent.contains("Postman")) {
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {// 非IE浏览器的处理:
fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
return fileName;
}
/**
* 实时查询在职员工
*
* @param userId
* @return
*/
public List<StaffBaseInfoDTO> queryRealTimeManageStaffs(Long userId) {
List<StaffBaseInfoDTO> managerStaffs = ehrRpcService.queryManageStaffs(userId);
if (PublicUtil.isEmpty(managerStaffs)) {
managerStaffs = new ArrayList<>();
}
List<StaffBaseInfoDTO> selectVOS = managerStaffs.stream()
.filter(staff -> (! staff.getId().equals(userId)))
.collect(Collectors.toList());
return selectVOS;
}
/**
* 查询离职、在职员工
*
* @param userId
* @param yearMonth
* @return
*/
public List<StaffBaseInfoDTO> queryManageStaffs(Long userId, YearMonth yearMonth) {
List<StaffBaseInfoDTO> managerStaffs = ehrRpcService.queryManageStaffs(userId);
if (PublicUtil.isEmpty(managerStaffs)) {
managerStaffs = new ArrayList<>();
}
LocalDate startDate = LocalDate.of(yearMonth.getYear(), yearMonth.getMonth(), 1);
LocalDate endDate = startDate.with(TemporalAdjusters.lastDayOfMonth());
LocalDateTime startDateTime = LocalDateTime.of(startDate, LocalTime.MIN);
LocalDateTime endDateTime = LocalDateTime.of(endDate, LocalTime.MAX);
Date firstDayOfMonth = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
Date endDayOfMonth = Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant());
List<StaffBaseInfoDTO> leaveManagerStaffs = ehrRpcService.queryLeaveManageStaffs(userId, firstDayOfMonth, endDayOfMonth);
if (PublicUtil.isNotEmpty(leaveManagerStaffs)) {
managerStaffs.addAll(leaveManagerStaffs);
}
List<StaffBaseInfoDTO> selectVOS = managerStaffs.stream()
.filter(staff -> (! staff.getId().equals(userId)))
.collect(Collectors.toList());
return selectVOS;
}
/**
* 查询离职、在职员工id
*
* @param userId
* @param yearMonth
* @return
*/
public List<Long> queryManageStaffIds(Long userId, YearMonth yearMonth) {
List<StaffBaseInfoDTO> staffBaseInfoDTOS = this.queryManageStaffs(userId, yearMonth);
if (PublicUtil.isNotEmpty(staffBaseInfoDTOS)) {
return staffBaseInfoDTOS.stream().map(StaffBaseInfoDTO::getId).collect(Collectors.toList());
}
return new ArrayList<>();
}
/**
* 查询离职员工id
*
* @param shopId
* @param yearMonth
* @return
*/
public List<Long> queryShopLeaveStaffIds(Long shopId, YearMonth yearMonth) {
List<Long> shopIds = new ArrayList<Long>(){{add(shopId);}};
LocalDate startDate = LocalDate.of(yearMonth.getYear(), yearMonth.getMonth(), 1);
LocalDate endDate = startDate.with(TemporalAdjusters.lastDayOfMonth());
LocalDateTime startDateTime = LocalDateTime.of(startDate, LocalTime.MIN);
LocalDateTime endDateTime = LocalDateTime.of(endDate, LocalTime.MAX);
Date firstDayOfMonth = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
Date endDayOfMonth = Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant());
List<StaffBaseInfoDTO> staffBaseInfoDTOS = ehrRpcService.queryShopLeaveStaff(shopIds, firstDayOfMonth, endDayOfMonth);
if (PublicUtil.isNotEmpty(staffBaseInfoDTOS)) {
return staffBaseInfoDTOS.stream().map(StaffBaseInfoDTO::getId).collect(Collectors.toList());
}
return new ArrayList<>();
}
/**
* 查询具体指标值
* @param indicatorCode
* @param indicatorUserValue
* @return
*/
public BigDecimal queryIndicatorValue(String indicatorCode, IndicatorUserValue indicatorUserValue) {
if (PublicUtil.isEmpty(indicatorUserValue)) {
return BigDecimal.ZERO;
}
BigDecimal value = BigDecimal.ZERO;
//条件计算类型 是车系id:销售台数
if (IndicatorValueTypeEnum.CONDITION.equals(indicatorUserValue.getValueType())) {
return value;
}
try {
JSONObject jsonObject = JSONObject.parseObject(indicatorUserValue.getIndicatorValue());
value = Optional.ofNullable(jsonObject.getBigDecimal(indicatorCode)).orElse(BigDecimal.ZERO);
} catch (Exception e) {
log.error("[{}]指标值转化失败", indicatorCode, e);
}
return value;
}
/**
* 查询具体指标值
* @param indicatorCode
* @param indicatorValue
* @return
*/
public BigDecimal queryEvalIndicatorValue(String indicatorCode, EvalIndicatorValue indicatorValue) {
if (PublicUtil.isEmpty(indicatorValue)) {
return BigDecimal.ZERO;
}
BigDecimal value = BigDecimal.ZERO;
try {
value = Optional.ofNullable(indicatorValue.getIndicatorValue()).orElse(BigDecimal.ZERO);
} catch (Exception e) {
log.error("[{}]指标值转化失败", indicatorCode, e);
}
return value;
}
/**
* 查询具体指标值
* @param indicatorCode
* @param indicatorUserValue
* @return
*/
public BigDecimal queryIndicatorValueVO(String indicatorCode, IndicatorUserValueVO indicatorUserValue) {
BigDecimal value = BigDecimal.ZERO;
//条件计算类型 是车系id:销售台数
if (IndicatorValueTypeEnum.CONDITION.equals(indicatorUserValue.getValueType())) {
return value;
}
try {
JSONObject jsonObject = JSONObject.parseObject(indicatorUserValue.getIndicatorValue());
value = Optional.ofNullable(jsonObject.getBigDecimal(indicatorCode)).orElse(BigDecimal.ZERO);
if (IndicatorValueTypeEnum.RATIO.equals(indicatorUserValue.getValueType())) {
value = value.multiply(Constant.ONE_HUNDRED);
}
} catch (Exception e) {
log.error("[{}]指标值转化失败", indicatorCode, e);
}
return value;
}
/**
* 查询具体指标值
* @param indicatorCode
* @param indicatorUserValue
* @return
*/
public BigDecimal queryEvalIndicatorValueVO(String indicatorCode, EvalIndicatorValue indicatorUserValue) {
BigDecimal value = BigDecimal.ZERO;
//条件计算类型 是车系id:销售台数
try {
value = Optional.ofNullable(indicatorUserValue.getIndicatorValue()).orElse(BigDecimal.ZERO);
if (DataTypeEnum.RATIO.equals(indicatorUserValue.getDataType())) {
value = value.multiply(Constant.ONE_HUNDRED);
}
} catch (Exception e) {
log.error("[{}]指标值转化失败", indicatorCode, e);
}
return value;
}
/**
* 是否拥有管理角色
*
* @return
*/
public Boolean hasManagerRole(List<String> roleCodes) {
if (PublicUtil.isEmpty(roleCodes)) {
return Boolean.FALSE;
}
HashSet<String> roleCodeSet = new HashSet<>(roleCodes);
return Arrays.stream(RoleManagerEnum.values())
.anyMatch(roleManagerEnum -> roleCodeSet.contains(roleManagerEnum.getCode()));
}
/**
* 获取重名的草稿
*
* @param name
* @param type
* @param excludeDraftId
* @return
*/
public List<SettingDraft> getEditDraftByName(String name, SettingDraftTypeEnum type, Long excludeDraftId) {
List<SettingDraft> settingDrafts = settingDraftService.list(Wrappers.<SettingDraft>lambdaQuery()
.eq(SettingDraft::getName, name)
.eq(SettingDraft::getType, type)
.ne(SettingDraft::getStatus, SettingDraftStatusEnum.RELEASE_APPROVAL_AGREE)
.ne(PublicUtil.isNotEmpty(excludeDraftId), SettingDraft::getId, excludeDraftId)
.eq(SettingDraft::getYn, Boolean.TRUE)
);
return settingDrafts;
}
/**
* 设置排名序号
*
* @return
*/
public void calcRank(List<EvalGroupRewardRankLog> rankLogs) {
int rank = 1;
BigDecimal lastIndicatorValue = null;
for (EvalGroupRewardRankLog rankLog : rankLogs) {
//初始化
if (PublicUtil.isEmpty(lastIndicatorValue)) {
rankLog.setRank(rank);
lastIndicatorValue = rankLog.getReachValue();
continue;
}
if (lastIndicatorValue.compareTo(rankLog.getReachValue()) != 0) {
rank++;
}
rankLog.setRank(rank);
lastIndicatorValue = rankLog.getReachValue();
}
}
}