Commit 03d2bc11f5254d8ac9f648af76ba9b5e26268aa2
1 parent
06128cdc
SNManage commit
Showing
7 changed files
with
113 additions
and
36 deletions
pom.xml
| @@ -52,6 +52,11 @@ | @@ -52,6 +52,11 @@ | ||
| 52 | <artifactId>hutool-http</artifactId> | 52 | <artifactId>hutool-http</artifactId> | 
| 53 | <version>4.1.14</version> | 53 | <version>4.1.14</version> | 
| 54 | </dependency> | 54 | </dependency> | 
| 55 | + <!--validation--> | ||
| 56 | + <dependency> | ||
| 57 | + <groupId>org.springframework.boot</groupId> | ||
| 58 | + <artifactId>spring-boot-starter-validation</artifactId> | ||
| 59 | + </dependency> | ||
| 55 | <dependency> | 60 | <dependency> | 
| 56 | <groupId>org.projectlombok</groupId> | 61 | <groupId>org.projectlombok</groupId> | 
| 57 | <artifactId>lombok</artifactId> | 62 | <artifactId>lombok</artifactId> | 
src/main/java/com/example/demo/controller/SNController.java
| 1 | package com.example.demo.controller; | 1 | package com.example.demo.controller; | 
| 2 | 2 | ||
| 3 | +import com.example.demo.dto.AddSNsDTO; | ||
| 4 | +import com.example.demo.dto.DeleteSNsDTO; | ||
| 3 | import com.example.demo.exception.SNRepetitiveException; | 5 | import com.example.demo.exception.SNRepetitiveException; | 
| 4 | import com.example.demo.service.SNService; | 6 | import com.example.demo.service.SNService; | 
| 5 | import com.example.demo.util.HttpResult; | 7 | import com.example.demo.util.HttpResult; | 
| @@ -13,32 +15,34 @@ import org.springframework.web.bind.annotation.*; | @@ -13,32 +15,34 @@ import org.springframework.web.bind.annotation.*; | ||
| 13 | 15 | ||
| 14 | import javax.servlet.http.HttpServletRequest; | 16 | import javax.servlet.http.HttpServletRequest; | 
| 15 | import javax.servlet.http.HttpServletResponse; | 17 | import javax.servlet.http.HttpServletResponse; | 
| 18 | +import javax.validation.Valid; | ||
| 16 | import java.io.IOException; | 19 | import java.io.IOException; | 
| 17 | 20 | ||
| 18 | @Api(tags = "SN操作") | 21 | @Api(tags = "SN操作") | 
| 19 | -@Controller | 22 | +@RestController | 
| 23 | +@RequestMapping("/sn") | ||
| 20 | public class SNController { | 24 | public class SNController { | 
| 21 | static Logger logger = LogManager.getLogger(SNController.class); | 25 | static Logger logger = LogManager.getLogger(SNController.class); | 
| 22 | 26 | ||
| 23 | @Autowired | 27 | @Autowired | 
| 24 | SNService snService; | 28 | SNService snService; | 
| 25 | 29 | ||
| 26 | - @ApiOperation("首页") | ||
| 27 | - @GetMapping("/index.html") | ||
| 28 | - public String index() { | ||
| 29 | - return "Index"; | ||
| 30 | - } | 30 | +// @ApiOperation("首页") | 
| 31 | +// @GetMapping("/index.html") | ||
| 32 | +// public String index() { | ||
| 33 | +// return "Index"; | ||
| 34 | +// } | ||
| 31 | 35 | ||
| 32 | @ApiOperation("添加SN") | 36 | @ApiOperation("添加SN") | 
| 33 | - @PostMapping("/add_SN") | 37 | + @PostMapping("/add_sn") | 
| 34 | @ResponseBody | 38 | @ResponseBody | 
| 35 | - public HttpResult addSN(@RequestBody String SNDto) { | 39 | + public HttpResult addSN(@RequestBody @Valid AddSNsDTO addSNsDTO) { | 
| 36 | 40 | ||
| 37 | try { | 41 | try { | 
| 38 | - snService.addSN(SNDto); | 42 | + snService.addSN(addSNsDTO); | 
| 39 | return HttpResult.success("添加成功"); | 43 | return HttpResult.success("添加成功"); | 
| 40 | } catch (SNRepetitiveException e) { | 44 | } catch (SNRepetitiveException e) { | 
| 41 | - return HttpResult.fail("SN已存在,请勿重复添加!"); | 45 | + return HttpResult.fail(e.getMessage()); | 
| 42 | } catch (Exception e) { | 46 | } catch (Exception e) { | 
| 43 | logger.error(e.getMessage()); | 47 | logger.error(e.getMessage()); | 
| 44 | return HttpResult.fail(); | 48 | return HttpResult.fail(); | 
| @@ -60,9 +64,9 @@ public class SNController { | @@ -60,9 +64,9 @@ public class SNController { | ||
| 60 | // } | 64 | // } | 
| 61 | 65 | ||
| 62 | @ApiOperation("获取全部base64加密后的SN") | 66 | @ApiOperation("获取全部base64加密后的SN") | 
| 63 | - @GetMapping("/get_SNs") | 67 | + @GetMapping("/get_sn") | 
| 64 | @ResponseBody | 68 | @ResponseBody | 
| 65 | - public HttpResult<String> getBase64SNs() throws IOException { | 69 | + public HttpResult<String> getBase64SNs() { | 
| 66 | try { | 70 | try { | 
| 67 | return HttpResult.success("",snService.getEncryptSNs()); | 71 | return HttpResult.success("",snService.getEncryptSNs()); | 
| 68 | } catch (Exception e) { | 72 | } catch (Exception e) { | 
| @@ -73,12 +77,12 @@ public class SNController { | @@ -73,12 +77,12 @@ public class SNController { | ||
| 73 | } | 77 | } | 
| 74 | 78 | ||
| 75 | @ApiOperation("删除指定SN") | 79 | @ApiOperation("删除指定SN") | 
| 76 | - @PostMapping("/delete_SN") | 80 | + @PostMapping("/delete_sn") | 
| 77 | @ResponseBody | 81 | @ResponseBody | 
| 78 | - public HttpResult deleteSN(@RequestBody String SN) { | 82 | + public HttpResult deleteSN(@RequestBody @Valid DeleteSNsDTO deleteSNsDTO) { | 
| 79 | try { | 83 | try { | 
| 80 | - snService.deleteSN(SN); | ||
| 81 | - return HttpResult.success("SN已删除或不存在!"); | 84 | + snService.deleteSN(deleteSNsDTO); | 
| 85 | + return HttpResult.success("删除成功!"); | ||
| 82 | } | 86 | } | 
| 83 | catch (Exception e) { | 87 | catch (Exception e) { | 
| 84 | logger.error(e.getMessage()); | 88 | logger.error(e.getMessage()); | 
src/main/java/com/example/demo/dto/AddSNsDTO.java
0 → 100644
| 1 | +package com.example.demo.dto; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import io.swagger.annotations.ApiModel; | ||
| 5 | +import io.swagger.annotations.ApiModelProperty; | ||
| 6 | +import lombok.Data; | ||
| 7 | + | ||
| 8 | +import javax.validation.constraints.NotBlank; | ||
| 9 | +import javax.validation.constraints.NotNull; | ||
| 10 | +import java.util.ArrayList; | ||
| 11 | +import java.util.List; | ||
| 12 | + | ||
| 13 | +@Data | ||
| 14 | +@ApiModel(description = "删除SNs入参") | ||
| 15 | +public class AddSNsDTO { | ||
| 16 | + | ||
| 17 | + @ApiModelProperty(value = "项目名称") | ||
| 18 | + @NotBlank(message = "项目名称不能为空!") | ||
| 19 | + private String projectName; | ||
| 20 | + | ||
| 21 | + @ApiModelProperty(value = "设备码") | ||
| 22 | + @NotNull(message = "设备码不能为空!") | ||
| 23 | + private ArrayList<String> sns; | ||
| 24 | +} | 
src/main/java/com/example/demo/dto/DeleteSNsDTO.java
0 → 100644
| 1 | +package com.example.demo.dto; | ||
| 2 | + | ||
| 3 | +import io.swagger.annotations.ApiModel; | ||
| 4 | +import io.swagger.annotations.ApiModelProperty; | ||
| 5 | +import lombok.Data; | ||
| 6 | + | ||
| 7 | +import javax.validation.constraints.NotNull; | ||
| 8 | +import java.util.ArrayList; | ||
| 9 | +import java.util.List; | ||
| 10 | + | ||
| 11 | +@Data | ||
| 12 | +@ApiModel(description = "删除SNs入参") | ||
| 13 | +public class DeleteSNsDTO { | ||
| 14 | + @ApiModelProperty(value = "设备码") | ||
| 15 | + @NotNull(message = "设备码不能为空!") | ||
| 16 | + private ArrayList<String> sns; | ||
| 17 | +} | 
src/main/java/com/example/demo/service/SNService.java
| 1 | package com.example.demo.service; | 1 | package com.example.demo.service; | 
| 2 | 2 | ||
| 3 | +import com.example.demo.dto.AddSNsDTO; | ||
| 4 | +import com.example.demo.dto.DeleteSNsDTO; | ||
| 3 | import com.example.demo.exception.SNRepetitiveException; | 5 | import com.example.demo.exception.SNRepetitiveException; | 
| 4 | 6 | ||
| 5 | import javax.servlet.http.HttpServletRequest; | 7 | import javax.servlet.http.HttpServletRequest; | 
| @@ -10,9 +12,9 @@ import java.io.IOException; | @@ -10,9 +12,9 @@ import java.io.IOException; | ||
| 10 | public interface SNService { | 12 | public interface SNService { | 
| 11 | /** | 13 | /** | 
| 12 | * 新增SN号 | 14 | * 新增SN号 | 
| 13 | - * @param SN SN号 | 15 | + * @param addSNsDTO SN号 | 
| 14 | */ | 16 | */ | 
| 15 | - void addSN(String SN) throws SNRepetitiveException, IOException; | 17 | + void addSN(AddSNsDTO addSNsDTO) throws SNRepetitiveException, IOException; | 
| 16 | 18 | ||
| 17 | /** | 19 | /** | 
| 18 | * 下载所有SN号 | 20 | * 下载所有SN号 | 
| @@ -27,8 +29,8 @@ public interface SNService { | @@ -27,8 +29,8 @@ public interface SNService { | ||
| 27 | 29 | ||
| 28 | /** | 30 | /** | 
| 29 | * 删除指定SN记录 | 31 | * 删除指定SN记录 | 
| 30 | - * @param SN SN号 | 32 | + * @param deleteSNsDTO SN号 | 
| 31 | */ | 33 | */ | 
| 32 | - void deleteSN(String SN) throws Exception; | 34 | + void deleteSN(DeleteSNsDTO deleteSNsDTO) throws Exception; | 
| 33 | 35 | ||
| 34 | } | 36 | } | 
src/main/java/com/example/demo/service/impl/SNServiceImpl.java
| 1 | package com.example.demo.service.impl; | 1 | package com.example.demo.service.impl; | 
| 2 | 2 | ||
| 3 | import cn.hutool.core.codec.Base64; | 3 | import cn.hutool.core.codec.Base64; | 
| 4 | +import com.example.demo.controller.SNController; | ||
| 5 | +import com.example.demo.dto.AddSNsDTO; | ||
| 6 | +import com.example.demo.dto.DeleteSNsDTO; | ||
| 4 | import com.example.demo.exception.SNRepetitiveException; | 7 | import com.example.demo.exception.SNRepetitiveException; | 
| 5 | import com.example.demo.service.SNService; | 8 | import com.example.demo.service.SNService; | 
| 9 | +import org.apache.logging.log4j.LogManager; | ||
| 10 | +import org.apache.logging.log4j.Logger; | ||
| 6 | import org.springframework.beans.factory.annotation.Value; | 11 | import org.springframework.beans.factory.annotation.Value; | 
| 7 | import org.springframework.stereotype.Service; | 12 | import org.springframework.stereotype.Service; | 
| 8 | 13 | ||
| 9 | import javax.servlet.http.HttpServletRequest; | 14 | import javax.servlet.http.HttpServletRequest; | 
| 10 | import javax.servlet.http.HttpServletResponse; | 15 | import javax.servlet.http.HttpServletResponse; | 
| 11 | import java.io.*; | 16 | import java.io.*; | 
| 17 | +import java.text.SimpleDateFormat; | ||
| 18 | +import java.util.Date; | ||
| 12 | import java.util.HashSet; | 19 | import java.util.HashSet; | 
| 13 | import java.util.Set; | 20 | import java.util.Set; | 
| 14 | import java.util.concurrent.locks.Lock; | 21 | import java.util.concurrent.locks.Lock; | 
| @@ -21,6 +28,8 @@ public class SNServiceImpl implements SNService { | @@ -21,6 +28,8 @@ public class SNServiceImpl implements SNService { | ||
| 21 | */ | 28 | */ | 
| 22 | private static final Lock lock = new ReentrantLock(); | 29 | private static final Lock lock = new ReentrantLock(); | 
| 23 | 30 | ||
| 31 | + static Logger logger = LogManager.getLogger(SNServiceImpl.class); | ||
| 32 | + | ||
| 24 | @Value("${SN.file.path}") | 33 | @Value("${SN.file.path}") | 
| 25 | String SNFilePath; | 34 | String SNFilePath; | 
| 26 | 35 | ||
| @@ -28,12 +37,16 @@ public class SNServiceImpl implements SNService { | @@ -28,12 +37,16 @@ public class SNServiceImpl implements SNService { | ||
| 28 | String SNTempFilePath; | 37 | String SNTempFilePath; | 
| 29 | 38 | ||
| 30 | @Override | 39 | @Override | 
| 31 | - public void addSN(String SNDto) throws SNRepetitiveException, IOException { | 40 | + public void addSN(AddSNsDTO addSNsDTO) throws SNRepetitiveException, IOException { | 
| 41 | + | ||
| 32 | //加try,catch只为释放锁 | 42 | //加try,catch只为释放锁 | 
| 33 | try { | 43 | try { | 
| 34 | //加锁 | 44 | //加锁 | 
| 35 | lock.lock(); | 45 | lock.lock(); | 
| 36 | - | 46 | + logger.info("-----------------------------开始添加SNs-----------------------------"); | 
| 47 | + StringBuilder SNs = new StringBuilder(); | ||
| 48 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
| 49 | + String currTime = sdf.format(new Date()); | ||
| 37 | //读取文件,判断SN是否重复 | 50 | //读取文件,判断SN是否重复 | 
| 38 | Set<String> set = new HashSet<>(); | 51 | Set<String> set = new HashSet<>(); | 
| 39 | BufferedReader bufferedReader = new BufferedReader(new FileReader(SNFilePath)); | 52 | BufferedReader bufferedReader = new BufferedReader(new FileReader(SNFilePath)); | 
| @@ -44,17 +57,21 @@ public class SNServiceImpl implements SNService { | @@ -44,17 +57,21 @@ public class SNServiceImpl implements SNService { | ||
| 44 | String SN = br.substring(0,index == -1? 0 : index); | 57 | String SN = br.substring(0,index == -1? 0 : index); | 
| 45 | set.add(SN); | 58 | set.add(SN); | 
| 46 | } | 59 | } | 
| 47 | - | ||
| 48 | - String addSn = SNDto.substring(0,SNDto.indexOf('#')); | ||
| 49 | - if (set.contains(addSn)) { | ||
| 50 | - throw new SNRepetitiveException("SN:" + addSn + " 已存在,请勿重复添加"); | 60 | + for (String SN : addSNsDTO.getSns()) { | 
| 61 | + if (set.contains(SN)) { | ||
| 62 | + throw new SNRepetitiveException("SN:" + SN + " 已存在,请勿重复添加"); | ||
| 63 | + } | ||
| 64 | + SNs.append(SN).append("##").append(addSNsDTO.getProjectName()).append("##").append(currTime); | ||
| 65 | + SNs.append(System.lineSeparator()); | ||
| 51 | } | 66 | } | 
| 52 | 67 | ||
| 68 | + | ||
| 53 | //写入 | 69 | //写入 | 
| 54 | FileWriter fileWriter = new FileWriter(SNFilePath,true); | 70 | FileWriter fileWriter = new FileWriter(SNFilePath,true); | 
| 55 | - fileWriter.write(SNDto+System.lineSeparator()); | 71 | + fileWriter.write(SNs.toString()); | 
| 56 | fileWriter.flush(); | 72 | fileWriter.flush(); | 
| 57 | fileWriter.close(); | 73 | fileWriter.close(); | 
| 74 | + logger.info("-----------------------------结束添加SNs-----------------------------"); | ||
| 58 | } finally { //释放锁 | 75 | } finally { //释放锁 | 
| 59 | lock.unlock(); | 76 | lock.unlock(); | 
| 60 | } | 77 | } | 
| @@ -74,23 +91,29 @@ public class SNServiceImpl implements SNService { | @@ -74,23 +91,29 @@ public class SNServiceImpl implements SNService { | ||
| 74 | } | 91 | } | 
| 75 | 92 | ||
| 76 | @Override | 93 | @Override | 
| 77 | - public void deleteSN(String SN) throws Exception{ | 94 | + public void deleteSN(DeleteSNsDTO deleteSNsDTO) throws Exception{ | 
| 78 | try { | 95 | try { | 
| 79 | lock.lock(); | 96 | lock.lock(); | 
| 97 | + logger.info("-----------------------------开始删除SNs-----------------------------"); | ||
| 98 | + | ||
| 99 | + HashSet<String> deleteSNsSet = new HashSet<>(deleteSNsDTO.getSns()); | ||
| 80 | 100 | ||
| 81 | //读取文件,记录不用删除的数据 | 101 | //读取文件,记录不用删除的数据 | 
| 82 | BufferedReader bufferedReader = new BufferedReader(new FileReader(SNFilePath)); | 102 | BufferedReader bufferedReader = new BufferedReader(new FileReader(SNFilePath)); | 
| 83 | String br; | 103 | String br; | 
| 84 | StringBuilder stringBuilder = new StringBuilder(); | 104 | StringBuilder stringBuilder = new StringBuilder(); | 
| 105 | + StringBuilder existSNs = new StringBuilder(); | ||
| 106 | + //所要删除的SN是否存在 | ||
| 85 | boolean SNExist = false; | 107 | boolean SNExist = false; | 
| 86 | while ((br=bufferedReader.readLine()) != null) { | 108 | while ((br=bufferedReader.readLine()) != null) { | 
| 87 | //截取SN | 109 | //截取SN | 
| 88 | int index = br.indexOf('#'); | 110 | int index = br.indexOf('#'); | 
| 89 | String str = br.substring(0,index == -1? 0 : index); | 111 | String str = br.substring(0,index == -1? 0 : index); | 
| 90 | - if (!SN.equals(str)) { | 112 | + if (!deleteSNsSet.contains(str)) { | 
| 91 | stringBuilder.append(br); | 113 | stringBuilder.append(br); | 
| 92 | stringBuilder.append(System.lineSeparator()); | 114 | stringBuilder.append(System.lineSeparator()); | 
| 93 | } else { | 115 | } else { | 
| 116 | + existSNs.append(str).append("、"); | ||
| 94 | SNExist = true; | 117 | SNExist = true; | 
| 95 | } | 118 | } | 
| 96 | } | 119 | } | 
| @@ -110,6 +133,8 @@ public class SNServiceImpl implements SNService { | @@ -110,6 +133,8 @@ public class SNServiceImpl implements SNService { | ||
| 110 | SNfileWriter.close(); | 133 | SNfileWriter.close(); | 
| 111 | 134 | ||
| 112 | } | 135 | } | 
| 136 | + logger.warn("成功删除SNs:{}",existSNs); | ||
| 137 | + logger.info("-----------------------------结束删除SNs-----------------------------"); | ||
| 113 | 138 | ||
| 114 | 139 | ||
| 115 | } finally { | 140 | } finally { | 
src/main/resources/application.properties
| @@ -10,13 +10,13 @@ logging.config=classpath:log4j2.xml | @@ -10,13 +10,13 @@ logging.config=classpath:log4j2.xml | ||
| 10 | #swagger.basic.username = test | 10 | #swagger.basic.username = test | 
| 11 | #swagger.basic.password = 123 | 11 | #swagger.basic.password = 123 | 
| 12 | 12 | ||
| 13 | -spring.thymeleaf.cache=false | ||
| 14 | -spring.thymeleaf.prefix=classpath:/templates/ | ||
| 15 | -spring.thymeleaf.suffix=.html | 13 | +#spring.thymeleaf.cache=false | 
| 14 | +#spring.thymeleaf.prefix=classpath:/templates/ | ||
| 15 | +#spring.thymeleaf.suffix=.html | ||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | -#SN.file.path=src/main/resources/sn/SN.txt | ||
| 19 | -#SN.file.temp.path=src/main/resources/sn/SNTemp.txt | 18 | +SN.file.path=src/main/resources/sn/SN.txt | 
| 19 | +SN.file.temp.path=src/main/resources/sn/SNTemp.txt | ||
| 20 | 20 | ||
| 21 | -SN.file.path= | ||
| 22 | -SN.file.temp.path= | ||
| 23 | \ No newline at end of file | 21 | \ No newline at end of file | 
| 22 | +#SN.file.path= | ||
| 23 | +#SN.file.temp.path= | ||
| 24 | \ No newline at end of file | 24 | \ No newline at end of file |