拆分think模块包

This commit is contained in:
2024-08-13 17:51:18 +08:00
parent 9dc17cd63e
commit 09a752e12a
1547 changed files with 88305 additions and 10329 deletions
+87
View File
@@ -0,0 +1,87 @@
package com.gxwebsoft.common.core.web;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
/**
* 返回结果
*
* @author WebSoft
* @since 2017-06-10 10:10:50
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResult<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "状态码")
private Integer code;
@ApiModelProperty(value = "状态信息")
private String message;
@ApiModelProperty(value = "返回数据")
private T data;
@ApiModelProperty(value = "错误信息")
private String error;
public ApiResult() {}
public ApiResult(Integer code) {
this(code, null);
}
public ApiResult(Integer code, String message) {
this(code, message, null);
}
public ApiResult(Integer code, String message, T data) {
this(code, message, data, null);
}
public ApiResult(Integer code, String message, T data, String error) {
setCode(code);
setMessage(message);
setData(data);
setError(error);
}
public Integer getCode() {
return this.code;
}
public ApiResult<T> setCode(Integer code) {
this.code = code;
return this;
}
public String getMessage() {
return this.message;
}
public ApiResult<T> setMessage(String message) {
this.message = message;
return this;
}
public T getData() {
return this.data;
}
public ApiResult<T> setData(T data) {
this.data = data;
return this;
}
public String getError() {
return this.error;
}
public ApiResult<T> setError(String error) {
this.error = error;
return this;
}
}
+263
View File
@@ -0,0 +1,263 @@
package com.gxwebsoft.common.core.web;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxwebsoft.common.core.Constants;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.system.entity.Company;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.CompanyService;
import com.gxwebsoft.common.system.service.UserService;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* Controller基类
*
* @author WebSoft
* @since 2017-06-10 10:10:19
*/
public class BaseController {
@Resource
private HttpServletRequest request;
@Resource
private UserService userService;
@Resource
private CompanyService companyService;
@Resource
private RedisUtil redisUtil;
/**
* 获取当前登录的user
*
* @return User
*/
public User getLoginUser() {
try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
Object object = authentication.getPrincipal();
if (object instanceof User) {
((User) object).setUid(13448);
System.out.println("object = " + object);
return (User) object;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
/**
* 获取当前登录的userId
*
* @return userId
*/
public Integer getLoginUserId() {
User loginUser = getLoginUser();
return loginUser == null ? null : loginUser.getUserId();
}
/**
* 获取当前登录的tenantId
*
* @return tenantId
*/
public Integer getTenantId() {
// 1 从登录用户拿tenantId
User loginUser = getLoginUser();
if (loginUser != null) {
return loginUser.getTenantId();
}
// 2 从域名拿ID
String Domain = request.getHeader("Domain");
if (StrUtil.isNotBlank(Domain)) {
String key = "Domain:" + Domain;
String tenantId = redisUtil.get(key);
if(tenantId != null){
// System.out.println("从域名拿ID = " + tenantId);
return Integer.valueOf(tenantId);
}
}
// 3 从请求头拿ID
String tenantId = request.getHeader("tenantId");
if(StrUtil.isNotBlank(tenantId)){
return Integer.valueOf(tenantId);
}
return null;
}
/**
* 获取当前登录的企业信息
*
* @return Company
*/
public Company getCompany() {
List<Company> list = companyService.list(new LambdaQueryWrapper<Company>().eq(Company::getAuthoritative, 1));
if (!CollectionUtils.isEmpty(list)) {
final Company company = list.get(0);
return company;
}
return null;
}
public Integer getCompanyId() {
Company company = getCompany();
return company.getCompanyId();
}
/**
* 返回成功
*
* @return ApiResult
*/
public ApiResult<?> success() {
return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG);
}
/**
* 返回成功
*
* @param message 状态信息
* @return ApiResult
*/
public ApiResult<?> success(String message) {
return success().setMessage(message);
}
/**
* 返回成功
*
* @param data 返回数据
* @return ApiResult
*/
public <T> ApiResult<T> success(T data) {
return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG, data);
}
/**
* 返回成功
*
* @param message 状态信息
* @return ApiResult
*/
public <T> ApiResult<T> success(String message, T data) {
return success(data).setMessage(message);
}
/**
* 返回分页查询数据
*
* @param list 当前页数据
* @param count 总数量
* @return ApiResult
*/
public <T> ApiResult<PageResult<T>> success(List<T> list, Long count) {
return success(new PageResult<>(list, count));
}
/**
* 返回分页查询数据
*
* @param iPage IPage
* @return ApiResult
*/
public <T> ApiResult<PageResult<T>> success(IPage<T> iPage) {
return success(iPage.getRecords(), iPage.getTotal());
}
/**
* 返回失败
*
* @return ApiResult
*/
public ApiResult<?> fail() {
return new ApiResult<>(Constants.RESULT_ERROR_CODE, Constants.RESULT_ERROR_MSG);
}
/**
* 返回失败
*
* @param message 状态信息
* @return ApiResult
*/
public ApiResult<?> fail(String message) {
return fail().setMessage(message);
}
/**
* 返回失败
*
* @param data 返回数据
* @return ApiResult
*/
public <T> ApiResult<T> fail(T data) {
return fail(Constants.RESULT_ERROR_MSG, data);
}
/**
* 返回失败
*
* @param message 状态信息
* @param data 返回数据
* @return ApiResult
*/
public <T> ApiResult<T> fail(String message, T data) {
return new ApiResult<>(Constants.RESULT_ERROR_CODE, message, data);
}
/**
* 请求参数的空字符串转为null
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
// 自定义函数
public String getAuthorization() {
return request.getHeader("Authorization");
}
public String getSign() {
return request.getParameter("sign");
}
/**
* 根据账号|手机号码|邮箱查找用户ID
*
* @return userId
*/
public Integer getUserIdByUsername(String username, Integer tenantId) {
// 按账号搜素
User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username).eq(User::getTenantId, tenantId));
if (user != null && user.getUserId() > 0) {
return user.getUserId();
}
// 按手机号码搜索
User userByPhone = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, username).eq(User::getTenantId, tenantId));
if (userByPhone != null && userByPhone.getUserId() > 0) {
return userByPhone.getUserId();
}
// 按邮箱搜索
User userByEmail = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getEmail, username).eq(User::getTenantId, tenantId));
if (userByEmail != null && userByEmail.getUserId() > 0) {
return userByEmail.getUserId();
}
throw new BusinessException("找不到该用户");
}
}
+76
View File
@@ -0,0 +1,76 @@
package com.gxwebsoft.common.core.web;
import com.baomidou.mybatisplus.annotation.TableField;
import com.gxwebsoft.common.core.annotation.QueryField;
import com.gxwebsoft.common.core.annotation.QueryType;
import com.gxwebsoft.common.core.utils.CommonUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 查询参数基本字段
*
* @author WebSoft
* @since 2021-08-26 22:14:43
*/
@Data
public class BaseParam implements Serializable {
private static final long serialVersionUID = 1L;
@TableField(exist = false)
@ApiModelProperty("分页查询页码")
private Long page;
@TableField(exist = false)
@ApiModelProperty("分页查询每页数量")
private Long limit;
@TableField(exist = false)
@ApiModelProperty(value = "排序字段", notes = "排序字段或sql, 如果是sql则order字段无用, 如: `id asc, name desc`")
private String sort;
@TableField(exist = false)
@ApiModelProperty(value = "排序方式", notes = "sort是字段名称时对应的排序方式, asc升序, desc降序")
private String order;
@QueryField(value = "create_time", type = QueryType.GE)
@TableField(exist = false)
@ApiModelProperty("创建时间起始值")
private String createTimeStart;
@QueryField(value = "create_time", type = QueryType.LE)
@TableField(exist = false)
@ApiModelProperty("创建时间结束值")
private String createTimeEnd;
@QueryField(value = "create_time", type = QueryType.GE)
@ApiModelProperty("搜索场景")
@TableField(exist = false)
private String sceneType;
@ApiModelProperty(value = "商户ID")
@TableField(exist = false)
private Integer merchantId;
@ApiModelProperty("租户ID")
@TableField(exist = false)
private Integer tenantId;
@ApiModelProperty("模糊搜素")
@TableField(exist = false)
private String keywords;
/**
* 获取集合中的第一条数据
*
* @param records 集合
* @return 第一条数据
*/
public <T> T getOne(List<T> records) {
return CommonUtil.listGetOne(records);
}
}
+57
View File
@@ -0,0 +1,57 @@
package com.gxwebsoft.common.core.web;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 批量修改通用参数
*
* @author WebSoft
* @since 2020-03-13 00:11:06
*/
@Data
public class BatchParam<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "需要修改的数据id集合")
private List<Serializable> ids;
@ApiModelProperty(value = "需要修改的字段和值")
private T data;
/**
* 通用批量修改方法
*
* @param service IService
* @param idField id字段名称
* @return boolean
*/
public boolean update(IService<T> service, String idField) {
if (this.data == null) {
return false;
}
return service.update(this.data, new UpdateWrapper<T>().in(idField, this.ids));
}
/**
* 通用批量修改方法
*
* @param service IService
* @param idField id字段名称
* @return boolean
*/
public boolean update(IService<T> service, SFunction<T, ?> idField) {
if (this.data == null) {
return false;
}
return service.update(this.data, new LambdaUpdateWrapper<T>().in(idField, this.ids));
}
}
@@ -0,0 +1,96 @@
package com.gxwebsoft.common.core.web;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* 检查是否存在通用参数
*
* @author WebSoft
* @since 2021-09-07 22:24:39
*/
@Data
public class ExistenceParam<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "检查的字段")
private String field;
@ApiModelProperty(value = "字段的值")
private String value;
@ApiModelProperty(value = "修改时的主键")
private Integer id;
/**
* 检查是否存在
*
* @param service IService
* @param idField 修改时的主键字段
* @return boolean
*/
public boolean isExistence(IService<T> service, String idField) {
return isExistence(service, idField, true);
}
/**
* 检查是否存在
*
* @param service IService
* @param idField 修改时的主键字段
* @param isToUnderlineCase 是否需要把field转为下划线格式
* @return boolean
*/
public boolean isExistence(IService<T> service, String idField, boolean isToUnderlineCase) {
if (StrUtil.hasBlank(this.field, this.value)) {
return false;
}
String fieldName = isToUnderlineCase ? StrUtil.toUnderlineCase(field) : field;
QueryWrapper<T> wrapper = new QueryWrapper<>();
wrapper.eq(fieldName, value);
if (id != null) {
wrapper.ne(idField, id);
}
return service.count(wrapper) > 0;
}
/**
* 检查是否存在
*
* @param service IService
* @param idField 修改时的主键字段
* @return boolean
*/
public boolean isExistence(IService<T> service, SFunction<T, ?> idField) {
return isExistence(service, idField, true);
}
/**
* 检查是否存在
*
* @param service IService
* @param idField 修改时的主键字段
* @param isToUnderlineCase 是否需要把field转为下划线格式
* @return boolean
*/
public boolean isExistence(IService<T> service, SFunction<T, ?> idField, boolean isToUnderlineCase) {
if (StrUtil.hasBlank(this.field, this.value)) {
return false;
}
String fieldName = isToUnderlineCase ? StrUtil.toUnderlineCase(field) : field;
LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
wrapper.apply(fieldName + " = {0}", value);
if (id != null) {
wrapper.ne(idField, id);
}
return service.count(wrapper) > 0;
}
}
+343
View File
@@ -0,0 +1,343 @@
package com.gxwebsoft.common.core.web;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxwebsoft.common.core.Constants;
import com.gxwebsoft.common.core.annotation.QueryField;
import com.gxwebsoft.common.core.annotation.QueryType;
import com.gxwebsoft.common.core.utils.CommonUtil;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 分页、排序、搜索参数封装
*
* @author WebSoft
* @since 2019-04-26 10:34:35
*/
public class PageParam<T, U extends BaseParam> extends Page<T> {
private static final long serialVersionUID = 1L;
/**
* 租户id字段名称
*/
private static final String TENANT_ID_FIELD = "tenantId";
/**
* 查询条件
*/
private final U where;
/**
* 是否把字段名称驼峰转下划线
*/
private final boolean isToUnderlineCase;
public PageParam() {
this(null);
}
public PageParam(U where) {
this(where, true);
}
public PageParam(U where, boolean isToUnderlineCase) {
super();
this.where = where;
this.isToUnderlineCase = isToUnderlineCase;
if (where != null) {
// 获取分页页码
if (where.getPage() != null) {
setCurrent(where.getPage());
}
// 获取分页每页数量
if (where.getLimit() != null) {
setSize(where.getLimit());
}
// 获取排序方式
if (where.getSort() != null) {
if (sortIsSQL(where.getSort())) {
setOrders(parseOrderSQL(where.getSort()));
} else {
List<OrderItem> orderItems = new ArrayList<>();
String column = this.isToUnderlineCase ? StrUtil.toUnderlineCase(where.getSort()) : where.getSort();
boolean asc = !Constants.ORDER_DESC_VALUE.equals(where.getOrder());
orderItems.add(new OrderItem(column, asc));
setOrders(orderItems);
}
}
}
}
/**
* 排序字段是否是sql
*/
private boolean sortIsSQL(String sort) {
return sort != null && (sort.contains(",") || sort.trim().contains(" "));
}
/**
* 解析排序sql
*/
private List<OrderItem> parseOrderSQL(String orderSQL) {
List<OrderItem> orders = new ArrayList<>();
if (StrUtil.isNotBlank(orderSQL)) {
for (String item : orderSQL.split(",")) {
String[] temp = item.trim().split(" ");
if (!temp[0].isEmpty()) {
String column = this.isToUnderlineCase ? StrUtil.toUnderlineCase(temp[0]) : temp[0];
boolean asc = temp.length == 1 || !temp[temp.length - 1].equals(Constants.ORDER_DESC_VALUE);
orders.add(new OrderItem(column, asc));
}
}
}
return orders;
}
/**
* 设置默认排序方式
*
* @param orderItems 排序方式
* @return PageParam
*/
public PageParam<T, U> setDefaultOrder(List<OrderItem> orderItems) {
if (orders() == null || orders().size() == 0) {
setOrders(orderItems);
}
return this;
}
/**
* 设置默认排序方式
*
* @param orderSQL 排序方式
* @return PageParam
*/
public PageParam<T, U> setDefaultOrder(String orderSQL) {
setDefaultOrder(parseOrderSQL(orderSQL));
return this;
}
/**
* 获取查询条件
*
* @param excludes 不包含的字段
* @return QueryWrapper
*/
public QueryWrapper<T> getWrapper(String... excludes) {
return buildWrapper(null, Arrays.asList(excludes));
}
/**
* 获取查询条件
*
* @param columns 只包含的字段
* @return QueryWrapper
*/
public QueryWrapper<T> getWrapperWith(String... columns) {
return buildWrapper(Arrays.asList(columns), null);
}
/**
* 构建QueryWrapper
*
* @param columns 包含的字段
* @param excludes 排除的字段
* @return QueryWrapper
*/
private QueryWrapper<T> buildWrapper(List<String> columns, List<String> excludes) {
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
Map<String, Object> map = BeanUtil.beanToMap(where, false, true);
for (String fieldName : map.keySet()) {
Object fieldValue = map.get(fieldName);
Field field = ReflectUtil.getField(where.getClass(), fieldName);
// 过滤不包含的字段
if (columns != null && !columns.contains(fieldName)) {
continue;
}
// 过滤排除的字段
if (excludes != null && excludes.contains(fieldName)) {
continue;
}
// 过滤逻辑删除字段
if (field.getAnnotation(TableLogic.class) != null) {
continue;
}
// 过滤租户id字段
if (fieldName.equals(TENANT_ID_FIELD)) {
continue;
}
// 获取注解指定的查询字段及查询方式
QueryType queryType = QueryType.LIKE;
QueryField queryField = field.getAnnotation(QueryField.class);
if (queryField != null) {
if (StrUtil.isNotEmpty(queryField.value())) {
fieldName = queryField.value();
}
if (queryField.type() != null) {
queryType = queryField.type();
}
} else {
// 过滤非本表的字段
TableField tableField = field.getAnnotation(TableField.class);
if (tableField != null && !tableField.exist()) {
continue;
}
}
// 字段名驼峰转下划线
if (this.isToUnderlineCase) {
fieldName = StrUtil.toUnderlineCase(fieldName);
}
//
switch (queryType) {
case EQ:
queryWrapper.eq(fieldName, fieldValue);
break;
case NE:
queryWrapper.ne(fieldName, fieldValue);
break;
case GT:
queryWrapper.gt(fieldName, fieldValue);
break;
case GE:
queryWrapper.ge(fieldName, fieldValue);
break;
case LT:
queryWrapper.lt(fieldName, fieldValue);
break;
case LE:
queryWrapper.le(fieldName, fieldValue);
break;
case LIKE:
queryWrapper.like(fieldName, fieldValue);
break;
case NOT_LIKE:
queryWrapper.notLike(fieldName, fieldValue);
break;
case LIKE_LEFT:
queryWrapper.likeLeft(fieldName, fieldValue);
break;
case LIKE_RIGHT:
queryWrapper.likeRight(fieldName, fieldValue);
break;
case IS_NULL:
queryWrapper.isNull(fieldName);
break;
case IS_NOT_NULL:
queryWrapper.isNotNull(fieldName);
break;
case IN:
queryWrapper.in(fieldName, fieldValue);
break;
case NOT_IN:
queryWrapper.notIn(fieldName, fieldValue);
break;
case IN_STR:
if (fieldValue instanceof String) {
queryWrapper.in(fieldName, Arrays.asList(((String) fieldValue).split(",")));
}
break;
case NOT_IN_STR:
if (fieldValue instanceof String) {
queryWrapper.notIn(fieldName, Arrays.asList(((String) fieldValue).split(",")));
}
break;
}
}
return queryWrapper;
}
/**
* 获取包含排序的查询条件
*
* @return 包含排序的QueryWrapper
*/
public QueryWrapper<T> getOrderWrapper() {
return getOrderWrapper(getWrapper());
}
/**
* 获取包含排序的查询条件
*
* @param queryWrapper 不含排序的QueryWrapper
* @return 包含排序的QueryWrapper
*/
public QueryWrapper<T> getOrderWrapper(QueryWrapper<T> queryWrapper) {
if (queryWrapper == null) {
queryWrapper = new QueryWrapper<>();
}
for (OrderItem orderItem : orders()) {
if (orderItem.isAsc()) {
queryWrapper.orderByAsc(orderItem.getColumn());
} else {
queryWrapper.orderByDesc(orderItem.getColumn());
}
}
return queryWrapper;
}
/**
* 获取集合中的第一条数据
*
* @param records 集合
* @return 第一条数据
*/
public T getOne(List<T> records) {
return CommonUtil.listGetOne(records);
}
/**
* 代码排序集合
*
* @param records 集合
* @return 排序后的集合
*/
public List<T> sortRecords(List<T> records) {
List<OrderItem> orderItems = orders();
if (records == null || records.size() < 2 || orderItems == null || orderItems.size() == 0) {
return records;
}
Comparator<T> comparator = null;
for (OrderItem item : orderItems) {
if (item.getColumn() == null) {
continue;
}
String field = this.isToUnderlineCase ? StrUtil.toCamelCase(item.getColumn()) : item.getColumn();
Function keyExtractor = t -> ReflectUtil.getFieldValue(t, field);
if (comparator == null) {
if (item.isAsc()) {
comparator = Comparator.comparing(keyExtractor);
} else {
comparator = Comparator.comparing(keyExtractor, Comparator.reverseOrder());
}
} else {
if (item.isAsc()) {
comparator.thenComparing(keyExtractor);
} else {
comparator.thenComparing(keyExtractor, Comparator.reverseOrder());
}
}
}
if (comparator != null) {
return records.stream().sorted(comparator).collect(Collectors.toList());
}
return records;
}
}
+51
View File
@@ -0,0 +1,51 @@
package com.gxwebsoft.common.core.web;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.List;
/**
* 分页查询返回结果
*
* @author WebSoft
* @since 2017-06-10 10:10:02
*/
public class PageResult<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "当前页数据")
private List<T> list;
@ApiModelProperty(value = "总数量")
private Long count;
public PageResult() {
}
public PageResult(List<T> list) {
this(list, null);
}
public PageResult(List<T> list, Long count) {
setList(list);
setCount(count);
}
public List<T> getList() {
return this.list;
}
public void setList(List<T> list) {
this.list = list;
}
public Long getCount() {
return this.count;
}
public void setCount(Long count) {
this.count = count;
}
}