Authored by zhaoyue

ADD report location log

... ... @@ -75,6 +75,12 @@
<artifactId>joda-time</artifactId>
<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
... ...
... ... @@ -2,7 +2,7 @@ git pull
git add --all src/main/java/*
git add push.sh
git add pom.xml
git commit -m "ADD admin log interface"
git commit -m "ADD report location log"
#git push origin master
git push origin zhaoyue-dev4
git status
... ...
... ... @@ -12,10 +12,13 @@ import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import javax.servlet.http.HttpServletRequest;
/**
* 增加方法注入,将含有CurrentAdmin注解的方法参数注入当前登录用户
*
... ... @@ -41,10 +44,13 @@ public class CurrentAdminMethodArgumentResolver implements HandlerMethodArgument
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
//取出鉴权时存入的登录用户Id
String currentAdminId = ((String) webRequest.getAttribute(Constants.CURRENT_USER_ID, RequestAttributes.SCOPE_REQUEST)).
replace(UpSoftAccountController.UPSOFT_TOKEN_PREFIX, "").replace(AdminAccountController.USPIH_TOKEN_PREFIX, "").replace(Constants.ADMIN_TOKEN_PREFIX,"");
replace(UpSoftAccountController.UPSOFT_TOKEN_PREFIX, "").replace(AdminAccountController.USPIH_TOKEN_PREFIX, "").replace(Constants.ADMIN_TOKEN_PREFIX, "");
if (currentAdminId != null) {
//从数据库中查询并返回
XklAdminEntity admin = adminRepository.findByIdAndStatus(Long.parseLong(currentAdminId), Constants.STATUS_OK);
HttpServletRequest request = ((ServletWebRequest) webRequest).getRequest();
String ip = request.getRemoteAddr();
admin.setIp(ip);
return admin;
}
throw new MissingServletRequestPartException(Constants.CURRENT_USER_ID);
... ...
... ... @@ -17,6 +17,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* 上传报告及删除报告接口。
... ... @@ -51,9 +53,9 @@ public class UpSoftReportController {
@ApiImplicitParams({
@ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
})
public ResponseEntity<ResultModel> save(@CurrentAdmin XklAdminEntity admin, @RequestParam String json_report) {
public ResponseEntity<ResultModel> save( @CurrentAdmin XklAdminEntity admin, @RequestParam String json_report) {
// 其中json_report格式为上传软件上传上来的原始report格式。
return reportService.save(admin, json_report);
return reportService.save(admin, json_report);
}
@RequestMapping(method = RequestMethod.DELETE)
... ...
package com.xkl.domain;
/**
* Created by zhaoyue on 2017/4/15.
*/
public class Location {
private String country ;
private String province ;
private String city ;
private String district ;
public Location(String country, String province, String city, String district) {
this.country = country;
this.province = province;
this.city = city;
this.district = district;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
}
... ...
... ... @@ -49,6 +49,10 @@ public class XklAdminEntity {
@Column(name = "status")
private int status;
// 最近一次登录的ip
@Column(name = "ip")
private String ip;
public long getId() {
return id;
}
... ... @@ -120,4 +124,12 @@ public class XklAdminEntity {
public void setStatus(int status) {
this.status = status;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
... ...
package com.xkl.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Timestamp;
/**
* 报告的location
*/
@Entity
@Table(name = "xkl_amp_report_location")
public class XklAmpReportLocationEntity {
// record id
@Id
@Column(name = "id")
private long id;
// AMP序列号
@Column(name = "AMPSerial")
private String AMPSerial;
// adminId
@Column(name = "admin_id")
private long adminId;
// ip
@Column(name = "ip")
private String IP;
//
@Column(name = "country")
private String country;
//
@Column(name = "province")
private String province;
//
@Column(name = "city")
private String city;
//
@Column(name = "district")
private String district;
// 检测时间
@Column(name = "checkup_time")
private Timestamp checkupTime;
// 上传时间
@Column(name = "upload_time")
private Timestamp uploadTime;
public XklAmpReportLocationEntity(String AMPSerial, long adminId, String IP,
String country, String province, String city,
String district, Timestamp checkupTime, Timestamp uploadTime) {
this.AMPSerial = AMPSerial;
this.adminId = adminId;
this.IP = IP;
this.country = country;
this.province = province;
this.city = city;
this.district = district;
this.checkupTime = checkupTime;
this.uploadTime = uploadTime;
}
public String getAMPSerial() {
return AMPSerial;
}
public void setAMPSerial(String AMPSerial) {
this.AMPSerial = AMPSerial;
}
public long getAdminId() {
return adminId;
}
public void setAdminId(long adminId) {
this.adminId = adminId;
}
public String getIP() {
return IP;
}
public void setIP(String IP) {
this.IP = IP;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public Timestamp getCheckupTime() {
return checkupTime;
}
public void setCheckupTime(Timestamp checkupTime) {
this.checkupTime = checkupTime;
}
public Timestamp getUploadTime() {
return uploadTime;
}
public void setUploadTime(Timestamp uploadTime) {
this.uploadTime = uploadTime;
}
}
... ...
package com.xkl.repository;
import com.xkl.domain.XklAmpReportLocationEntity;
import org.springframework.data.repository.CrudRepository;
/**
* create by zhaoyue at 20170415
*/
public interface XklAmpReportLocationRespository extends CrudRepository<XklAmpReportLocationEntity, Long> {
}
... ...
... ... @@ -9,6 +9,7 @@ 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;
... ... @@ -38,6 +39,9 @@ public class ReportService implements IReportService {
private ReportResultRepository reportResultRepository;
@Autowired
private XklAmpReportLocationRespository xklAmpReportLocationRespository;
@Autowired
private UserRepository userRepository;
@Autowired
... ... @@ -218,7 +222,7 @@ public class ReportService implements IReportService {
}
}
} catch (Exception e) {
log.info("ERROR, JSON FROMAT ERROR!!!"+e);
log.info("ERROR, JSON FROMAT ERROR!!!" + e);
return null;
}
markItemStatus(sex, detailList);
... ... @@ -244,11 +248,27 @@ public class ReportService implements IReportService {
}
reportDetailRepository.save(report.getRpDetailList());
reportResultRepository.save(report.getBasicResList());
// 保存报告地理位置。
saveRpLocation2DB(report, admin);
scoreService.getScore(ampReport.getId());
return ampReport.getId();
}
/*
* 保存报告地理位置。
*/
private void saveRpLocation2DB(ReportData report, XklAdminEntity admin) {
String ip = admin.getIp();
Location location = IPTools.getLocationWithIP(ip);
if (location == null) return;
XklAmpReportLocationEntity rpLoc = new XklAmpReportLocationEntity(report.getAmpReport().getMachineNum(),
admin.getId(), 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) {
... ...
package com.xkl.tools;
import com.alibaba.fastjson.JSONObject;
import com.xkl.domain.Location;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
* Created by zhaoyue on 2017/4/15.
*/
public class IPTools {
public static final String sinaIPGetter = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&";
public static Location getLocationWithIP(String IP) {
try {
Map<String, String> params = new HashMap<String, String>();
params.put("ip",IP);
String url = sinaIPGetter + URLEncoder.encode(IP, "utf-8");
String content = HttpTools.requestByMap(url, "POST", params);
JSONObject ipJson = JSONObject.parseObject(content);
String country = ipJson.getString("country");
String province = ipJson.getString("province");
String city = ipJson.getString("city");
String district = ipJson.getString("district");
Location location = new Location(country, province, city, district);
return location;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}
... ...
package PACKAGE_NAME;
/**
* Created by zhaoyue on 2017/4/15.
*/
public class IPToolsTest {
}
... ...