重构:初步封装后端 CRUD 公共组件(BaseController、BaseService、BaseServiceImpl)
This commit is contained in:
parent
dab3e597c2
commit
d7851bc811
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.cnadmin.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 增删改查请求映射器注解
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/27 9:54
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface CrudRequestMapping {
|
||||
|
||||
/**
|
||||
* 路径映射 URI(等同于:@RequestMapping("/foo1"))
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* API 列表
|
||||
*/
|
||||
Api[] api() default Api.ALL;
|
||||
|
||||
/**
|
||||
* API 枚举
|
||||
*/
|
||||
enum Api {
|
||||
/**
|
||||
* 所有 API
|
||||
*/
|
||||
ALL,
|
||||
/**
|
||||
* 分页
|
||||
*/
|
||||
PAGE,
|
||||
/**
|
||||
* 列表
|
||||
*/
|
||||
LIST,
|
||||
/**
|
||||
* 详情
|
||||
*/
|
||||
DETAIL,
|
||||
/**
|
||||
* 新增
|
||||
*/
|
||||
CREATE,
|
||||
/**
|
||||
* 修改
|
||||
*/
|
||||
UPDATE,
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
DELETE,;
|
||||
}
|
||||
}
|
@ -0,0 +1,149 @@
|
||||
/*
|
||||
* 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.cnadmin.common.base;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
import top.charles7c.cnadmin.common.model.vo.R;
|
||||
|
||||
/**
|
||||
* 控制器基类
|
||||
*
|
||||
* @param <S>
|
||||
* 业务接口
|
||||
* @param <V>
|
||||
* 列表信息
|
||||
* @param <D>
|
||||
* 详情信息
|
||||
* @param <Q>
|
||||
* 查询条件
|
||||
* @param <C>
|
||||
* 创建信息
|
||||
* @param <U>
|
||||
* 修改信息
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 10:45
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public abstract class BaseController<S extends BaseService<V, D, Q, C, U>, V, D, Q, C, U> {
|
||||
|
||||
@Autowired
|
||||
protected S baseService;
|
||||
|
||||
/**
|
||||
* 分页查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 分页列表信息
|
||||
*/
|
||||
@Operation(summary = "分页查询列表")
|
||||
@GetMapping
|
||||
protected R<PageInfo<V>> page(@Validated Q query, @Validated PageQuery pageQuery) {
|
||||
PageInfo<V> pageInfo = baseService.page(query, pageQuery);
|
||||
return R.ok(pageInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
@Operation(summary = "查询列表")
|
||||
@GetMapping("/all")
|
||||
protected R<List<V>> list(@Validated Q query) {
|
||||
List<V> list = baseService.list(query);
|
||||
return R.ok(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看详情
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @return 详情信息
|
||||
*/
|
||||
@Operation(summary = "查看详情")
|
||||
@Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
|
||||
@GetMapping("/{id}")
|
||||
protected R<D> detail(@PathVariable Long id) {
|
||||
D detail = baseService.detail(id);
|
||||
return R.ok(detail);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param request
|
||||
* 创建信息
|
||||
* @return 自增 ID
|
||||
*/
|
||||
@Operation(summary = "新增")
|
||||
@PostMapping
|
||||
protected R<Long> create(@Validated @RequestBody C request) {
|
||||
Long id = baseService.create(request);
|
||||
return R.ok("新增成功", id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @param request
|
||||
* 修改信息
|
||||
* @return /
|
||||
*/
|
||||
@Operation(summary = "修改")
|
||||
@Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
|
||||
@PutMapping("/{id}")
|
||||
protected R update(@PathVariable Long id, @Validated @RequestBody U request) {
|
||||
baseService.update(id, request);
|
||||
return R.ok("修改成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param ids
|
||||
* ID 列表
|
||||
* @return /
|
||||
*/
|
||||
@Operation(summary = "删除")
|
||||
@Parameter(name = "ids", description = "ID 列表", in = ParameterIn.PATH)
|
||||
@DeleteMapping("/{ids}")
|
||||
protected R delete(@PathVariable List<Long> ids) {
|
||||
baseService.delete(ids);
|
||||
return R.ok("删除成功");
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.cnadmin.common.base;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
/**
|
||||
* 详情 VO 基类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 10:40
|
||||
*/
|
||||
@Data
|
||||
public class BaseDetailVO extends BaseVO {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@JsonIgnore
|
||||
private Long createUser;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@Schema(description = "创建人")
|
||||
private String createUserString;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Schema(description = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
}
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.model.entity;
|
||||
package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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.cnadmin.common.base;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
|
||||
/**
|
||||
* 业务接口基类
|
||||
*
|
||||
* @param <V>
|
||||
* 列表信息
|
||||
* @param <D>
|
||||
* 详情信息
|
||||
* @param <Q>
|
||||
* 查询条件
|
||||
* @param <C>
|
||||
* 创建信息
|
||||
* @param <U>
|
||||
* 修改信息
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 16:54
|
||||
*/
|
||||
public interface BaseService<V, D, Q, C, U> {
|
||||
|
||||
/**
|
||||
* 分页查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 分页列表信息
|
||||
*/
|
||||
default PageInfo<V> page(Q query, PageQuery pageQuery) {
|
||||
return new PageInfo<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
default List<V> list(Q query) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看详情
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @return 详情信息
|
||||
*/
|
||||
default D detail(Long id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param request
|
||||
* 创建信息
|
||||
* @return 自增 ID
|
||||
*/
|
||||
default Long create(C request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @param request
|
||||
* 修改信息
|
||||
*/
|
||||
default void update(Long id, U request) {}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param ids
|
||||
* ID 列表
|
||||
*/
|
||||
default void delete(List<Long> ids) {}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
/*
|
||||
* 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.cnadmin.common.base;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
import top.charles7c.cnadmin.common.util.helper.QueryHelper;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
|
||||
/**
|
||||
* 业务实现基类
|
||||
*
|
||||
* @param <M>
|
||||
* Mapper 接口
|
||||
* @param <T>
|
||||
* 实体类
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 21:52
|
||||
*/
|
||||
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C, U> implements BaseService<V, D, Q, C, U> {
|
||||
|
||||
@Autowired
|
||||
protected M baseMapper;
|
||||
|
||||
protected Class<T> entityClass = currentEntityClass();
|
||||
protected Class<V> voClass = currentVoClass();
|
||||
protected Class<D> detailVoClass = currentDetailVoClass();
|
||||
|
||||
@Override
|
||||
public PageInfo<V> page(Q query, PageQuery pageQuery) {
|
||||
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
|
||||
IPage<T> page = baseMapper.selectPage(pageQuery.toPage(), queryWrapper);
|
||||
return PageInfo.build(page, voClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<V> list(Q query) {
|
||||
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
|
||||
List<T> entityList = baseMapper.selectList(queryWrapper);
|
||||
return BeanUtil.copyToList(entityList, voClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public D detail(Long id) {
|
||||
T entity = baseMapper.selectById(id);
|
||||
CheckUtils.throwIfNull(entity, String.format("ID为 [%s] 的记录已不存在", id));
|
||||
return BeanUtil.copyProperties(entity, detailVoClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public abstract Long create(C request);
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(Long id, U request) {
|
||||
T entity = BeanUtil.copyProperties(request, entityClass);
|
||||
baseMapper.updateById(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(List<Long> ids) {
|
||||
baseMapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类 Class 对象
|
||||
*
|
||||
* @return 实体类 Class 对象
|
||||
*/
|
||||
protected Class<T> currentEntityClass() {
|
||||
return (Class<T>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表信息类 Class 对象
|
||||
*
|
||||
* @return 列表信息类 Class 对象
|
||||
*/
|
||||
protected Class<V> currentVoClass() {
|
||||
return (Class<V>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详情信息类 Class 对象
|
||||
*
|
||||
* @return 详情信息类 Class 对象
|
||||
*/
|
||||
protected Class<D> currentDetailVoClass() {
|
||||
return (Class<D>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 3);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.cnadmin.common.base;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* VO 基类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 10:40
|
||||
*/
|
||||
@Data
|
||||
public class BaseVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@JsonIgnore
|
||||
private Long updateUser;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@Schema(description = "修改人")
|
||||
private String updateUserString;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
@Schema(description = "修改时间")
|
||||
private LocalDateTime updateTime;
|
||||
}
|
@ -31,7 +31,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
|
||||
import top.charles7c.cnadmin.common.config.properties.ContiNewAdminProperties;
|
||||
|
||||
/**
|
||||
* 接口文档配置
|
||||
@ -44,7 +44,7 @@ import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
|
||||
@ConditionalOnProperty(name = "springdoc.swagger-ui.enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class SwaggerConfiguration {
|
||||
|
||||
private final ContinewAdminProperties continewAdminProperties;
|
||||
private final ContiNewAdminProperties continewAdminProperties;
|
||||
|
||||
/**
|
||||
* 接口文档配置
|
||||
|
@ -33,9 +33,11 @@ import org.springframework.web.filter.CorsFilter;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import top.charles7c.cnadmin.common.config.properties.CorsProperties;
|
||||
import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties;
|
||||
import top.charles7c.cnadmin.common.handler.CrudRequestMappingHandlerMapping;
|
||||
|
||||
/**
|
||||
* Web MVC 配置
|
||||
@ -108,4 +110,12 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
|
||||
converters.add(0, mappingJackson2HttpMessageConverter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CRUD 请求映射器处理器映射器
|
||||
*/
|
||||
@Bean
|
||||
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
|
||||
return new CrudRequestMappingHandlerMapping();
|
||||
}
|
||||
}
|
||||
|
@ -24,8 +24,8 @@ import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseEntity;
|
||||
import top.charles7c.cnadmin.common.exception.ServiceException;
|
||||
import top.charles7c.cnadmin.common.model.entity.BaseEntity;
|
||||
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
|
||||
|
||||
/**
|
||||
|
@ -37,7 +37,7 @@ import cn.hutool.extra.spring.SpringUtil;
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "continew-admin")
|
||||
public class ContinewAdminProperties {
|
||||
public class ContiNewAdminProperties {
|
||||
|
||||
/**
|
||||
* 名称
|
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.config;
|
||||
package top.charles7c.cnadmin.common.config.threadpool;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.Executor;
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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.cnadmin.common.handler;
|
||||
|
||||
import static top.charles7c.cnadmin.common.annotation.CrudRequestMapping.Api;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.annotation.CrudRequestMapping;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
|
||||
/**
|
||||
* CRUD 请求映射器处理器映射器
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/27 10:30
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public class CrudRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
|
||||
|
||||
@Override
|
||||
protected RequestMappingInfo getMappingForMethod(@NonNull Method method, @NonNull Class<?> handlerType) {
|
||||
RequestMappingInfo requestMappingInfo = super.getMappingForMethod(method, handlerType);
|
||||
if (requestMappingInfo == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 如果没有声明 @CrudRequestMapping 注解,直接返回
|
||||
if (!handlerType.isAnnotationPresent(CrudRequestMapping.class)) {
|
||||
return requestMappingInfo;
|
||||
}
|
||||
|
||||
// 获取 @CrudRequestMapping 注解信息
|
||||
CrudRequestMapping crudRequestMapping = handlerType.getDeclaredAnnotation(CrudRequestMapping.class);
|
||||
// 拼接路径前缀(合并了 @RequestMapping 的部分能力)
|
||||
String pathPrefix = crudRequestMapping.value();
|
||||
if (StrUtil.isNotBlank(pathPrefix)) {
|
||||
requestMappingInfo = RequestMappingInfo.paths(pathPrefix).build().combine(requestMappingInfo);
|
||||
}
|
||||
|
||||
// 过滤 API
|
||||
Api[] apiArr = crudRequestMapping.api();
|
||||
// 如果非本类中定义,且 API 列表中不包含,则忽略
|
||||
Api api = ExceptionUtils.exToNull(() -> Api.valueOf(method.getName().toUpperCase()));
|
||||
if (method.getDeclaringClass() != handlerType && !ArrayUtil.containsAny(apiArr, Api.ALL, api)) {
|
||||
return null;
|
||||
}
|
||||
return requestMappingInfo;
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
|
||||
import top.charles7c.cnadmin.common.config.properties.ContiNewAdminProperties;
|
||||
|
||||
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
|
||||
import net.dreamlu.mica.ip2region.core.IpInfo;
|
||||
@ -55,7 +55,7 @@ public class IpUtils {
|
||||
* @return 归属地信息
|
||||
*/
|
||||
public static String getCityInfo(String ip) {
|
||||
if (ContinewAdminProperties.IP_ADDR_LOCAL_PARSE_ENABLED) {
|
||||
if (ContiNewAdminProperties.IP_ADDR_LOCAL_PARSE_ENABLED) {
|
||||
return getLocalCityInfo(ip);
|
||||
} else {
|
||||
return getHttpCityInfo(ip);
|
||||
|
@ -21,8 +21,8 @@ import lombok.Data;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseEntity;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
import top.charles7c.cnadmin.common.model.entity.BaseEntity;
|
||||
|
||||
/**
|
||||
* 部门实体
|
||||
|
@ -23,9 +23,9 @@ import lombok.Data;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseEntity;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
import top.charles7c.cnadmin.common.enums.GenderEnum;
|
||||
import top.charles7c.cnadmin.common.model.entity.BaseEntity;
|
||||
|
||||
/**
|
||||
* 用户实体
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package top.charles7c.cnadmin.system.model.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Data;
|
||||
@ -25,8 +23,7 @@ import lombok.experimental.Accessors;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseVO;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
|
||||
/**
|
||||
@ -38,7 +35,7 @@ import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@Schema(description = "部门信息")
|
||||
public class DeptVO implements Serializable {
|
||||
public class DeptVO extends BaseVO {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@ -78,24 +75,6 @@ public class DeptVO implements Serializable {
|
||||
@Schema(description = "状态(1启用 2禁用)")
|
||||
private DisEnableStatusEnum status;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@JsonIgnore
|
||||
private Long updateUser;
|
||||
|
||||
/**
|
||||
* 修改人昵称
|
||||
*/
|
||||
@Schema(description = "修改人昵称")
|
||||
private String updateUserString;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
@Schema(description = "修改时间")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
/**
|
||||
* 子部门列表
|
||||
*/
|
||||
|
@ -20,6 +20,7 @@ import java.util.List;
|
||||
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseService;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
import top.charles7c.cnadmin.system.model.query.DeptQuery;
|
||||
import top.charles7c.cnadmin.system.model.request.CreateDeptRequest;
|
||||
@ -31,16 +32,7 @@ import top.charles7c.cnadmin.system.model.vo.DeptVO;
|
||||
* @author Charles7c
|
||||
* @since 2023/1/22 17:54
|
||||
*/
|
||||
public interface DeptService {
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @return 列表数据
|
||||
*/
|
||||
List<DeptVO> list(DeptQuery query);
|
||||
public interface DeptService extends BaseService<DeptVO, DeptVO, DeptQuery, CreateDeptRequest, CreateDeptRequest> {
|
||||
|
||||
/**
|
||||
* 构建树
|
||||
@ -60,15 +52,6 @@ public interface DeptService {
|
||||
*/
|
||||
List<Tree<Long>> buildTree(List<DeptVO> list);
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param request
|
||||
* 创建信息
|
||||
* @return 新增记录 ID
|
||||
*/
|
||||
Long create(CreateDeptRequest request);
|
||||
|
||||
/**
|
||||
* 修改状态
|
||||
*
|
||||
@ -79,14 +62,6 @@ public interface DeptService {
|
||||
*/
|
||||
void updateStatus(List<Long> ids, DisEnableStatusEnum status);
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param ids
|
||||
* ID 列表
|
||||
*/
|
||||
void delete(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 检查部门名称是否存在
|
||||
*
|
||||
|
@ -33,6 +33,7 @@ import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseServiceImpl;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
import top.charles7c.cnadmin.common.util.TreeUtils;
|
||||
@ -54,9 +55,10 @@ import top.charles7c.cnadmin.system.service.UserService;
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class DeptServiceImpl implements DeptService {
|
||||
public class DeptServiceImpl
|
||||
extends BaseServiceImpl<DeptMapper, SysDept, DeptVO, DeptVO, DeptQuery, CreateDeptRequest, CreateDeptRequest>
|
||||
implements DeptService {
|
||||
|
||||
private final DeptMapper deptMapper;
|
||||
private final UserService userService;
|
||||
|
||||
@Override
|
||||
@ -64,7 +66,7 @@ public class DeptServiceImpl implements DeptService {
|
||||
QueryWrapper<SysDept> queryWrapper = QueryHelper.build(query);
|
||||
queryWrapper.lambda().orderByAsc(SysDept::getParentId).orderByAsc(SysDept::getDeptSort)
|
||||
.orderByDesc(SysDept::getUpdateTime);
|
||||
List<SysDept> list = deptMapper.selectList(queryWrapper);
|
||||
List<SysDept> list = baseMapper.selectList(queryWrapper);
|
||||
List<DeptVO> voList = BeanUtil.copyToList(list, DeptVO.class);
|
||||
voList.forEach(this::fill);
|
||||
return voList;
|
||||
@ -142,27 +144,27 @@ public class DeptServiceImpl implements DeptService {
|
||||
// 保存部门信息
|
||||
SysDept sysDept = BeanUtil.copyProperties(request, SysDept.class);
|
||||
sysDept.setStatus(DisEnableStatusEnum.ENABLE);
|
||||
deptMapper.insert(sysDept);
|
||||
baseMapper.insert(sysDept);
|
||||
return sysDept.getDeptId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateStatus(List<Long> ids, DisEnableStatusEnum status) {
|
||||
deptMapper.update(null,
|
||||
baseMapper.update(null,
|
||||
Wrappers.<SysDept>lambdaUpdate().set(SysDept::getStatus, status).in(SysDept::getDeptId, ids));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(List<Long> ids) {
|
||||
deptMapper.deleteBatchIds(ids);
|
||||
deptMapper.delete(Wrappers.<SysDept>lambdaQuery().in(SysDept::getParentId, ids));
|
||||
super.delete(ids);
|
||||
baseMapper.delete(Wrappers.<SysDept>lambdaQuery().in(SysDept::getParentId, ids));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkDeptNameExist(String deptName, Long parentId, Long deptId) {
|
||||
return deptMapper.exists(Wrappers.<SysDept>lambdaQuery().eq(SysDept::getDeptName, deptName)
|
||||
return baseMapper.exists(Wrappers.<SysDept>lambdaQuery().eq(SysDept::getDeptName, deptName)
|
||||
.eq(SysDept::getParentId, parentId).ne(deptId != null, SysDept::getDeptId, deptId));
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,7 @@ export interface DeptParams {
|
||||
}
|
||||
|
||||
export function getDeptList(params: DeptParams) {
|
||||
return axios.get<DeptRecord[]>('/system/dept', {
|
||||
return axios.get<DeptRecord[]>('/system/dept/all', {
|
||||
params,
|
||||
paramsSerializer: (obj) => {
|
||||
return qs.stringify(obj);
|
||||
|
@ -35,7 +35,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
|
||||
import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
|
||||
import top.charles7c.cnadmin.common.config.properties.ContiNewAdminProperties;
|
||||
|
||||
/**
|
||||
* 启动程序
|
||||
@ -51,7 +51,7 @@ import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
|
||||
@ComponentScan(basePackages = {"top.charles7c.cnadmin", "cn.hutool.extra.spring"})
|
||||
public class ContinewAdminApplication implements ApplicationRunner {
|
||||
|
||||
private final ContinewAdminProperties properties;
|
||||
private final ContiNewAdminProperties properties;
|
||||
private final ServerProperties serverProperties;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -42,7 +42,7 @@ import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.config.properties.CaptchaProperties;
|
||||
import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
|
||||
import top.charles7c.cnadmin.common.config.properties.ContiNewAdminProperties;
|
||||
import top.charles7c.cnadmin.common.consts.CacheConstants;
|
||||
import top.charles7c.cnadmin.common.model.vo.CaptchaVO;
|
||||
import top.charles7c.cnadmin.common.model.vo.R;
|
||||
@ -64,7 +64,7 @@ import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
public class CaptchaController {
|
||||
|
||||
private final CaptchaProperties captchaProperties;
|
||||
private final ContinewAdminProperties properties;
|
||||
private final ContiNewAdminProperties properties;
|
||||
|
||||
@Operation(summary = "获取图片验证码", description = "获取图片验证码(Base64编码,带图片格式:data:image/gif;base64)")
|
||||
@GetMapping("/img")
|
||||
|
@ -16,19 +16,20 @@
|
||||
|
||||
package top.charles7c.cnadmin.webapi.controller.system;
|
||||
|
||||
import java.util.List;
|
||||
import static top.charles7c.cnadmin.common.annotation.CrudRequestMapping.Api;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import java.util.List;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import top.charles7c.cnadmin.common.annotation.CrudRequestMapping;
|
||||
import top.charles7c.cnadmin.common.base.BaseController;
|
||||
import top.charles7c.cnadmin.common.model.request.UpdateStatusRequest;
|
||||
import top.charles7c.cnadmin.common.model.vo.R;
|
||||
import top.charles7c.cnadmin.system.model.query.DeptQuery;
|
||||
@ -44,39 +45,22 @@ import top.charles7c.cnadmin.system.service.DeptService;
|
||||
*/
|
||||
@Tag(name = "部门管理 API")
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping(value = "/system/dept", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public class DeptController {
|
||||
@CrudRequestMapping(value = "/system/dept", api = {Api.ALL})
|
||||
public class DeptController
|
||||
extends BaseController<DeptService, DeptVO, DeptVO, DeptQuery, CreateDeptRequest, CreateDeptRequest> {
|
||||
|
||||
private final DeptService deptService;
|
||||
|
||||
@Operation(summary = "查询部门列表")
|
||||
@GetMapping
|
||||
@Override
|
||||
@Operation(summary = "查询部门列表树")
|
||||
public R<List<DeptVO>> list(@Validated DeptQuery query) {
|
||||
List<DeptVO> list = deptService.list(query);
|
||||
return R.ok(deptService.buildListTree(list));
|
||||
}
|
||||
|
||||
@Operation(summary = "新增部门")
|
||||
@PostMapping
|
||||
public R<Long> create(@Validated @RequestBody CreateDeptRequest request) {
|
||||
Long id = deptService.create(request);
|
||||
return R.ok("新增成功", id);
|
||||
List<DeptVO> list = baseService.list(query);
|
||||
return R.ok(baseService.buildListTree(list));
|
||||
}
|
||||
|
||||
@Operation(summary = "修改部门状态")
|
||||
@Parameter(name = "ids", description = "ID 列表", in = ParameterIn.PATH)
|
||||
@PatchMapping("/{ids}")
|
||||
public R updateStatus(@PathVariable List<Long> ids, @Validated @RequestBody UpdateStatusRequest request) {
|
||||
deptService.updateStatus(ids, request.getStatus());
|
||||
baseService.updateStatus(ids, request.getStatus());
|
||||
return R.ok(String.format("%s成功", request.getStatus().getDescription()));
|
||||
}
|
||||
|
||||
@Operation(summary = "删除部门")
|
||||
@Parameter(name = "ids", description = "ID 列表", in = ParameterIn.PATH)
|
||||
@DeleteMapping("/{ids}")
|
||||
public R delete(@PathVariable List<Long> ids) {
|
||||
deptService.delete(ids);
|
||||
return R.ok("删除成功");
|
||||
}
|
||||
}
|
||||
|
@ -160,6 +160,8 @@ spring:
|
||||
# 启用的环境
|
||||
# 配合 Maven Profile 选择不同配置文件进行启动,在 IntelliJ IDEA 右侧 Maven 工具窗口可以快速切换环境
|
||||
active: @profiles.active@
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
## JSON 配置
|
||||
jackson:
|
||||
# 时区配置
|
||||
|
Loading…
Reference in New Issue
Block a user