将一期项目中相关代码合并到二期项目中去
This commit is contained in:
@@ -118,8 +118,38 @@
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>${mybatis.plus}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 阿里JSON解析器 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>2.0.53</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.25</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-models</artifactId>
|
||||
<version>1.6.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -171,4 +171,12 @@ public class Constants
|
||||
*/
|
||||
public static final String[] JOB_ERROR_STR = { "java.net.URL", "javax.naming.InitialContext", "org.yaml.snakeyaml",
|
||||
"org.springframework", "org.apache", "com.ltgk.smartFishingPort.common.utils.file", "com.ltgk.smartFishingPort.common.config", "com.ltgk.smartFishingPort.generator" };
|
||||
|
||||
public static final String SUCCESS_CODE = "0";
|
||||
|
||||
public static final String CODE = "code";
|
||||
|
||||
// 默认boole类型
|
||||
public final static int FLAG_TRUE = 1;
|
||||
public final static int FLAG_FALSE = 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.ltgk.smartFishingPort.common.constant;
|
||||
|
||||
/**
|
||||
* 司空平台接口地址
|
||||
*/
|
||||
public class DroneConstruct {
|
||||
public final static String SYSTEM_STATUS = "system_status"; // 司空平台状态
|
||||
public final static String PROJECT = "project"; // 获取组织下的项目列表
|
||||
public final static String DEVICE = "device"; // 获取项目下的设备列表
|
||||
public final static String COMMAND = "device/$command/command";//实时控制指令下发
|
||||
public final static String AIRPORT_CAMERA_SWITCH = "device/change-camera"; // 机场相机切换
|
||||
public final static String DRONE_CAMERA_SWITCH = "device/change-lens"; // 飞行器相机切换
|
||||
public final static String DRONE_CONTROL_OBTAIN = "device/control"; // 获取飞行器控制权
|
||||
public final static String DRONE_CONTROL_RELEASE = "device/control"; // 释放飞行器控制权
|
||||
public final static String DRONE_WAY_LINE = "wayline"; // 获取航线列表
|
||||
public final static String DRONE_WAY_LINE_DETAIL = "wayline/$waylineId"; // 获取航线详情
|
||||
public final static String START_LIVE = "live-stream/start"; // 开始直播
|
||||
public final static String CREATE_FLIGHT_TASK = "flight-task";
|
||||
public final static String CREATE_FLIGHT_TASK_STATUS = "flight-task/$taskUuid/status";
|
||||
public final static String GET_FLIGHT_TASK_LIST = "flight-task/list";
|
||||
public final static String ONE_TOUCH_TAKEOFF = "task/api/v1/workspaces/$taskUuid/flight-tasks/drone-take-off";
|
||||
public final static String DRONE_CONTROL_OBTAIN_V2 = "drc/api/v1/projects/$projUuid/cloud_controls"; // 获取飞行器控制权
|
||||
public final static String ADD_STREAM_DETECT_TASK = "add_stream_detect_task";
|
||||
public final static String CONTROL_DETECTION = "control_detection";
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ltgk.smartFishingPort.common.constant;
|
||||
|
||||
/**
|
||||
* @Author: zhouyaoyao
|
||||
* @Date 2025/2/11 13:19
|
||||
* Description: 全局常量
|
||||
*/
|
||||
public class GlobalConstant {
|
||||
|
||||
public static final String SOURCE_TYPE_NAME_DH = "大华";
|
||||
public static final String SOURCE_TYPE_NAME_HK = "海康";
|
||||
|
||||
}
|
||||
@@ -24,25 +24,21 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* web层通用数据处理
|
||||
*
|
||||
*
|
||||
* @author ltgk
|
||||
*/
|
||||
public class BaseController
|
||||
{
|
||||
public class BaseController {
|
||||
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
/**
|
||||
* 将前台传递过来的日期格式的字符串,自动转化为Date类型
|
||||
*/
|
||||
@InitBinder
|
||||
public void initBinder(WebDataBinder binder)
|
||||
{
|
||||
public void initBinder(WebDataBinder binder) {
|
||||
// Date 类型转换
|
||||
binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
|
||||
{
|
||||
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
|
||||
@Override
|
||||
public void setAsText(String text)
|
||||
{
|
||||
public void setAsText(String text) {
|
||||
setValue(DateUtils.parseDate(text));
|
||||
}
|
||||
});
|
||||
@@ -51,19 +47,23 @@ public class BaseController
|
||||
/**
|
||||
* 设置请求分页数据
|
||||
*/
|
||||
protected void startPage()
|
||||
{
|
||||
protected void startPage() {
|
||||
PageUtils.startPage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求分页数据
|
||||
*/
|
||||
protected void startPage(Integer pageNum, Integer pageSize) {
|
||||
PageUtils.startPage(pageNum, pageSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求排序数据
|
||||
*/
|
||||
protected void startOrderBy()
|
||||
{
|
||||
protected void startOrderBy() {
|
||||
PageDomain pageDomain = TableSupport.buildPageRequest();
|
||||
if (StringUtils.isNotEmpty(pageDomain.getOrderBy()))
|
||||
{
|
||||
if (StringUtils.isNotEmpty(pageDomain.getOrderBy())) {
|
||||
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
|
||||
PageHelper.orderBy(orderBy);
|
||||
}
|
||||
@@ -72,17 +72,15 @@ public class BaseController
|
||||
/**
|
||||
* 清理分页的线程变量
|
||||
*/
|
||||
protected void clearPage()
|
||||
{
|
||||
protected void clearPage() {
|
||||
PageUtils.clearPage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应请求分页数据
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
protected TableDataInfo getDataTable(List<?> list)
|
||||
{
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
protected TableDataInfo getDataTable(List<?> list) {
|
||||
TableDataInfo rspData = new TableDataInfo();
|
||||
rspData.setCode(HttpStatus.SUCCESS);
|
||||
rspData.setMsg("查询成功");
|
||||
@@ -94,110 +92,97 @@ public class BaseController
|
||||
/**
|
||||
* 返回成功
|
||||
*/
|
||||
public AjaxResult success()
|
||||
{
|
||||
public AjaxResult success() {
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回失败消息
|
||||
*/
|
||||
public AjaxResult error()
|
||||
{
|
||||
public AjaxResult error() {
|
||||
return AjaxResult.error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回成功消息
|
||||
*/
|
||||
public AjaxResult success(String message)
|
||||
{
|
||||
public AjaxResult success(String message) {
|
||||
return AjaxResult.success(message);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回成功消息
|
||||
*/
|
||||
public AjaxResult success(Object data)
|
||||
{
|
||||
public AjaxResult success(Object data) {
|
||||
return AjaxResult.success(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回失败消息
|
||||
*/
|
||||
public AjaxResult error(String message)
|
||||
{
|
||||
public AjaxResult error(String message) {
|
||||
return AjaxResult.error(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回警告消息
|
||||
*/
|
||||
public AjaxResult warn(String message)
|
||||
{
|
||||
public AjaxResult warn(String message) {
|
||||
return AjaxResult.warn(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应返回结果
|
||||
*
|
||||
*
|
||||
* @param rows 影响行数
|
||||
* @return 操作结果
|
||||
*/
|
||||
protected AjaxResult toAjax(int rows)
|
||||
{
|
||||
protected AjaxResult toAjax(int rows) {
|
||||
return rows > 0 ? AjaxResult.success() : AjaxResult.error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应返回结果
|
||||
*
|
||||
*
|
||||
* @param result 结果
|
||||
* @return 操作结果
|
||||
*/
|
||||
protected AjaxResult toAjax(boolean result)
|
||||
{
|
||||
protected AjaxResult toAjax(boolean result) {
|
||||
return result ? success() : error();
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面跳转
|
||||
*/
|
||||
public String redirect(String url)
|
||||
{
|
||||
public String redirect(String url) {
|
||||
return StringUtils.format("redirect:{}", url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户缓存信息
|
||||
*/
|
||||
public LoginUser getLoginUser()
|
||||
{
|
||||
public LoginUser getLoginUser() {
|
||||
return SecurityUtils.getLoginUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户id
|
||||
*/
|
||||
public Long getUserId()
|
||||
{
|
||||
public Long getUserId() {
|
||||
return getLoginUser().getUserId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录部门id
|
||||
*/
|
||||
public Long getDeptId()
|
||||
{
|
||||
public Long getDeptId() {
|
||||
return getLoginUser().getDeptId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录用户名
|
||||
*/
|
||||
public String getUsername()
|
||||
{
|
||||
public String getUsername() {
|
||||
return getLoginUser().getUsername();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.ltgk.smartFishingPort.common.core.domain;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author liutailin
|
||||
* @date 2019-06-02 9:09.
|
||||
*/
|
||||
public final class Arguments {
|
||||
private Arguments() {
|
||||
}
|
||||
|
||||
public static <T extends Collection> boolean isNullOrEmpty(T t) {
|
||||
return Objects.isNull(t) || isEmpty(t);
|
||||
}
|
||||
|
||||
public static boolean isEmpty(String s) {
|
||||
return Strings.isNullOrEmpty(s);
|
||||
}
|
||||
|
||||
public static <T extends Collection> boolean isEmpty(T t) {
|
||||
return t.isEmpty();
|
||||
}
|
||||
|
||||
public static boolean notEmpty(String s) {
|
||||
return !isEmpty(s);
|
||||
}
|
||||
|
||||
public static <T extends Collection> boolean notEmpty(T l) {
|
||||
return Objects.nonNull(l) && !l.isEmpty();
|
||||
}
|
||||
|
||||
public static boolean positive(Number n) {
|
||||
return n.doubleValue() > 0.0D;
|
||||
}
|
||||
|
||||
public static boolean isPositive(Number n) {
|
||||
return n != null && n.doubleValue() > 0.0D;
|
||||
}
|
||||
|
||||
public static boolean negative(Number n) {
|
||||
return n.doubleValue() < 0.0D;
|
||||
}
|
||||
|
||||
public static boolean isNegative(Number n) {
|
||||
return n != null && n.doubleValue() < 0.0D;
|
||||
}
|
||||
|
||||
public static <T> boolean equalWith(T source, T target) {
|
||||
return com.google.common.base.Objects.equal(source, target);
|
||||
}
|
||||
|
||||
public static boolean not(Boolean t) {
|
||||
Preconditions.checkArgument(Objects.nonNull(t));
|
||||
return !t.booleanValue();
|
||||
}
|
||||
|
||||
public static boolean isDecimal(String str) {
|
||||
char[] var1 = str.toCharArray();
|
||||
int var2 = var1.length;
|
||||
|
||||
for(int var3 = 0; var3 < var2; ++var3) {
|
||||
char c = var1[var3];
|
||||
if(c < 48 || c > 57) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isNumberic(String str) {
|
||||
Boolean dot = false;
|
||||
char[] var2 = str.toCharArray();
|
||||
int var3 = var2.length;
|
||||
|
||||
for(int var4 = 0; var4 < var3; ++var4) {
|
||||
char c = var2[var4];
|
||||
if(c == 46 && !dot) {
|
||||
dot = true;
|
||||
} else {
|
||||
if(c == 46) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(c < 48 || c > 57) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.ltgk.smartFishingPort.common.core.domain;
|
||||
|
||||
import com.google.common.base.MoreObjects;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author liutailin
|
||||
* @date 2019-06-02 9:06.
|
||||
*/
|
||||
@Data
|
||||
public class Response<T> implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private boolean success;
|
||||
private T result;
|
||||
private String msg;
|
||||
private String code;
|
||||
|
||||
public Response(String code, String msg, boolean success, T result) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
this.success = success;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public Response() {
|
||||
}
|
||||
|
||||
public static <T> Response<T> build(String code, String msg, boolean success, T result) {
|
||||
return new Response(code,msg,success,result);
|
||||
}
|
||||
|
||||
public static <T> Response<T> ok(T data) {
|
||||
return new Response("200","操作成功.",true,data);
|
||||
}
|
||||
|
||||
public static <T> Response<T> ok() {
|
||||
return ok((T) null);
|
||||
}
|
||||
|
||||
public static <T> Response<T> fail(String msg) {
|
||||
return new Response("500",msg,false,null);
|
||||
}
|
||||
|
||||
public static <T> Response<T> fail(String code, String msg) {
|
||||
return new Response(code,msg,false,null);
|
||||
}
|
||||
|
||||
public static <T> Response<T> or(Boolean flag) {
|
||||
return flag.booleanValue() ? new Response("200",null,true,"操作成功.")
|
||||
: new Response("500","操作失败.",false,null);
|
||||
}
|
||||
|
||||
public static <T> Response<T> or(T t) {
|
||||
return Objects.nonNull(t) ? new Response("200",null,true,t)
|
||||
: new Response("200",null,true,null);
|
||||
}
|
||||
|
||||
public static <T extends Collection> Response<T> or(T t) {
|
||||
return Arguments.notEmpty(t) ? new Response("200",null,true,t)
|
||||
: new Response("200",null,true,null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MoreObjects.toStringHelper(this).add("success", this.success).add("result", this.result).add("msg", this.msg).omitNullValues().toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* spring redis 工具类
|
||||
@@ -262,4 +263,15 @@ public class RedisCache
|
||||
{
|
||||
return redisTemplate.keys(pattern);
|
||||
}
|
||||
|
||||
public List<Object> getMByScript(String patternKey) {
|
||||
Set<String> tempPatternKey = redisTemplate.keys(patternKey);
|
||||
List<Object> valueList = tempPatternKey.stream()
|
||||
.map(m -> {
|
||||
ValueOperations<String, Object> operation = redisTemplate.opsForValue();
|
||||
return operation.get(m);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
return valueList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.ltgk.smartFishingPort.common.enums;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 识别类型
|
||||
*/
|
||||
public enum ClassCodes {
|
||||
SAND_MINING_SHIP("采砂矿石船",140000),
|
||||
PASSENGER_SHIP("客轮",140001),
|
||||
CONTAINER_SHIP("集装箱船",140002),
|
||||
BULK_CARRIER("bulkCargoCarrier",140003),
|
||||
GENERAL_CARGO_SHIP("普通货船",140004),
|
||||
FISHING_SHIP("渔船",140005),
|
||||
//国旗
|
||||
FLAG_SHIP("国旗船",140006),
|
||||
//清洁船
|
||||
CLEAN_SHIP("清洁船",140007),
|
||||
//未封仓
|
||||
UNDRESSED_SHIP("未封仓",140008),
|
||||
// 未挂国旗
|
||||
UNDRESSED_FLAG_SHIP("未挂国旗",140009),
|
||||
NOT_DRESSED_SAVESHIP("未穿救生衣",139000),
|
||||
DRESSED_SAVESHIP("穿救生衣",139001),
|
||||
DISCARDED_SHIP("废弃船",111002),
|
||||
FISHING_FARMING_SHIP("捕鱼养殖",111003);
|
||||
// SHIP("船",141000),
|
||||
// FLAG("插旗旗",141001),
|
||||
// SHIP_NO_FLAG("未插旗船",141002);
|
||||
private String name;
|
||||
private int code;
|
||||
ClassCodes(String name, int code) {
|
||||
this.name = name;
|
||||
this.code = code;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
public static ClassCodes getClassCodesByCode(int code) {
|
||||
for (ClassCodes classCodes : ClassCodes.values()) {
|
||||
if (classCodes.getCode() == code) {
|
||||
return classCodes;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// 获取所有code 并转为字符串 用逗号隔开
|
||||
public static String getAllCode() {
|
||||
List<String> codes = new ArrayList<>();
|
||||
for (ClassCodes value : ClassCodes.values()){
|
||||
codes.add(String.valueOf(value.getCode()));
|
||||
}
|
||||
return String.join(",", codes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.ltgk.smartFishingPort.common.enums;
|
||||
|
||||
public enum DroneToRtmp {
|
||||
YJM("1581F8HGX254M00A09YQ", "rtmp://198.16.74.210:1936/live/yjm", "rtmp://198.16.74.211/live/livestream/uav01","http://198.16.74.211:18080/live/livestream/uav01.flv"),
|
||||
|
||||
LCSF("1581F8HGX256700A0HNV", "rtmp://198.16.74.210:1936/live/lcsf", "rtmp://198.16.74.211/live/livestream/uav02","http://198.16.74.211:18080/live/livestream/uav02.flv"),
|
||||
OBMT("1581F8HGX257Q00A0PJT", "rtmp://198.16.74.210:1936/live/obmt", "rtmp://198.16.74.211/live/livestream/uav03","http://198.16.74.211:18080/live/livestream/uav03.flv");
|
||||
private String name;
|
||||
private String srcUrl;
|
||||
private String dstUrl;
|
||||
private String httpUrl;
|
||||
private DroneToRtmp(String name, String srcUrl, String dstUrl, String httpUrl) {
|
||||
this.name = name;
|
||||
this.srcUrl = srcUrl;
|
||||
this.dstUrl = dstUrl;
|
||||
this.httpUrl = httpUrl;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getSrcUrl() {
|
||||
return srcUrl;
|
||||
}
|
||||
public String getDstUrl() {
|
||||
return dstUrl;
|
||||
}
|
||||
public String getHttpUrl() {
|
||||
return httpUrl;
|
||||
}
|
||||
public static DroneToRtmp getRtmpByName(String name) {
|
||||
for (DroneToRtmp item : DroneToRtmp.values()) {
|
||||
if (item.getName().equals(name)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// 查看那么在不在
|
||||
public static boolean isExist(String name) {
|
||||
for (DroneToRtmp item : DroneToRtmp.values()) {
|
||||
if (item.getName().equals(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import java.lang.management.ManagementFactory;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
@@ -185,4 +186,17 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
||||
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
|
||||
return Date.from(zdt.toInstant());
|
||||
}
|
||||
|
||||
public static String toDate(String str) {
|
||||
try {
|
||||
DateTimeFormatter parsePatterns = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
LocalDateTime localDateTime = LocalDateTime.parse(str, parsePatterns);
|
||||
// 将 LocalDateTime 转换为 ZonedDateTime 并指定时区为 UTC
|
||||
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneOffset.systemDefault()).withZoneSameInstant(ZoneOffset.UTC);
|
||||
// 格式化输出为 ISO 8601 格式
|
||||
return zonedDateTime.format(DateTimeFormatter.ISO_INSTANT);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.ltgk.smartFishingPort.common.utils;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.SqlCondition;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class MybatisUtil {
|
||||
private static Pattern humpPattern = Pattern.compile("[A-Z]");
|
||||
|
||||
public static<T> QueryWrapper<T> notNullField(T T){
|
||||
QueryWrapper<T> wrapper = new QueryWrapper<>();
|
||||
for (Field field : T.getClass().getDeclaredFields()) {
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
//序列化 字段不需要查询
|
||||
if("serialVersionUID".equals(field.getName())){
|
||||
continue;
|
||||
}
|
||||
//属性为空,不用查询
|
||||
if(field.get(T) == null || StringUtils.isEmpty(field.get(T).toString())){
|
||||
continue;
|
||||
}
|
||||
//主键 注解TableId
|
||||
TableId tableId = field.getAnnotation(TableId.class);
|
||||
if (tableId != null){
|
||||
//主键
|
||||
wrapper.eq(tableId.value(),field.get(T));
|
||||
continue;
|
||||
}
|
||||
//数据库中字段名和实体类属性不一致 注解TableField
|
||||
TableField tableField = field.getAnnotation(TableField.class);
|
||||
if(tableField != null){
|
||||
if(tableField.exist()){
|
||||
if (SqlCondition.LIKE.equals(tableField.condition())) {
|
||||
wrapper.like(tableField.value(),field.get(T));
|
||||
} else {
|
||||
wrapper.eq(tableField.value(),field.get(T));
|
||||
}
|
||||
}// @TableField(exist = false) 不是表中内容 不形成查询条件
|
||||
continue;
|
||||
}
|
||||
//数据库中字段名和实体类属性一致
|
||||
wrapper.eq(humpToLine(field.getName()),field.get(T));
|
||||
return wrapper;
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
public static String humpToLine(String str) {
|
||||
Matcher matcher = humpPattern.matcher(str);
|
||||
StringBuffer sb = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user