ReportService.java
12.7 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package com.xkl.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.SecurityTool;
import com.xkl.tools.IPTools;
import lombok.extern.apachecommons.CommonsLog;
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.stereotype.Service;
import java.sql.Timestamp;
import java.util.*;
/**
* Created by zhao yue on 2016/11/13.
*/
@Service
@CommonsLog
public class ReportService implements IReportService {
@Autowired
private ReportMetaItemsRepository reportMetaItemsRepository;
@Autowired
private ReportRepository reportRepository;
@Autowired
private ReportDetailRepository reportDetailRepository;
@Autowired
private ReportResultRepository reportResultRepository;
@Autowired
private XklAmpReportLocationRespository xklAmpReportLocationRespository;
@Autowired
private UserRepository userRepository;
@Autowired
private IScoreService scoreService;
// 存储报告相关md5,防止重复上传已存在报告,防止重复上传错误报告。
private RedisTemplate<String, String> redis;
@Autowired
public void setRedis(RedisTemplate redis) {
this.redis = redis;
}
private static Map<Integer, XklAmpReportMetaItemsEntity> rpMetaItemMap = new HashMap<Integer, XklAmpReportMetaItemsEntity>();
/*
* 存储报告
*/
public ResponseEntity<ResultModel> save(XklAdminEntity admin, String json_report) {
// 验证存在性
String reportMd5 = SecurityTool.encode("MD5", json_report);
// 验证是无对应的会员,rediskey
String reportWithNoUser = reportMd5 + "Member";
// 验证报告格式有问题,rediskey
String reportWrongFormat = reportMd5 + "Format";
/*
* 如果已经处理过的报告,不再进行处理。
*/
XklAmpReportEntity report = reportRepository.findByMd5AndStatus(reportMd5, Constants.STATUS_OK);
if (report != null && report.getStatus() > 0) {
// 返回,报告已存在。
return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_EXISTED_ERROR, new ReportIdModel(report.getId())), HttpStatus.OK);
} else if (redis.hasKey(reportWithNoUser)) {
// 返回,报告对应会员不存在。
return new ResponseEntity<>(ResultModel.error(ResultStatus.INVALID_USER_ERROR), HttpStatus.OK);
} else if (redis.hasKey(reportWrongFormat)) {
// 返回,报告格式有问题。
return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_FORMAT_ERROR), HttpStatus.OK);
}
/*
* 解析报告数据
*/
ReportData reportData = parseReport(json_report, reportMd5);
/*
* 检验报告格式
*/
if (reportData == null) {
redis.boundValueOps(reportWrongFormat).set("");
// 返回,报告格式有问题。
return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_FORMAT_ERROR), HttpStatus.OK);
}
/*
* 检验会员存在性
*/
User user = userRepository.findByLoginAccountAndStatus(reportData.getAmpReport().getAccountStr(), Constants.STATUS_OK2);
if (user == null) {
redis.boundValueOps(reportWithNoUser).set("");
// 返回,报告对应会员不存在。
return new ResponseEntity<>(ResultModel.error(ResultStatus.INVALID_USER_ERROR), HttpStatus.OK);
}
/*
* 存储报告
*/
long reportId = save2DB(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.OK);
}
}
/*
* 操作员删除报告,只能删除该操作员自己创建的报告。
*/
public ResponseEntity<ResultModel> delete(XklAdminEntity admin, long report_id) {
// 1. 得到report,验证报告存在性
XklAmpReportEntity report = reportRepository.findById(report_id);
if (report == null || report.getStatus() == 0) {
// 报告不存在,返回
return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_INVALID_ERROR), HttpStatus.OK);
}
// 2. 验证admin
if (report.getCreateBy() != admin.getId()) {
// 非此admin创建,不能删除,返回
return new ResponseEntity<>(ResultModel.error(ResultStatus.INVALID_ADMIN_RPDEL_ERROR), HttpStatus.OK);
}
// 3. 删除report和detail,返回ok
reportRepository.delete(report);
List<XklAmpReportDetailEntity> detailList = reportDetailRepository.findByReportId(report.getId());
reportDetailRepository.delete(detailList);
List<XklAmpReportResultEntity> rpResList = reportResultRepository.findByReportId(report.getId());
reportResultRepository.delete(rpResList);
return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
}
/*
验证md5
获取report
获取detail
评判detail
验证member
获取admin
*/
// 需要程喆增加 title,account,machine_num字段 String; 修改set字段为int,0男,1女。125项目,改为float类型。
//
private ReportData parseReport(String reportJson, String md5) {
ReportData reportData = new ReportData();
XklAmpReportEntity ampReport = new XklAmpReportEntity();
List<XklAmpReportDetailEntity> detailList = new ArrayList<>();
List<XklAmpReportResultEntity> basicResList = new ArrayList<>();
int sex;
int age;
/*
* 2. 获取report基础信息
*/
try {
log.info("INFO, got json:" + reportJson);
JSONObject rpJson = JSONObject.parseObject(reportJson);
sex = rpJson.getInteger("sex").intValue();
age = rpJson.getInteger("age").intValue();
if (sex != Constants.FEMALE && sex != Constants.MALE) {
return null;
}
if (age < 0 || age > 127) {
return null;
}
ampReport.setReport(rpJson.getString("name"),
rpJson.getString("title"),/// "AMP快速无创身心健康评估报告",
new Timestamp(rpJson.getLong("report_date")),
new Timestamp(System.currentTimeMillis()),
rpJson.getString("account").trim(),///
rpJson.getInteger("sex").byteValue(),///
rpJson.getInteger("age").byteValue(),
rpJson.getInteger("weight").intValue(),
rpJson.getInteger("pulse").intValue(),
rpJson.getInteger("respiratory_rate").intValue(),
rpJson.getDouble("atmospheric_pressure").doubleValue(),
rpJson.getDouble("LCA").doubleValue(),
rpJson.getDouble("RCA").doubleValue(),
rpJson.getDouble("LAC").doubleValue(),
rpJson.getDouble("RAC").doubleValue(),
rpJson.getDouble("ABD").doubleValue(),
rpJson.getDouble("total").doubleValue(),
rpJson.getInteger("stable").intValue(),
md5, rpJson.getString("machine_num"),
rpJson.getString("basic_result"),
rpJson.getString("T0"),
rpJson.getString("T1"),
rpJson.getString("T2"),
rpJson.getString("T3"),
rpJson.getString("T4")
);
/*
* 3. 获取detail信息,id,float类型
*/
JSONObject rpDetails = rpJson.getJSONObject("detail");
for (int item_id = 1; item_id <= 125; item_id++) {
float val = rpDetails.getFloat(String.valueOf(item_id)).floatValue();
XklAmpReportDetailEntity detail = new XklAmpReportDetailEntity();
detail.setItemValue(val);
detail.setItemId(item_id);
detailList.add(detail);
}
/*
* 4. 获取诊断结论编号
*/
JSONArray basicResIdsArr = rpJson.getJSONArray("basic_result_ids");
if (basicResIdsArr != null && basicResIdsArr.size() > 0) {
for (int resId = 0; resId < basicResIdsArr.size(); resId++) {
XklAmpReportResultEntity rpRes = new XklAmpReportResultEntity();
rpRes.setResultItemId(basicResIdsArr.getInteger(resId));
basicResList.add(rpRes);
}
}
} catch (Exception e) {
log.info("ERROR, JSON FROMAT ERROR!!!" + e);
return null;
}
markItemStatus(sex, detailList);
reportData.setAmpReport(ampReport);
reportData.setRpDetailList(detailList);
reportData.setBasicResList(basicResList);
return reportData;
}
/*
* 存储报告
*/
private long save2DB(ReportData report, XklAdminEntity admin, User user) {
report.getAmpReport().setCreateBy(admin.getId());
report.getAmpReport().setCompanyId((long) admin.getCoid());
report.getAmpReport().setMemberId((long) user.getMemberId());
XklAmpReportEntity ampReport = reportRepository.save(report.getAmpReport());
for (XklAmpReportDetailEntity detail : report.getRpDetailList()) {
detail.setReportId(ampReport.getId());
}
for (XklAmpReportResultEntity rpRes : report.getBasicResList()) {
rpRes.setReportId(ampReport.getId());
}
reportDetailRepository.save(report.getRpDetailList());
reportResultRepository.save(report.getBasicResList());
// 保存报告地理位置。
saveRpLocation2DB(report, ampReport.getId(), admin);
// 健康评分
scoreService.getScore(ampReport.getId());
return ampReport.getId();
}
/*
* 保存报告地理位置。
*/
private void saveRpLocation2DB(ReportData report, long reportId, XklAdminEntity admin) {
String ip = admin.getIp();
Location location = IPTools.getLocationWithIP(ip);
if (location == null) return;
XklAmpReportLocationEntity rpLoc = new XklAmpReportLocationEntity(report.getAmpReport().getMachineNum(),
admin.getId(), reportId, ip, location.getCountry(), location.getProvince(), location.getCity(), location.getDistrict(),
report.getAmpReport().getCheckTime(), report.getAmpReport().getUptime());
xklAmpReportLocationRespository.save(rpLoc);
}
/*
* 判断detail是正常,高于标准或低于标准。
*/
private void markItemStatus(int sex, List<XklAmpReportDetailEntity> detailList) {
// load ReportMetaItems into memory.
synchronized (this) {
if (rpMetaItemMap.size() == 0) {
Iterator<XklAmpReportMetaItemsEntity> rpMetaIter = reportMetaItemsRepository.findAll().iterator();
while (rpMetaIter.hasNext()) {
XklAmpReportMetaItemsEntity rpMetaItem = rpMetaIter.next();
rpMetaItemMap.put(rpMetaItem.getItemId(), rpMetaItem);
}
}
}
// mark status
for (XklAmpReportDetailEntity detail : detailList) {
double lowSt;
double highSt;
// get standard
if (sex == Constants.MALE) { // male
lowSt = rpMetaItemMap.get(detail.getItemId()).getStandardLowMale();
highSt = rpMetaItemMap.get(detail.getItemId()).getStandardHighMale();
} else { // female
lowSt = rpMetaItemMap.get(detail.getItemId()).getStandardLowFemale();
highSt = rpMetaItemMap.get(detail.getItemId()).getStandardHighFemale();
}
int status;
if (detail.getItemValue() < lowSt) {
status = Constants.LOWER;
} else if (detail.getItemValue() > highSt) {
status = Constants.HIGHER;
} else {
status = Constants.NORMAL;
}
detail.setStatus(status);
}
}
}