DriverServiceImpl.java
8.09 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
package com.ruoyi.driver.service.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.job.DriverJob;
import com.ruoyi.pojo.response.ResponseScheduling;
import com.ruoyi.utils.ConstDateUtil;
import com.ruoyi.utils.ListUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.driver.mapper.DriverMapper;
import com.ruoyi.driver.domain.Driver;
import com.ruoyi.driver.service.IDriverService;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import static com.ruoyi.redispre.GlobalRedisPreName.DRIVER_SCHEDULING_PRE;
/**
* 驾驶员信息Service业务层处理
*
* @author 古自健
* @date 2023-07-04
*/
@Service
public class DriverServiceImpl implements IDriverService {
@Value("${api.url.getSchedulingInfo}")
private String schedulingInfoUrl;
@Value("${api.config.password}")
private String password;
@Value("${api.config.nonce}")
private String nonce;
@Autowired
private DriverJob driverJob;
@Autowired
private ServerConfig serverConfig;
@Autowired
private DriverMapper driverMapper;
@Autowired
private RedisCache redisCache;
/**
* 查询驾驶员信息
*
* @param id 驾驶员信息主键
* @return 驾驶员信息
*/
@Override
public Driver selectDriverById(Long id) {
return driverMapper.selectDriverById(id);
}
/**
* 查询驾驶员信息列表
*
* @param driver 驾驶员信息
* @return 驾驶员信息
*/
@Override
public List<Driver> selectDriverList(Driver driver) {
return driverMapper.selectDriverList(driver);
}
/**
* 新增驾驶员信息
*
* @param driver 驾驶员信息
* @return 结果
*/
@Override
public int insertDriver(Driver driver) {
// 判断是否存在相同的工号数据 存在既更新
// Driver driverById = driverMapper.selectDriverById(driver.getId());
// if (driverById == null){
//
// }
return driverMapper.insertDriver(driver);
}
/**
* 修改驾驶员信息
*
* @param driver 驾驶员信息
* @return 结果
*/
@Override
public int updateDriver(Driver driver) {
return driverMapper.updateDriver(driver);
}
/**
* 批量删除驾驶员信息
*
* @param ids 需要删除的驾驶员信息主键
* @return 结果
*/
@Override
public int deleteDriverByIds(Long[] ids) {
return driverMapper.deleteDriverByIds(ids);
}
/**
* 删除驾驶员信息信息
*
* @param id 驾驶员信息主键
* @return 结果
*/
@Override
public int deleteDriverById(Long id) {
return driverMapper.deleteDriverById(id);
}
@Override
public void insertDrivers(List<Driver> driverList) {
driverMapper.saveDrivers(driverList);
}
@Override
public AjaxResult getDriverSchedulingInfo(String schedulingDate, String jobCode) {
String key = DRIVER_SCHEDULING_PRE + schedulingDate;
List<ResponseScheduling> cacheMapValue = redisCache.getCacheMapValue(key, jobCode);
// 优先从缓存中读取
if (cacheMapValue != null && cacheMapValue.size() > 0) {
return AjaxResult.success(cacheMapValue);
}
// 没有再次请求地址获取值
long timestamp = System.currentTimeMillis();
// 请求资源地址格式化
String getSchedulingInfoUrl = null;
try {
getSchedulingInfoUrl = String.format(schedulingInfoUrl, "99", schedulingDate, timestamp, nonce, password, driverJob.getSHA1(driverJob.getStringStringMap(String.valueOf(timestamp))));
} catch (Exception e) {
throw new RuntimeException(e);
}
Map<String, List<ResponseScheduling>> scheduling = driverJob.saveSchedulingToRedis(getSchedulingInfoUrl, Collections.emptyList(), schedulingDate);
// 数据库中读取所有接收到的排班信息
// List<ResponseScheduling> responseSchedulings = driverMapper.getDriverSchedulingList(schedulingDate, jobCode);
return AjaxResult.success(scheduling.get(jobCode));
}
@Override
public void saveDriverScheduling(List<ResponseScheduling> schedulings) {
List<List<ResponseScheduling>> lists = ListUtils.splitList(schedulings, 1000);
for (List<ResponseScheduling> responseSchedulings : lists) {
driverMapper.saveDriverScheduling(responseSchedulings);
}
}
@Override
public AjaxResult getDriverSchedulingAll() {
return AjaxResult.success(redisCache.getHashKeys(DRIVER_SCHEDULING_PRE + ConstDateUtil.formatDate("yyyyMMdd")));
}
@Override
public AjaxResult checkJobCode(Driver driver) {
Integer result = driverMapper.jobCodeIsEmpty(driver.getJobCode());
Map<String, Boolean> resultMap = new HashMap<>();
if (result > 0) {
resultMap.put("result", true);
} else {
resultMap.put("result", false);
}
return AjaxResult.success(resultMap);
}
@Override
public AjaxResult uploadImage(MultipartFile file) throws InvalidExtensionException, IOException {
// 上传文件路径
String baseUrl = RuoYiConfig.getUploadPath() + "/head/image";
// 上传并返回新文件名称
FileUploadUtils.assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
// 后期可以通过请求头拿到对应的工号
String fileName = FilenameUtils.getBaseName(file.getOriginalFilename()) + "." + FileUploadUtils.getExtension(file);
String absPath = FileUploadUtils.getAbsoluteFile(baseUrl, fileName).getAbsolutePath();
fileName = FileUploadUtils.getPathFileName(baseUrl, fileName);
file.transferTo(Paths.get(absPath));
String url = serverConfig.getUrl() + fileName;
AjaxResult ajax = AjaxResult.success();
ajax.put("url", url);
ajax.put("fileName", fileName);
ajax.put("newFileName", FileUtils.getName(fileName));
ajax.put("originalFilename", file.getOriginalFilename());
return ajax;
}
@Override
public void downloadHeadImage(String jobCode, HttpServletResponse response) {
File file = getLocationFile(jobCode);
ServletOutputStream ops = null;
try {
if (file.exists()) {
byte[] bytes = com.alibaba.excel.util.FileUtils.readFileToByteArray(file);
ops = response.getOutputStream();
ops.write(bytes);
ops.flush();
}
} catch (Exception e) {
throw new RuntimeException("download fail cause:" + e.getMessage());
} finally {
try {
if (ops != null) {
ops.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override
public List<Driver> getDrivers(Driver driver) {
// TODO 人事比对
List<Driver> drivers = driverMapper.getDrivers(driver);
return drivers;
}
@Override
public int addDriver(Driver driver) {
// TODO 人脸注册
return 0;
}
private File getLocationFile(String jobCode) {
String image = driverMapper.getDriverInfoByJobCode(jobCode);
return new File(RuoYiConfig.getProfile() + File.separator + image.replace("/profile", ""));
}
}