fix(): 修复代码格式

feat():增加超级基础类
boot3.0
dongzeliang 2025-01-07 15:55:52 +08:00
parent 25888aa95a
commit 1af2baea05
10 changed files with 127 additions and 74 deletions

View File

@ -2,13 +2,13 @@ package com.muyu.common.core.utils.reflect;
import com.muyu.common.core.text.Convert;
import com.muyu.common.core.utils.DateUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.poi.ss.usermodel.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Date;
/**
@ -17,6 +17,7 @@ import java.util.Date;
* @author muyu
*/
@SuppressWarnings("rawtypes")
@Log4j2
public class ReflectUtils {
private static final String SETTER_PREFIX = "set";
@ -24,8 +25,6 @@ public class ReflectUtils {
private static final String CGLIB_CLASS_SEPARATOR = "$$";
private static Logger logger = LoggerFactory.getLogger(ReflectUtils.class);
/**
* Getter.
* ..
@ -65,14 +64,14 @@ public class ReflectUtils {
public static <E> E getFieldValue (final Object obj, final String fieldName) {
Field field = getAccessibleField(obj, fieldName);
if (field == null) {
logger.debug("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 ");
log.debug("在 [{}] 中,没有找到 [{}] 字段 ", obj.getClass(), fieldName);
return null;
}
E result = null;
try {
result = (E) field.get(obj);
} catch (IllegalAccessException e) {
logger.error("不可能抛出的异常{}", e.getMessage());
log.error("不可能抛出的异常{}", e.getMessage());
}
return result;
}
@ -84,13 +83,13 @@ public class ReflectUtils {
Field field = getAccessibleField(obj, fieldName);
if (field == null) {
// throw new IllegalArgumentException("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 ");
logger.debug("在 [" + obj.getClass() + "] 中,没有找到 [" + fieldName + "] 字段 ");
log.debug("在 [{}] 中,没有找到 [{}] 字段 ", obj.getClass(), fieldName);
return;
}
try {
field.set(obj, value);
} catch (IllegalAccessException e) {
logger.error("不可能抛出的异常: {}", e.getMessage());
log.error("不可能抛出的异常: {}", e.getMessage());
}
}
@ -107,13 +106,13 @@ public class ReflectUtils {
}
Method method = getAccessibleMethod(obj, methodName, parameterTypes);
if (method == null) {
logger.debug("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 ");
log.debug("在 [{}] 中,没有找到 [{}] 方法 ", obj.getClass(), methodName);
return null;
}
try {
return (E) method.invoke(obj, args);
} catch (Exception e) {
String msg = "method: " + method + ", obj: " + obj + ", args: " + args + "";
String msg = "method: " + method + ", obj: " + obj + ", args: " + Arrays.toString(args);
throw convertReflectionExceptionToUnchecked(msg, e);
}
}
@ -128,7 +127,7 @@ public class ReflectUtils {
Method method = getAccessibleMethodByName(obj, methodName, args.length);
if (method == null) {
// 如果为空不报错,直接返回空。
logger.debug("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 ");
log.debug("在 [" + obj.getClass() + "] 中,没有找到 [" + methodName + "] 方法 ");
return null;
}
try {
@ -182,9 +181,7 @@ public class ReflectUtils {
Field field = superClass.getDeclaredField(fieldName);
makeAccessible(field);
return field;
} catch (NoSuchFieldException e) {
continue;
}
} catch (NoSuchFieldException ignored) {}
}
return null;
}
@ -207,9 +204,7 @@ public class ReflectUtils {
Method method = searchType.getDeclaredMethod(methodName, parameterTypes);
makeAccessible(method);
return method;
} catch (NoSuchMethodException e) {
continue;
}
} catch (NoSuchMethodException ignored) {}
}
return null;
}
@ -275,19 +270,18 @@ public class ReflectUtils {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
logger.debug(clazz.getSimpleName() + "'s superclass not ParameterizedType");
log.debug("{}'s superclass not ParameterizedType", clazz.getSimpleName());
return Object.class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (index >= params.length || index < 0) {
logger.debug("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
+ params.length);
log.debug("Index: {}, Size of {}'s Parameterized Type: {}", index, clazz.getSimpleName(), params.length);
return Object.class;
}
if (!(params[index] instanceof Class)) {
logger.debug(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
log.debug("{} not set the actual class on superclass generic parameter", clazz.getSimpleName());
return Object.class;
}
@ -299,7 +293,7 @@ public class ReflectUtils {
throw new RuntimeException("Instance must not be null");
}
Class clazz = instance.getClass();
if (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) {
if (clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) {
Class<?> superClass = clazz.getSuperclass();
if (superClass != null && !Object.class.equals(superClass)) {
return superClass;

View File

@ -0,0 +1,41 @@
package com.muyu.common.core.web;
import com.muyu.common.core.utils.reflect.ReflectUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.io.Serial;
import java.io.Serializable;
/**
*
*
* @author DongZeLiang
* @date 2024-01-07 10:01
*/
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class SuperBasic<OC> implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* ID -
*
* tracId
*/
private String serviceId;
/**
*
* @return
*/
public static <OC> OC ofNull(){
return (OC) builder().build();
}
}

View File

@ -4,6 +4,7 @@ package com.muyu.common.core.web.model;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.core.web.SuperBasic;
import com.muyu.common.core.web.page.PageReq;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -22,7 +23,7 @@ import java.util.List;
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class QueryModel<T> {
public class QueryModel<T> extends SuperBasic<T> {
/**
*

View File

@ -9,6 +9,8 @@ import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.SysDept;
import com.muyu.system.domain.model.SysDeptPageQueryModel;
import com.muyu.system.domain.rep.SysDeptListReq;
import com.muyu.system.service.SysDeptService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.ArrayUtils;
@ -36,9 +38,9 @@ public class SysDeptController extends BaseController {
*/
@RequiresPermissions("system:dept:list")
@PostMapping("/list")
public Result list (@RequestBody SysDept dept) {
List<SysDept> depts = deptService.selectDeptList(dept);
return success(depts);
public Result<List<SysDept>> list (@RequestBody SysDeptListReq sysDeptListReq) {
List<SysDept> deptList = deptService.queryList(SysDeptPageQueryModel.reqBuild(sysDeptListReq));
return success(deptList);
}
/**
@ -47,9 +49,9 @@ public class SysDeptController extends BaseController {
@RequiresPermissions("system:dept:list")
@GetMapping("/list/exclude/{deptId}")
public Result excludeChild (@PathVariable(value = "deptId", required = false) Long deptId) {
List<SysDept> depts = deptService.selectDeptList(new SysDept());
depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
return success(depts);
List<SysDept> deptList = deptService.queryList(SysDeptPageQueryModel.builder().build());
deptList.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId));
return success(deptList);
}
/**

View File

@ -66,7 +66,7 @@ public class SysDictDataController extends BaseController {
public Result dictType (@PathVariable("dictType") String dictType) {
List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
if (StringUtils.isNull(data)) {
data = new ArrayList<SysDictData>();
data = new ArrayList<>();
}
return success(data);
}
@ -99,7 +99,7 @@ public class SysDictDataController extends BaseController {
@RequiresPermissions("system:dict:remove")
@Log(title = "字典类型", businessType = BusinessType.DELETE)
@DeleteMapping("/{dictCodes}")
public Result remove (@PathVariable("dictCode") Long[] dictCodes) {
public Result remove (@PathVariable("dictCodes") Long[] dictCodes) {
dictDataService.deleteDictDataByIds(dictCodes);
return success();
}

View File

@ -204,7 +204,7 @@ public class SysRoleController extends BaseController {
public Result deptTree (@PathVariable("roleId") Long roleId) {
return Result.success(
DeptTreeResp.builder()
.depts(deptService.selectDeptTreeList(new SysDept()))
.depts(deptService.selectDeptTreeList())
.checkedKeys(deptService.selectDeptListByRoleId(roleId))
.build()
);

View File

@ -286,7 +286,7 @@ public class SysUserController extends BaseController {
*/
@RequiresPermissions("system:user:list")
@GetMapping("/deptTree")
public Result deptTree (SysDept dept) {
return success(deptService.selectDeptTreeList(dept));
public Result deptTree () {
return success(deptService.selectDeptTreeList());
}
}

View File

@ -1,7 +1,6 @@
package com.muyu.system.domain.model;
import com.muyu.common.core.web.model.QueryModel;
import com.muyu.system.domain.rep.SysConfigListReq;
import com.muyu.system.domain.rep.SysDeptListReq;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -9,8 +8,6 @@ import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.util.Date;
/**
*
*/
@ -21,6 +18,11 @@ import java.util.Date;
@EqualsAndHashCode(callSuper = false)
public class SysDeptPageQueryModel extends QueryModel<SysDeptPageQueryModel> {
/**
* ID
*/
private Long deptId;
/**
*
*/
@ -32,12 +34,26 @@ public class SysDeptPageQueryModel extends QueryModel<SysDeptPageQueryModel> {
private String status;
/**
*
* @param sysDeptListReq
* @return
*/
public static SysDeptPageQueryModel reqBuild(SysDeptListReq sysDeptListReq) {
SysDeptPageQueryModel sysDeptPageQueryModel = SysDeptPageQueryModel.builder()
return SysDeptPageQueryModel.builder()
.deptName(sysDeptListReq.getDeptName())
.status(sysDeptListReq.getStatus())
.build();
sysDeptPageQueryModel.domainBuild(sysDeptListReq);
return sysDeptPageQueryModel;
}
/**
* ID
* @param deptId ID
* @return
*/
public static SysDeptPageQueryModel ofToDeptId(Long deptId) {
return SysDeptPageQueryModel.builder()
.deptId(deptId)
.build();
}
}

View File

@ -1,10 +1,7 @@
package com.muyu.system.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.common.system.domain.SysDept;
import com.muyu.system.domain.SysConfig;
import com.muyu.system.domain.model.SysConfigPageQueryModel;
import com.muyu.system.domain.model.SysDeptPageQueryModel;
import com.muyu.system.domain.vo.TreeSelect;
@ -19,20 +16,18 @@ public interface SysDeptService extends IService<SysDept> {
/**
*
*
* @param dept
* @param deptPageQueryModel
*
* @return
*/
public List<SysDept> selectDeptList (SysDept dept);
public List<SysDept> selectDeptList (SysDeptPageQueryModel deptPageQueryModel);
/**
*
*
* @param dept
*
* @return
*/
public List<TreeSelect> selectDeptTreeList (SysDept dept);
public List<TreeSelect> selectDeptTreeList ();
/**
*
@ -140,5 +135,10 @@ public interface SysDeptService extends IService<SysDept> {
*/
public int deleteDeptById (Long deptId);
PageQueryModel<SysDept> pageQuery(SysDeptPageQueryModel sysDeptPageQueryModel);
/**
*
* @param sysDeptPageQueryModel
* @return
*/
List<SysDept> queryList(SysDeptPageQueryModel sysDeptPageQueryModel);
}

View File

@ -1,20 +1,17 @@
package com.muyu.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.constant.UserConstants;
import com.muyu.common.core.exception.ServiceException;
import com.muyu.common.core.text.Convert;
import com.muyu.common.core.utils.SpringUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.common.datascope.annotation.DataScope;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.SysDept;
import com.muyu.common.system.domain.SysRole;
import com.muyu.common.system.domain.SysUser;
import com.muyu.system.domain.SysConfig;
import com.muyu.system.domain.model.SysDeptPageQueryModel;
import com.muyu.system.domain.vo.TreeSelect;
import com.muyu.system.mapper.SysDeptMapper;
@ -24,7 +21,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@ -45,39 +41,45 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
@Override
public PageQueryModel<SysDept> pageQuery(SysDeptPageQueryModel sysDeptPageQueryModel) {
public List<SysDept> queryList(SysDeptPageQueryModel sysDeptPageQueryModel) {
LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(StringUtils.isNotEmpty(sysDeptPageQueryModel.getDeptName()),SysDept::getDeptName, sysDeptPageQueryModel.getDeptName());
queryWrapper.eq(StringUtils.isNotEmpty(sysDeptPageQueryModel.getStatus()),SysDept::getStatus,sysDeptPageQueryModel.getStatus());
Page<SysDept> page = this.page(sysDeptPageQueryModel.buildPage(), queryWrapper);
return PageQueryModel.of(page);
return this.list(queryWrapper);
}
/**
*
*
* @param dept
* @param sysDeptPageQueryModel
*
* @return
*/
@Override
@DataScope(deptAlias = "d")
public List<SysDept> selectDeptList (SysDept dept) {
return deptMapper.selectDeptList(dept);
public List<SysDept> selectDeptList (SysDeptPageQueryModel sysDeptPageQueryModel) {
LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(
StringUtils.isNotNull(sysDeptPageQueryModel.getDeptName()),SysDept::getDeptName, sysDeptPageQueryModel.getDeptName()
);
queryWrapper.eq(Objects.nonNull(sysDeptPageQueryModel.getStatus()),SysDept::getStatus,sysDeptPageQueryModel.getStatus());
queryWrapper.eq(sysDeptPageQueryModel.getDeptId() != null,SysDept::getDeptId,sysDeptPageQueryModel.getDeptId());
return this.list(queryWrapper);
}
/**
*
*
* @param dept
*
* @return
*/
@Override
public List<TreeSelect> selectDeptTreeList (SysDept dept) {
List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
return buildDeptTreeSelect(depts);
public List<TreeSelect> selectDeptTreeList () {
return buildDeptTreeSelect(
SpringUtils.getAopProxy(this).selectDeptList(
SysDeptPageQueryModel.ofNull()
)
);
}
/**
@ -89,8 +91,8 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
*/
@Override
public List<SysDept> buildDeptTree (List<SysDept> depts) {
List<SysDept> returnList = new ArrayList<SysDept>();
List<Long> tempList = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList());
List<SysDept> returnList = new ArrayList<>();
List<Long> tempList = depts.stream().map(SysDept::getDeptId).toList();
for (SysDept dept : depts) {
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(dept.getParentId())) {
@ -205,9 +207,8 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
@Override
public void checkDeptDataScope (Long deptId) {
if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
SysDept dept = new SysDept();
dept.setDeptId(deptId);
List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
List<SysDept> depts = SpringUtils.getAopProxy(this)
.selectDeptList(SysDeptPageQueryModel.ofToDeptId(deptId));
if (StringUtils.isEmpty(depts)) {
throw new ServiceException("没有权限访问部门数据!");
}
@ -281,7 +282,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
for (SysDept child : children) {
child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
}
if (children.size() > 0) {
if (!children.isEmpty()) {
deptMapper.updateDeptChildren(children);
}
}
@ -319,9 +320,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
*/
private List<SysDept> getChildList (List<SysDept> list, SysDept t) {
List<SysDept> tlist = new ArrayList<SysDept>();
Iterator<SysDept> it = list.iterator();
while (it.hasNext()) {
SysDept n = (SysDept) it.next();
for (SysDept n : list) {
if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) {
tlist.add(n);
}
@ -333,6 +332,6 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
*
*/
private boolean hasChild (List<SysDept> list, SysDept t) {
return getChildList(list, t).size() > 0 ? true : false;
return !getChildList(list, t).isEmpty();
}
}