Commit bde82a60213d4787537628fff5902ce1beac69b8

Authored by guzijian
1 parent 9c7fbffa

fix: 新增异步上传

ruoyi-admin/src/main/java/com/ruoyi/driver/controller/DriverController.java
@@ -125,4 +125,17 @@ public class DriverController extends BaseController { @@ -125,4 +125,17 @@ public class DriverController extends BaseController {
125 public AjaxResult remove(@ApiParam @PathVariable Long[] ids) { 125 public AjaxResult remove(@ApiParam @PathVariable Long[] ids) {
126 return toAjax(driverService.deleteDriverByIds(ids)); 126 return toAjax(driverService.deleteDriverByIds(ids));
127 } 127 }
  128 +
  129 + /**
  130 + * 校验工号
  131 + * @param jobCode
  132 + * @return
  133 + */
  134 + @GetMapping("/check/{jobCode}")
  135 + @ApiOperation("校验jobCode是否存入当前系统")
  136 + public AjaxResult checkJobCode(@PathVariable("jobCode") String jobCode){
  137 + return driverService.checkJobCode(jobCode);
  138 + }
  139 +
  140 +// @ApiOperation("保存员工大头像")
128 } 141 }
ruoyi-admin/src/main/java/com/ruoyi/driver/mapper/DriverMapper.java
@@ -85,4 +85,11 @@ public interface DriverMapper @@ -85,4 +85,11 @@ public interface DriverMapper
85 * @return 85 * @return
86 */ 86 */
87 List<Driver> getNameByJobCode(@Param("jobCodes") Set<String> jobCodes); 87 List<Driver> getNameByJobCode(@Param("jobCodes") Set<String> jobCodes);
  88 +
  89 + /**
  90 + * 是否存在对应工号在数据库中
  91 + * @param jobCode
  92 + * @return
  93 + */
  94 + Integer jobCodeIsEmpty(@Param("jobCode") String jobCode);
88 } 95 }
ruoyi-admin/src/main/java/com/ruoyi/driver/service/IDriverService.java
@@ -75,4 +75,5 @@ public interface IDriverService @@ -75,4 +75,5 @@ public interface IDriverService
75 75
76 AjaxResult getDriverSchedulingAll(); 76 AjaxResult getDriverSchedulingAll();
77 77
  78 + AjaxResult checkJobCode(String jobCode);
78 } 79 }
ruoyi-admin/src/main/java/com/ruoyi/driver/service/impl/DriverServiceImpl.java
@@ -109,7 +109,7 @@ public class DriverServiceImpl implements IDriverService { @@ -109,7 +109,7 @@ public class DriverServiceImpl implements IDriverService {
109 @Override 109 @Override
110 public AjaxResult getDriverSchedulingInfo(String schedulingDate, String jobCode) { 110 public AjaxResult getDriverSchedulingInfo(String schedulingDate, String jobCode) {
111 String key = DRIVER_SCHEDULING_PRE + schedulingDate; 111 String key = DRIVER_SCHEDULING_PRE + schedulingDate;
112 - List<ResponseScheduling> cacheMapValue = redisCache.getCacheMapValue(key , jobCode); 112 + List<ResponseScheduling> cacheMapValue = redisCache.getCacheMapValue(key, jobCode);
113 // 优先从缓存中读取 113 // 优先从缓存中读取
114 if (cacheMapValue != null && cacheMapValue.size() > 0) { 114 if (cacheMapValue != null && cacheMapValue.size() > 0) {
115 return AjaxResult.success(cacheMapValue); 115 return AjaxResult.success(cacheMapValue);
@@ -131,4 +131,16 @@ public class DriverServiceImpl implements IDriverService { @@ -131,4 +131,16 @@ public class DriverServiceImpl implements IDriverService {
131 public AjaxResult getDriverSchedulingAll() { 131 public AjaxResult getDriverSchedulingAll() {
132 return AjaxResult.success(redisCache.getHashKeys(DRIVER_SCHEDULING_PRE + ConstDateUtil.formatDate("yyyyMMdd"))); 132 return AjaxResult.success(redisCache.getHashKeys(DRIVER_SCHEDULING_PRE + ConstDateUtil.formatDate("yyyyMMdd")));
133 } 133 }
  134 +
  135 + @Override
  136 + public AjaxResult checkJobCode(String jobCode) {
  137 + Integer result = driverMapper.jobCodeIsEmpty(jobCode);
  138 + Map<String, Boolean> resultMap = new HashMap<>();
  139 + if (result > 0) {
  140 + resultMap.put("result", true);
  141 + } else {
  142 + resultMap.put("result", false);
  143 + }
  144 + return AjaxResult.success(resultMap);
  145 + }
134 } 146 }
ruoyi-admin/src/main/java/com/ruoyi/in/domain/SignIn.java
@@ -30,6 +30,18 @@ public class SignIn extends BaseEntity @@ -30,6 +30,18 @@ public class SignIn extends BaseEntity
30 @ApiModelProperty("工号") 30 @ApiModelProperty("工号")
31 private String jobCode; 31 private String jobCode;
32 32
  33 + @Excel(name = "设备号")
  34 + @ApiModelProperty("设备号")
  35 + private String deviceId;
  36 +
  37 + public String getDeviceId() {
  38 + return deviceId;
  39 + }
  40 +
  41 + public void setDeviceId(String deviceId) {
  42 + this.deviceId = deviceId;
  43 + }
  44 +
33 /** 设备地址 */ 45 /** 设备地址 */
34 @Excel(name = "设备地址") 46 @Excel(name = "设备地址")
35 @ApiModelProperty("设备ip 不用填写") 47 @ApiModelProperty("设备ip 不用填写")
ruoyi-admin/src/main/java/com/ruoyi/in/service/impl/SignInServiceImpl.java
@@ -17,6 +17,7 @@ import com.ruoyi.common.utils.StringUtils; @@ -17,6 +17,7 @@ import com.ruoyi.common.utils.StringUtils;
17 import com.ruoyi.common.utils.ip.IpUtils; 17 import com.ruoyi.common.utils.ip.IpUtils;
18 import com.ruoyi.common.utils.uuid.Seq; 18 import com.ruoyi.common.utils.uuid.Seq;
19 import com.ruoyi.common.utils.uuid.UUID; 19 import com.ruoyi.common.utils.uuid.UUID;
  20 +import com.ruoyi.service.ThreadJobService;
20 import org.apache.commons.io.FilenameUtils; 21 import org.apache.commons.io.FilenameUtils;
21 import org.springframework.beans.factory.annotation.Autowired; 22 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.stereotype.Service; 23 import org.springframework.stereotype.Service;
@@ -25,6 +26,8 @@ import com.ruoyi.in.domain.SignIn; @@ -25,6 +26,8 @@ import com.ruoyi.in.domain.SignIn;
25 import com.ruoyi.in.service.ISignInService; 26 import com.ruoyi.in.service.ISignInService;
26 import sun.misc.BASE64Decoder; 27 import sun.misc.BASE64Decoder;
27 28
  29 +import javax.annotation.Resource;
  30 +
28 /** 31 /**
29 * 签到Service业务层处理 32 * 签到Service业务层处理
30 * 33 *
@@ -36,6 +39,9 @@ public class SignInServiceImpl implements ISignInService { @@ -36,6 +39,9 @@ public class SignInServiceImpl implements ISignInService {
36 @Autowired 39 @Autowired
37 private SignInMapper signInMapper; 40 private SignInMapper signInMapper;
38 41
  42 + @Resource
  43 + private ThreadJobService threadJobConfig;
  44 +
39 /** 45 /**
40 * 查询签到 46 * 查询签到
41 * 47 *
@@ -123,12 +129,12 @@ public class SignInServiceImpl implements ISignInService { @@ -123,12 +129,12 @@ public class SignInServiceImpl implements ISignInService {
123 fileName = extractFilename(fileName); 129 fileName = extractFilename(fileName);
124 // 获取相对路径 130 // 获取相对路径
125 String absPath = getAbsoluteFile(filePath, fileName).getAbsolutePath(); 131 String absPath = getAbsoluteFile(filePath, fileName).getAbsolutePath();
126 - // 上传文件  
127 - startUpload(absPath, base64); 132 +
128 // 获取文件上传路径 133 // 获取文件上传路径
129 String pathFileName = getPathFileName(filePath, fileName); 134 String pathFileName = getPathFileName(filePath, fileName);
130 signIn.setImage(pathFileName); 135 signIn.setImage(pathFileName);
131 - 136 + // 异步上传文件
  137 + threadJobConfig.startUpload(absPath,base64);
132 return signInMapper.insertSignIn(signIn); 138 return signInMapper.insertSignIn(signIn);
133 } 139 }
134 140
@@ -174,35 +180,6 @@ public class SignInServiceImpl implements ISignInService { @@ -174,35 +180,6 @@ public class SignInServiceImpl implements ISignInService {
174 return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName; 180 return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
175 } 181 }
176 182
177 - public void startUpload(String url, String base64) throws FileUploadException, IOException {  
178 - FileOutputStream outputStream = null;  
179 - base64 = base64.replaceAll(" ", "");  
180 -// byte[] imageBytes = Base64.getDecoder().decode(base64);  
181 - BASE64Decoder decoder = new BASE64Decoder();  
182 - byte[] photoBase64 = decoder.decodeBuffer(base64);  
183 - for (int i = 0; i < photoBase64.length; ++i) {  
184 - //调整异常数据  
185 - if (photoBase64[i] < 0) {  
186 - photoBase64[i] += 256;  
187 - }  
188 - }  
189 - try {  
190 - outputStream = new FileOutputStream(url);  
191 - outputStream.write(photoBase64);  
192 - outputStream.flush();  
193 - } catch (Exception e) {  
194 - throw new FileUploadException("文件上传异常");  
195 - } finally {  
196 - try {  
197 - if (!Objects.isNull(outputStream)) {  
198 - outputStream.close();  
199 - }  
200 - } catch (IOException e) {  
201 - throw new RuntimeException(e);  
202 - }  
203 - }  
204 - }  
205 -  
206 /** 183 /**
207 * 检查文件类型 184 * 检查文件类型
208 * 185 *
ruoyi-admin/src/main/java/com/ruoyi/service/ThreadJobService.java 0 → 100644
  1 +package com.ruoyi.service;
  2 +
  3 +
  4 +import com.ruoyi.common.config.RuoYiConfig;
  5 +import com.ruoyi.common.constant.Constants;
  6 +import com.ruoyi.common.exception.file.FileUploadException;
  7 +import com.ruoyi.common.utils.DateUtils;
  8 +import com.ruoyi.common.utils.StringUtils;
  9 +import com.ruoyi.common.utils.uuid.Seq;
  10 +import com.ruoyi.common.utils.uuid.UUID;
  11 +import com.ruoyi.in.mapper.SignInMapper;
  12 +import lombok.extern.slf4j.Slf4j;
  13 +import org.apache.commons.io.FilenameUtils;
  14 +import org.springframework.beans.factory.annotation.Autowired;
  15 +import org.springframework.scheduling.annotation.Async;
  16 +import org.springframework.scheduling.annotation.EnableAsync;
  17 +import org.springframework.stereotype.Component;
  18 +import sun.misc.BASE64Decoder;
  19 +
  20 +import java.io.File;
  21 +import java.io.FileOutputStream;
  22 +import java.io.IOException;
  23 +import java.util.Base64;
  24 +import java.util.Objects;
  25 +
  26 +
  27 +/**
  28 + * 多线程任务
  29 + *
  30 + * @author 20412
  31 + */
  32 +@EnableAsync
  33 +@Component
  34 +@Slf4j
  35 +public class ThreadJobService {
  36 +
  37 +
  38 + /**
  39 + * 异步上传图片
  40 + *
  41 + * @param url
  42 + * @param base64
  43 + * @throws FileUploadException
  44 + * @throws IOException
  45 + */
  46 + @Async
  47 + public void startUpload(String url, String base64) {
  48 + FileOutputStream outputStream = null;
  49 + base64 = base64.replaceAll(" ", "");
  50 +// byte[] imageBytes = Base64.getDecoder().decode(base64);
  51 + BASE64Decoder decoder = new BASE64Decoder();
  52 + try {
  53 + byte[] photoBase64 = decoder.decodeBuffer(base64);
  54 + for (int i = 0; i < photoBase64.length; ++i) {
  55 + //调整异常数据
  56 + if (photoBase64[i] < 0) {
  57 + photoBase64[i] += 256;
  58 + }
  59 + }
  60 + outputStream = new FileOutputStream(url);
  61 + outputStream.write(photoBase64);
  62 + outputStream.flush();
  63 + } catch (Exception e) {
  64 + log.error("文件上传异常:{}", e.getMessage());
  65 + } finally {
  66 + try {
  67 + if (!Objects.isNull(outputStream)) {
  68 + outputStream.close();
  69 + }
  70 + } catch (IOException e) {
  71 + throw new RuntimeException(e);
  72 + }
  73 + }
  74 + }
  75 +
  76 + /**
  77 + * 检查文件类型
  78 + *
  79 + * @param base64ImgData
  80 + * @return
  81 + */
  82 + public static String checkImageBase64Format(String base64ImgData) {
  83 + byte[] b = Base64.getDecoder().decode(base64ImgData);
  84 + String type = "";
  85 + if (0x424D == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
  86 + type = "bmp";
  87 + } else if (0x8950 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
  88 + type = "png";
  89 + } else {
  90 + type = "jpg";
  91 + }
  92 +// else if (0xFFD8 == ((b[0] & 0xff) << 8 | (b[1] & 0xff))) {
  93 +// type = "jpg";
  94 +// }
  95 + return type;
  96 + }
  97 +
  98 + /**
  99 + * 获取相对路径名
  100 + *
  101 + * @param uploadDir
  102 + * @param fileName
  103 + * @return
  104 + * @throws IOException
  105 + */
  106 + public File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
  107 + File desc = new File(uploadDir + File.separator + fileName);
  108 +
  109 + if (!desc.exists()) {
  110 + if (!desc.getParentFile().exists()) {
  111 + desc.getParentFile().mkdirs();
  112 + }
  113 + }
  114 + return desc;
  115 + }
  116 +
  117 + /**
  118 + * 获取扩展文件名
  119 + *
  120 + * @param extendFileName
  121 + * @return
  122 + */
  123 + public String extractFilename(String extendFileName) {
  124 + return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
  125 + FilenameUtils.getBaseName(UUID.randomUUID().toString().replace("-", "")), Seq.getId(Seq.uploadSeqType), extendFileName);
  126 + }
  127 +
  128 + /**
  129 + * @param uploadDir
  130 + * @param fileName
  131 + * @return
  132 + * @throws IOException
  133 + */
  134 + public String getPathFileName(String uploadDir, String fileName) throws IOException {
  135 + int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
  136 + String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
  137 + return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
  138 + }
  139 +
  140 +}
ruoyi-admin/src/main/resources/mapper/driver/DriverMapper.xml
@@ -99,6 +99,11 @@ @@ -99,6 +99,11 @@
99 99
100 </where> 100 </where>
101 </select> 101 </select>
  102 + <select id="jobCodeIsEmpty" resultType="java.lang.Integer">
  103 + select count(*)
  104 + from driver
  105 + where job_code = #{jobCode}
  106 + </select>
102 107
103 <insert id="insertDriver" parameterType="Driver" useGeneratedKeys="true" keyProperty="id"> 108 <insert id="insertDriver" parameterType="Driver" useGeneratedKeys="true" keyProperty="id">
104 insert into driver 109 insert into driver
ruoyi-admin/src/main/resources/mapper/in/SignInMapper.xml
@@ -8,6 +8,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; @@ -8,6 +8,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
8 <result property="id" column="id" /> 8 <result property="id" column="id" />
9 <result property="createTime" column="create_time" /> 9 <result property="createTime" column="create_time" />
10 <result property="jobCode" column="jobCode" /> 10 <result property="jobCode" column="jobCode" />
  11 + <result property="deviceId" column="device_id" />
11 <result property="ip" column="ip" /> 12 <result property="ip" column="ip" />
12 <result property="image" column="image" /> 13 <result property="image" column="image" />
13 <result property="status" column="status" /> 14 <result property="status" column="status" />
@@ -21,7 +22,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; @@ -21,7 +22,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
21 </resultMap> 22 </resultMap>
22 23
23 <sql id="selectSignInVo"> 24 <sql id="selectSignInVo">
24 - select id, create_time, jobCode, ip,image, status, update_by, singn_in, alcohol_flag, type, update_time, alcohol_intake, remark from sign_in 25 + select id, create_time, jobCode, device_id,ip,image, status, update_by, singn_in, alcohol_flag, type, update_time, alcohol_intake, remark from sign_in
25 </sql> 26 </sql>
26 27
27 <select id="selectSignInList" parameterType="SignIn" resultMap="SignInResult"> 28 <select id="selectSignInList" parameterType="SignIn" resultMap="SignInResult">
@@ -92,12 +93,13 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; @@ -92,12 +93,13 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
92 <if test="createTime != null">create_time,</if> 93 <if test="createTime != null">create_time,</if>
93 <if test="jobCode != null">jobCode,</if> 94 <if test="jobCode != null">jobCode,</if>
94 <if test="ip != null">ip,</if> 95 <if test="ip != null">ip,</if>
  96 + <if test="deviceId != null">device_id,</if>
95 <if test="image != null">image,</if> 97 <if test="image != null">image,</if>
96 <if test="status != null">status,</if> 98 <if test="status != null">status,</if>
97 <if test="updateBy != null">update_by,</if> 99 <if test="updateBy != null">update_by,</if>
98 <if test="singnIn != null">singn_in,</if> 100 <if test="singnIn != null">singn_in,</if>
99 <if test="alcoholFlag != null">alcohol_flag,</if> 101 <if test="alcoholFlag != null">alcohol_flag,</if>
100 - <if test="type != null">type,</if> 102 + <if test="type != null">`type`,</if>
101 <if test="updateTime != null">update_time,</if> 103 <if test="updateTime != null">update_time,</if>
102 <if test="alcoholIntake != null">alcohol_intake,</if> 104 <if test="alcoholIntake != null">alcohol_intake,</if>
103 <if test="remark != null">remark,</if> 105 <if test="remark != null">remark,</if>
@@ -106,6 +108,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; @@ -106,6 +108,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
106 <if test="createTime != null">#{createTime},</if> 108 <if test="createTime != null">#{createTime},</if>
107 <if test="jobCode != null">#{jobCode},</if> 109 <if test="jobCode != null">#{jobCode},</if>
108 <if test="ip != null">#{ip},</if> 110 <if test="ip != null">#{ip},</if>
  111 + <if test="deviceId != null">#{deviceId},</if>
109 <if test="image != null">#{image},</if> 112 <if test="image != null">#{image},</if>
110 <if test="status != null">#{status},</if> 113 <if test="status != null">#{status},</if>
111 <if test="updateBy != null">#{updateBy},</if> 114 <if test="updateBy != null">#{updateBy},</if>
@@ -124,6 +127,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot; @@ -124,6 +127,7 @@ PUBLIC &quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;
124 <if test="createTime != null">create_time = #{createTime},</if> 127 <if test="createTime != null">create_time = #{createTime},</if>
125 <if test="jobCode != null">jobCode = #{jobCode},</if> 128 <if test="jobCode != null">jobCode = #{jobCode},</if>
126 <if test="ip != null">ip = #{ip},</if> 129 <if test="ip != null">ip = #{ip},</if>
  130 + <if test="deviceId != null">device_id = #{deviceId},</if>
127 <if test="image != null">image = #{image},</if> 131 <if test="image != null">image = #{image},</if>
128 <if test="status != null">status = #{status},</if> 132 <if test="status != null">status = #{status},</if>
129 <if test="updateBy != null">update_by = #{updateBy},</if> 133 <if test="updateBy != null">update_by = #{updateBy},</if>