DeliverLogServiceImpl.java
8.14 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
package vion.service.impl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONUtil;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import vion.constant.RedisKeyEnum;
import vion.dto.DeliverLogDTO;
import vion.mapper.DeliverLogMapper;
import vion.model.*;
import vion.service.IDeliverLogService;
import vion.service.IRStoreConfederateService;
import vion.service.IStoreService;
import vion.third.DingMod;
import vion.vo.DeliverLogVO;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class DeliverLogServiceImpl extends MPJBaseServiceImpl<DeliverLogMapper, DeliverLog> implements IDeliverLogService {
private final IStoreService storeService;
private final IRStoreConfederateService storeConfederateService;
private final RedissonClient redissonClient;
private final Converter converter;
private final DingMod dingMod;
@Override
public Map<String, TreeMap<String, List<DeliverLogVO>>> list(DeliverLogDTO dto) {
var wrapper = new MPJLambdaWrapper<>(converter.convert(dto, DeliverLog.class))
.selectAll(DeliverLog.class)
.selectAs(Store::getName, DeliverLogVO::getStoreName)
.selectAs(RStoreConfederate::getName, DeliverLogVO::getConfederateName)
.selectAs(User::getUsername, DeliverLogVO::getUserName)
.selectAs(Contract::getSaleName, DeliverLogVO::getContractSaleName)
.leftJoin(Store.class, Store::getId, DeliverLog::getStoreId)
.leftJoin(RStoreConfederate.class, RStoreConfederate::getId, DeliverLog::getConfederateId)
.leftJoin(User.class, User::getId, DeliverLog::getUserId)
.leftJoin(Contract.class, Contract::getContractNo, DeliverLog::getContractNo)
.between(DeliverLog::getLogDate, dto.getLogDateStart(), dto.getLogDateEnd());
var deliverLogList = this.selectJoinList(DeliverLogVO.class, wrapper);
return deliverLogList.stream().collect(Collectors.groupingBy(DeliverLogVO::getUserName,
Collectors.groupingBy(v -> DateUtil.formatDate(v.getLogDate()), () -> new TreeMap<String,
List<DeliverLogVO>>(Comparator.reverseOrder()), Collectors.toList())));
}
@Override
public DeliverLogVO getById(Long id) {
var wrapper = new MPJLambdaWrapper<DeliverLog>()
.selectAll(DeliverLog.class)
.selectAs(Store::getName, DeliverLogVO::getStoreName)
.selectAs(RStoreConfederate::getName, DeliverLogVO::getConfederateName)
.selectAs(User::getUsername, DeliverLogVO::getUserName)
.selectAs(Contract::getSaleName, DeliverLogVO::getContractSaleName)
.leftJoin(Store.class, Store::getId, DeliverLog::getStoreId)
.leftJoin(RStoreConfederate.class, RStoreConfederate::getId, DeliverLog::getConfederateId)
.leftJoin(User.class, User::getId, DeliverLog::getUserId)
.leftJoin(Contract.class, Contract::getContractNo, DeliverLog::getContractNo)
.eq(DeliverLog::getId, id)
.orderByDesc(DeliverLog::getUpdateTime);
return this.selectJoinOne(DeliverLogVO.class, wrapper);
}
@Override
public String save(List<DeliverLogDTO> deliverLogDTOList) {
var deliverLogList = converter.convert(deliverLogDTOList, DeliverLog.class);
var storeIdList = deliverLogList.stream().map(DeliverLog::getStoreId).toList();
var confederateIdList = deliverLogList.stream().map(DeliverLog::getConfederateId).toList();
var stores = storeService.listByIds(storeIdList);
var id2StoreMap = stores.stream().collect(Collectors.toMap(Store::getId, Function.identity()));
var confederateList = storeConfederateService.listByIds(confederateIdList);
var storeId2NameMap = confederateList.stream().collect(Collectors.toMap(RStoreConfederate::getId, RStoreConfederate::getName));
deliverLogList.forEach(deliverLog -> {
var storeId = deliverLog.getStoreId();
var store = id2StoreMap.get(storeId);
var confederateName = Opt.ofNullable(deliverLog.getConfederateId()).map(storeId2NameMap::get).orElse("暂无");
Opt.ofNullable(redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.USER_ID.getVal() + store.getSalesperson()).get())
.map(u -> (User) u)
.map(User::getUserid)
.ifPresent(userid -> dingMod.workMsg(buildMsg(userid, store.getName(), confederateName, deliverLog)));
});
return this.saveBatch(deliverLogList) ? "新增成功" : "新增失败";
}
@Override
public String update(DeliverLogDTO dto) {
var deliverLog = converter.convert(dto, DeliverLog.class);
var store = storeService.getById(dto.getStoreId());
var confederate = storeConfederateService.getById(dto.getConfederateId());
Opt.ofNullable(redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.USER_ID.getVal() + store.getSalesperson()).get())
.map(u -> (User) u)
.map(User::getUserid)
.ifPresent(userid -> dingMod.workMsg(buildMsg(userid, store.getName(),
Opt.ofNullable(confederate).map(RStoreConfederate::getName).orElse("暂无"),
deliverLog)));
return this.updateById(deliverLog) ? "修改成功" : "修改失败";
}
@Override
public Boolean logNotify(String[] userIdStr) {
var pushRes = dingMod.workMsg(buildMsg1(userIdStr));
return StrUtil.containsIgnoreCase(pushRes, "ok");
}
JSONObject buildMsg(String userid, String storeName, String confederateName, DeliverLog deliverLog) {
var jsonObj = JSONUtil.ofObj()
.set("agent_id", 2358374016L)
.set("userid_list", userid);
var msg = new JSONObject();
var content = new JSONObject();
content.set("title", "项目交付日志提醒")
.set("text", StrUtil.format("""
#### 项目:{}
#### 合同编号:{}
#### 日志提交人:{}
#### 干系人:{}
#### 工时:{}
#### 工作内容:{}
#### 发送时间:{}""", storeName,
Opt.ofBlankAble(deliverLog.getContractNo()).orElse("暂无"),
Opt.ofNullable(redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.USER_ID.getVal() + deliverLog.getUserId()).get()).map(u -> (User) u).map(User::getUsername).orElse("不存在该提交人"),
confederateName,
deliverLog.getLogHour(),
deliverLog.getContent(),
DateUtil.now()));
msg.set("msgtype", "markdown").set("markdown", content);
jsonObj.set("msg", msg);
return jsonObj;
}
JSONObject buildMsg1(String[] userIdStr) {
var jsonObj = JSONUtil.ofObj()
.set("agent_id", 2358374016L)
.set("userid_list", String.join(",", userIdStr));
var msg = new JSONObject();
var content = new JSONObject();
content.set("title", "项目交付日志提醒")
.set("text", StrUtil.format("""
#### 您的日志还未提交,请尽快提交!
#### 发送时间:{}""", DateUtil.now()));
msg.set("msgtype", "markdown").set("markdown", content);
jsonObj.set("msg", msg);
return jsonObj;
}
}