| @@ -0,0 +1,36 @@ | |||
| HELP.md | |||
| target/ | |||
| !.mvn/wrapper/maven-wrapper.jar | |||
| !**/src/main/** | |||
| !**/src/test/** | |||
| ### STS ### | |||
| .apt_generated | |||
| .classpath | |||
| .factorypath | |||
| .project | |||
| .settings | |||
| .springBeans | |||
| .sts4-cache | |||
| ### IntelliJ IDEA ### | |||
| .idea | |||
| *.iws | |||
| *.iml | |||
| *.ipr | |||
| ### NetBeans ### | |||
| /nbproject/private/ | |||
| /nbbuild/ | |||
| /dist/ | |||
| /nbdist/ | |||
| /.nb-gradle/ | |||
| build/ | |||
| ### VS Code ### | |||
| .vscode/ | |||
| *.DS_Store | |||
| *.log | |||
| *.log.* | |||
| html/ | |||
| @@ -0,0 +1,31 @@ | |||
| ## 一、更新记录 | |||
| ## 二、项目描述 | |||
| - 一个基于 spring-boot 框架的 " **接受 Ailink 设备数据推送的API Demo项目** "。 | |||
| - 拉取项目之后,建议按照自己的功能区域重命名文件,现以简单的位置进行区分。 | |||
| - 项目环境:JDK1.8、 SpringBoot2、Maven、Intellij IDEA、Mysql8、Redis | |||
| - 请拉取 master 分支的代码,其余是开发分支。 | |||
| 友情链接: | |||
| 1. []() | |||
| ## 三、主要文件介绍 | |||
| | 文件 | 作用/功能 | | |||
| |----------------------------|----------------------------| | |||
| | pom.xml | jar包管理配置文件 | | |||
| | application.properties | 项目启动配置主文件,切换不同的使用环境 | | |||
| | application-dev.properties | 测试环境启动配置文件 | | |||
| | com/inet/*Application.java | 项目启动类 | | |||
| ## 四、使用介绍 | |||
| 1. **如何启动项目** | |||
| - 需要提前安装好`jdk1.8`与`maven`,使用maven下载`pom.xml`对应的jar,完成后,找到项目启动文件,使用debug方式启动 | |||
| ## 五、其余 | |||
| @@ -0,0 +1,56 @@ | |||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||
| <modelVersion>4.0.0</modelVersion> | |||
| <parent> | |||
| <groupId>org.springframework.boot</groupId> | |||
| <artifactId>spring-boot-starter-parent</artifactId> | |||
| <version>2.1.3.RELEASE</version> | |||
| </parent> | |||
| <groupId>com.inet</groupId> | |||
| <artifactId>ailink-receiver-pull-demo</artifactId> | |||
| <version>1.0.0</version> | |||
| <packaging>jar</packaging> | |||
| <name>ailink-receiver-demo</name> | |||
| <url>http://maven.apache.org</url> | |||
| <properties> | |||
| <java.version>1.8</java.version> | |||
| <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> | |||
| </properties> | |||
| <dependencies> | |||
| <dependency> | |||
| <groupId>org.springframework.boot</groupId> | |||
| <artifactId>spring-boot-starter-web</artifactId> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>commons-lang</groupId> | |||
| <artifactId>commons-lang</artifactId> | |||
| <version>2.5</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>commons-codec</groupId> | |||
| <artifactId>commons-codec</artifactId> | |||
| <version>1.4</version> | |||
| </dependency> | |||
| </dependencies> | |||
| <build> | |||
| <plugins> | |||
| <plugin> | |||
| <groupId>org.springframework.boot</groupId> | |||
| <artifactId>spring-boot-maven-plugin</artifactId> | |||
| <configuration> | |||
| <mainClass>com.inet.ailink.receiver.ReceiverApplication</mainClass> | |||
| <layout>JAR</layout> | |||
| </configuration> | |||
| </plugin> | |||
| </plugins> | |||
| </build> | |||
| </project> | |||
| @@ -0,0 +1,16 @@ | |||
| package com.inet.ailink.receiver; | |||
| import org.springframework.boot.SpringApplication; | |||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | |||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | |||
| import org.springframework.scheduling.annotation.EnableAsync; | |||
| @SpringBootApplication | |||
| @EnableAutoConfiguration | |||
| @EnableAsync | |||
| public class ReceiverApplication { | |||
| public static void main(String args[]){ | |||
| SpringApplication.run(ReceiverApplication.class, args); | |||
| } | |||
| } | |||
| @@ -0,0 +1,13 @@ | |||
| package com.inet.ailink.receiver; | |||
| import org.springframework.boot.builder.SpringApplicationBuilder; | |||
| import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; | |||
| public class ServletInitializer extends SpringBootServletInitializer { | |||
| @Override | |||
| protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { | |||
| return application.sources(ReceiverApplication.class); | |||
| } | |||
| } | |||
| @@ -0,0 +1,37 @@ | |||
| package com.inet.ailink.receiver.common.enums; | |||
| public enum StatusCode{ | |||
| SUCCESS("1","success"), | |||
| FAIL("0","faile") | |||
| ; | |||
| private String code=""; | |||
| private String msg=""; | |||
| StatusCode(String code,String msg){ | |||
| this.code=code; | |||
| this.msg=msg; | |||
| } | |||
| public String getCode() { | |||
| return code; | |||
| } | |||
| public void setCode(String code) { | |||
| this.code = code; | |||
| } | |||
| public String getMsg() { | |||
| return msg; | |||
| } | |||
| public void setMsg(String msg) { | |||
| this.msg = msg; | |||
| } | |||
| public static StatusCode getByCode(String param){ | |||
| for(StatusCode thisCode:StatusCode.values()){ | |||
| if(thisCode.code.equals(param)){ | |||
| return thisCode; | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| } | |||
| @@ -0,0 +1,92 @@ | |||
| package com.inet.ailink.receiver.common.exception; | |||
| import com.inet.ailink.receiver.common.enums.StatusCode; | |||
| public class BizException extends Exception { | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -4155383900948675082L; | |||
| private String code="9999"; | |||
| private String msg=""; | |||
| private String[] params; | |||
| private Object data; | |||
| public BizException() { | |||
| super(); | |||
| } | |||
| public BizException(StatusCode statusCode) { | |||
| super(); | |||
| code=statusCode.getCode(); | |||
| msg=statusCode.getMsg(); | |||
| } | |||
| public BizException(StatusCode statusCode,Object data) { | |||
| super(); | |||
| code=statusCode.getCode(); | |||
| msg=statusCode.getMsg(); | |||
| this.data = data; | |||
| } | |||
| public BizException(String code) { | |||
| this.code = code; | |||
| } | |||
| public BizException(String code,String msg) { | |||
| this.code = code; | |||
| this.msg = msg; | |||
| } | |||
| public BizException(Throwable cause) { | |||
| super(cause); | |||
| } | |||
| public BizException(String code,Throwable exception) { | |||
| super(exception); | |||
| this.code = code; | |||
| } | |||
| public BizException(String code, String msg, Throwable exception) { | |||
| super(msg, exception); | |||
| this.code = code; | |||
| } | |||
| public BizException(String code,String[] params) { | |||
| super(); | |||
| this.code = code; | |||
| this.params = params; | |||
| } | |||
| public BizException(String code,String[] params,Throwable exception) { | |||
| super(exception); | |||
| this.code = code; | |||
| this.params = params; | |||
| } | |||
| public String getCode() { | |||
| return this.code; | |||
| } | |||
| public String[] getParams() { | |||
| return params; | |||
| } | |||
| @Override | |||
| public String toString() { | |||
| String s = getClass().getName(); | |||
| return s + ": " + code + " params:"+params; | |||
| } | |||
| public String getMsg() { | |||
| return msg; | |||
| } | |||
| public void setMsg(String msg) { | |||
| this.msg = msg; | |||
| } | |||
| public void setCode(String code) { | |||
| this.code = code; | |||
| } | |||
| public Object getData() { | |||
| return data; | |||
| } | |||
| public void setData(Object data) { | |||
| this.data = data; | |||
| } | |||
| } | |||
| @@ -0,0 +1,56 @@ | |||
| package com.inet.ailink.receiver.common.response; | |||
| import com.inet.ailink.receiver.common.enums.StatusCode; | |||
| //@ApiModel("基础响应参数") | |||
| //@JsonInclude(JsonInclude.Include.NON_NULL) | |||
| public class Response<T> { | |||
| private String code; | |||
| private String msg; | |||
| private T data; | |||
| public Response() { | |||
| this(StatusCode.SUCCESS); | |||
| } | |||
| public void setData(T data) { | |||
| this.data = data; | |||
| } | |||
| public Response(StatusCode errorEnum) { | |||
| setErr(errorEnum); | |||
| } | |||
| public Response(StatusCode err, T data) { | |||
| setErr(err); | |||
| setData(data); | |||
| } | |||
| public void setErr(StatusCode error) { | |||
| this.code = error.getCode(); | |||
| this.msg = error.getMsg(); | |||
| } | |||
| public String getCode() { | |||
| return code; | |||
| } | |||
| public void setCode(String code) { | |||
| this.code = code; | |||
| } | |||
| public String getMsg() { | |||
| return msg; | |||
| } | |||
| public void setMsg(String msg) { | |||
| this.msg = msg; | |||
| } | |||
| public T getData() { | |||
| return data; | |||
| } | |||
| } | |||
| @@ -0,0 +1,76 @@ | |||
| package com.inet.ailink.receiver.common.utils; | |||
| import org.apache.commons.codec.binary.Base64; | |||
| import javax.crypto.Cipher; | |||
| import javax.crypto.spec.IvParameterSpec; | |||
| import javax.crypto.spec.SecretKeySpec; | |||
| import java.nio.charset.StandardCharsets; | |||
| /** | |||
| * AES 加解密工具类 | |||
| */ | |||
| public class AESUtils { | |||
| /** | |||
| * CBC 密码分组链接模式加密,需要一个向量iv,可增加加密算法的强度 | |||
| * @param content 待加密内容 | |||
| * @param key 秘钥 | |||
| * @param offset 偏移量字符串 | |||
| */ | |||
| public static String aesCBCEncrypt(String content, String key,String offset) throws Exception { | |||
| key = convertKey(key); | |||
| offset = convertOffset(offset); | |||
| Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |||
| SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); | |||
| IvParameterSpec iv = new IvParameterSpec(offset.getBytes()); | |||
| cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv); | |||
| byte[] encrypted = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)); | |||
| // return Base64.encodeBase64String(encrypted); | |||
| return Base64Util.encoderString(encrypted); | |||
| } | |||
| /** | |||
| * CBC 密码分组链接模式解密 | |||
| * @param content 待解密内容 | |||
| * @param key 秘钥 | |||
| * @param offset 偏移量字符串 | |||
| */ | |||
| public static String aesCBCDecrypt(String content, String key,String offset) throws Exception { | |||
| key = convertKey(key); | |||
| offset = convertOffset(offset); | |||
| Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |||
| SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); | |||
| IvParameterSpec iv = new IvParameterSpec(offset.getBytes()); | |||
| cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv); | |||
| byte[] encrypted = cipher.doFinal(Base64.decodeBase64(content)); | |||
| return new String(encrypted, StandardCharsets.UTF_8); | |||
| } | |||
| public static String convertKey(String key){ | |||
| if(StringSelfUtil.isEmpty(key)){ | |||
| throw new NullPointerException(); | |||
| } | |||
| int length = key.length(); | |||
| if(length > 16) { | |||
| key = key.substring(0, 16); | |||
| } | |||
| return key; | |||
| } | |||
| public static String convertOffset(String offset){ | |||
| if(StringSelfUtil.isEmpty(offset)){ | |||
| throw new NullPointerException(); | |||
| } | |||
| int length = offset.length(); | |||
| if(length > 16){ | |||
| offset = offset.substring(0,16); | |||
| } | |||
| return offset; | |||
| } | |||
| public static void main(String[] args) throws Exception{ | |||
| } | |||
| } | |||
| @@ -0,0 +1,194 @@ | |||
| package com.inet.ailink.receiver.common.utils; | |||
| import java.io.UnsupportedEncodingException; | |||
| import java.util.Base64; | |||
| public class Base64Util | |||
| { | |||
| final static Base64.Decoder decoder = Base64.getDecoder(); | |||
| final static Base64.Encoder encoder = Base64.getEncoder(); | |||
| public Base64Util(){} | |||
| /** | |||
| * 解码 | |||
| * @param encoderStr | |||
| * @return | |||
| * @throws UnsupportedEncodingException | |||
| */ | |||
| public static String decoderString(String base64Text) throws UnsupportedEncodingException{ | |||
| String result = ""; | |||
| if(base64Text != null && !base64Text.isEmpty() && !base64Text.equals("")){ | |||
| byte[] textByte = decoder.decode(base64Text); | |||
| result = bytes_String16(textByte); | |||
| } | |||
| return result; | |||
| } | |||
| /** | |||
| * byte[]转16进制字符串 | |||
| * @param b | |||
| * @return | |||
| */ | |||
| public static String bytes_String16(byte[] b) { | |||
| StringBuilder sb = new StringBuilder(); | |||
| for(int i=0;i<b.length;i++) { | |||
| sb.append(String.format("%02x", b[i])); | |||
| } | |||
| return sb.toString(); | |||
| } | |||
| /** | |||
| * 编码 | |||
| * @param encoderStr | |||
| * @return | |||
| * @throws UnsupportedEncodingException | |||
| */ | |||
| public static String encoderString(String base64Text) throws UnsupportedEncodingException{ | |||
| String result = ""; | |||
| if(base64Text != null && !base64Text.isEmpty() && !base64Text.equals("")){ | |||
| byte[] textByte = base64Text.getBytes("UTF-8"); | |||
| result = encoder.encodeToString(textByte); | |||
| } | |||
| return result; | |||
| } | |||
| /** | |||
| * 编码 | |||
| * @param encoderStr | |||
| * @return | |||
| * @throws UnsupportedEncodingException | |||
| */ | |||
| public static String encoderString(byte[] textByte) throws UnsupportedEncodingException{ | |||
| return encoder.encodeToString(textByte); | |||
| } | |||
| public static int getLength(String s) { | |||
| int length = 0; | |||
| for (int i = 0; i < s.length(); i++) { | |||
| int ascii = Character.codePointAt(s, i); | |||
| if (ascii >= 0 && ascii <= 255) { | |||
| length++; | |||
| } else { | |||
| length += 2; | |||
| } | |||
| } | |||
| return length; | |||
| } | |||
| /** | |||
| * 十进制转16进制 | |||
| * @param n | |||
| * @return | |||
| */ | |||
| private static String intToHex(int n) { | |||
| StringBuilder sb = new StringBuilder(8); | |||
| String a; | |||
| char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; | |||
| if(n==0){ | |||
| return "00"; | |||
| } | |||
| while(n != 0){ | |||
| if(n<0){ | |||
| n= 256 +n; | |||
| } | |||
| sb = sb.append(b[n%16]); | |||
| n = n/16; | |||
| } | |||
| a = sb.reverse().toString(); | |||
| if(a.length() == 1){ | |||
| a= "0"+a; | |||
| } | |||
| return a; | |||
| } | |||
| public static byte[] hexStrToByteArray(String str) | |||
| { | |||
| if (str == null) { | |||
| return null; | |||
| } | |||
| if (str.length() == 0) { | |||
| return new byte[0]; | |||
| } | |||
| byte[] byteArray = new byte[str.length() / 2]; | |||
| for (int i = 0; i < byteArray.length; i++){ | |||
| String subStr = str.substring(2 * i, 2 * i + 2); | |||
| byteArray[i] = ((byte)Integer.parseInt(subStr, 16)); | |||
| } | |||
| return byteArray; | |||
| } | |||
| /** | |||
| * 获取开始下标到结束下标的16进制的值 | |||
| * @param start | |||
| * @param end | |||
| * @param params | |||
| * @return | |||
| */ | |||
| public static String getStartToEndForHex(int start,int end,byte[] params){ | |||
| String result = ""; | |||
| for(int i=start;i<=end;i++){ | |||
| if(i==end){ | |||
| result = result+intToHex(params[i]); | |||
| }else{ | |||
| result = result+intToHex(params[i])+":"; | |||
| } | |||
| } | |||
| return result; | |||
| } | |||
| /** | |||
| * 获取开始下标到结束下标的10进制的值 | |||
| * @param start | |||
| * @param end | |||
| * @param params | |||
| * @return | |||
| */ | |||
| public static String getStartToEndForInt(int start,int end,byte[] params){ | |||
| String result = ""; | |||
| for(int i=start;i<=end;i++){ | |||
| int temp = params[i]; | |||
| if(temp < 0){ | |||
| temp = 256 + temp; | |||
| } | |||
| if(i==end){ | |||
| result = result+temp; | |||
| }else{ | |||
| result = result+temp+":"; | |||
| } | |||
| } | |||
| return result; | |||
| } | |||
| /** | |||
| * 获取低位到高位的移位+或运算的值 | |||
| * @param indexLow | |||
| * @param indexHigh | |||
| * @param params | |||
| * @return | |||
| */ | |||
| public static String getLowConactHighEndForInt(int indexLow,int indexHigh,byte[] params){ | |||
| String result = ""; | |||
| int tempResult = 0; | |||
| for(int i = indexLow;i<=indexHigh;i++){ | |||
| int temp = (params[i] < 0 ? 256 + params[i] : params[i]); | |||
| int tempmov = (indexHigh-i)+2; | |||
| tempmov = (i == indexHigh ? 0 : (int) Math.pow(2, tempmov)); | |||
| tempResult = (temp << tempmov) | tempResult; | |||
| } | |||
| return result+tempResult; | |||
| } | |||
| public static Integer getLowConactHighEndForByte(byte b,int start,int end){ | |||
| String bitStr = ""; | |||
| for(int i=start;i<=end;i++){ | |||
| bitStr += String.valueOf((b >> (7-i)) & 0x1); | |||
| } | |||
| return Integer.parseInt(bitStr,2); | |||
| } | |||
| } | |||
| @@ -0,0 +1,703 @@ | |||
| package com.inet.ailink.receiver.common.utils; | |||
| import java.math.BigDecimal; | |||
| import java.text.DateFormat; | |||
| import java.text.ParseException; | |||
| import java.text.SimpleDateFormat; | |||
| import java.util.Calendar; | |||
| import java.util.Date; | |||
| import java.util.GregorianCalendar; | |||
| import java.util.TimeZone; | |||
| import org.apache.commons.lang.StringUtils; | |||
| import org.apache.commons.lang.time.FastDateFormat; | |||
| /** | |||
| * @ClassName: DateUtils | |||
| * @Description: 日期处理工具类 | |||
| * @author Yann | |||
| * @date 2020-08-20 下午3:11:57 | |||
| * | |||
| */ | |||
| public class DateUtils { | |||
| /** | |||
| * ISO8601 formatter for date without time zone. The format used is | |||
| * <tt>yyyy-MM-dd</tt>. | |||
| */ | |||
| public static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd"); | |||
| /** | |||
| * ISO8601 date format: yyyy-MM-dd | |||
| */ | |||
| public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd"; | |||
| public static final String DATE_FORMAT_PATTERN_NO_SEPARATOR = "yyyyMMdd"; | |||
| /** | |||
| * ISO8601 date-time format: yyyy-MM-dd HH:mm:ss | |||
| */ | |||
| public static final String DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss"; | |||
| /** | |||
| * for bet view format: yyyy-MM-dd HH:mm:ss | |||
| */ | |||
| public static final String DATETIME_JSVIEW_FORMAT_PATTERN = "yyyy/MM/dd HH:mm:ss"; | |||
| public static final String DATETIME_SENDTIME_FORMAT_PATTERN = "yyyyMMddHHmmssSSS"; | |||
| /** | |||
| * 日期时间格式:yyyy-MM-dd HH:mm,不显示秒 | |||
| */ | |||
| public static final String DATETIME_WITHOUT_SECOND_FORMAT_PATTERN = "yyyy-MM-dd HH:mm"; | |||
| /** | |||
| * 日期时间格式:yyyy-MM-dd HH | |||
| */ | |||
| public static final String DATETIME_WITHOUT_MINUTES_FORMAT_PATTERN = "yyyy-MM-dd HH"; | |||
| public static final String DATE_TIME_START00 = " 00:00:00"; | |||
| public static final String DATE_TIME_END23 = " 23:59:59"; | |||
| /** | |||
| * 日期时间格式:yyyyMMddHHmm,不显示秒 | |||
| */ | |||
| public static final String DATETIME_WITHOUT_SECOND_FORMAT= "yyyyMMddHHmm"; | |||
| /** | |||
| * 获得当前时间 | |||
| */ | |||
| public static Date currentDate() { | |||
| return new Date(); | |||
| } | |||
| public static Date getDate(Long time) { | |||
| return new Date(time); | |||
| } | |||
| public static Long getCurTime() { | |||
| return System.currentTimeMillis(); | |||
| } | |||
| public static Date parse(String str) { | |||
| return parse(str, DATE_FORMAT_PATTERN); | |||
| } | |||
| public static Date parse(String str, String pattern) { | |||
| if (StringUtils.isBlank(str)) { | |||
| return null; | |||
| } | |||
| DateFormat parser = new SimpleDateFormat(pattern); | |||
| try { | |||
| return parser.parse(str); | |||
| } catch (ParseException e) { | |||
| throw new IllegalArgumentException("Can't parse " + str + " using " + pattern); | |||
| } | |||
| } | |||
| /** | |||
| * 根据时间变量返回时间字符串 | |||
| */ | |||
| public static String format(Date date, String pattern) { | |||
| if (date == null) { | |||
| return null; | |||
| } | |||
| FastDateFormat df = FastDateFormat.getInstance(pattern); | |||
| return df.format(date); | |||
| } | |||
| public static String format(Long time, String pattern) { | |||
| if (time == null) { | |||
| return null; | |||
| } | |||
| FastDateFormat df = FastDateFormat.getInstance(pattern); | |||
| return df.format(new Date(time)); | |||
| } | |||
| /** | |||
| * return date format is <code>yyyy-MM-dd</code> | |||
| */ | |||
| public static String format(Date date) { | |||
| return date == null ? null : DATE_FORMAT.format(date); | |||
| } | |||
| public static Date getEndDateTimeOfCurrentYear() { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(currentDate()); | |||
| cal.set(Calendar.MONTH, Calendar.DECEMBER); | |||
| cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); | |||
| cal.set(Calendar.HOUR_OF_DAY, 23); | |||
| cal.set(Calendar.MINUTE, 59); | |||
| cal.set(Calendar.SECOND, 59); | |||
| return cal.getTime(); | |||
| } | |||
| public static Date getStartDateTimeOfCurrentYear() { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(currentDate()); | |||
| cal.set(Calendar.MONTH, Calendar.JANUARY); | |||
| cal.set(Calendar.DAY_OF_MONTH, 1); | |||
| cal.set(Calendar.HOUR_OF_DAY, 0); | |||
| cal.set(Calendar.MINUTE, 0); | |||
| cal.set(Calendar.SECOND, 0); | |||
| return parse(format(cal.getTime())); | |||
| } | |||
| public static Date getStartDateTimeOfYear(int year) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.set(Calendar.YEAR, year); | |||
| cal.set(Calendar.MONTH, Calendar.JANUARY); | |||
| cal.set(Calendar.DAY_OF_MONTH, 1); | |||
| cal.set(Calendar.HOUR_OF_DAY, 0); | |||
| cal.set(Calendar.MINUTE, 0); | |||
| cal.set(Calendar.SECOND, 0); | |||
| return parse(format(cal.getTime())); | |||
| } | |||
| public static Date getEndDateTimeOfYear(int year) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.set(Calendar.YEAR, year); | |||
| cal.set(Calendar.MONTH, Calendar.DECEMBER); | |||
| cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); | |||
| cal.set(Calendar.HOUR_OF_DAY, 23); | |||
| cal.set(Calendar.MINUTE, 59); | |||
| cal.set(Calendar.SECOND, 59); | |||
| return cal.getTime(); | |||
| } | |||
| public static Date getStartTimeOfCurrentDate() { | |||
| return getStartTimeOfDate(currentDate()); | |||
| } | |||
| public static Date getEndTimeOfCurrentDate() { | |||
| return getEndTimeOfDate(currentDate()); | |||
| } | |||
| public static Date getStartTimeOfCurrentMonth() { | |||
| return getStartDateTimeOfMonth(DateUtils.currentDate()); | |||
| } | |||
| public static Date getEndTimeOfCurrentMonth() { | |||
| return getEndDateTimeOfMonth(DateUtils.currentDate()); | |||
| } | |||
| public static Date getStartTimeOfDate(Date date) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(date); | |||
| cal.set(Calendar.HOUR_OF_DAY, 0); | |||
| cal.set(Calendar.MINUTE, 0); | |||
| cal.set(Calendar.SECOND, 0); | |||
| return parse(format(cal.getTime())); | |||
| } | |||
| public static Date getEndTimeOfDate(Date date) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(date); | |||
| cal.set(Calendar.HOUR_OF_DAY, 23); | |||
| cal.set(Calendar.MINUTE, 59); | |||
| cal.set(Calendar.SECOND, 59); | |||
| return cal.getTime(); | |||
| } | |||
| public static Date getSpecialEndTimeOfDate(Date date) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(date); | |||
| cal.set(Calendar.HOUR_OF_DAY, 24); | |||
| cal.set(Calendar.MINUTE, 00); | |||
| cal.set(Calendar.SECOND, 00); | |||
| return cal.getTime(); | |||
| } | |||
| public static Date getStartDateTimeOfMonth(Date date) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(date); | |||
| cal.set(Calendar.DAY_OF_MONTH, 1); | |||
| cal.set(Calendar.HOUR_OF_DAY, 0); | |||
| cal.set(Calendar.MINUTE, 0); | |||
| cal.set(Calendar.SECOND, 0); | |||
| return parse(format(cal.getTime())); | |||
| } | |||
| public static Date getStartDateTimeOfMonth(int year, int month) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.set(Calendar.YEAR, year); | |||
| cal.set(Calendar.MONTH, month - 1); | |||
| cal.set(Calendar.DAY_OF_MONTH, 1); | |||
| cal.set(Calendar.HOUR_OF_DAY, 0); | |||
| cal.set(Calendar.MINUTE, 0); | |||
| cal.set(Calendar.SECOND, 0); | |||
| return parse(format(cal.getTime())); | |||
| } | |||
| public static Date getEndDateTimeOfMonth(Date date) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(date); | |||
| cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); | |||
| cal.set(Calendar.HOUR_OF_DAY, 23); | |||
| cal.set(Calendar.MINUTE, 59); | |||
| cal.set(Calendar.SECOND, 59); | |||
| return cal.getTime(); | |||
| } | |||
| public static Date addHours(Date date, int hours) { | |||
| return add(date, Calendar.HOUR_OF_DAY, hours); | |||
| } | |||
| public static Date addMinutes(Date date, int minutes) { | |||
| return add(date, Calendar.MINUTE, minutes); | |||
| } | |||
| public static Date addSeconds(Date date, int seconds) { | |||
| return add(date, Calendar.SECOND, seconds); | |||
| } | |||
| public static Date addDays(Date date, int days) { | |||
| return add(date, Calendar.DATE, days); | |||
| } | |||
| public static Date addMonths(Date date, int months) { | |||
| return add(date, Calendar.MONTH, months); | |||
| } | |||
| public static Date addYears(Date date, int years) { | |||
| return add(date, Calendar.YEAR, years); | |||
| } | |||
| private static Date add(Date date, int field, int amount) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(date); | |||
| cal.add(field, amount); | |||
| return cal.getTime(); | |||
| } | |||
| public static long calcDateBetween(Date start, Date end) { | |||
| if (start == null || end == null) { | |||
| return 0; | |||
| } | |||
| return ((end.getTime() - start.getTime()) / 86400001) + 1; | |||
| } | |||
| public static long calcHoursBetween(Date start, Date end) { | |||
| if (start == null || end == null) { | |||
| return 0; | |||
| } | |||
| return ((end.getTime() - start.getTime()) / (60000 * 60)); | |||
| } | |||
| public static Double calcHoursDoubleBetween(Date start, Date end) { | |||
| if (start == null || end == null) { | |||
| return 0d; | |||
| } | |||
| Double time = ((end.getTime() - start.getTime()) / (60000.0 * 60.0)); | |||
| return Double.valueOf(new java.text.DecimalFormat("#.0").format(time)); | |||
| } | |||
| public static Double calcHoursDouble(Date start, Date end) { | |||
| if (start == null || end == null) { | |||
| return 0d; | |||
| } | |||
| // (new BigDecimal(end.getTime()).subtract(new | |||
| // BigDecimal(start.getTime()))).divide(new BigDecimal(60000.0*60.0), 7, | |||
| // BigDecimal.ROUND_HALF_UP).doubleValue(); | |||
| Double time = (new BigDecimal(end.getTime()).subtract(new BigDecimal(start.getTime()))) | |||
| .divide(new BigDecimal(60000.0 * 60.0), 7, BigDecimal.ROUND_HALF_UP).doubleValue();// ((end.getTime() - | |||
| // start.getTime()) | |||
| // / | |||
| // (60000.0*60.0)); | |||
| return time; | |||
| } | |||
| public static long calcMinutesBetween(Date start, Date end) { | |||
| if (start == null || end == null) { | |||
| return 0; | |||
| } | |||
| return ((end.getTime() - start.getTime()) / 60000); | |||
| } | |||
| public static long calcSecondsBetween(Date start, Date end) { | |||
| if (start == null || end == null) { | |||
| return 0; | |||
| } | |||
| return ((end.getTime() - start.getTime()) / 1000); | |||
| } | |||
| public static long calcSecondsBetween(Long start, Long end) { | |||
| return (end - start) / 1000; | |||
| } | |||
| /** | |||
| * 获得日期是否为星期天。 | |||
| * | |||
| * @param date 日期 | |||
| * @return | |||
| */ | |||
| public static boolean isSunday(Date date) { | |||
| return getDate(date) == Calendar.SUNDAY; | |||
| } | |||
| /** | |||
| * 获得日期是否为星期一。 | |||
| * | |||
| * @param date 日期 | |||
| * @return | |||
| */ | |||
| public static boolean isMonday(Date date) { | |||
| return getDate(date) == Calendar.MONDAY; | |||
| } | |||
| /** | |||
| * 获得日期是否为星期二。 | |||
| * | |||
| * @param date 日期 | |||
| * @return | |||
| */ | |||
| public static boolean isTuesday(Date date) { | |||
| return getDate(date) == Calendar.TUESDAY; | |||
| } | |||
| /** | |||
| * 获得日期是否为星期三。 | |||
| * | |||
| * @param date 日期 | |||
| * @return | |||
| */ | |||
| public static boolean isWednesday(Date date) { | |||
| return getDate(date) == Calendar.WEDNESDAY; | |||
| } | |||
| /** | |||
| * 获得日期是否为星期四。 | |||
| * | |||
| * @param date 日期 | |||
| * @return | |||
| */ | |||
| public static boolean isThursday(Date date) { | |||
| return getDate(date) == Calendar.THURSDAY; | |||
| } | |||
| /** | |||
| * 获得日期是否为星期五。 | |||
| * | |||
| * @param date 日期 | |||
| * @return | |||
| */ | |||
| public static boolean isFriday(Date date) { | |||
| return getDate(date) == Calendar.FRIDAY; | |||
| } | |||
| /** | |||
| * 获得日期是否为星期六。 | |||
| * | |||
| * @param date 日期 | |||
| * @return | |||
| */ | |||
| public static boolean isSaturday(Date date) { | |||
| return getDate(date) == Calendar.SATURDAY; | |||
| } | |||
| public static int getDate(Date date) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(date); | |||
| return cal.get(Calendar.DAY_OF_WEEK); | |||
| } | |||
| /** | |||
| * 获得相对于今天的昨天的时间。 | |||
| * | |||
| * @return 昨天此时。 | |||
| */ | |||
| public static Date getYesterday() { | |||
| return addDays(currentDate(), -1); | |||
| } | |||
| /** | |||
| * 获得月份 | |||
| * | |||
| * @param date | |||
| * @return | |||
| */ | |||
| public static int getMonth(Date date) { | |||
| Calendar calendar = Calendar.getInstance(); | |||
| calendar.setTime(date); | |||
| return calendar.get(Calendar.MONTH) + 1; | |||
| } | |||
| /** | |||
| * 获得年份 | |||
| * | |||
| * @param date | |||
| * @return | |||
| */ | |||
| public static int getYear(Date date) { | |||
| Calendar calendar = Calendar.getInstance(); | |||
| calendar.setTime(date); | |||
| return calendar.get(Calendar.YEAR); | |||
| } | |||
| /** | |||
| * 获得天 | |||
| * | |||
| * @param date | |||
| * @return | |||
| */ | |||
| public static int getDay(Date date) { | |||
| Calendar calendar = Calendar.getInstance(); | |||
| calendar.setTime(date); | |||
| return calendar.get(Calendar.DATE); | |||
| } | |||
| /** | |||
| * 获得天 | |||
| * | |||
| * @param date | |||
| * @return | |||
| */ | |||
| public static int getDayOfYear(Date date) { | |||
| Calendar calendar = Calendar.getInstance(); | |||
| calendar.setTime(date); | |||
| return calendar.get(Calendar.DAY_OF_YEAR); | |||
| } | |||
| /** | |||
| * 获得小时 | |||
| * | |||
| * @param date | |||
| * @return | |||
| */ | |||
| public static int getHours(Date date) { | |||
| Calendar calendar = Calendar.getInstance(); | |||
| calendar.setTime(date); | |||
| return calendar.get(Calendar.HOUR_OF_DAY); | |||
| } | |||
| public static int getMinutes(Date date) { | |||
| Calendar calendar = Calendar.getInstance(); | |||
| calendar.setTime(date); | |||
| return calendar.get(Calendar.MINUTE); | |||
| } | |||
| public static int getSeconds(Date date) { | |||
| Calendar calendar = Calendar.getInstance(); | |||
| calendar.setTime(date); | |||
| return calendar.get(Calendar.SECOND); | |||
| } | |||
| public static int getMILLISECOND(Date date) { | |||
| Calendar calendar = Calendar.getInstance(); | |||
| calendar.setTime(date); | |||
| return calendar.get(Calendar.MILLISECOND); | |||
| } | |||
| public static long getTimeDiff(Date beginDate, Date endDate) { | |||
| return (endDate.getTime() - beginDate.getTime()) / 1000; | |||
| } | |||
| public static int getDaysOfMonth(int year, int month) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.set(Calendar.YEAR, year); | |||
| cal.set(Calendar.MONTH, month);// 7月 | |||
| return cal.getActualMaximum(Calendar.DATE); | |||
| } | |||
| public static long convertDate2Long(Date date) { | |||
| if (date == null) { | |||
| return 0l; | |||
| } | |||
| return date.getTime(); | |||
| } | |||
| public static Date convertLong2Date(Long unixTimestamp) { | |||
| if (unixTimestamp != null) { | |||
| return new java.util.Date(unixTimestamp); | |||
| } | |||
| return null; | |||
| } | |||
| /** | |||
| * 判断时间是否在指定的时间范围内。 | |||
| * | |||
| * @param low | |||
| * @param high | |||
| * @return | |||
| */ | |||
| public static boolean between(Date low, Date high) { | |||
| return currentDate().after(low) && currentDate().before(high); | |||
| } | |||
| public static int getDayValue(Date date) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(date); | |||
| int i = cal.get(Calendar.DAY_OF_WEEK); | |||
| if (i == 1) { | |||
| return 7; | |||
| } else { | |||
| return i - 1; | |||
| } | |||
| } | |||
| public static boolean isBefore(Date date1, Date date2) { | |||
| return date1.getTime() < date2.getTime(); | |||
| } | |||
| public static Date average(Date date1, Date date2) { | |||
| return new Date((date1.getTime() + date2.getTime()) / 2); | |||
| } | |||
| public static int getDaysOfMonth(Date date) { | |||
| Calendar cal = Calendar.getInstance(); | |||
| cal.setTime(date); | |||
| return cal.get(Calendar.DAY_OF_MONTH); | |||
| } | |||
| public static int getCurrentRemainingSeconds() { | |||
| // *** 计算过期时间 一整天 获取 时:分:秒 | |||
| Calendar calendar = Calendar.getInstance(); | |||
| int hours = calendar.get(Calendar.HOUR_OF_DAY); // 时 | |||
| int minutes = calendar.get(Calendar.MINUTE); // 分 | |||
| int seconds = calendar.get(Calendar.SECOND); // 秒 | |||
| return 24 * 60 * 60 - hours * 60 * 60 - minutes * 60 - seconds; | |||
| } | |||
| public static Long getTimeLong(String str) { | |||
| return parse(str, DATETIME_FORMAT_PATTERN).getTime(); | |||
| } | |||
| public static String getStringTodayC() { | |||
| Date currentTime = new Date(); | |||
| SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS"); | |||
| String dateString = formatter.format(currentTime); | |||
| return dateString; | |||
| } | |||
| public static String getStringTodayB() { | |||
| Date currentTime = new Date(); | |||
| SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); | |||
| String dateString = formatter.format(currentTime); | |||
| return dateString; | |||
| } | |||
| /////////////////////////////////////////////////// | |||
| // 引用s https://blog.csdn.net/tz_gx/article/details/25284237 | |||
| // s 上月第一天 | |||
| public static Date getPreviousMonthDayBegin() { | |||
| Calendar lastDate = Calendar.getInstance(); | |||
| lastDate.set(Calendar.DATE, 1);// 设为当前月的1号 | |||
| lastDate.add(Calendar.MONTH, -1);// 减一个月,变为上月的1号 | |||
| return lastDate.getTime(); | |||
| } | |||
| // s 获得上月最后一天的日期 | |||
| public static Date getPreviousMonthDayEnd() { | |||
| Calendar lastDate = Calendar.getInstance(); | |||
| lastDate.add(Calendar.MONTH, -1);// 减一个月 | |||
| lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天 | |||
| lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天 | |||
| return lastDate.getTime(); | |||
| } | |||
| // s 获取当月第一天 | |||
| public static Date getCurrentMonthDayBegin() { | |||
| Calendar lastDate = Calendar.getInstance(); | |||
| lastDate.set(Calendar.DATE, 1);// 设为当前月的1号 | |||
| return lastDate.getTime(); | |||
| } | |||
| // s 计算当月最后一天,返回字符串 | |||
| public static Date getCurrentMonthDayEnd() { | |||
| Calendar lastDate = Calendar.getInstance(); | |||
| lastDate.set(Calendar.DATE, 1);// 设为当前月的1号 | |||
| lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号 | |||
| lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天 | |||
| return lastDate.getTime(); | |||
| } | |||
| // s 获得本周一的日期 | |||
| public static Date getCurrentWeekDayBegin() { | |||
| int mondayPlus = getMondayPlus(); | |||
| GregorianCalendar currentDate = new GregorianCalendar(); | |||
| currentDate.add(GregorianCalendar.DATE, mondayPlus); | |||
| return currentDate.getTime(); | |||
| } | |||
| // s 获得本周星期日的日期 | |||
| public static Date getCurrentWeekDayEnd() { | |||
| int mondayPlus = getMondayPlus(); | |||
| GregorianCalendar currentDate = new GregorianCalendar(); | |||
| currentDate.add(GregorianCalendar.DATE, mondayPlus + 6); | |||
| Date monday = currentDate.getTime(); | |||
| return monday; | |||
| } | |||
| // s 获得当前日期与本周一相差的天数 | |||
| private static int getMondayPlus() { | |||
| Calendar cd = Calendar.getInstance(); | |||
| // 获得今天是一周的第几天,星期日是第一天,星期二是第二天...... | |||
| int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // s 因为按中国礼拜一作为第一天所以这里减1 | |||
| if (dayOfWeek == 1) { | |||
| return 0; | |||
| } else { | |||
| return 1 - dayOfWeek; | |||
| } | |||
| } | |||
| // s 获得上周星期一的日期 | |||
| public static Date getPreviousWeekDayBegin() { | |||
| int mondayPlus = getMondayPlus(); | |||
| GregorianCalendar currentDate = new GregorianCalendar(); | |||
| currentDate.add(GregorianCalendar.DATE, mondayPlus - 7); | |||
| return currentDate.getTime(); | |||
| } | |||
| // s 获得上周星期日的日期 | |||
| public static Date getPreviousWeekDayEnd() { | |||
| int mondayPlus = getMondayPlus(); | |||
| GregorianCalendar currentDate = new GregorianCalendar(); | |||
| currentDate.add(GregorianCalendar.DATE, mondayPlus - 1); | |||
| return currentDate.getTime(); | |||
| } | |||
| public static int getAge(Date birthDay){ | |||
| Calendar cal = Calendar.getInstance(); | |||
| if (cal.before(birthDay)) { //出生日期晚于当前时间,无法计算 | |||
| return 0; | |||
| } | |||
| int yearNow = cal.get(Calendar.YEAR); //当前年份 | |||
| int monthNow = cal.get(Calendar.MONTH); //当前月份 | |||
| int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期 | |||
| cal.setTime(birthDay); | |||
| int yearBirth = cal.get(Calendar.YEAR); | |||
| int monthBirth = cal.get(Calendar.MONTH); | |||
| int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); | |||
| int age = yearNow - yearBirth; //计算整岁数 | |||
| if (monthNow <= monthBirth) { | |||
| if (monthNow == monthBirth) { | |||
| if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前,年龄减一 | |||
| }else{ | |||
| age--;//当前月份在生日之前,年龄减一 | |||
| } | |||
| } | |||
| return age; | |||
| } | |||
| public static Date getCurrZoneGMT0(){ | |||
| SimpleDateFormat dateFormat = new SimpleDateFormat(DATETIME_FORMAT_PATTERN); | |||
| dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00")); | |||
| return parse(dateFormat.format(new Date()), DATETIME_FORMAT_PATTERN) ; | |||
| } | |||
| /////////////////////////////////// | |||
| public static void main(String[] args) { | |||
| // System.out.println(getMILLISECOND(new Date())); | |||
| System.out.println(format(getCurrZoneGMT0(),DATETIME_FORMAT_PATTERN)); | |||
| } | |||
| } | |||
| @@ -0,0 +1,21 @@ | |||
| package com.inet.ailink.receiver.common.utils; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| public class IpUtils { | |||
| public static String getIpAddr(HttpServletRequest request) { | |||
| String ip = request.getHeader("x-forwarded-for"); | |||
| if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |||
| ip = request.getHeader("Proxy-Client-IP"); | |||
| } | |||
| if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |||
| ip = request.getHeader("WL-Proxy-Client-IP"); | |||
| } | |||
| if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | |||
| ip = request.getRemoteAddr(); | |||
| } | |||
| return ip; | |||
| } | |||
| } | |||
| @@ -0,0 +1,125 @@ | |||
| package com.inet.ailink.receiver.common.utils; | |||
| import java.io.IOException; | |||
| import org.apache.commons.lang.StringUtils; | |||
| import com.fasterxml.jackson.annotation.JsonInclude.Include; | |||
| import com.fasterxml.jackson.core.JsonProcessingException; | |||
| import com.fasterxml.jackson.databind.DeserializationFeature; | |||
| import com.fasterxml.jackson.databind.JavaType; | |||
| import com.fasterxml.jackson.databind.ObjectMapper; | |||
| import com.fasterxml.jackson.databind.SerializationFeature; | |||
| import com.fasterxml.jackson.databind.util.JSONPObject; | |||
| public class JsonMapper { | |||
| private ObjectMapper mapper; | |||
| public JsonMapper() { | |||
| this(null); | |||
| } | |||
| public JsonMapper(Include include) { | |||
| mapper = new ObjectMapper(); | |||
| if (include != null) { | |||
| mapper.setSerializationInclusion(include); | |||
| } | |||
| mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | |||
| mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); | |||
| } | |||
| public static JsonMapper nonEmptyMapper() { | |||
| return new JsonMapper(Include.NON_EMPTY); | |||
| } | |||
| public static JsonMapper nonDefaultMapper() { | |||
| return new JsonMapper(Include.NON_DEFAULT); | |||
| } | |||
| public static JsonMapper nonAlwaysMapper() { | |||
| return new JsonMapper(Include.ALWAYS); | |||
| } | |||
| public String toJson(Object object) { | |||
| try { | |||
| return mapper.writeValueAsString(object); | |||
| } catch (IOException e) { | |||
| return null; | |||
| } | |||
| } | |||
| public <T> T fromMapToObject(Object map, Class<T> cls) { | |||
| return this.fromJson(this.toJson(map), cls); | |||
| } | |||
| public <T> T fromMapToObject(Object map, JavaType cls) { | |||
| return this.fromJson(this.toJson(map), cls); | |||
| } | |||
| public <T> T fromJson(String jsonString, Class<T> clazz) { | |||
| if (StringUtils.isEmpty(jsonString)) { | |||
| return null; | |||
| } | |||
| try { | |||
| return mapper.readValue(jsonString, clazz); | |||
| } catch (IOException e) { | |||
| e.printStackTrace(); | |||
| return null; | |||
| } | |||
| } | |||
| public <T> T fromJson(String jsonString, JavaType javaType) { | |||
| if (StringUtils.isEmpty(jsonString)) { | |||
| return null; | |||
| } | |||
| try { | |||
| return (T) mapper.readValue(jsonString, javaType); | |||
| } catch (IOException e) { | |||
| e.printStackTrace(); | |||
| return null; | |||
| } | |||
| } | |||
| public JavaType createCollectionType(Class<?> collectionClass, Class<?>... elementClasses) { | |||
| return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); | |||
| } | |||
| public <T> T update(String jsonString, T object) { | |||
| try { | |||
| return (T) mapper.readerForUpdating(object).readValue(jsonString); | |||
| } catch (JsonProcessingException e) { | |||
| e.printStackTrace(); | |||
| } catch (IOException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| return null; | |||
| } | |||
| public String toJsonP(String functionName, Object object) { | |||
| return toJson(new JSONPObject(functionName, object)); | |||
| } | |||
| public void enableEnumUseToString() { | |||
| mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); | |||
| mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); | |||
| } | |||
| public ObjectMapper getMapper() { | |||
| return mapper; | |||
| } | |||
| } | |||
| @@ -0,0 +1,62 @@ | |||
| package com.inet.ailink.receiver.common.utils; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| public class JsonUtils { | |||
| protected static JsonMapper jmapper = JsonMapper.nonEmptyMapper(); | |||
| /** | |||
| * json数据结构转换对象 | |||
| * @param jsonStr | |||
| * @param clazz | |||
| * @return | |||
| */ | |||
| public static <T> T fromJson(String jsonStr,Class<T> clazz){ | |||
| return jmapper.fromJson(jsonStr, clazz); | |||
| } | |||
| /**json数据结构转换List<MyBean> | |||
| * @param jsonStr | |||
| * @param clazz | |||
| * @return | |||
| */ | |||
| public static <T> List<T> fromJson2List(String jsonStr,Class<T> clazz){ | |||
| return jmapper.fromJson(jsonStr, | |||
| JsonMapper.nonDefaultMapper().createCollectionType(List.class, clazz)); | |||
| } | |||
| /** | |||
| * 对象转换json数据结构 | |||
| * @param obj | |||
| * @return | |||
| */ | |||
| public static String toJson(Object obj){ | |||
| return jmapper.toJson(obj); | |||
| } | |||
| public static void main(String[] args){ | |||
| /*String s="[{\"level\":\"3\",\"time\":\"1428551739622\"},{\"level\":\"4\",\"time\":\"1428551739623\"}]"; | |||
| List<DeviceSignalRecordRequest> ss=fromJson2List(s, DeviceSignalRecordRequest.class); | |||
| String sss=toJson(new DeviceSignalRecordRequest()); | |||
| System.out.println(ss);*/ | |||
| List<String> a=new ArrayList<String>(); | |||
| List<String> b=new ArrayList<String>(); | |||
| List<List<String>> cc =new ArrayList<List<String>>(); | |||
| a.add("11:00"); | |||
| a.add("12:00"); | |||
| b.add("13:00"); | |||
| b.add("14:00"); | |||
| cc.add(a); | |||
| cc.add(b); | |||
| System.out.println(JsonUtils.toJson(a)); | |||
| List<String> k=JsonUtils.fromJson2List(JsonUtils.toJson(a),String.class); | |||
| for(String s:k){ | |||
| System.out.println(s); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,54 @@ | |||
| package com.inet.ailink.receiver.common.utils; | |||
| import java.security.MessageDigest; | |||
| import java.util.Arrays; | |||
| import java.util.Map; | |||
| import java.util.Set; | |||
| public class MD5Util { | |||
| public MD5Util(){} | |||
| /** | |||
| * 生成签名 | |||
| * @param data 待签名数据 | |||
| * @param key API密钥 | |||
| * @return 签名 | |||
| */ | |||
| public static String generateSignature(Map<String, Object> data, String key) throws Exception { | |||
| Set<String> keySet = data.keySet(); | |||
| String[] keyArray = keySet.toArray(new String[keySet.size()]); | |||
| Arrays.sort(keyArray); | |||
| StringBuilder sb = new StringBuilder(); | |||
| for (String k : keyArray) { | |||
| if(data.get(k) != null){ | |||
| String keyValueTemp = data.get(k).toString().trim(); | |||
| if (keyValueTemp.length() > 0) // 参数值为空,则不参与签名 | |||
| sb.append(k).append("=").append(keyValueTemp).append("&"); | |||
| } | |||
| } | |||
| sb.append("key=").append(key); | |||
| String md5Str = getMD5(sb.toString()).toUpperCase(); | |||
| return md5Str; | |||
| } | |||
| /** | |||
| * 生成 MD5 | |||
| * @param data 待处理数据 | |||
| * @return MD5结果 | |||
| */ | |||
| public static String getMD5(String data) throws Exception { | |||
| MessageDigest md = MessageDigest.getInstance("MD5"); | |||
| byte[] array = md.digest(data.getBytes("UTF-8")); | |||
| StringBuilder sb = new StringBuilder(); | |||
| for (byte item : array) { | |||
| sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); | |||
| } | |||
| return sb.toString().toUpperCase(); | |||
| } | |||
| public static void main(String [] args) throws Exception{ | |||
| } | |||
| } | |||
| @@ -0,0 +1,41 @@ | |||
| package com.inet.ailink.receiver.common.utils; | |||
| import java.util.regex.Pattern; | |||
| public class StringSelfUtil { | |||
| public static boolean startWithChar(String s) { | |||
| if (s != null && s.length() > 0) { | |||
| String start = s.trim().substring(0, 1); | |||
| Pattern pattern = Pattern.compile("^[A-Za-z]+$"); | |||
| return pattern.matcher(start).matches(); | |||
| } else { | |||
| return false; | |||
| } | |||
| } | |||
| public static boolean isNumeric(String str){ | |||
| Pattern pattern = Pattern.compile("[0-9]*"); | |||
| return pattern.matcher(str).matches(); | |||
| } | |||
| public static String byteArrayToStr(byte[] byteArray) { | |||
| if (byteArray == null) { | |||
| return null; | |||
| } | |||
| String str = new String(byteArray); | |||
| return str; | |||
| } | |||
| public static boolean isEmpty(String s){ | |||
| if(s==null || s.equals("") || s.isEmpty()){ | |||
| return true; | |||
| }else{ | |||
| return false; | |||
| } | |||
| } | |||
| public static boolean isBlank(String s){ | |||
| return isEmpty(s); | |||
| } | |||
| } | |||
| @@ -0,0 +1,39 @@ | |||
| package com.inet.ailink.receiver.common.utils; | |||
| import java.io.BufferedReader; | |||
| import java.io.File; | |||
| import java.io.FileOutputStream; | |||
| import java.io.FileReader; | |||
| import java.io.IOException; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| public class TxtUtils { | |||
| public static List<String> txt2String(File file){ | |||
| StringBuilder result = new StringBuilder(); | |||
| List<String> list = new ArrayList<String>(); | |||
| try{ | |||
| BufferedReader br = new BufferedReader(new FileReader(file)); | |||
| String s = null; | |||
| while((s = br.readLine())!=null){ | |||
| list.add(s); | |||
| } | |||
| br.close(); | |||
| }catch(Exception e){ | |||
| e.printStackTrace(); | |||
| } | |||
| return list; | |||
| } | |||
| public static void string2Txt(String str, String logpath) throws IOException{ | |||
| byte[] sb = str.getBytes(); | |||
| //若是true 在原有的文本上追加写入 false覆盖原有的文本 ,不写默认为false | |||
| FileOutputStream fos = new FileOutputStream(logpath,true); | |||
| fos.write(sb); | |||
| fos.flush(); | |||
| fos.close(); | |||
| } | |||
| } | |||
| @@ -0,0 +1,47 @@ | |||
| package com.inet.ailink.receiver.common.vo; | |||
| import com.fasterxml.jackson.databind.annotation.JsonSerialize; | |||
| @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) | |||
| public class Response<T>{ | |||
| private String status; | |||
| public T data; | |||
| public T config; | |||
| public T msg; | |||
| public Response(){} | |||
| public T getData() { | |||
| return data; | |||
| } | |||
| public void setData(T data) { | |||
| this.data = data; | |||
| } | |||
| public String getStatus() { | |||
| return status; | |||
| } | |||
| public void setStatus(String status) { | |||
| this.status = status; | |||
| } | |||
| public T getConfig() { | |||
| return config; | |||
| } | |||
| public void setConfig(T config) { | |||
| this.config = config; | |||
| } | |||
| public T getMsg() { | |||
| return msg; | |||
| } | |||
| public void setMsg(T msg) { | |||
| this.msg = msg; | |||
| } | |||
| } | |||
| @@ -0,0 +1,31 @@ | |||
| package com.inet.ailink.receiver.controller; | |||
| import java.io.IOException; | |||
| import java.io.PrintWriter; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| public abstract class BaseController extends java.beans.PropertyEditorSupport | |||
| { | |||
| /** | |||
| * 写回任意原始数据 | |||
| * @param response | |||
| * @param result | |||
| * @throws IOException | |||
| */ | |||
| public void writeOutResponse(HttpServletResponse response,String result) throws IOException{ | |||
| response.setHeader("Access-Control-Allow-Origin",""); | |||
| response.setHeader("Access-Control-Allow-Credentials",""); | |||
| response.setHeader("Access-Control-Allow-Methods",""); | |||
| response.setHeader("Access-Control-Max-Age",""); | |||
| response.setHeader("Access-Control-Allow-Headers",""); | |||
| response.setHeader("X-Application-Context",""); | |||
| response.setHeader("Transfer-Encoding", ""); | |||
| response.setHeader("ResponseBodySize", result.length()+""); | |||
| PrintWriter out = response.getWriter(); | |||
| out.write(result); | |||
| out.flush(); | |||
| out.close(); | |||
| } | |||
| } | |||
| @@ -0,0 +1,51 @@ | |||
| package com.inet.ailink.receiver.controller; | |||
| import java.io.IOException; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.web.bind.annotation.PathVariable; | |||
| import org.springframework.web.bind.annotation.RequestBody; | |||
| import org.springframework.web.bind.annotation.RequestMapping; | |||
| import org.springframework.web.bind.annotation.ResponseBody; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| import com.inet.ailink.receiver.common.utils.JsonUtils; | |||
| import com.inet.ailink.receiver.service.impl.SysLogsServiceImpl; | |||
| @RestController | |||
| @RequestMapping(value="deData",produces={"application/json;charset=UTF-8"}) | |||
| public class DeDataBaseController extends BaseController { | |||
| @Autowired | |||
| SysLogsServiceImpl sysLogsServiceImpl; | |||
| /** | |||
| * 接受4G/WIFI+BLE设备测量结果数据 | |||
| * @param params | |||
| * @param request | |||
| * @param response | |||
| * @throws IOException | |||
| */ | |||
| @RequestMapping(value="pullData") | |||
| @ResponseBody | |||
| public Object saveDataBase(@RequestBody String params,HttpServletRequest request,HttpServletResponse response) throws IOException { | |||
| return sysLogsServiceImpl.save(params,request); | |||
| } | |||
| /** | |||
| * 获取4G/WIFI+BLE设备测量结果数据 | |||
| * @param date | |||
| * @param request | |||
| * @param response | |||
| * @throws IOException | |||
| */ | |||
| @RequestMapping(value="getData/{date}") | |||
| @ResponseBody | |||
| public String getData(@PathVariable String date,HttpServletRequest request,HttpServletResponse response) throws IOException { | |||
| return JsonUtils.toJson(sysLogsServiceImpl.getData(date)) ; | |||
| } | |||
| } | |||
| @@ -0,0 +1,145 @@ | |||
| package com.inet.ailink.receiver.entity; | |||
| import com.inet.ailink.receiver.common.vo.Response; | |||
| /** | |||
| * @author Dream_Kang | |||
| * | |||
| */ | |||
| @SuppressWarnings("serial") | |||
| public class SysLogs implements Comparable<SysLogs>{ | |||
| /** | |||
| * 原始参数 | |||
| */ | |||
| private String encryptParams; | |||
| /** | |||
| * 数据接收时间 | |||
| */ | |||
| private String createTime; | |||
| /** | |||
| * 访问者IP地址 | |||
| */ | |||
| private String ip; | |||
| /** | |||
| * 请求URI | |||
| */ | |||
| private String uri; | |||
| /** | |||
| * 解密后的设备数据 | |||
| */ | |||
| private Object decryptParams; | |||
| /** | |||
| * 签名 | |||
| */ | |||
| private String sign; | |||
| /** | |||
| * appKey | |||
| */ | |||
| private String productKey; | |||
| /** | |||
| * errorMsg | |||
| */ | |||
| private String errorMsg; | |||
| private Long sortCloumns; | |||
| private Response<Object> result; | |||
| public String getEncryptParams() { | |||
| return encryptParams; | |||
| } | |||
| public void setEncryptParams(String encryptParams) { | |||
| this.encryptParams = encryptParams; | |||
| } | |||
| public String getCreateTime() { | |||
| return createTime; | |||
| } | |||
| public void setCreateTime(String createTime) { | |||
| this.createTime = createTime; | |||
| } | |||
| public String getIp() { | |||
| return ip; | |||
| } | |||
| public void setIp(String ip) { | |||
| this.ip = ip; | |||
| } | |||
| public String getUri() { | |||
| return uri; | |||
| } | |||
| public void setUri(String uri) { | |||
| this.uri = uri; | |||
| } | |||
| public Object getDecryptParams() { | |||
| return decryptParams; | |||
| } | |||
| public void setDecryptParams(Object decryptParams) { | |||
| this.decryptParams = decryptParams; | |||
| } | |||
| public String getSign() { | |||
| return sign; | |||
| } | |||
| public void setSign(String sign) { | |||
| this.sign = sign; | |||
| } | |||
| public String getProductKey() { | |||
| return productKey; | |||
| } | |||
| public void setProductKey(String productKey) { | |||
| this.productKey = productKey; | |||
| } | |||
| public String getErrorMsg() { | |||
| return errorMsg; | |||
| } | |||
| public void setErrorMsg(String errorMsg) { | |||
| this.errorMsg = errorMsg; | |||
| } | |||
| /** | |||
| * @return the sortCloumns | |||
| */ | |||
| public Long getSortCloumns() { | |||
| return sortCloumns; | |||
| } | |||
| /** | |||
| * @param sortCloumns the sortCloumns to set | |||
| */ | |||
| public void setSortCloumns(Long sortCloumns) { | |||
| this.sortCloumns = sortCloumns; | |||
| } | |||
| @Override | |||
| public int compareTo(SysLogs o) { | |||
| return (int) (o.sortCloumns - this.sortCloumns); | |||
| } | |||
| public Response<Object> getResult() { | |||
| return result; | |||
| } | |||
| public void setResult(Response<Object> result) { | |||
| this.result = result; | |||
| } | |||
| } | |||
| @@ -0,0 +1,7 @@ | |||
| package com.inet.ailink.receiver.mapper; | |||
| public interface ISysLogsDao{ | |||
| } | |||
| @@ -0,0 +1,17 @@ | |||
| /** | |||
| * | |||
| */ | |||
| package com.inet.ailink.receiver.service; | |||
| import java.util.List; | |||
| import com.inet.ailink.receiver.common.exception.BizException; | |||
| public interface IBaseService<E>{ | |||
| public int update(E entity) throws BizException; | |||
| public int save(List<E> entityList) throws BizException; | |||
| public int save(E entity) throws BizException; | |||
| E get(Long id) throws BizException; | |||
| int remove(Long id) throws BizException; | |||
| public List<E> selectListObjByAttribute(Object entity) throws BizException ; | |||
| } | |||
| @@ -0,0 +1,110 @@ | |||
| package com.inet.ailink.receiver.service.impl; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.math.BigDecimal; | |||
| import java.nio.charset.StandardCharsets; | |||
| import java.util.ArrayList; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import com.inet.ailink.receiver.common.utils.*; | |||
| import org.springframework.stereotype.Service; | |||
| import com.inet.ailink.receiver.common.enums.StatusCode; | |||
| import com.inet.ailink.receiver.common.vo.Response; | |||
| import com.inet.ailink.receiver.entity.SysLogs; | |||
| import java.util.Collections; | |||
| @Service | |||
| public class SysLogsServiceImpl | |||
| { | |||
| String logPath = "-log.log"; | |||
| String productKey = "FjPkkwq902nAodaC"; | |||
| String productSecret = "kPO5mod7mi9jhobgabvopqinkAnujh"; | |||
| public Response<Object> save(String params, HttpServletRequest request) throws IOException { | |||
| Response<Object> result = new Response<Object>(); | |||
| SysLogs log = new SysLogs(); | |||
| try { | |||
| Map<String,String> paramsMap = JsonUtils.fromJson(params, Map.class); | |||
| String deviceDataEncrypt = paramsMap.get("params"); | |||
| log.setEncryptParams(deviceDataEncrypt); | |||
| log.setIp(IpUtils.getIpAddr(request)); | |||
| log.setUri(request.getRequestURI()); | |||
| log.setCreateTime(DateUtils.getStringTodayB()); | |||
| log.setSortCloumns(DateUtils.getCurTime()); | |||
| //获取请求头信息中的productKey,通过productKey,获取productSecret | |||
| // String productKey = request.getHeader("productKey"); | |||
| // String productSecret = ""; | |||
| //解析数据 | |||
| //1.解密数据,使用AES/CBC/PKCS5Padding进行解密 | |||
| String deviceDataDecrypt = AESUtils.aesCBCDecrypt(deviceDataEncrypt,productSecret,productKey); | |||
| //2.校验签名 | |||
| Map<String,Object> deviceDataMap = JsonUtils.fromJson(deviceDataDecrypt,Map.class); | |||
| String sign = MD5Util.generateSignature(deviceDataMap,productSecret); | |||
| String requestSign = request.getHeader("sign"); | |||
| if(!sign.equals(requestSign)){ | |||
| //处理签名不一致的情况 | |||
| } | |||
| //3.消息去重,同一条数据的requestId相同,可以根据requestId进行数据去重 | |||
| String requestId = deviceDataMap.get("requestId").toString(); | |||
| //4.业务处理,依据数据类型CID,区分不同类型的设备数据 | |||
| String cid = deviceDataMap.get("cid").toString(); | |||
| if(cid.equals("80")){//噪音计的CID=80 | |||
| saveNoiseData(deviceDataMap); | |||
| } | |||
| //保存日志记录 | |||
| log.setDecryptParams(deviceDataMap); | |||
| log.setProductKey(productKey); | |||
| log.setSign(requestSign); | |||
| } catch (Exception e) { | |||
| log.setErrorMsg(e.getMessage()); | |||
| } | |||
| //测试返回错误信息,重新推送 | |||
| int a = (int) (Math.random()*10); | |||
| if(a == 1 || a == 3 || a == 5){ | |||
| result.setStatus(StatusCode.FAIL.getCode()); | |||
| result.setMsg(StatusCode.FAIL.getMsg()); | |||
| }else{ | |||
| result.setStatus(StatusCode.SUCCESS.getCode()); | |||
| result.setMsg(StatusCode.SUCCESS.getMsg()); | |||
| } | |||
| log.setResult(result); | |||
| TxtUtils.string2Txt(JsonUtils.toJson(log)+"\n",DateUtils.format(new Date(),DateUtils.DATE_FORMAT_PATTERN_NO_SEPARATOR)+logPath); | |||
| return result; | |||
| } | |||
| /** | |||
| * 保存噪音计数据 | |||
| * @param deviceDataMap | |||
| */ | |||
| public void saveNoiseData(Map<String,Object> deviceDataMap ){ | |||
| //持久化数据或者进行消息通知等业务处理 | |||
| } | |||
| /** | |||
| * 获取保存的日志记录数据 | |||
| * @param date | |||
| * @return | |||
| */ | |||
| public Object getData(String date) { | |||
| if(StringSelfUtil.isEmpty(date)){ | |||
| date = DateUtils.format(new Date(),DateUtils.DATE_FORMAT_PATTERN_NO_SEPARATOR); | |||
| } | |||
| List<String> strList = TxtUtils.txt2String(new File(date+logPath)); | |||
| List<SysLogs> resultList = new ArrayList<SysLogs>(); | |||
| for(String obj:strList){ | |||
| resultList.add(JsonUtils.fromJson(obj, SysLogs.class)); | |||
| } | |||
| Collections.sort(resultList); | |||
| return resultList; | |||
| } | |||
| } | |||
| @@ -0,0 +1,21 @@ | |||
| spring.application.name=ailink-receiver-pull-demo | |||
| server.port=8099 | |||
| #spring.datasource.driver-class-name=com.mysql.jdbc.Driver | |||
| spring.datasource.driver-class-name= | |||
| spring.datasource.url= | |||
| spring.datasource.username= | |||
| spring.datasource.password= | |||
| spring.jpa.properties.hibernate.hbm2ddl.auto=update | |||
| spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect | |||
| spring.jpa.show-sql= true | |||
| mybatis.config-locations=classpath:mybatis/mybatis-config.xml | |||
| mybatis.mapper-locations=classpath:mybatis/mapper/*.xml | |||
| mybatis.type-aliases-package=com.inet.ailink.receiver.entity | |||
| logging.level.com.inet.ailink.receiver.mapper=debug | |||
| #jackson | |||
| spring.jackson.default-property-inclusion=non_null | |||
| spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=true | |||
| @@ -0,0 +1,7 @@ | |||
| spring.profiles.active=dev | |||
| #allow upload size | |||
| #spring.http.multipart.maxFileSize = 20Mb | |||
| #spring.http.multipart.maxRequestSize=100Mb | |||
| spring.servlet.multipart.maxFileSize=20MB | |||
| spring.servlet.multipart.maxRequestSize=100MB | |||
| @@ -0,0 +1,258 @@ | |||
| <?xml version="1.0" encoding="UTF-8" ?> | |||
| <!DOCTYPE mapper | |||
| PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" | |||
| "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> | |||
| <mapper namespace="com.inet.ailink.receiver.mapper.ISysLogsDao"> | |||
| <resultMap id="sysLogsResult" type="com.inet.ailink.receiver.entity.SysLogs"> | |||
| </resultMap> | |||
| <!-- 用于select查询公用抽取的列 --> | |||
| <sql id="sysLogsColumns"> | |||
| <![CDATA[ | |||
| id as id, | |||
| beanName as beanName, | |||
| methodName as methodName, | |||
| serviceType as serviceType, | |||
| uri as uri, | |||
| localHostAddr as localHostAddr, | |||
| remoteAddr as remoteAddr, | |||
| userId as userId, | |||
| userName as userName, | |||
| startTime as startTime, | |||
| executeTime as executeTime, | |||
| pramString as pramString, | |||
| operatorType as operatorType, | |||
| isSuccess as isSuccess, | |||
| failMessage as failMessage, | |||
| responseResult as responseResult, | |||
| logLevel as logLevel, | |||
| executeSql as executeSql, | |||
| rowId as rowId, | |||
| delStatus as delStatus, | |||
| appId as appId | |||
| ]]> | |||
| </sql> | |||
| <!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql --> | |||
| <insert id="insert" parameterType="com.inet.ailink.receiver.entity.SysLogs" | |||
| useGeneratedKeys="true" keyProperty="id" | |||
| flushCache="true"> | |||
| <![CDATA[ | |||
| INSERT INTO | |||
| sys_logs ( | |||
| beanName , | |||
| methodName , | |||
| serviceType , | |||
| uri , | |||
| localHostAddr , | |||
| remoteAddr , | |||
| userId , | |||
| userName , | |||
| startTime , | |||
| executeTime , | |||
| pramString , | |||
| operatorType , | |||
| isSuccess , | |||
| failMessage , | |||
| responseResult , | |||
| logLevel , | |||
| executeSql , | |||
| rowId , | |||
| delStatus , | |||
| appId | |||
| ) VALUES ( | |||
| #{beanName,javaType=string,jdbcType=VARCHAR} , | |||
| #{methodName,javaType=string,jdbcType=VARCHAR} , | |||
| #{serviceType,javaType=string,jdbcType=VARCHAR} , | |||
| #{uri,javaType=string,jdbcType=VARCHAR} , | |||
| #{localHostAddr,javaType=string,jdbcType=VARCHAR} , | |||
| #{remoteAddr,javaType=string,jdbcType=VARCHAR} , | |||
| #{userId,javaType=long,jdbcType=BIGINT} , | |||
| #{userName,javaType=string,jdbcType=VARCHAR} , | |||
| #{startTime,javaType=date,jdbcType=TIMESTAMP} , | |||
| #{executeTime,javaType=long,jdbcType=BIGINT} , | |||
| #{pramString,javaType=string,jdbcType=VARCHAR} , | |||
| #{operatorType,javaType=string,jdbcType=VARCHAR} , | |||
| #{isSuccess,javaType=integer,jdbcType=INTEGER} , | |||
| #{failMessage,javaType=string,jdbcType=VARCHAR} , | |||
| #{responseResult,javaType=string,jdbcType=LONGVARCHAR} , | |||
| #{logLevel,javaType=integer,jdbcType=INTEGER} , | |||
| #{executeSql,javaType=string,jdbcType=VARCHAR} , | |||
| #{rowId,javaType=string,jdbcType=VARCHAR} , | |||
| #{delStatus,javaType=integer,jdbcType=INTEGER}, | |||
| #{appId,javaType=long,jdbcType=BIGINT} | |||
| ) | |||
| ]]> | |||
| </insert> | |||
| <update id="update" parameterType="com.inet.ailink.receiver.entity.SysLogs"> | |||
| UPDATE sys_logs SET | |||
| <if test="beanName != null"> | |||
| beanName = #{beanName,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="methodName != null"> | |||
| methodName = #{methodName,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="serviceType != null"> | |||
| serviceType = #{serviceType,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="uri != null"> | |||
| uri = #{uri,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="localHostAddr != null"> | |||
| localHostAddr = #{localHostAddr,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="remoteAddr != null"> | |||
| remoteAddr = #{remoteAddr,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="userId != null"> | |||
| userId = #{userId,javaType=long,jdbcType=BIGINT} , | |||
| </if> | |||
| <if test="userName != null"> | |||
| userName = #{userName,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="startTime != null"> | |||
| startTime = #{startTime,javaType=date,jdbcType=TIMESTAMP} , | |||
| </if> | |||
| <if test="executeTime != null"> | |||
| executeTime = #{executeTime,javaType=long,jdbcType=BIGINT} , | |||
| </if> | |||
| <if test="pramString != null"> | |||
| pramString = #{pramString,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="operatorType != null"> | |||
| operatorType = #{operatorType,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="isSuccess != null"> | |||
| isSuccess = #{isSuccess,javaType=integer,jdbcType=INTEGER} , | |||
| </if> | |||
| <if test="failMessage != null"> | |||
| failMessage = #{failMessage,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="responseResult != null"> | |||
| responseResult = #{responseResult,javaType=string,jdbcType=LONGVARCHAR} , | |||
| </if> | |||
| <if test="logLevel != null"> | |||
| logLevel = #{logLevel,javaType=integer,jdbcType=INTEGER} , | |||
| </if> | |||
| <if test="executeSql != null"> | |||
| executeSql = #{executeSql,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="rowId != null"> | |||
| rowId = #{rowId,javaType=string,jdbcType=VARCHAR} , | |||
| </if> | |||
| <if test="delStatus != null"> | |||
| delStatus = #{delStatus,javaType=integer,jdbcType=INTEGER} | |||
| </if> | |||
| WHERE | |||
| id = #{id} | |||
| </update> | |||
| <delete id="delete" parameterType="java.lang.Long"> | |||
| <![CDATA[ | |||
| delete from sys_logs where | |||
| id = #{id} | |||
| ]]> | |||
| </delete> | |||
| <select id="getById" parameterType="long" resultMap="sysLogsResult" flushCache="false"> | |||
| select <include refid="sysLogsColumns" /> | |||
| <![CDATA[ | |||
| from sys_logs | |||
| where | |||
| id = #{id} | |||
| ]]> | |||
| </select> | |||
| <select id="getByIds" parameterType="list" resultMap="sysLogsResult" flushCache="false"> | |||
| select <include refid="sysLogsColumns" /> | |||
| from sys_logs | |||
| where id in | |||
| <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> | |||
| #{item} </foreach> | |||
| </select> | |||
| <sql id="sysLogsDynamicWhere"> | |||
| <!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用,具体请查看Ognl类 --> | |||
| <where> | |||
| <if test="@Ognl@isNotEmpty(id)"> | |||
| and id = #{id} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(beanName)"> | |||
| and beanName = #{beanName} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(methodName)"> | |||
| and methodName = #{methodName} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(serviceType)"> | |||
| and serviceType = #{serviceType} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(uri)"> | |||
| and uri = #{uri} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(localHostAddr)"> | |||
| and localHostAddr = #{localHostAddr} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(remoteAddr)"> | |||
| and remoteAddr = #{remoteAddr} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(userId)"> | |||
| and userId = #{userId} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(userName)"> | |||
| and userName = #{userName} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(startTime)"> | |||
| and startTime = #{startTime} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(executeTime)"> | |||
| and executeTime = #{executeTime} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(pramString)"> | |||
| and pramString = #{pramString} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(operatorType)"> | |||
| and operatorType = #{operatorType} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(isSuccess)"> | |||
| and isSuccess = #{isSuccess} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(failMessage)"> | |||
| and failMessage = #{failMessage} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(responseResult)"> | |||
| and responseResult = #{responseResult} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(logLevel)"> | |||
| and logLevel = #{logLevel} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(executeSql)"> | |||
| and executeSql = #{executeSql} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(rowId)"> | |||
| and rowId = #{rowId} | |||
| </if> | |||
| <if test="@Ognl@isNotEmpty(delStatus)"> | |||
| and delStatus = #{delStatus} | |||
| </if> | |||
| </where> | |||
| </sql> | |||
| <select id="getAll" resultMap="sysLogsResult" flushCache="false"> | |||
| select <include refid="sysLogsColumns" /> | |||
| from sys_logs | |||
| <if test="@Ognl@isNotEmpty(sortColumns)"> | |||
| ORDER BY ${sortColumns} | |||
| </if> | |||
| </select> | |||
| <select id="getObjByAttribute" resultMap="sysLogsResult"> | |||
| select <include refid="sysLogsColumns" /> | |||
| from sys_logs | |||
| <include refid="sysLogsDynamicWhere"/> | |||
| </select> | |||
| </mapper> | |||
| @@ -0,0 +1,12 @@ | |||
| <?xml version="1.0" encoding="UTF-8" ?> | |||
| <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> | |||
| <configuration> | |||
| <typeAliases> | |||
| <typeAlias alias="Integer" type="java.lang.Integer" /> | |||
| <typeAlias alias="Long" type="java.lang.Long" /> | |||
| <typeAlias alias="HashMap" type="java.util.HashMap" /> | |||
| <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" /> | |||
| <typeAlias alias="ArrayList" type="java.util.ArrayList" /> | |||
| <typeAlias alias="LinkedList" type="java.util.LinkedList" /> | |||
| </typeAliases> | |||
| </configuration> | |||