DeliveryRecordServiceImpl.java
10 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
package vion.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.io.file.FileNameUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.crypto.SecureUtil;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vion.dto.ContractDTO;
import vion.dto.DeliveryRecordDTO;
import vion.mapper.DeliveryRecordMapper;
import vion.model.*;
import vion.service.*;
import vion.third.DingMod;
import vion.vo.DeliveryRecordVO;
import vion.vo.UserVO;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author HlQ
* @date 2023/12/5
*/
@Service
@Slf4j
@RequiredArgsConstructor
public class DeliveryRecordServiceImpl extends MPJBaseServiceImpl<DeliveryRecordMapper, DeliveryRecord> implements IDeliveryRecordService {
private final IFileService fileService;
private final IContractService contractService;
private final IRContractUserService contractUserService;
private final IPointInfoService pointInfoService;
private final DingMod dingMod;
private final Converter converter;
@Value("${fileUrl:}")
private String fileUrl;
@Override
public Page<DeliveryRecordVO> list(DeliveryRecordDTO dto) {
MPJLambdaWrapper<DeliveryRecord> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, DeliveryRecord.class))
.selectAll(DeliveryRecord.class)
.selectAs(Contract::getName, DeliveryRecordVO::getContractName)
.selectAs(Contract::getCustomerName, DeliveryRecordVO::getCustomerName)
.leftJoin(Contract.class, Contract::getContractNo, DeliveryRecord::getContractNo)
.like(StrUtil.isNotBlank(dto.getContractName()), Contract::getName, dto.getContractName())
.like(StrUtil.isNotBlank(dto.getCustomerName()), Contract::getCustomerName, dto.getCustomerName())
.orderByDesc(DeliveryRecord::getCreateTime);
Page<DeliveryRecordVO> voPage = this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), DeliveryRecordVO.class, wrapper);
Opt.ofEmptyAble(voPage.getRecords())
.map(recs -> recs.stream().map(DeliveryRecordVO::getId).collect(Collectors.toList()))
.map(ids -> {
List<FileInfo> fileInfos = fileService.lambdaQuery().in(FileInfo::getSourceId, ids).eq(FileInfo::getSourceType, 6).list();
return fileInfos.stream().collect(Collectors.groupingBy(FileInfo::getSourceId));
})
.filter(MapUtil::isNotEmpty)
.ifPresent(map -> voPage.getRecords().forEach(rec -> rec.setFileList(map.get(rec.getId()))));
return voPage;
}
@Override
public String save(DeliveryRecordDTO dto) {
DeliveryRecord deliveryRecord = converter.convert(dto, DeliveryRecord.class);
if (this.save(deliveryRecord)) {
updStatusAndPushMsg(deliveryRecord);
saveFile(dto, deliveryRecord);
return "新增成功";
} else {
return "新增失败";
}
}
@Override
public String saveBatch(List<DeliveryRecordDTO> dto) {
List<DeliveryRecord> records = converter.convert(dto, DeliveryRecord.class);
if (this.saveBatch(records)) {
records.forEach(this::updStatusAndPushMsg);
return "新增成功";
} else {
return "新增失败";
}
}
@Override
public String updateById(DeliveryRecordDTO dto) {
DeliveryRecord record = converter.convert(dto, DeliveryRecord.class);
if (this.updateById(record)) {
DeliveryRecord deliveryRecord = this.getById(dto.getId());
saveFile(dto, deliveryRecord);
return "更新成功";
} else {
return "更新失败";
}
}
/**
* 更新合同状态和点位设计记录状态,并推送发货消息提醒
*
* @param record 发货记录
*/
private void updStatusAndPushMsg(DeliveryRecord record) {
ContractDTO contractDTO = new ContractDTO();
contractDTO.setStatus(2);
Contract existContract = contractService.lambdaQuery().eq(Contract::getContractNo, record.getContractNo()).one();
if (ObjUtil.isNull(existContract) || ObjUtil.isNull(existContract.getStatus())) {
log.error("根据发货记录,更新合同状态出错。该合同不存在或合同状态为空:{}", record.getContractNo());
}
if (ObjUtil.isNotNull(existContract) && existContract.getStatus() < 2) {
// todo 发货后,更新对应合同的付款的节点时间字段 合同id获取不到
/*contractPaymentService.lambdaUpdate()
.set(ContractPayment::getNodeDate, record.getShipDate())
.eq(ContractPayment::getContractId, )
.update(new ContractPayment());*/
contractService.updateById(null, record.getContractNo(), contractDTO);
List<String> useridList = contractUserService.listObjs(Wrappers.<RContractUser>lambdaQuery().select(RContractUser::getUserId).eq(RContractUser::getContractId, existContract.getId()), Object::toString);
dingMod.workMsg(buildMsg(useridList.stream().distinct().collect(Collectors.joining(",")), record, existContract));
}
// 更新点位信息的发货状态
pointInfoService.lambdaUpdate().set(PointInfo::getShippingStatus, 1)
.set(PointInfo::getCourierCompany, record.getCourierCompany())
.set(PointInfo::getTrackingNumber, record.getTrackingNumber())
.set(PointInfo::getStatus, 9)
.eq(PointInfo::getContractNo, record.getContractNo()).update(new PointInfo());
Opt.ofNullable(pointInfoService.lambdaQuery().eq(PointInfo::getContractNo, record.getContractNo()).one())
.ifPresent(p -> pointInfoService.designPush(null, p, "发货"));
}
JSONObject buildMsg(String userid, DeliveryRecord rec, Contract contract) {
var jsonObj = JSONUtil.ofObj()
.set("agent_id", 2358374016L)
.set("userid_list", userid);
var msg = JSONUtil.ofObj();
var content = JSONUtil.ofObj().set("title", "设备发货提醒");
String template = """
#### 发货通知
#### 合同名称:**{}**
#### 合同编号:**{}**
#### 快递公司:{}
#### 快递单号:{}
#### 收件人:{}
#### 发货日期:{}
#### 发送时间:{}
""";
String markdown = StrUtil.format(template,
contract.getName(), contract.getContractNo(), rec.getCourierCompany(), rec.getTrackingNumber(), rec.getConsignee(), DateUtil.formatDate(rec.getShipDate()), DateUtil.now());
content.set("markdown", markdown).set("btn_orientation", "1");
var jsonArray = JSONUtil.ofArray()
.put(JSONUtil.ofObj()
.set("title", "查看详情")
.set("action_url", "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingkrzwks0jpi2di3uo&response_type=code&scope=snsapi_auth&state=STATE&redirect_uri=https%3A%2F%2Fyunwei.vionyun.com%3A8443%2Fyunwei%2Fapi%2Fding%2Fcallback%2Finside%3FdeliveryId%3D" + rec.getId()));
content.set("btn_json_list", jsonArray);
msg.set("msgtype", "action_card").set("action_card", content);
jsonObj.set("msg", msg);
return jsonObj;
}
private void saveFile(DeliveryRecordDTO dto, DeliveryRecord deliveryRecord) {
Opt.ofNullable(dto.getFiles())
.ifPresent(fileList -> {
UserVO userVO = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
Arrays.stream(fileList).forEach(infile -> {
String orgName = infile.getOriginalFilename();
String mainName = FileNameUtil.mainName(orgName);
String fileExt = FileNameUtil.extName(orgName);
String filename = StrUtil.format("{}_{}.{}", mainName, DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS"), fileExt);
String path = fileUrl + FileUtil.FILE_SEPARATOR + "delivery" + FileUtil.FILE_SEPARATOR + deliveryRecord.getId() + FileUtil.FILE_SEPARATOR + filename;
File file = FileUtil.touch(path);
try {
infile.transferTo(file);
} catch (IOException e) {
log.error("保存文件出错", e);
}
FileInfo fileInfo = new FileInfo();
fileInfo.setStoreId(0L);
fileInfo.setSourceId(deliveryRecord.getId());
fileInfo.setContractId(deliveryRecord.getContractId());
fileInfo.setSourceType(6);
fileInfo.setName(filename);
fileInfo.setUrl(path);
fileInfo.setType(FileNameUtil.extName(file));
fileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
fileInfo.setUploader(userVO.getUsername());
fileService.save(fileInfo);
});
});
}
}