refactor: 适配 Crane4j 数据填充组件

This commit is contained in:
Charles7c 2024-01-12 23:19:00 +08:00
parent ac0608f371
commit d5984087a3
9 changed files with 109 additions and 123 deletions

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.admin.common.config;
import org.springframework.context.annotation.Configuration;
/**
* Crane4j 配置
*
* @author Charles7c
* @since 2024/1/11 21:22
*/
@Configuration
public class Crane4jConfiguration extends cn.crane4j.spring.boot.config.Crane4jAutoConfiguration {}

View File

@ -16,16 +16,16 @@
package top.charles7c.continew.admin.monitor.model.resp; package top.charles7c.continew.admin.monitor.model.resp;
import cn.crane4j.annotation.Assemble;
import cn.crane4j.annotation.Mapping;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial; import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import com.fasterxml.jackson.annotation.JsonIgnore;
/** /**
* 基础日志信息 * 基础日志信息
* *
@ -48,6 +48,7 @@ public class LogResp implements Serializable {
* 创建人 * 创建人
*/ */
@JsonIgnore @JsonIgnore
@Assemble(container = "userNickname", props = @Mapping(ref = "createUserString"))
private Long createUser; private Long createUser;
/** /**

View File

@ -16,9 +16,6 @@
package top.charles7c.continew.admin.monitor.service; package top.charles7c.continew.admin.monitor.service;
import java.util.List;
import java.util.Map;
import top.charles7c.continew.admin.monitor.model.query.LoginLogQuery; import top.charles7c.continew.admin.monitor.model.query.LoginLogQuery;
import top.charles7c.continew.admin.monitor.model.query.OperationLogQuery; import top.charles7c.continew.admin.monitor.model.query.OperationLogQuery;
import top.charles7c.continew.admin.monitor.model.query.SystemLogQuery; import top.charles7c.continew.admin.monitor.model.query.SystemLogQuery;
@ -26,6 +23,9 @@ import top.charles7c.continew.admin.monitor.model.resp.*;
import top.charles7c.continew.starter.extension.crud.model.query.PageQuery; import top.charles7c.continew.starter.extension.crud.model.query.PageQuery;
import top.charles7c.continew.starter.extension.crud.model.resp.PageResp; import top.charles7c.continew.starter.extension.crud.model.resp.PageResp;
import java.util.List;
import java.util.Map;
/** /**
* 系统日志业务接口 * 系统日志业务接口
* *

View File

@ -16,21 +16,14 @@
package top.charles7c.continew.admin.monitor.service.impl; package top.charles7c.continew.admin.monitor.service.impl;
import java.util.List; import cn.crane4j.annotation.AutoOperate;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.charles7c.continew.admin.common.constant.SysConstants; import top.charles7c.continew.admin.common.constant.SysConstants;
import top.charles7c.continew.admin.monitor.mapper.LogMapper; import top.charles7c.continew.admin.monitor.mapper.LogMapper;
import top.charles7c.continew.admin.monitor.model.entity.LogDO; import top.charles7c.continew.admin.monitor.model.entity.LogDO;
@ -39,14 +32,16 @@ import top.charles7c.continew.admin.monitor.model.query.OperationLogQuery;
import top.charles7c.continew.admin.monitor.model.query.SystemLogQuery; import top.charles7c.continew.admin.monitor.model.query.SystemLogQuery;
import top.charles7c.continew.admin.monitor.model.resp.*; import top.charles7c.continew.admin.monitor.model.resp.*;
import top.charles7c.continew.admin.monitor.service.LogService; import top.charles7c.continew.admin.monitor.service.LogService;
import top.charles7c.continew.starter.core.util.ExceptionUtils;
import top.charles7c.continew.starter.core.util.ReflectUtils; import top.charles7c.continew.starter.core.util.ReflectUtils;
import top.charles7c.continew.starter.core.util.validate.CheckUtils; import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.data.mybatis.plus.query.QueryHelper; import top.charles7c.continew.starter.data.mybatis.plus.query.QueryHelper;
import top.charles7c.continew.starter.extension.crud.base.CommonUserService;
import top.charles7c.continew.starter.extension.crud.model.query.PageQuery; import top.charles7c.continew.starter.extension.crud.model.query.PageQuery;
import top.charles7c.continew.starter.extension.crud.model.resp.PageResp; import top.charles7c.continew.starter.extension.crud.model.resp.PageResp;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/** /**
* 系统日志业务实现 * 系统日志业务实现
* *
@ -59,9 +54,9 @@ import top.charles7c.continew.starter.extension.crud.model.resp.PageResp;
public class LogServiceImpl implements LogService { public class LogServiceImpl implements LogService {
private final LogMapper logMapper; private final LogMapper logMapper;
private final CommonUserService commonUserService;
@Override @Override
@AutoOperate(type = OperationLogResp.class, on = "list")
public PageResp<OperationLogResp> page(OperationLogQuery query, PageQuery pageQuery) { public PageResp<OperationLogResp> page(OperationLogQuery query, PageQuery pageQuery) {
QueryWrapper<LogDO> queryWrapper = QueryHelper.build(query); QueryWrapper<LogDO> queryWrapper = QueryHelper.build(query);
// 限定查询信息 // 限定查询信息
@ -73,18 +68,11 @@ public class LogServiceImpl implements LogService {
queryWrapper.select(columnNameList); queryWrapper.select(columnNameList);
// 分页查询 // 分页查询
IPage<LogDO> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper); IPage<LogDO> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper);
PageResp<OperationLogResp> pageResp = PageResp.build(page, OperationLogResp.class); return PageResp.build(page, OperationLogResp.class);
// 填充数据如果是查询个人操作日志只查询一次用户信息即可
if (null != query.getUid()) {
String nickname = ExceptionUtils.exToNull(() -> commonUserService.getNicknameById(query.getUid()));
pageResp.getList().forEach(o -> o.setCreateUserString(nickname));
} else {
pageResp.getList().forEach(this::fill);
}
return pageResp;
} }
@Override @Override
@AutoOperate(type = LoginLogResp.class, on = "list")
public PageResp<LoginLogResp> page(LoginLogQuery query, PageQuery pageQuery) { public PageResp<LoginLogResp> page(LoginLogQuery query, PageQuery pageQuery) {
QueryWrapper<LogDO> queryWrapper = QueryHelper.build(query); QueryWrapper<LogDO> queryWrapper = QueryHelper.build(query);
queryWrapper.eq("module", "登录"); queryWrapper.eq("module", "登录");
@ -97,13 +85,11 @@ public class LogServiceImpl implements LogService {
queryWrapper.select(columnNameList); queryWrapper.select(columnNameList);
// 分页查询 // 分页查询
IPage<LogDO> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper); IPage<LogDO> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper);
PageResp<LoginLogResp> pageResp = PageResp.build(page, LoginLogResp.class); return PageResp.build(page, LoginLogResp.class);
// 填充数据
pageResp.getList().forEach(this::fill);
return pageResp;
} }
@Override @Override
@AutoOperate(type = SystemLogResp.class, on = "list")
public PageResp<SystemLogResp> page(SystemLogQuery query, PageQuery pageQuery) { public PageResp<SystemLogResp> page(SystemLogQuery query, PageQuery pageQuery) {
QueryWrapper<LogDO> queryWrapper = QueryHelper.build(query); QueryWrapper<LogDO> queryWrapper = QueryHelper.build(query);
// 限定查询信息 // 限定查询信息
@ -115,19 +101,15 @@ public class LogServiceImpl implements LogService {
queryWrapper.select(columnNameList); queryWrapper.select(columnNameList);
// 分页查询 // 分页查询
IPage<LogDO> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper); IPage<LogDO> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper);
PageResp<SystemLogResp> pageResp = PageResp.build(page, SystemLogResp.class); return PageResp.build(page, SystemLogResp.class);
// 填充数据
pageResp.getList().forEach(this::fill);
return pageResp;
} }
@Override @Override
@AutoOperate(type = SystemLogDetailResp.class)
public SystemLogDetailResp get(Long id) { public SystemLogDetailResp get(Long id) {
LogDO logDO = logMapper.selectById(id); LogDO logDO = logMapper.selectById(id);
CheckUtils.throwIfNotExists(logDO, "LogDO", "ID", id); CheckUtils.throwIfNotExists(logDO, "LogDO", "ID", id);
SystemLogDetailResp detail = BeanUtil.copyProperties(logDO, SystemLogDetailResp.class); return BeanUtil.copyProperties(logDO, SystemLogDetailResp.class);
this.fill(detail);
return detail;
} }
@Override @Override
@ -149,17 +131,4 @@ public class LogServiceImpl implements LogService {
public List<Map<String, Object>> listDashboardGeoDistribution() { public List<Map<String, Object>> listDashboardGeoDistribution() {
return logMapper.selectListDashboardGeoDistribution(); return logMapper.selectListDashboardGeoDistribution();
} }
/**
* 填充数据
*
* @param logResp 日志信息
*/
private void fill(LogResp logResp) {
Long createUser = logResp.getCreateUser();
if (null == createUser) {
return;
}
logResp.setCreateUserString(ExceptionUtils.exToNull(() -> commonUserService.getNicknameById(createUser)));
}
} }

View File

@ -16,21 +16,12 @@
package top.charles7c.continew.admin.system.service.impl; package top.charles7c.continew.admin.system.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.charles7c.continew.admin.common.constant.SysConstants; import top.charles7c.continew.admin.common.constant.SysConstants;
import top.charles7c.continew.admin.common.enums.DisEnableStatusEnum; import top.charles7c.continew.admin.common.enums.DisEnableStatusEnum;
import top.charles7c.continew.admin.system.mapper.DeptMapper; import top.charles7c.continew.admin.system.mapper.DeptMapper;
@ -46,6 +37,11 @@ import top.charles7c.continew.starter.core.util.ExceptionUtils;
import top.charles7c.continew.starter.core.util.validate.CheckUtils; import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl; import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/** /**
* 部门业务实现 * 部门业务实现
* *
@ -126,9 +122,9 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptRes
} }
@Override @Override
public void fillDetail(Object detailObj) { protected void fill(Object obj) {
super.fillDetail(detailObj); super.fill(obj);
if (detailObj instanceof DeptDetailResp detail) { if (obj instanceof DeptDetailResp detail) {
if (Objects.equals(SysConstants.SUPER_PARENT_ID, detail.getParentId())) { if (Objects.equals(SysConstants.SUPER_PARENT_ID, detail.getParentId())) {
return; return;
} }

View File

@ -16,14 +16,10 @@
package top.charles7c.continew.admin.system.service.impl; package top.charles7c.continew.admin.system.service.impl;
import java.util.List;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import top.charles7c.continew.admin.common.constant.CacheConstants; import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.common.model.resp.LabelValueResp; import top.charles7c.continew.admin.common.model.resp.LabelValueResp;
import top.charles7c.continew.admin.system.mapper.DictItemMapper; import top.charles7c.continew.admin.system.mapper.DictItemMapper;
@ -37,6 +33,8 @@ import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl; import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
import top.charles7c.continew.starter.extension.crud.model.query.SortQuery; import top.charles7c.continew.starter.extension.crud.model.query.SortQuery;
import java.util.List;
/** /**
* 字典项业务实现 * 字典项业务实现
* *
@ -71,7 +69,7 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
SortQuery sortQuery = new SortQuery(); SortQuery sortQuery = new SortQuery();
sortQuery.setSort(new String[] {"sort,asc"}); sortQuery.setSort(new String[] {"sort,asc"});
List<DictItemDetailResp> detailList = super.list(dictItemQuery, sortQuery, DictItemDetailResp.class); List<DictItemDetailResp> detailList = super.list(dictItemQuery, sortQuery, DictItemDetailResp.class);
detailList.forEach(super::fillDetail); detailList.forEach(super::fill);
return detailList; return detailList;
} }

View File

@ -16,22 +16,17 @@
package top.charles7c.continew.admin.system.service.impl; package top.charles7c.continew.admin.system.service.impl;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil; import cn.hutool.core.util.URLUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.ProgressListener;
import org.dromara.x.file.storage.core.UploadPretreatment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.charles7c.continew.admin.system.enums.StorageTypeEnum; import top.charles7c.continew.admin.system.enums.StorageTypeEnum;
import top.charles7c.continew.admin.system.mapper.FileMapper; import top.charles7c.continew.admin.system.mapper.FileMapper;
import top.charles7c.continew.admin.system.model.entity.FileDO; import top.charles7c.continew.admin.system.model.entity.FileDO;
@ -47,6 +42,10 @@ import top.charles7c.continew.starter.core.util.URLUtils;
import top.charles7c.continew.starter.core.util.validate.CheckUtils; import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl; import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/** /**
* 文件业务实现 * 文件业务实现
* *
@ -117,11 +116,11 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
} }
@Override @Override
protected void fill(Object baseObj) { protected void fill(Object obj) {
if (baseObj instanceof FileResp fileResp && !URLUtils.isHttpUrl(fileResp.getUrl())) { super.fill(obj);
if (obj instanceof FileResp fileResp && !URLUtils.isHttpUrl(fileResp.getUrl())) {
StorageDetailResp storage = storageService.get(fileResp.getStorageId()); StorageDetailResp storage = storageService.get(fileResp.getStorageId());
fileResp.setUrl(URLUtil.normalize(storage.getDomain() + StringConstants.SLASH + fileResp.getUrl())); fileResp.setUrl(URLUtil.normalize(storage.getDomain() + StringConstants.SLASH + fileResp.getUrl()));
} }
super.fill(baseObj);
} }
} }

View File

@ -16,19 +16,13 @@
package top.charles7c.continew.admin.system.service.impl; package top.charles7c.continew.admin.system.service.impl;
import java.util.*;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.charles7c.continew.admin.auth.service.OnlineUserService; import top.charles7c.continew.admin.auth.service.OnlineUserService;
import top.charles7c.continew.admin.common.constant.CacheConstants; import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.common.constant.SysConstants; import top.charles7c.continew.admin.common.constant.SysConstants;
@ -47,6 +41,9 @@ import top.charles7c.continew.admin.system.service.*;
import top.charles7c.continew.starter.core.util.validate.CheckUtils; import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl; import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
import java.util.*;
import java.util.stream.Collectors;
/** /**
* 角色业务实现 * 角色业务实现
* *
@ -133,9 +130,9 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
} }
@Override @Override
public void fillDetail(Object detailObj) { protected void fill(Object obj) {
super.fillDetail(detailObj); super.fill(obj);
if (detailObj instanceof RoleDetailResp detail) { if (obj instanceof RoleDetailResp detail) {
Long roleId = detail.getId(); Long roleId = detail.getId();
if (SysConstants.ADMIN_ROLE_CODE.equals(detail.getCode())) { if (SysConstants.ADMIN_ROLE_CODE.equals(detail.getCode())) {
List<MenuResp> list = menuService.list(null, null); List<MenuResp> list = menuService.list(null, null);

View File

@ -16,13 +16,12 @@
package top.charles7c.continew.admin.system.service.impl; package top.charles7c.continew.admin.system.service.impl;
import java.time.LocalDateTime; import cn.hutool.core.collection.CollUtil;
import java.util.*; import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo; import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService; import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheConfig;
@ -30,12 +29,6 @@ import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import top.charles7c.continew.admin.common.constant.CacheConstants; import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.common.constant.FileConstants; import top.charles7c.continew.admin.common.constant.FileConstants;
import top.charles7c.continew.admin.common.constant.SysConstants; import top.charles7c.continew.admin.common.constant.SysConstants;
@ -57,6 +50,11 @@ import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl; import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
import top.charles7c.continew.starter.extension.crud.base.CommonUserService; import top.charles7c.continew.starter.extension.crud.base.CommonUserService;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
/** /**
* 用户业务实现 * 用户业务实现
* *
@ -148,9 +146,9 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
} }
@Override @Override
public void fillDetail(Object detailObj) { protected void fill(Object obj) {
super.fillDetail(detailObj); super.fill(obj);
if (detailObj instanceof UserDetailResp detail) { if (obj instanceof UserDetailResp detail) {
detail.setDeptName(ExceptionUtils.exToNull(() -> deptService.get(detail.getDeptId()).getName())); detail.setDeptName(ExceptionUtils.exToNull(() -> deptService.get(detail.getDeptId()).getName()));
List<Long> roleIdList = userRoleService.listRoleIdByUserId(detail.getId()); List<Long> roleIdList = userRoleService.listRoleIdByUserId(detail.getId());
detail.setRoleIds(roleIdList); detail.setRoleIds(roleIdList);