Authored by zhaoyue

Merge branch 'zhaoyue-dev4' into 'master'

ADD report location log



See merge request !43
@@ -75,6 +75,12 @@ @@ -75,6 +75,12 @@
75 <artifactId>joda-time</artifactId> 75 <artifactId>joda-time</artifactId>
76 <version>2.4</version> 76 <version>2.4</version>
77 </dependency> 77 </dependency>
  78 + <!-- https://mvnrepository.com/artifact/junit/junit -->
  79 + <dependency>
  80 + <groupId>junit</groupId>
  81 + <artifactId>junit</artifactId>
  82 + <version>4.12</version>
  83 + </dependency>
78 </dependencies> 84 </dependencies>
79 85
80 <build> 86 <build>
@@ -2,7 +2,7 @@ git pull @@ -2,7 +2,7 @@ git pull
2 git add --all src/main/java/* 2 git add --all src/main/java/*
3 git add push.sh 3 git add push.sh
4 git add pom.xml 4 git add pom.xml
5 -git commit -m "ADD admin log interface" 5 +git commit -m "ADD report location log"
6 #git push origin master 6 #git push origin master
7 git push origin zhaoyue-dev4 7 git push origin zhaoyue-dev4
8 git status 8 git status
@@ -12,10 +12,13 @@ import org.springframework.stereotype.Component; @@ -12,10 +12,13 @@ import org.springframework.stereotype.Component;
12 import org.springframework.web.bind.support.WebDataBinderFactory; 12 import org.springframework.web.bind.support.WebDataBinderFactory;
13 import org.springframework.web.context.request.NativeWebRequest; 13 import org.springframework.web.context.request.NativeWebRequest;
14 import org.springframework.web.context.request.RequestAttributes; 14 import org.springframework.web.context.request.RequestAttributes;
  15 +import org.springframework.web.context.request.ServletWebRequest;
15 import org.springframework.web.method.support.HandlerMethodArgumentResolver; 16 import org.springframework.web.method.support.HandlerMethodArgumentResolver;
16 import org.springframework.web.method.support.ModelAndViewContainer; 17 import org.springframework.web.method.support.ModelAndViewContainer;
17 import org.springframework.web.multipart.support.MissingServletRequestPartException; 18 import org.springframework.web.multipart.support.MissingServletRequestPartException;
18 19
  20 +import javax.servlet.http.HttpServletRequest;
  21 +
19 /** 22 /**
20 * 增加方法注入,将含有CurrentAdmin注解的方法参数注入当前登录用户 23 * 增加方法注入,将含有CurrentAdmin注解的方法参数注入当前登录用户
21 * 24 *
@@ -41,10 +44,13 @@ public class CurrentAdminMethodArgumentResolver implements HandlerMethodArgument @@ -41,10 +44,13 @@ public class CurrentAdminMethodArgumentResolver implements HandlerMethodArgument
41 public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { 44 public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
42 //取出鉴权时存入的登录用户Id 45 //取出鉴权时存入的登录用户Id
43 String currentAdminId = ((String) webRequest.getAttribute(Constants.CURRENT_USER_ID, RequestAttributes.SCOPE_REQUEST)). 46 String currentAdminId = ((String) webRequest.getAttribute(Constants.CURRENT_USER_ID, RequestAttributes.SCOPE_REQUEST)).
44 - replace(UpSoftAccountController.UPSOFT_TOKEN_PREFIX, "").replace(AdminAccountController.USPIH_TOKEN_PREFIX, "").replace(Constants.ADMIN_TOKEN_PREFIX,""); 47 + replace(UpSoftAccountController.UPSOFT_TOKEN_PREFIX, "").replace(AdminAccountController.USPIH_TOKEN_PREFIX, "").replace(Constants.ADMIN_TOKEN_PREFIX, "");
45 if (currentAdminId != null) { 48 if (currentAdminId != null) {
46 //从数据库中查询并返回 49 //从数据库中查询并返回
47 XklAdminEntity admin = adminRepository.findByIdAndStatus(Long.parseLong(currentAdminId), Constants.STATUS_OK); 50 XklAdminEntity admin = adminRepository.findByIdAndStatus(Long.parseLong(currentAdminId), Constants.STATUS_OK);
  51 + HttpServletRequest request = ((ServletWebRequest) webRequest).getRequest();
  52 + String ip = request.getRemoteAddr();
  53 + admin.setIp(ip);
48 return admin; 54 return admin;
49 } 55 }
50 throw new MissingServletRequestPartException(Constants.CURRENT_USER_ID); 56 throw new MissingServletRequestPartException(Constants.CURRENT_USER_ID);
@@ -17,6 +17,8 @@ import org.springframework.web.bind.annotation.RequestMethod; @@ -17,6 +17,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
17 import org.springframework.web.bind.annotation.RequestParam; 17 import org.springframework.web.bind.annotation.RequestParam;
18 import org.springframework.web.bind.annotation.RestController; 18 import org.springframework.web.bind.annotation.RestController;
19 19
  20 +import javax.servlet.http.HttpServletRequest;
  21 +
20 22
21 /** 23 /**
22 * 上传报告及删除报告接口。 24 * 上传报告及删除报告接口。
@@ -51,7 +53,7 @@ public class UpSoftReportController { @@ -51,7 +53,7 @@ public class UpSoftReportController {
51 @ApiImplicitParams({ 53 @ApiImplicitParams({
52 @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"), 54 @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
53 }) 55 })
54 - public ResponseEntity<ResultModel> save(@CurrentAdmin XklAdminEntity admin, @RequestParam String json_report) { 56 + public ResponseEntity<ResultModel> save( @CurrentAdmin XklAdminEntity admin, @RequestParam String json_report) {
55 // 其中json_report格式为上传软件上传上来的原始report格式。 57 // 其中json_report格式为上传软件上传上来的原始report格式。
56 return reportService.save(admin, json_report); 58 return reportService.save(admin, json_report);
57 } 59 }
  1 +package com.xkl.domain;
  2 +
  3 +/**
  4 + * Created by zhaoyue on 2017/4/15.
  5 + */
  6 +public class Location {
  7 + private String country ;
  8 + private String province ;
  9 + private String city ;
  10 + private String district ;
  11 +
  12 + public Location(String country, String province, String city, String district) {
  13 + this.country = country;
  14 + this.province = province;
  15 + this.city = city;
  16 + this.district = district;
  17 + }
  18 +
  19 + public String getCountry() {
  20 + return country;
  21 + }
  22 +
  23 + public void setCountry(String country) {
  24 + this.country = country;
  25 + }
  26 +
  27 + public String getProvince() {
  28 + return province;
  29 + }
  30 +
  31 + public void setProvince(String province) {
  32 + this.province = province;
  33 + }
  34 +
  35 + public String getCity() {
  36 + return city;
  37 + }
  38 +
  39 + public void setCity(String city) {
  40 + this.city = city;
  41 + }
  42 +
  43 + public String getDistrict() {
  44 + return district;
  45 + }
  46 +
  47 + public void setDistrict(String district) {
  48 + this.district = district;
  49 + }
  50 +}
@@ -49,6 +49,10 @@ public class XklAdminEntity { @@ -49,6 +49,10 @@ public class XklAdminEntity {
49 @Column(name = "status") 49 @Column(name = "status")
50 private int status; 50 private int status;
51 51
  52 + // 最近一次登录的ip
  53 + @Column(name = "ip")
  54 + private String ip;
  55 +
52 public long getId() { 56 public long getId() {
53 return id; 57 return id;
54 } 58 }
@@ -120,4 +124,12 @@ public class XklAdminEntity { @@ -120,4 +124,12 @@ public class XklAdminEntity {
120 public void setStatus(int status) { 124 public void setStatus(int status) {
121 this.status = status; 125 this.status = status;
122 } 126 }
  127 +
  128 + public String getIp() {
  129 + return ip;
  130 + }
  131 +
  132 + public void setIp(String ip) {
  133 + this.ip = ip;
  134 + }
123 } 135 }
  1 +package com.xkl.domain;
  2 +
  3 +import javax.persistence.Column;
  4 +import javax.persistence.Entity;
  5 +import javax.persistence.Id;
  6 +import javax.persistence.Table;
  7 +import java.sql.Timestamp;
  8 +
  9 +/**
  10 + * 报告的location
  11 + */
  12 +@Entity
  13 +@Table(name = "xkl_amp_report_location")
  14 +public class XklAmpReportLocationEntity {
  15 + // record id
  16 + @Id
  17 + @Column(name = "id")
  18 + private long id;
  19 +
  20 + // AMP序列号
  21 + @Column(name = "AMPSerial")
  22 + private String AMPSerial;
  23 +
  24 + // adminId
  25 + @Column(name = "admin_id")
  26 + private long adminId;
  27 +
  28 + // ip
  29 + @Column(name = "ip")
  30 + private String IP;
  31 +
  32 + //
  33 + @Column(name = "country")
  34 + private String country;
  35 +
  36 + //
  37 + @Column(name = "province")
  38 + private String province;
  39 +
  40 + //
  41 + @Column(name = "city")
  42 + private String city;
  43 +
  44 + //
  45 + @Column(name = "district")
  46 + private String district;
  47 +
  48 +
  49 + // 检测时间
  50 + @Column(name = "checkup_time")
  51 + private Timestamp checkupTime;
  52 +
  53 + // 上传时间
  54 + @Column(name = "upload_time")
  55 + private Timestamp uploadTime;
  56 +
  57 + public XklAmpReportLocationEntity(String AMPSerial, long adminId, String IP,
  58 + String country, String province, String city,
  59 + String district, Timestamp checkupTime, Timestamp uploadTime) {
  60 + this.AMPSerial = AMPSerial;
  61 + this.adminId = adminId;
  62 + this.IP = IP;
  63 + this.country = country;
  64 + this.province = province;
  65 + this.city = city;
  66 + this.district = district;
  67 + this.checkupTime = checkupTime;
  68 + this.uploadTime = uploadTime;
  69 + }
  70 +
  71 + public String getAMPSerial() {
  72 + return AMPSerial;
  73 + }
  74 +
  75 + public void setAMPSerial(String AMPSerial) {
  76 + this.AMPSerial = AMPSerial;
  77 + }
  78 +
  79 + public long getAdminId() {
  80 + return adminId;
  81 + }
  82 +
  83 + public void setAdminId(long adminId) {
  84 + this.adminId = adminId;
  85 + }
  86 +
  87 + public String getIP() {
  88 + return IP;
  89 + }
  90 +
  91 + public void setIP(String IP) {
  92 + this.IP = IP;
  93 + }
  94 +
  95 + public String getCountry() {
  96 + return country;
  97 + }
  98 +
  99 + public void setCountry(String country) {
  100 + this.country = country;
  101 + }
  102 +
  103 + public String getProvince() {
  104 + return province;
  105 + }
  106 +
  107 + public void setProvince(String province) {
  108 + this.province = province;
  109 + }
  110 +
  111 + public String getCity() {
  112 + return city;
  113 + }
  114 +
  115 + public void setCity(String city) {
  116 + this.city = city;
  117 + }
  118 +
  119 + public String getDistrict() {
  120 + return district;
  121 + }
  122 +
  123 + public void setDistrict(String district) {
  124 + this.district = district;
  125 + }
  126 +
  127 + public Timestamp getCheckupTime() {
  128 + return checkupTime;
  129 + }
  130 +
  131 + public void setCheckupTime(Timestamp checkupTime) {
  132 + this.checkupTime = checkupTime;
  133 + }
  134 +
  135 + public Timestamp getUploadTime() {
  136 + return uploadTime;
  137 + }
  138 +
  139 + public void setUploadTime(Timestamp uploadTime) {
  140 + this.uploadTime = uploadTime;
  141 + }
  142 +}
  1 +package com.xkl.repository;
  2 +
  3 +import com.xkl.domain.XklAmpReportLocationEntity;
  4 +import org.springframework.data.repository.CrudRepository;
  5 +
  6 +/**
  7 + * create by zhaoyue at 20170415
  8 + */
  9 +public interface XklAmpReportLocationRespository extends CrudRepository<XklAmpReportLocationEntity, Long> {
  10 +
  11 +}
@@ -9,6 +9,7 @@ import com.xkl.model.ReportIdModel; @@ -9,6 +9,7 @@ import com.xkl.model.ReportIdModel;
9 import com.xkl.model.ResultModel; 9 import com.xkl.model.ResultModel;
10 import com.xkl.repository.*; 10 import com.xkl.repository.*;
11 import com.xkl.security.SecurityTool; 11 import com.xkl.security.SecurityTool;
  12 +import com.xkl.tools.IPTools;
12 import lombok.extern.apachecommons.CommonsLog; 13 import lombok.extern.apachecommons.CommonsLog;
13 import org.springframework.beans.factory.annotation.Autowired; 14 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.data.redis.core.RedisTemplate; 15 import org.springframework.data.redis.core.RedisTemplate;
@@ -38,6 +39,9 @@ public class ReportService implements IReportService { @@ -38,6 +39,9 @@ public class ReportService implements IReportService {
38 private ReportResultRepository reportResultRepository; 39 private ReportResultRepository reportResultRepository;
39 40
40 @Autowired 41 @Autowired
  42 + private XklAmpReportLocationRespository xklAmpReportLocationRespository;
  43 +
  44 + @Autowired
41 private UserRepository userRepository; 45 private UserRepository userRepository;
42 46
43 @Autowired 47 @Autowired
@@ -218,7 +222,7 @@ public class ReportService implements IReportService { @@ -218,7 +222,7 @@ public class ReportService implements IReportService {
218 } 222 }
219 } 223 }
220 } catch (Exception e) { 224 } catch (Exception e) {
221 - log.info("ERROR, JSON FROMAT ERROR!!!"+e); 225 + log.info("ERROR, JSON FROMAT ERROR!!!" + e);
222 return null; 226 return null;
223 } 227 }
224 markItemStatus(sex, detailList); 228 markItemStatus(sex, detailList);
@@ -244,11 +248,27 @@ public class ReportService implements IReportService { @@ -244,11 +248,27 @@ public class ReportService implements IReportService {
244 } 248 }
245 reportDetailRepository.save(report.getRpDetailList()); 249 reportDetailRepository.save(report.getRpDetailList());
246 reportResultRepository.save(report.getBasicResList()); 250 reportResultRepository.save(report.getBasicResList());
  251 + // 保存报告地理位置。
  252 + saveRpLocation2DB(report, admin);
247 scoreService.getScore(ampReport.getId()); 253 scoreService.getScore(ampReport.getId());
248 return ampReport.getId(); 254 return ampReport.getId();
249 } 255 }
250 256
251 /* 257 /*
  258 + * 保存报告地理位置。
  259 + */
  260 + private void saveRpLocation2DB(ReportData report, XklAdminEntity admin) {
  261 + String ip = admin.getIp();
  262 + Location location = IPTools.getLocationWithIP(ip);
  263 + if (location == null) return;
  264 + XklAmpReportLocationEntity rpLoc = new XklAmpReportLocationEntity(report.getAmpReport().getMachineNum(),
  265 + admin.getId(), ip, location.getCountry(), location.getProvince(), location.getCity(), location.getDistrict(),
  266 + report.getAmpReport().getCheckTime(), report.getAmpReport().getUptime());
  267 + xklAmpReportLocationRespository.save(rpLoc);
  268 + }
  269 +
  270 +
  271 + /*
252 * 判断detail是正常,高于标准或低于标准。 272 * 判断detail是正常,高于标准或低于标准。
253 */ 273 */
254 private void markItemStatus(int sex, List<XklAmpReportDetailEntity> detailList) { 274 private void markItemStatus(int sex, List<XklAmpReportDetailEntity> detailList) {
  1 +package com.xkl.tools;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.xkl.domain.Location;
  5 +
  6 +import java.io.UnsupportedEncodingException;
  7 +import java.net.URLEncoder;
  8 +import java.util.HashMap;
  9 +import java.util.Map;
  10 +
  11 +/**
  12 + * Created by zhaoyue on 2017/4/15.
  13 + */
  14 +public class IPTools {
  15 + public static final String sinaIPGetter = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&";
  16 +
  17 + public static Location getLocationWithIP(String IP) {
  18 + try {
  19 + Map<String, String> params = new HashMap<String, String>();
  20 + params.put("ip",IP);
  21 + String url = sinaIPGetter + URLEncoder.encode(IP, "utf-8");
  22 + String content = HttpTools.requestByMap(url, "POST", params);
  23 + JSONObject ipJson = JSONObject.parseObject(content);
  24 + String country = ipJson.getString("country");
  25 + String province = ipJson.getString("province");
  26 + String city = ipJson.getString("city");
  27 + String district = ipJson.getString("district");
  28 + Location location = new Location(country, province, city, district);
  29 + return location;
  30 + } catch (UnsupportedEncodingException e) {
  31 + e.printStackTrace();
  32 + return null;
  33 + }
  34 + }
  35 +
  36 +}
  1 +package PACKAGE_NAME;
  2 +
  3 +/**
  4 + * Created by zhaoyue on 2017/4/15.
  5 + */
  6 +public class IPToolsTest {
  7 +}