This commit is contained in:
zhangzhiqiang
2022-12-15 14:33:03 +08:00
parent 2545e92dde
commit 2046d2276d
13 changed files with 115 additions and 151 deletions

View File

@@ -1,110 +0,0 @@
package org.nl.common.domain;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import net.dreamlu.mica.core.exception.ServiceException;
import org.nl.common.utils.StringUtils;
import org.nl.common.utils.sql.SqlUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* 分页参数
*/
@Data
public class PageQuery implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 分页大小
*/
private Integer pageSize;
/**
* 当前页数
*/
private Integer pageNum;
/**
* 排序列
*/
private String orderByColumn;
/**
* 排序的方向desc或者asc
*/
private String isAsc;
/**
* 当前记录起始索引 默认值
*/
public static final int DEFAULT_PAGE_NUM = 1;
/**
* 每页显示记录数 默认值 默认查全部
*/
public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;
public <T> Page<T> build() {
Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM);
Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE);
if (pageNum <= 0) {
pageNum = DEFAULT_PAGE_NUM;
}
Page<T> page = new Page<>(pageNum, pageSize);
List<OrderItem> orderItems = buildOrderItem();
if (CollUtil.isNotEmpty(orderItems)) {
page.addOrder(orderItems);
}
return page;
}
/**
* 构建排序
*
* 支持的用法如下:
* {isAsc:"asc",orderByColumn:"id"} order by id asc
* {isAsc:"asc",orderByColumn:"id,createTime"} order by id asc,create_time asc
* {isAsc:"desc",orderByColumn:"id,createTime"} order by id desc,create_time desc
* {isAsc:"asc,desc",orderByColumn:"id,createTime"} order by id asc,create_time desc
*/
private List<OrderItem> buildOrderItem() {
if (StringUtils.isBlank(orderByColumn) || StringUtils.isBlank(isAsc)) {
return null;
}
String orderBy = SqlUtil.escapeOrderBySql(orderByColumn);
orderBy = StringUtils.toUnderScoreCase(orderBy);
// 兼容前端排序类型
isAsc = StringUtils.replaceEach(isAsc, new String[]{"ascending", "descending"}, new String[]{"asc", "desc"});
String[] orderByArr = orderBy.split(",");
String[] isAscArr = isAsc.split(",");
if (isAscArr.length != 1 && isAscArr.length != orderByArr.length) {
throw new ServiceException("排序参数有误");
}
List<OrderItem> list = new ArrayList<>();
// 每个字段各自排序
for (int i = 0; i < orderByArr.length; i++) {
String orderByStr = orderByArr[i];
String isAscStr = isAscArr.length == 1 ? isAscArr[0] : isAscArr[i];
if ("asc".equals(isAscStr)) {
list.add(OrderItem.asc(orderByStr));
} else if ("desc".equals(isAscStr)) {
list.add(OrderItem.desc(orderByStr));
} else {
throw new ServiceException("排序参数有误");
}
}
return list;
}
}

View File

@@ -1,17 +1,13 @@
package org.nl.common.domain;
package org.nl.common.domain.query;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Builder;
import lombok.Data;
import org.nl.common.enums.QueryTEnum;
import org.nl.modules.tools.MapOf;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
/*
* @author ZZQ
@@ -20,10 +16,10 @@ import java.util.stream.Collectors;
@Data
public class BaseQuery<T> {
public Map<String,QParam> doP = MapOf.of("blurry",QParam.builder().k(new String[]{"name"}).type(QueryType.LK)
,"startTime",QParam.builder().k(new String[]{"createTime"}).type(QueryType.LT)
,"endTime",QParam.builder().k(new String[]{"createTime"}).type(QueryType.LE)
,"sort",QParam.builder().k(new String[]{"sort"}).type(QueryType.BY)
public Map<String, QParam> doP = MapOf.of("blurry", QParam.builder().k(new String[]{"name"}).type(QueryTEnum.LK)
,"startTime", QParam.builder().k(new String[]{"createTime"}).type(QueryTEnum.LT)
,"endTime", QParam.builder().k(new String[]{"createTime"}).type(QueryTEnum.LE)
,"sort", QParam.builder().k(new String[]{"sort"}).type(QueryTEnum.BY)
);
private String blurry;
@@ -37,14 +33,14 @@ public class BaseQuery<T> {
private String sort;
public QueryWrapper<T> build(){
List<String> collect = doP.keySet().stream().collect(Collectors.toList());
this.paramMapping();
QueryWrapper<T> wrapper = new QueryWrapper<>();
JSONObject json = (JSONObject)JSONObject.toJSON(this);
json.forEach((key, vel) -> {
if (vel != null){
QParam qParam = doP.get(key);
if (qParam != null){
QueryType.build(qParam.type,wrapper,qParam.k,vel);
QueryTEnum.build(qParam.type,wrapper,qParam.k,vel);
}else {
wrapper.eq(key,vel);
}
@@ -52,10 +48,6 @@ public class BaseQuery<T> {
});
return wrapper;
}
}
@Builder
@Data
class QParam{
public String[] k;
public QueryType type;
public void paramMapping(){};
}

View File

@@ -1,4 +1,4 @@
package org.nl.common.domain;
package org.nl.common.domain.query;
import java.util.Objects;

View File

@@ -0,0 +1,64 @@
package org.nl.common.domain.query;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import java.io.Serializable;
/**
* 分页参数
*/
@Data
public class PageQuery implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 分页大小
*/
private Integer pageSize;
/**
* 当前页数
*/
private Integer pageNum;
/**
* 排序列
*/
private String sort;
/**
* 排序的方向desc或者asc
*/
private Boolean isAsc;
/**
* 当前记录起始索引 默认值
*/
public static final int DEFAULT_PAGE_NUM = 1;
/**
* 每页显示记录数 默认值 默认查全部
*/
public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;
public <T> Page<T> build() {
Integer pageNum = ObjectUtil.defaultIfNull(getPageNum(), DEFAULT_PAGE_NUM);
Integer pageSize = ObjectUtil.defaultIfNull(getPageSize(), DEFAULT_PAGE_SIZE);
if (pageNum <= 0) {
pageNum = DEFAULT_PAGE_NUM;
}
Page<T> page = new Page<>(pageNum, pageSize);
OrderItem item = new OrderItem();
item.setColumn(sort);
item.setAsc(isAsc);
page.addOrder(item);
return page;
}
}

View File

@@ -0,0 +1,14 @@
package org.nl.common.domain.query;
import lombok.Builder;
import org.nl.common.enums.QueryTEnum;
/*
* @author ZZQ
* @Date 2022/12/15 1:41 下午
*/
@Builder
public class QParam {
public String[] k;
public QueryTEnum type;
}

View File

@@ -1,19 +1,17 @@
package org.nl.common.domain;
package org.nl.common.enums;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.nl.common.domain.query.LConsumer;
import java.util.Collection;
import java.util.Map;
/*
* @author ZZQ
* @Date 2022/12/14 8:26 下午
*/
@Getter
public enum QueryType {
public enum QueryTEnum {
//
EQ((q, k, v) -> { q.eq(k[0],v); }),
IN((q, key, o) -> { if (o instanceof Collection){ q.in(key[0],(Collection) o); } }),
@@ -24,11 +22,11 @@ public enum QueryType {
private LConsumer<QueryWrapper,String[], Object> doP;
QueryType(LConsumer<QueryWrapper,String[], Object> doP) {
QueryTEnum(LConsumer<QueryWrapper,String[], Object> doP) {
this.doP = doP;
}
public static void build(QueryType type, QueryWrapper q,String[] k ,Object v){
public static void build(QueryTEnum type, QueryWrapper q, String[] k , Object v){
type.getDoP().accept(q,k,v);
}
}

View File

@@ -22,7 +22,6 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.wf.captcha.base.Captcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@@ -41,7 +40,7 @@ import org.nl.modules.security.service.dto.AuthUserDto;
import org.nl.modules.system.service.RoleService;
import org.nl.modules.system.service.dto.UserDto;
import org.nl.modules.wql.core.bean.WQLObject;
import org.nl.system.service.user.UserService;
import org.nl.system.service.user.ISysUserService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -69,7 +68,7 @@ import java.util.concurrent.TimeUnit;
public class AuthorizationController {
private final RedisUtils redisUtils;
private final OnlineUserService onlineUserService;
private final UserService userService;
private final ISysUserService userService;
private final RoleService roleService;
@Resource

View File

@@ -18,7 +18,7 @@ package org.nl.modules.system.domain;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.nl.system.service.user.dao.User;
import org.nl.system.service.user.dao.SysUser;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
@@ -38,7 +38,7 @@ public class Role implements Serializable {
@ApiModelProperty(value = "ID", hidden = true)
private Long id;
private Set<User> users;
private Set<SysUser> users;
@ApiModelProperty(value = "菜单", hidden = true)
private Set<Menu> menus;

View File

@@ -16,7 +16,7 @@
package org.nl.modules.system.service;
import org.nl.modules.system.service.dto.UserDto;
import org.nl.system.service.user.dao.User;
import org.nl.system.service.user.dao.SysUser;
import java.util.List;
@@ -32,5 +32,5 @@ public interface DataService {
* @param user /
* @return /
*/
List<Long> getDeptIds(User user);
List<Long> getDeptIds(SysUser user);
}

View File

@@ -20,7 +20,7 @@ import org.nl.modules.system.service.DataService;
import org.nl.modules.system.service.DeptService;
import org.nl.modules.system.service.RoleService;
import org.nl.modules.system.service.dto.RoleSmallDto;
import org.nl.system.service.user.dao.User;
import org.nl.system.service.user.dao.SysUser;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
@@ -48,7 +48,7 @@ public class DataServiceImpl implements DataService {
* @return /
*/
@Override
public List<Long> getDeptIds(User user) {
public List<Long> getDeptIds(SysUser user) {
// 用于存储部门id
Set<Long> deptIds = new HashSet<>();
// 查询用户角色

View File

@@ -15,6 +15,7 @@ import java.util.Map;
* @since 2022-12-15
*/
public interface ISysUserService extends IService<SysUser> {
Map<String, String> updateAvatar(MultipartFile avatar);
}

View File

@@ -1,7 +1,9 @@
package org.nl.system.service.user.dto;
import lombok.Data;
import org.nl.common.domain.BaseQuery;
import org.nl.common.domain.query.BaseQuery;
import org.nl.common.domain.query.QParam;
import org.nl.common.enums.QueryTEnum;
import org.nl.system.service.user.dao.SysUser;
/*
@@ -11,6 +13,10 @@ import org.nl.system.service.user.dao.SysUser;
@Data
public class UserQuery extends BaseQuery<SysUser> {
private Long DeptId;
private Long deptId;
@Override
public void paramMapping() {
this.doP.put("deptId", QParam.builder().k(new String[]{"deptId"}).type(QueryTEnum.LK).build());
}
}

View File

@@ -4,8 +4,8 @@ import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import org.nl.AppRun;
import org.nl.system.service.user.UserService;
import org.nl.system.service.user.dao.User;
import org.nl.system.service.user.ISysUserService;
import org.nl.system.service.user.dao.SysUser;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@@ -20,10 +20,10 @@ import java.util.List;
public class MybatisTest {
@Resource
UserService sysUserService;
ISysUserService sysUserService;
@Test
public void mybatisTest(){
List<User> list = sysUserService.list();
List<SysUser> list = sysUserService.list();
System.out.println(JSON.toJSONString(list));
}