Commit 03d2bc11f5254d8ac9f648af76ba9b5e26268aa2

Authored by 岑健浩
1 parent 06128cdc

SNManage commit

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