Authored by zhaoyue

Merge branch 'zhaoyue-dev2' into 'master'

MOD move some code from controller to service



See merge request !4
... ... @@ -25,7 +25,6 @@ public interface Constants {
*/
String AUTHORIZATION = "authorization";
/**
* 单项打分标准
*/
... ...
... ... @@ -27,7 +27,7 @@ public class SwaggerConfig {
@Bean
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(new ApiInfo("Spring RESTful Authorization Demo Api",
.apiInfo(new ApiInfo("xkl RESTful Api",
null, null, null, null, null)).
//将Timestamp类型全部转为Long类型
directModelSubstitute(Timestamp.class, Long.class);
... ...
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.wordnik.swagger.annotations.*;
import com.xkl.authorization.annotation.Authorization;
import com.xkl.authorization.annotation.CurrentAdmin;
import com.xkl.authorization.annotation.CurrentUser;
import com.xkl.authorization.manager.ITokenManager;
import com.xkl.authorization.model.TokenModel;
import com.xkl.config.Constants;
import com.xkl.config.ResultStatus;
import com.xkl.domain.AMPMachine;
import com.xkl.domain.Admin;
import com.xkl.model.ResultModel;
import com.xkl.repository.AMPMachineRepository;
import com.xkl.repository.AdminRepository;
import org.hibernate.validator.constraints.SafeHtml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
... ... @@ -28,7 +28,8 @@ import org.springframework.web.bind.annotation.RestController;
* 获取和删除token的请求地址,在Restful设计中其实就对应着登录和退出登录的资源映射
*/
@RestController
@RequestMapping("/upsoftaccount")
@Api("AMP报告上传软件客户端登录及退出接口")
@RequestMapping("/upsoft/account")
public class UpSoftAccountController {
@Autowired
... ... @@ -41,7 +42,8 @@ public class UpSoftAccountController {
private static final String UPSOFT_TOKEN_PREFIX = "UPSOFT_TOKEN";
@RequestMapping(method = RequestMethod.POST)
@ApiOperation(value = "报告上传软件登录")
@ApiOperation(value = "报告上传软件登录", notes = "login")
public ResponseEntity<ResultModel> login(@RequestParam String account, @RequestParam String password
, @RequestParam String ampserial, @RequestParam String ampkey) {
Assert.notNull(account, "account can not be empty");
... ... @@ -72,7 +74,7 @@ public class UpSoftAccountController {
@RequestMapping(method = RequestMethod.DELETE)
@Authorization
@ApiOperation(value = "报告上传软件退出登录")
@ApiOperation(value = "报告上传软件注销")
@ApiImplicitParams({
@ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
})
... ... @@ -81,9 +83,9 @@ public class UpSoftAccountController {
return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
}
@RequestMapping(value = "/modpwd", method = RequestMethod.PUT)
@RequestMapping(value = "/upsoft/modpwd", method = RequestMethod.PUT)
@Authorization
@ApiOperation(value = "报告上传软件修改用户密码")
@ApiOperation(value = "报告上传软件修改密码")
@ApiImplicitParams({
@ApiImplicitParam(name = "authorization", value = "请以如下格式输入登录返回信息:adminId_tokens", required = true, dataType = "string", paramType = "header"),
})
... ...
... ... @@ -30,7 +30,7 @@ import java.util.List;
* 上传报告及删除报告接口。
*/
@RestController
@RequestMapping("/upsoftreport")
@RequestMapping("/upsoft/report")
public class UpSoftReportController {
@Autowired
... ... @@ -45,13 +45,6 @@ public class UpSoftReportController {
@Autowired
private ReportDetailRepository reportDetailRepository;
// 存储报告相关md5,防止重复上传已存在报告,防止重复上传错误报告。
private RedisTemplate<String, String> redis;
@Autowired
public void setRedis(RedisTemplate redis) {
this.redis = redis;
}
@Autowired
private UserRepository userRepository;
... ... @@ -67,59 +60,8 @@ public class UpSoftReportController {
@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);
}
// 其中json_report格式为上传软件上传上来的原始report格式。
return reportService.save(admin, json_report);
}
@RequestMapping(method = RequestMethod.DELETE)
... ... @@ -130,24 +72,7 @@ public class UpSoftReportController {
@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);
return reportService.delete(admin, report_id);
}
}
... ...
... ... @@ -21,8 +21,8 @@ import java.util.List;
* AMP报告上传软件客户端获取最新软件版本。
*/
@RestController
@RequestMapping("/upsoftversion")
public class SoftVersionController {
@RequestMapping("/upsoft/version")
public class UpSoftVerController {
@Autowired
private UpSoftVersionRepository upSoftVersionRepository;
... ...
... ... @@ -3,12 +3,21 @@ package com.xkl.service;
import com.xkl.domain.Admin;
import com.xkl.domain.ReportData;
import com.xkl.domain.User;
import com.xkl.model.ResultModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Created by zhao yue on 2016/11/26.
*/
public interface IReportService {
public ReportData parseReport(String reportJson, String md5);
public int saveReport(ReportData report, Admin admin, User user);
// public ReportData parseReport(String reportJson, String md5);
// public int saveReport(ReportData report, Admin admin, User user);
public ResponseEntity<ResultModel> delete(Admin admin, long report_id);
public ResponseEntity<ResultModel> save(Admin admin, String json_report);
}
... ...
... ... @@ -2,11 +2,19 @@ package com.xkl.service;
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.ReportDetailRepository;
import com.xkl.repository.ReportMetaItemsRepository;
import com.xkl.repository.ReportRepository;
import com.xkl.repository.UserRepository;
import com.xkl.security.SecurityTool;
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;
... ... @@ -19,15 +27,109 @@ import java.util.*;
public class ReportService implements IReportService {
@Autowired
private ReportMetaItemsRepository reportMetaItemsRepository;
@Autowired
private ReportRepository reportRepository;
@Autowired
private ReportDetailRepository reportDetailRepository;
@Autowired
private UserRepository userRepository;
// 存储报告相关md5,防止重复上传已存在报告,防止重复上传错误报告。
private RedisTemplate<String, String> redis;
@Autowired
public void setRedis(RedisTemplate redis) {
this.redis = redis;
}
private static Map<Integer, ReportMetaItem> rpMetaItemMap = new HashMap<Integer, ReportMetaItem>();
/*
* 存储报告
*/
public ResponseEntity<ResultModel> save(Admin admin, 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 = 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 = 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.NOT_FOUND);
}
}
/*
* 操作员删除报告,只能删除该操作员自己创建的报告。
*/
public ResponseEntity<ResultModel> delete(Admin admin, 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);
}
/*
验证md5
获取report
获取detail
... ... @@ -37,7 +139,7 @@ public class ReportService implements IReportService {
*/
// 需要程喆增加 title,account,machine_num字段 String; 修改set字段为int,0男,1女。
// 125项目,改为float类型。
public ReportData parseReport(String reportJson, String md5) {
private ReportData parseReport(String reportJson, String md5) {
ReportData reportData = new ReportData();
AMPReport ampReport = new AMPReport();
List<AMPReportDetail> detailList = new ArrayList<>();
... ... @@ -91,7 +193,7 @@ public class ReportService implements IReportService {
/*
* 存储报告
*/
public int saveReport(ReportData report, Admin admin, User user) {
private int save2DB(ReportData report, Admin admin, User user) {
report.getAmpReport().setCreate_by((int) admin.getId());
report.getAmpReport().setCompany_id(admin.getCoid());
report.getAmpReport().setMember_id(user.getMemberId());
... ...