mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 20:57:23 +08:00
refactor(extension/crud): 适配 Crane4j 数据填充组件
This commit is contained in:
@@ -44,5 +44,11 @@
|
||||
<groupId>top.charles7c.continew</groupId>
|
||||
<artifactId>continew-starter-api-doc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Crane4j(一个基于注解的,用于完成一切 “根据 A 的 key 值拿到 B,再把 B 的属性映射到 A” 这类需求的字段填充框架) -->
|
||||
<dependency>
|
||||
<groupId>cn.crane4j</groupId>
|
||||
<artifactId>crane4j-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@@ -21,6 +21,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.format.support.FormattingConversionService;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
@@ -37,6 +38,7 @@ import top.charles7c.continew.starter.extension.crud.handler.CrudRequestMappingH
|
||||
*/
|
||||
@Slf4j
|
||||
@AutoConfiguration
|
||||
@Import({UserNicknameContainer.class})
|
||||
public class CrudAutoConfiguration extends DelegatingWebMvcConfiguration {
|
||||
|
||||
/**
|
||||
|
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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.starter.extension.crud.autoconfigure;
|
||||
|
||||
import cn.crane4j.core.container.Container;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.charles7c.continew.starter.extension.crud.base.CommonUserService;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 用户昵称容器
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class UserNicknameContainer implements Container<Long> {
|
||||
|
||||
private final CommonUserService userService;
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
return "userNickname";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, String> get(Collection<Long> ids) {
|
||||
Long id = CollUtil.getFirst(ids);
|
||||
String name = userService.getNicknameById(id);
|
||||
return Collections.singletonMap(id, name);
|
||||
}
|
||||
}
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package top.charles7c.continew.starter.extension.crud.base;
|
||||
|
||||
import cn.crane4j.annotation.Assemble;
|
||||
import cn.crane4j.annotation.Mapping;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
@@ -42,6 +44,7 @@ public class BaseDetailResp extends BaseResp {
|
||||
* 修改人
|
||||
*/
|
||||
@JsonIgnore
|
||||
@Assemble(container = "userNickname", props = @Mapping(ref = "updateUserString"))
|
||||
private Long updateUser;
|
||||
|
||||
/**
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package top.charles7c.continew.starter.extension.crud.base;
|
||||
|
||||
import cn.crane4j.annotation.Assemble;
|
||||
import cn.crane4j.annotation.Mapping;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
@@ -47,6 +49,7 @@ public class BaseResp implements Serializable {
|
||||
* 创建人
|
||||
*/
|
||||
@JsonIgnore
|
||||
@Assemble(container = "userNickname", props = @Mapping(ref = "createUserString"))
|
||||
private Long createUser;
|
||||
|
||||
/**
|
||||
|
@@ -16,6 +16,7 @@
|
||||
|
||||
package top.charles7c.continew.starter.extension.crud.base;
|
||||
|
||||
import cn.crane4j.core.support.OperateTemplate;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
@@ -34,7 +35,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import top.charles7c.continew.starter.core.util.ClassUtils;
|
||||
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.base.BaseMapper;
|
||||
@@ -137,26 +137,11 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
|
||||
return BeanUtil.copyToList(entityList, targetClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置排序
|
||||
*
|
||||
* @param queryWrapper 查询 Wrapper
|
||||
* @param sortQuery 排序查询条件
|
||||
*/
|
||||
protected void sort(QueryWrapper<T> queryWrapper, SortQuery sortQuery) {
|
||||
Sort sort = Opt.ofNullable(sortQuery).orElseGet(SortQuery::new).getSort();
|
||||
for (Sort.Order order : sort) {
|
||||
if (null != order) {
|
||||
queryWrapper.orderBy(true, order.isAscending(), StrUtil.toUnderlineCase(order.getProperty()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public D get(Long id) {
|
||||
T entity = this.getById(id);
|
||||
D detail = BeanUtil.copyProperties(entity, detailClass);
|
||||
this.fillDetail(detail);
|
||||
this.fill(detail);
|
||||
return detail;
|
||||
}
|
||||
|
||||
@@ -186,10 +171,20 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
|
||||
@Override
|
||||
public void export(Q query, SortQuery sortQuery, HttpServletResponse response) {
|
||||
List<D> list = this.list(query, sortQuery, detailClass);
|
||||
list.forEach(this::fillDetail);
|
||||
list.forEach(this::fill);
|
||||
ExcelUtils.export(list, "导出数据", detailClass, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充数据
|
||||
*
|
||||
* @param obj 待填充信息
|
||||
*/
|
||||
protected void fill(Object obj) {
|
||||
OperateTemplate operateTemplate = SpringUtil.getBean(OperateTemplate.class);
|
||||
operateTemplate.execute(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 查询
|
||||
*
|
||||
@@ -203,35 +198,17 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充数据
|
||||
* 设置排序
|
||||
*
|
||||
* @param baseObj 待填充列表信息
|
||||
* @param queryWrapper 查询 Wrapper
|
||||
* @param sortQuery 排序查询条件
|
||||
*/
|
||||
protected void fill(Object baseObj) {
|
||||
if (baseObj instanceof BaseResp baseResp) {
|
||||
Long createUser = baseResp.getCreateUser();
|
||||
if (null == createUser) {
|
||||
return;
|
||||
protected void sort(QueryWrapper<T> queryWrapper, SortQuery sortQuery) {
|
||||
Sort sort = Opt.ofNullable(sortQuery).orElseGet(SortQuery::new).getSort();
|
||||
for (Sort.Order order : sort) {
|
||||
if (null != order) {
|
||||
queryWrapper.orderBy(true, order.isAscending(), StrUtil.toUnderlineCase(order.getProperty()));
|
||||
}
|
||||
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
|
||||
baseResp.setCreateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(createUser)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充详情数据
|
||||
*
|
||||
* @param detailObj 待填充详情信息
|
||||
*/
|
||||
public void fillDetail(Object detailObj) {
|
||||
if (detailObj instanceof BaseDetailResp detailResp) {
|
||||
this.fill(detailResp);
|
||||
Long updateUser = detailResp.getUpdateUser();
|
||||
if (null == updateUser) {
|
||||
return;
|
||||
}
|
||||
CommonUserService userService = SpringUtil.getBean(CommonUserService.class);
|
||||
detailResp.setUpdateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(updateUser)));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user