ReportController.java
5.99 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
package com.xkl.controller.uploadsoft;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.authorization.annotation.Authorization;
import com.xkl.authorization.annotation.CurrentAdmin;
import com.xkl.config.Constants;
import com.xkl.config.ResultStatus;
import com.xkl.domain.*;
import com.xkl.model.ReportIdModel;
import com.xkl.model.ResultModel;
import com.xkl.repository.*;
import com.xkl.security.AntiXSS;
import com.xkl.security.SecurityTool;
import com.xkl.service.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 上传报告及删除报告接口。
*/
@RestController
@RequestMapping("/report")
public class ReportController {
@Autowired
private UpSoftVersionRepository upSoftVersionRepository;
@Autowired
private IReportService reportService;
@Autowired
private ReportRepository reportRepository;
@Autowired
private ReportDetailRepository reportDetailRepository;
// 存储报告相关md5,防止重复上传已存在报告,防止重复上传错误报告。
private RedisTemplate<String, String> redis;
@Autowired
public void setRedis(RedisTemplate redis) {
this.redis = redis;
}
@Autowired
private UserRepository userRepository;
@Autowired
private AdminRepository adminRepository;
@RequestMapping(method = RequestMethod.POST)
@AntiXSS
@Authorization
@ApiOperation(value = "上传并存储报告")
@ApiImplicitParams({
@ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
})
public ResponseEntity<ResultModel> save(@CurrentAdmin Admin admin, @RequestParam String json_report) {
// 验证存在性
String reportMd5 = SecurityTool.encode("MD5", json_report);
// 验证是否有对应的会员
String reportWithNoUser = reportMd5 + "Member";
// 验证报告格式是否有问题
String reportWrongFormat = reportMd5 + "Format";
/*
* 如果已经处理过的报告,不再进行处理。
*/
AMPReport report = reportRepository.findByMd5(reportMd5);
if (report != null && report.getStatus() > 0) {
// 返回,报告已存在。
return new ResponseEntity<>(ResultModel.ok(new ReportIdModel(report.getId())), HttpStatus.OK);
} else if (redis.hasKey(reportWithNoUser)) {
// 返回,报告对应会员不存在。
return new ResponseEntity<>(ResultModel.error(ResultStatus.INVALID_USER_ERROR), HttpStatus.NOT_FOUND);
} else if (redis.hasKey(reportWrongFormat)) {
// 返回,报告格式有问题。
return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_FORMAT_ERROR), HttpStatus.NOT_FOUND);
}
/*
* 解析报告数据
*/
ReportData reportData = reportService.parseReport(json_report, reportMd5);
/*
* 检验报告格式
*/
if (reportData == null) {
redis.boundValueOps(reportWrongFormat).set("");
// 返回,报告格式有问题。
return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_FORMAT_ERROR), HttpStatus.NOT_FOUND);
}
/*
* 检验会员存在性
*/
User user = userRepository.findByLoginAccount(reportData.getAmpReport().getAccount_str());
if (user == null) {
redis.boundValueOps(reportWithNoUser).set("");
// 返回,报告对应会员不存在。
return new ResponseEntity<>(ResultModel.error(ResultStatus.INVALID_USER_ERROR), HttpStatus.NOT_FOUND);
}
/*
* 存储报告
*/
int reportId = reportService.saveReport(reportData, admin, user);
if (reportId > 0) {
// 返回,报告存储成功,报告id
return new ResponseEntity<>(ResultModel.ok(new ReportIdModel(reportId)), HttpStatus.OK);
} else {
// 返回,服务器存储问题。
return new ResponseEntity<>(ResultModel.error(ResultStatus.DB_ERROR), HttpStatus.NOT_FOUND);
}
}
@RequestMapping(method = RequestMethod.DELETE)
@AntiXSS
@Authorization
@ApiOperation(value = "删除报告")
@ApiImplicitParams({
@ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
})
public ResponseEntity<ResultModel> delete(@CurrentAdmin Admin admin, @RequestParam long report_id) {
// 1. 得到report,验证报告存在性
AMPReport report = reportRepository.findById((int) report_id);
if (report == null) {
// 报告不存在,返回
return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_INVALID__ERROR), HttpStatus.NOT_FOUND);
}
// 2. 验证admin
if (report.getCreate_by() != admin.getId()) {
// 非此admin创建,不能删除,返回
return new ResponseEntity<>(ResultModel.error(ResultStatus.INVALID_ADMIN_RPDEL_ERROR), HttpStatus.NOT_FOUND);
}
// 3. 删除report和detail,返回ok
reportRepository.delete(report);
List<AMPReportDetail> detailList = reportDetailRepository.findByReportId(report.getId());
reportDetailRepository.delete(detailList);
return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
}
}