Skip to content

Commit

Permalink
refactor: 适配 Crane4j 数据填充组件
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Jan 12, 2024
1 parent ac0608f commit d598408
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -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:https://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 {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

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.Serializable;
import java.time.LocalDateTime;

import lombok.Data;

import io.swagger.v3.oas.annotations.media.Schema;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* 基础日志信息
*
Expand All @@ -48,6 +48,7 @@ public class LogResp implements Serializable {
* 创建人
*/
@JsonIgnore
@Assemble(container = "userNickname", props = @Mapping(ref = "createUserString"))
private Long createUser;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

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.OperationLogQuery;
import top.charles7c.continew.admin.monitor.model.query.SystemLogQuery;
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.resp.PageResp;

import java.util.List;
import java.util.Map;

/**
* 系统日志业务接口
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@

package top.charles7c.continew.admin.monitor.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.crane4j.annotation.AutoOperate;
import cn.hutool.core.bean.BeanUtil;
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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;

import top.charles7c.continew.admin.common.constant.SysConstants;
import top.charles7c.continew.admin.monitor.mapper.LogMapper;
import top.charles7c.continew.admin.monitor.model.entity.LogDO;
Expand All @@ -39,14 +32,16 @@
import top.charles7c.continew.admin.monitor.model.query.SystemLogQuery;
import top.charles7c.continew.admin.monitor.model.resp.*;
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.validate.CheckUtils;
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.resp.PageResp;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 系统日志业务实现
*
Expand All @@ -59,9 +54,9 @@
public class LogServiceImpl implements LogService {

private final LogMapper logMapper;
private final CommonUserService commonUserService;

@Override
@AutoOperate(type = OperationLogResp.class, on = "list")
public PageResp<OperationLogResp> page(OperationLogQuery query, PageQuery pageQuery) {
QueryWrapper<LogDO> queryWrapper = QueryHelper.build(query);
// 限定查询信息
Expand All @@ -73,18 +68,11 @@ public PageResp<OperationLogResp> page(OperationLogQuery query, PageQuery pageQu
queryWrapper.select(columnNameList);
// 分页查询
IPage<LogDO> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper);
PageResp<OperationLogResp> pageResp = 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;
return PageResp.build(page, OperationLogResp.class);
}

@Override
@AutoOperate(type = LoginLogResp.class, on = "list")
public PageResp<LoginLogResp> page(LoginLogQuery query, PageQuery pageQuery) {
QueryWrapper<LogDO> queryWrapper = QueryHelper.build(query);
queryWrapper.eq("module", "登录");
Expand All @@ -97,13 +85,11 @@ public PageResp<LoginLogResp> page(LoginLogQuery query, PageQuery pageQuery) {
queryWrapper.select(columnNameList);
// 分页查询
IPage<LogDO> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper);
PageResp<LoginLogResp> pageResp = PageResp.build(page, LoginLogResp.class);
// 填充数据
pageResp.getList().forEach(this::fill);
return pageResp;
return PageResp.build(page, LoginLogResp.class);
}

@Override
@AutoOperate(type = SystemLogResp.class, on = "list")
public PageResp<SystemLogResp> page(SystemLogQuery query, PageQuery pageQuery) {
QueryWrapper<LogDO> queryWrapper = QueryHelper.build(query);
// 限定查询信息
Expand All @@ -115,19 +101,15 @@ public PageResp<SystemLogResp> page(SystemLogQuery query, PageQuery pageQuery) {
queryWrapper.select(columnNameList);
// 分页查询
IPage<LogDO> page = logMapper.selectPage(pageQuery.toPage(), queryWrapper);
PageResp<SystemLogResp> pageResp = PageResp.build(page, SystemLogResp.class);
// 填充数据
pageResp.getList().forEach(this::fill);
return pageResp;
return PageResp.build(page, SystemLogResp.class);
}

@Override
@AutoOperate(type = SystemLogDetailResp.class)
public SystemLogDetailResp get(Long id) {
LogDO logDO = logMapper.selectById(id);
CheckUtils.throwIfNotExists(logDO, "LogDO", "ID", id);
SystemLogDetailResp detail = BeanUtil.copyProperties(logDO, SystemLogDetailResp.class);
this.fill(detail);
return detail;
return BeanUtil.copyProperties(logDO, SystemLogDetailResp.class);
}

@Override
Expand All @@ -149,17 +131,4 @@ public List<DashboardPopularModuleResp> listDashboardPopularModule() {
public List<Map<String, Object>> listDashboardGeoDistribution() {
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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@

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 cn.hutool.core.collection.CollUtil;
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 cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;

import top.charles7c.continew.admin.common.constant.SysConstants;
import top.charles7c.continew.admin.common.enums.DisEnableStatusEnum;
import top.charles7c.continew.admin.system.mapper.DeptMapper;
Expand All @@ -46,6 +37,11 @@
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
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;

/**
* 部门业务实现
*
Expand Down Expand Up @@ -126,9 +122,9 @@ public void delete(List<Long> ids) {
}

@Override
public void fillDetail(Object detailObj) {
super.fillDetail(detailObj);
if (detailObj instanceof DeptDetailResp detail) {
protected void fill(Object obj) {
super.fill(obj);
if (obj instanceof DeptDetailResp detail) {
if (Objects.equals(SysConstants.SUPER_PARENT_ID, detail.getParentId())) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@

package top.charles7c.continew.admin.system.service.impl;

import java.util.List;

import lombok.RequiredArgsConstructor;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.common.model.resp.LabelValueResp;
import top.charles7c.continew.admin.system.mapper.DictItemMapper;
Expand All @@ -37,6 +33,8 @@
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
import top.charles7c.continew.starter.extension.crud.model.query.SortQuery;

import java.util.List;

/**
* 字典项业务实现
*
Expand Down Expand Up @@ -71,7 +69,7 @@ public List<DictItemDetailResp> listByDictId(Long dictId) {
SortQuery sortQuery = new SortQuery();
sortQuery.setSort(new String[] {"sort,asc"});
List<DictItemDetailResp> detailList = super.list(dictItemQuery, sortQuery, DictItemDetailResp.class);
detailList.forEach(super::fillDetail);
detailList.forEach(super::fill);
return detailList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,17 @@

package top.charles7c.continew.admin.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
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.*;
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 cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;

import top.charles7c.continew.admin.system.enums.StorageTypeEnum;
import top.charles7c.continew.admin.system.mapper.FileMapper;
import top.charles7c.continew.admin.system.model.entity.FileDO;
Expand All @@ -47,6 +42,10 @@
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 文件业务实现
*
Expand Down Expand Up @@ -117,11 +116,11 @@ public Long countByStorageIds(List<Long> storageIds) {
}

@Override
protected void fill(Object baseObj) {
if (baseObj instanceof FileResp fileResp && !URLUtils.isHttpUrl(fileResp.getUrl())) {
protected void fill(Object obj) {
super.fill(obj);
if (obj instanceof FileResp fileResp && !URLUtils.isHttpUrl(fileResp.getUrl())) {
StorageDetailResp storage = storageService.get(fileResp.getStorageId());
fileResp.setUrl(URLUtil.normalize(storage.getDomain() + StringConstants.SLASH + fileResp.getUrl()));
}
super.fill(baseObj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@

package top.charles7c.continew.admin.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
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 cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;

import top.charles7c.continew.admin.auth.service.OnlineUserService;
import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.common.constant.SysConstants;
Expand All @@ -47,6 +41,9 @@
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

/**
* 角色业务实现
*
Expand Down Expand Up @@ -133,9 +130,9 @@ public void delete(List<Long> ids) {
}

@Override
public void fillDetail(Object detailObj) {
super.fillDetail(detailObj);
if (detailObj instanceof RoleDetailResp detail) {
protected void fill(Object obj) {
super.fill(obj);
if (obj instanceof RoleDetailResp detail) {
Long roleId = detail.getId();
if (SysConstants.ADMIN_ROLE_CODE.equals(detail.getCode())) {
List<MenuResp> list = menuService.list(null, null);
Expand Down
Loading

0 comments on commit d598408

Please sign in to comment.