codegen:1)增加 vue3 + crud 模式下的单测;2)增加主子表的 db 字段

This commit is contained in:
zhijiantianya@gmail.com 2023-11-07 20:43:53 +08:00
parent 0af205ede1
commit 9705ae061c
51 changed files with 1643 additions and 24 deletions

View File

@ -4,6 +4,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.column.CodegenColumnBaseVO;
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table.CodegenTableBaseVO;
import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -43,6 +44,12 @@ public class CodegenUpdateReqVO {
|| getParentMenuId() != null;
}
@AssertTrue(message = "关联的子表与字段不能为空")
public boolean isSubValid() {
return ObjectUtil.notEqual(getTemplateType(), CodegenTemplateTypeEnum.MASTER_SUB)
|| (getSubTableId() != null && getSubColumnId() != null);
}
}
@Schema(description = "更新表定义")

View File

@ -58,4 +58,9 @@ public class CodegenTableBaseVO {
@Schema(description = "父菜单编号", example = "1024")
private Long parentMenuId;
@Schema(description = "子表的表编号", example = "2048")
private Long subTableId;
@Schema(description = "子表的关联字段编号", example = "4096")
private Long subColumnId;
}

View File

@ -116,4 +116,19 @@ public class CodegenTableDO extends BaseDO {
*/
private Long parentMenuId;
// ========== 主子表相关字段 ==========
/**
* 子表的表编号
*
* 关联 {@link CodegenTableDO#getId()}
*/
private Long subTableId;
/**
* 子表的关联字段编号
*
* 关联 {@link CodegenColumnDO#getId()}
*/
private Long subColumnId;
}

View File

@ -14,6 +14,7 @@ public enum CodegenTemplateTypeEnum {
CRUD(1), // 单表增删改查
TREE(2), // 树表增删改查
MASTER_SUB(3), // 主子表
;
/**

View File

@ -235,8 +235,16 @@ public class CodegenServiceImpl implements CodegenService {
throw exception(CODEGEN_COLUMN_NOT_EXISTS);
}
// 校验子表是否已经存在
CodegenTableDO subTable = null;
List<CodegenColumnDO> subColumns = null;
if (table.getSubTableId() != null) {
subTable = codegenTableMapper.selectById(table.getSubTableId());
subColumns = codegenColumnMapper.selectListByTableId(table.getSubTableId());
}
// 执行生成
return codegenEngine.execute(table, columns);
return codegenEngine.execute(table, columns, subTable, subColumns);
}
@Override

View File

@ -26,6 +26,7 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenTableDO;
import cn.iocoder.yudao.module.infra.enums.codegen.CodegenFrontTypeEnum;
import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
import cn.iocoder.yudao.module.infra.framework.codegen.config.CodegenProperties;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;
@ -37,6 +38,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import static cn.hutool.core.map.MapUtil.getStr;
import static cn.hutool.core.text.CharSequenceUtil.*;
@ -149,7 +151,8 @@ public class CodegenEngine {
}
@PostConstruct
private void initGlobalBindingMap() {
@VisibleForTesting
void initGlobalBindingMap() {
// 全局配置
globalBindingMap.put("basePackage", codegenProperties.getBasePackage());
globalBindingMap.put("baseFrameworkPackage", codegenProperties.getBasePackage()
@ -176,13 +179,28 @@ public class CodegenEngine {
globalBindingMap.put("OperateTypeEnumClassName", OperateTypeEnum.class.getName());
}
public Map<String, String> execute(CodegenTableDO table, List<CodegenColumnDO> columns) {
/**
* 生成代码
*
* @param table 表定义
* @param columns table 的字段定义数组
* @param subTable 子表定义当且仅当主子表时使用
* @param subColumns subTable 的字段定义数组
* @return 生成的代码key 是路径value 是对应代码
*/
public Map<String, String> execute(CodegenTableDO table, List<CodegenColumnDO> columns,
CodegenTableDO subTable, List<CodegenColumnDO> subColumns) {
// 创建 bindingMap
Map<String, Object> bindingMap = new HashMap<>(globalBindingMap);
bindingMap.put("table", table);
bindingMap.put("columns", columns);
bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, CodegenColumnDO::getPrimaryKey)); // 主键字段
bindingMap.put("sceneEnum", CodegenSceneEnum.valueOf(table.getScene()));
if (subTable != null) {
bindingMap.put("subTable", subTable);
bindingMap.put("subColumns", subColumns);
bindingMap.put("subColumn", CollectionUtils.findFirst(subColumns, column -> column.getId().equals(table.getSubColumnId())));
}
// className 相关
// 去掉指定前缀 TestDictType 转换成 DictType. 因为在 create 等方法后不需要带上 Test 前缀

View File

@ -108,4 +108,4 @@ public class ${sceneEnum.prefixClass}${table.className}Controller {
ExcelUtils.write(response, "${table.classComment}.xls", "数据", ${sceneEnum.prefixClass}${table.className}ExcelVO.class, datas);
}
}
}

View File

@ -1,7 +1,7 @@
## 提供给 baseVO、createVO、updateVO 生成字段
@Schema(description = "${column.columnComment}"#if (!${column.nullable}), requiredMode = Schema.RequiredMode.REQUIRED#end#if ("$!column.example" != ""), example = "${column.example}"#end)
#if (!${column.nullable})## 判断 @NotEmpty 和 @NotNull 注解
#if (${field.fieldType} == 'String')
#if (${column.javaType} == 'String')
@NotEmpty(message = "${column.columnComment}不能为空")
#else
@NotNull(message = "${column.columnComment}不能为空")

View File

@ -27,4 +27,4 @@ public class ${sceneEnum.prefixClass}${table.className}CreateReqVO extends ${sce
#end
#end
}
}

View File

@ -42,4 +42,4 @@ public class ${sceneEnum.prefixClass}${table.className}ExcelVO {
#end
#end
}
}

View File

@ -36,4 +36,4 @@ public class ${sceneEnum.prefixClass}${table.className}ExportReqVO {
#end
#end
}
}

View File

@ -38,4 +38,4 @@ public class ${sceneEnum.prefixClass}${table.className}PageReqVO extends PagePar
#end
#end
}
}

View File

@ -22,4 +22,4 @@ public class ${sceneEnum.prefixClass}${table.className}RespVO extends ${sceneEnu
#end
#end
}
}

View File

@ -27,4 +27,4 @@ public class ${sceneEnum.prefixClass}${table.className}UpdateReqVO extends ${sce
#end
#end
}
}

View File

@ -31,4 +31,4 @@ public interface ${table.className}Convert {
List<${sceneEnum.prefixClass}${table.className}ExcelVO> convertList02(List<${table.className}DO> list);
}
}

View File

@ -44,4 +44,4 @@ public class ${table.className}DO extends BaseDO {
#end
#end
}
}

View File

@ -63,4 +63,4 @@ public interface ${table.className}Mapper extends BaseMapperX<${table.className}
}
}
}

View File

@ -9,4 +9,4 @@
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
</mapper>
</mapper>

View File

@ -1,3 +1,3 @@
// TODO 待办:请将下面的错误码复制到 yudao-module-${table.moduleName}-api 模块的 ErrorCodeConstants 类中。注意请给“TODO 补充编号”设置一个错误码编号!!!
// ========== ${table.classComment} TODO 补充编号 ==========
ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS = new ErrorCode(TODO 补充编号, "${table.classComment}不存在");
ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS = new ErrorCode(TODO 补充编号, "${table.classComment}不存在");

View File

@ -67,4 +67,4 @@ public interface ${table.className}Service {
*/
List<${table.className}DO> get${simpleClassName}List(${sceneEnum.prefixClass}${table.className}ExportReqVO exportReqVO);
}
}

View File

@ -85,4 +85,4 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
return ${classNameVar}Mapper.selectList(exportReqVO);
}
}
}

View File

@ -162,4 +162,4 @@ public class ${table.className}ServiceImplTest extends BaseDbUnitTest {
assertPojoEquals(db${simpleClassName}, list.get(0));
}
}
}

View File

@ -32,4 +32,4 @@ CREATE TABLE IF NOT EXISTS "${table.tableName.toLowerCase()}" (
) COMMENT '${table.tableComment}';
-- 将该删表 SQL 语句,添加到 yudao-module-${table.moduleName}-biz 模块的 test/resources/sql/clean.sql 文件里
DELETE FROM "${table.tableName}";
DELETE FROM "${table.tableName}";

View File

@ -25,4 +25,4 @@ VALUES (
'${table.classComment}${functionName}', '${permissionPrefix}:${functionOps.get($index)}', 3, $foreach.count, @parentId,
'', '', '', 0
);
#end
#end

View File

@ -43,4 +43,4 @@ export const delete${simpleClassName} = async (id: number) => {
// 导出${table.classComment} Excel
export const export${simpleClassName} = async (params) => {
return await request.download({ url: `${baseURL}/export-excel`, params })
}
}

View File

@ -231,4 +231,4 @@ const resetForm = () => {
}
formRef.value?.resetFields()
}
</script>
</script>

View File

@ -286,4 +286,4 @@ const handleExport = async () => {
onMounted(() => {
getList()
})
</script>
</script>

View File

@ -0,0 +1,187 @@
package cn.iocoder.yudao.module.infra.service.codegen.inner;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.ClassUtil;
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenColumnDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenTableDO;
import cn.iocoder.yudao.module.infra.enums.codegen.*;
import cn.iocoder.yudao.module.infra.framework.codegen.config.CodegenProperties;
import org.apache.ibatis.type.JdbcType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* {@link CodegenEngine} 的单元测试
*
* @author 芋道源码
*/
public class CodegenEngineTest extends BaseMockitoUnitTest {
@InjectMocks
private CodegenEngine codegenEngine;
@Spy
private CodegenProperties codegenProperties = new CodegenProperties()
.setBasePackage("cn.iocoder.yudao.module");
@BeforeEach
public void setUp() {
codegenEngine.initGlobalBindingMap();
}
@Test
public void testExecute_vue3_crud() {
// 准备请求参数
CodegenTableDO table = new CodegenTableDO().setScene(CodegenSceneEnum.ADMIN.getScene())
.setTableName("system_user").setTableComment("用户表")
.setModuleName("system").setBusinessName("user").setClassName("SystemUser")
.setClassComment("用户").setAuthor("芋道源码")
.setTemplateType(CodegenTemplateTypeEnum.CRUD.getType())
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
.setParentMenuId(10L);
CodegenColumnDO idColumn = new CodegenColumnDO().setColumnName("id").setDataType(JdbcType.BIGINT.name())
.setColumnComment("编号").setNullable(false).setPrimaryKey(true).setAutoIncrement(true)
.setOrdinalPosition(1).setJavaType("Long").setJavaField("id").setExample("1024")
.setCreateOperation(false).setUpdateOperation(true).setListOperation(false)
.setListOperationResult(true);
CodegenColumnDO nameColumn = new CodegenColumnDO().setColumnName("name").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("名字").setNullable(false).setPrimaryKey(false)
.setOrdinalPosition(2).setJavaType("String").setJavaField("name").setExample("芋头")
.setCreateOperation(true).setUpdateOperation(true).setListOperation(true)
.setListOperationCondition(CodegenColumnListConditionEnum.LIKE.getCondition()).setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.INPUT.getType());
CodegenColumnDO avatarColumn = new CodegenColumnDO().setColumnName("avatar").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("头像").setNullable(true).setPrimaryKey(false)
.setOrdinalPosition(3).setJavaType("String").setJavaField("avatar").setExample("https://www.iocoder.cn/1.png")
.setCreateOperation(true).setUpdateOperation(true).setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.UPLOAD_IMAGE.getType());
CodegenColumnDO videoColumn = new CodegenColumnDO().setColumnName("video").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("视频").setNullable(true).setPrimaryKey(false)
.setOrdinalPosition(4).setJavaType("String").setJavaField("video").setExample("https://www.iocoder.cn/1.mp4")
.setCreateOperation(true).setUpdateOperation(true).setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.UPLOAD_FILE.getType());
CodegenColumnDO descriptionColumn = new CodegenColumnDO().setColumnName("description").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("个人简介").setNullable(true).setPrimaryKey(false)
.setOrdinalPosition(5).setJavaType("String").setJavaField("description").setExample("我是介绍")
.setCreateOperation(true).setUpdateOperation(true).setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.EDITOR.getType());
CodegenColumnDO sex1Column = new CodegenColumnDO().setColumnName("sex1").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("性别 1").setNullable(true).setPrimaryKey(false)
.setOrdinalPosition(6).setJavaType("String").setJavaField("sex1").setExample("")
.setCreateOperation(true).setUpdateOperation(true).setListOperation(true)
.setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition()).setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.SELECT.getType()).setDictType("system_sex1");
CodegenColumnDO sex2Column = new CodegenColumnDO().setColumnName("sex2").setDataType(JdbcType.INTEGER.name())
.setColumnComment("性别 2").setNullable(true).setPrimaryKey(false)
.setOrdinalPosition(7).setJavaType("Integer").setJavaField("sex2").setExample("1")
.setCreateOperation(true).setUpdateOperation(true).setListOperation(true)
.setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition()).setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.CHECKBOX.getType()).setDictType("system_sex2");
CodegenColumnDO sex3Column = new CodegenColumnDO().setColumnName("sex3").setDataType(JdbcType.BOOLEAN.name())
.setColumnComment("性别 3").setNullable(true).setPrimaryKey(false)
.setOrdinalPosition(8).setJavaType("Boolean").setJavaField("sex3").setExample("true")
.setCreateOperation(true).setUpdateOperation(true).setListOperation(true)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.RADIO.getType()).setDictType("system_sex3");
CodegenColumnDO birthdayColumn = new CodegenColumnDO().setColumnName("birthday").setDataType(JdbcType.DATE.name())
.setColumnComment("出生日期").setNullable(true).setPrimaryKey(false)
.setOrdinalPosition(9).setJavaType("LocalDateTime").setJavaField("birthday")
.setCreateOperation(true).setUpdateOperation(true).setListOperation(true)
.setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition()).setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.DATETIME.getType());
CodegenColumnDO memoColumn = new CodegenColumnDO().setColumnName("memo").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("备注").setNullable(true).setPrimaryKey(false)
.setOrdinalPosition(10).setJavaType("String").setJavaField("memo").setExample("我是备注")
.setCreateOperation(true).setUpdateOperation(true).setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.TEXTAREA.getType());
CodegenColumnDO createTimeColumn = new CodegenColumnDO().setColumnName("create_time").setDataType(JdbcType.DATE.name())
.setColumnComment("创建时间").setNullable(true).setPrimaryKey(false)
.setOrdinalPosition(11).setJavaType("LocalDateTime").setJavaField("createTime")
.setCreateOperation(true).setUpdateOperation(true).setListOperation(true)
.setListOperationCondition(CodegenColumnListConditionEnum.BETWEEN.getCondition()).setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.DATETIME.getType());
List<CodegenColumnDO> columns = Arrays.asList(idColumn, nameColumn, avatarColumn, descriptionColumn,
sex1Column, sex2Column, sex3Column, birthdayColumn, memoColumn, createTimeColumn);
// 调用
Map<String, String> result = codegenEngine.execute(table, columns, null, null);
// 断言
assertEquals(21, result.size());
// 断言 vo
for (String vo : new String[]{"SystemUserBaseVO", "SystemUserCreateReqVO", "SystemUserUpdateReqVO", "SystemUserRespVO",
"SystemUserPageReqVO", "SystemUserExportReqVO", "SystemUserExcelVO"}) {
assertPathContentEquals("vue3_crud/java/" + vo,
result, "yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/module/system/controller/admin/user/vo/" + vo + ".java");
}
// 断言 controller
assertPathContentEquals("vue3_crud/java/SystemUserController",
result, "yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/module/system/controller/admin/user/SystemUserController.java");
// 断言 service
assertPathContentEquals("vue3_crud/java/SystemUserService",
result, "yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/module/system/service/user/SystemUserService.java");
assertPathContentEquals("vue3_crud/java/SystemUserServiceImpl",
result, "yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/module/system/service/user/SystemUserServiceImpl.java");
// 断言 convert
assertPathContentEquals("vue3_crud/java/SystemUserConvert",
result, "yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/module/system/convert/user/SystemUserConvert.java");
// 断言 enums
assertPathContentEquals("vue3_crud/java/ErrorCodeConstants",
result, "yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/module/system/enums/ErrorCodeConstants_手动操作.java");
// 断言 dal
assertPathContentEquals("vue3_crud/java/SystemUserDO",
result, "yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/module/system/dal/dataobject/user/SystemUserDO.java");
assertPathContentEquals("vue3_crud/java/SystemUserMapper",
result, "yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/module/system/dal/mysql/user/SystemUserMapper.java");
assertPathContentEquals("vue3_crud/java/SystemUserMapper_xml",
result, "yudao-module-system/yudao-module-system-biz/src/main/resources/mapper/user/SystemUserMapper.xml");
// 断言 test
assertPathContentEquals("vue3_crud/java/SystemUserServiceImplTest",
result, "yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/module/system/service/user/SystemUserServiceImplTest.java");
// 断言 sql 语句
assertPathContentEquals("vue3_crud/sql/h2",
result, "sql/h2.sql");
assertPathContentEquals("vue3_crud/sql/sql",
result, "sql/sql.sql");
// 断言 vue 语句
assertPathContentEquals("vue3_crud/vue/index",
result, "yudao-ui-admin-vue3/src/views/system/user/index.vue");
assertPathContentEquals("vue3_crud/vue/form",
result, "yudao-ui-admin-vue3/src/views/system/user/UserForm.vue");
assertPathContentEquals("vue3_crud/vue/api",
result, "yudao-ui-admin-vue3/src/api/system/user/index.ts");
// result.forEach(new BiConsumer<String, String>() {
// @Override
// public void accept(String s, String s2) {
// System.out.println(s);
// System.out.println(s2);
// System.out.println("=================");
// }
// });
}
private void assertPathContentEquals(String path, Map<String, String> result, String key) {
String pathContent = ResourceUtil.readUtf8Str("codegen/" + path);
String valueContent = result.get(key);
assertEquals(pathContent, valueContent);
}
}

View File

@ -0,0 +1,4 @@
/**
* 占位无其它作用
*/
package cn.iocoder.yudao.module.infra.service.codegen;

View File

@ -0,0 +1,3 @@
// TODO 待办:请将下面的错误码复制到 yudao-module-system-api 模块的 ErrorCodeConstants 类中。注意请给“TODO 补充编号”设置一个错误码编号!!!
// ========== 用户 TODO 补充编号 ==========
ErrorCode USER_NOT_EXISTS = new ErrorCode(TODO 补充编号, "用户不存在");

View File

@ -0,0 +1,50 @@
package cn.iocoder.yudao.module.module.system.controller.admin.user.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import java.time.LocalDateTime;
import java.time.LocalDateTime;
import javax.validation.constraints.*;
import org.springframework.format.annotation.DateTimeFormat;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
/**
* 用户 Base VO提供给添加、修改、详细的子 VO 使用
* 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成
*/
@Data
public class SystemUserBaseVO {
@Schema(description = "名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋头")
@NotEmpty(message = "名字不能为空")
private String name;
@Schema(description = "头像", example = "https://www.iocoder.cn/1.png")
private String avatar;
@Schema(description = "个人简介", example = "我是介绍")
private String description;
@Schema(description = "性别 1", example = "男")
private String sex1;
@Schema(description = "性别 2", example = "1")
private Integer sex2;
@Schema(description = "性别 3", example = "true")
private Boolean sex3;
@Schema(description = "出生日期")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime birthday;
@Schema(description = "备注", example = "我是备注")
private String memo;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime createTime;
}

View File

@ -0,0 +1,102 @@
package cn.iocoder.yudao.module.module.system.controller.admin.user;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.module.system.controller.admin.user.vo.*;
import cn.iocoder.yudao.module.module.system.dal.dataobject.user.SystemUserDO;
import cn.iocoder.yudao.module.module.system.convert.user.SystemUserConvert;
import cn.iocoder.yudao.module.module.system.service.user.SystemUserService;
@Tag(name = "管理后台 - 用户")
@RestController
@RequestMapping("/system/user")
@Validated
public class SystemUserController {
@Resource
private SystemUserService userService;
@PostMapping("/create")
@Operation(summary = "创建用户")
@PreAuthorize("@ss.hasPermission('system:user:create')")
public CommonResult<Long> createUser(@Valid @RequestBody SystemUserCreateReqVO createReqVO) {
return success(userService.createUser(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新用户")
@PreAuthorize("@ss.hasPermission('system:user:update')")
public CommonResult<Boolean> updateUser(@Valid @RequestBody SystemUserUpdateReqVO updateReqVO) {
userService.updateUser(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除用户")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('system:user:delete')")
public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
userService.deleteUser(id);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得用户")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('system:user:query')")
public CommonResult<SystemUserRespVO> getUser(@RequestParam("id") Long id) {
SystemUserDO user = userService.getUser(id);
return success(SystemUserConvert.INSTANCE.convert(user));
}
@GetMapping("/list")
@Operation(summary = "获得用户列表")
@Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
@PreAuthorize("@ss.hasPermission('system:user:query')")
public CommonResult<List<SystemUserRespVO>> getUserList(@RequestParam("ids") Collection<Long> ids) {
List<SystemUserDO> list = userService.getUserList(ids);
return success(SystemUserConvert.INSTANCE.convertList(list));
}
@GetMapping("/page")
@Operation(summary = "获得用户分页")
@PreAuthorize("@ss.hasPermission('system:user:query')")
public CommonResult<PageResult<SystemUserRespVO>> getUserPage(@Valid SystemUserPageReqVO pageVO) {
PageResult<SystemUserDO> pageResult = userService.getUserPage(pageVO);
return success(SystemUserConvert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/export-excel")
@Operation(summary = "导出用户 Excel")
@PreAuthorize("@ss.hasPermission('system:user:export')")
@OperateLog(type = EXPORT)
public void exportUserExcel(@Valid SystemUserExportReqVO exportReqVO,
HttpServletResponse response) throws IOException {
List<SystemUserDO> list = userService.getUserList(exportReqVO);
// 导出 Excel
List<SystemUserExcelVO> datas = SystemUserConvert.INSTANCE.convertList02(list);
ExcelUtils.write(response, "用户.xls", "数据", SystemUserExcelVO.class, datas);
}
}

View File

@ -0,0 +1,34 @@
package cn.iocoder.yudao.module.module.system.convert.user;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.module.system.controller.admin.user.vo.*;
import cn.iocoder.yudao.module.module.system.dal.dataobject.user.SystemUserDO;
/**
* 用户 Convert
*
* @author 芋道源码
*/
@Mapper
public interface SystemUserConvert {
SystemUserConvert INSTANCE = Mappers.getMapper(SystemUserConvert.class);
SystemUserDO convert(SystemUserCreateReqVO bean);
SystemUserDO convert(SystemUserUpdateReqVO bean);
SystemUserRespVO convert(SystemUserDO bean);
List<SystemUserRespVO> convertList(List<SystemUserDO> list);
PageResult<SystemUserRespVO> convertPage(PageResult<SystemUserDO> page);
List<SystemUserExcelVO> convertList02(List<SystemUserDO> list);
}

View File

@ -0,0 +1,14 @@
package cn.iocoder.yudao.module.module.system.controller.admin.user.vo;
import lombok.*;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.*;
@Schema(description = "管理后台 - 用户创建 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SystemUserCreateReqVO extends SystemUserBaseVO {
}

View File

@ -0,0 +1,69 @@
package cn.iocoder.yudao.module.module.system.dal.dataobject.user;
import lombok.*;
import java.util.*;
import java.time.LocalDateTime;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.*;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
/**
* 用户 DO
*
* @author 芋道源码
*/
@TableName("system_user")
@KeySequence("system_user_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SystemUserDO extends BaseDO {
/**
* 编号
*/
@TableId
private Long id;
/**
* 名字
*/
private String name;
/**
* 头像
*/
private String avatar;
/**
* 个人简介
*/
private String description;
/**
* 性别 1
*
* 枚举 {@link TODO system_sex1 对应的类}
*/
private String sex1;
/**
* 性别 2
*
* 枚举 {@link TODO system_sex2 对应的类}
*/
private Integer sex2;
/**
* 性别 3
*
* 枚举 {@link TODO system_sex3 对应的类}
*/
private Boolean sex3;
/**
* 出生日期
*/
private LocalDateTime birthday;
/**
* 备注
*/
private String memo;
}

View File

@ -0,0 +1,55 @@
package cn.iocoder.yudao.module.module.system.controller.admin.user.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import java.time.LocalDateTime;
import java.time.LocalDateTime;
import com.alibaba.excel.annotation.ExcelProperty;
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
/**
* 用户 Excel VO
*
* @author 芋道源码
*/
@Data
public class SystemUserExcelVO {
@ExcelProperty("编号")
private Long id;
@ExcelProperty("名字")
private String name;
@ExcelProperty("头像")
private String avatar;
@ExcelProperty("个人简介")
private String description;
@ExcelProperty(value = "性别 1", converter = DictConvert.class)
@DictFormat("system_sex1") // TODO 代码优化:建议设置到对应的 XXXDictTypeConstants 枚举类中
private String sex1;
@ExcelProperty(value = "性别 2", converter = DictConvert.class)
@DictFormat("system_sex2") // TODO 代码优化:建议设置到对应的 XXXDictTypeConstants 枚举类中
private Integer sex2;
@ExcelProperty(value = "性别 3", converter = DictConvert.class)
@DictFormat("system_sex3") // TODO 代码优化:建议设置到对应的 XXXDictTypeConstants 枚举类中
private Boolean sex3;
@ExcelProperty("出生日期")
private LocalDateTime birthday;
@ExcelProperty("备注")
private String memo;
@ExcelProperty("创建时间")
private LocalDateTime createTime;
}

View File

@ -0,0 +1,35 @@
package cn.iocoder.yudao.module.module.system.controller.admin.user.vo;
import lombok.*;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - 用户 Excel 导出 Request VO参数和 SystemUserPageReqVO 是一致的")
@Data
public class SystemUserExportReqVO {
@Schema(description = "名字", example = "芋头")
private String name;
@Schema(description = "性别 1", example = "男")
private String sex1;
@Schema(description = "性别 2", example = "1")
private Integer sex2;
@Schema(description = "性别 3", example = "true")
private Boolean sex3;
@Schema(description = "出生日期")
private LocalDateTime birthday;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
}

View File

@ -0,0 +1,40 @@
package cn.iocoder.yudao.module.module.system.dal.mysql.user;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.module.system.dal.dataobject.user.SystemUserDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.module.system.controller.admin.user.vo.*;
/**
* 用户 Mapper
*
* @author 芋道源码
*/
@Mapper
public interface SystemUserMapper extends BaseMapperX<SystemUserDO> {
default PageResult<SystemUserDO> selectPage(SystemUserPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<SystemUserDO>()
.likeIfPresent(SystemUserDO::getName, reqVO.getName())
.eqIfPresent(SystemUserDO::getSex1, reqVO.getSex1())
.eqIfPresent(SystemUserDO::getSex2, reqVO.getSex2())
.eqIfPresent(SystemUserDO::getBirthday, reqVO.getBirthday())
.betweenIfPresent(SystemUserDO::getCreateTime, reqVO.getCreateTime())
.orderByDesc(SystemUserDO::getId));
}
default List<SystemUserDO> selectList(SystemUserExportReqVO reqVO) {
return selectList(new LambdaQueryWrapperX<SystemUserDO>()
.likeIfPresent(SystemUserDO::getName, reqVO.getName())
.eqIfPresent(SystemUserDO::getSex1, reqVO.getSex1())
.eqIfPresent(SystemUserDO::getSex2, reqVO.getSex2())
.eqIfPresent(SystemUserDO::getBirthday, reqVO.getBirthday())
.betweenIfPresent(SystemUserDO::getCreateTime, reqVO.getCreateTime())
.orderByDesc(SystemUserDO::getId));
}
}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.module.system.dal.mysql.user.SystemUserMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
</mapper>

View File

@ -0,0 +1,37 @@
package cn.iocoder.yudao.module.module.system.controller.admin.user.vo;
import lombok.*;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - 用户分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SystemUserPageReqVO extends PageParam {
@Schema(description = "名字", example = "芋头")
private String name;
@Schema(description = "性别 1", example = "男")
private String sex1;
@Schema(description = "性别 2", example = "1")
private Integer sex2;
@Schema(description = "性别 3", example = "true")
private Boolean sex3;
@Schema(description = "出生日期")
private LocalDateTime birthday;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
}

View File

@ -0,0 +1,16 @@
package cn.iocoder.yudao.module.module.system.controller.admin.user.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.time.LocalDateTime;
@Schema(description = "管理后台 - 用户 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SystemUserRespVO extends SystemUserBaseVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
private Long id;
}

View File

@ -0,0 +1,70 @@
package cn.iocoder.yudao.module.module.system.service.user;
import java.util.*;
import javax.validation.*;
import cn.iocoder.yudao.module.module.system.controller.admin.user.vo.*;
import cn.iocoder.yudao.module.module.system.dal.dataobject.user.SystemUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
/**
* 用户 Service 接口
*
* @author 芋道源码
*/
public interface SystemUserService {
/**
* 创建用户
*
* @param createReqVO 创建信息
* @return 编号
*/
Long createUser(@Valid SystemUserCreateReqVO createReqVO);
/**
* 更新用户
*
* @param updateReqVO 更新信息
*/
void updateUser(@Valid SystemUserUpdateReqVO updateReqVO);
/**
* 删除用户
*
* @param id 编号
*/
void deleteUser(Long id);
/**
* 获得用户
*
* @param id 编号
* @return 用户
*/
SystemUserDO getUser(Long id);
/**
* 获得用户列表
*
* @param ids 编号
* @return 用户列表
*/
List<SystemUserDO> getUserList(Collection<Long> ids);
/**
* 获得用户分页
*
* @param pageReqVO 分页查询
* @return 用户分页
*/
PageResult<SystemUserDO> getUserPage(SystemUserPageReqVO pageReqVO);
/**
* 获得用户列表, 用于 Excel 导出
*
* @param exportReqVO 查询条件
* @return 用户列表
*/
List<SystemUserDO> getUserList(SystemUserExportReqVO exportReqVO);
}

View File

@ -0,0 +1,88 @@
package cn.iocoder.yudao.module.module.system.service.user;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.module.module.system.controller.admin.user.vo.*;
import cn.iocoder.yudao.module.module.system.dal.dataobject.user.SystemUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.module.system.convert.user.SystemUserConvert;
import cn.iocoder.yudao.module.module.system.dal.mysql.user.SystemUserMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.module.system.enums.ErrorCodeConstants.*;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
/**
* 用户 Service 实现类
*
* @author 芋道源码
*/
@Service
@Validated
public class SystemUserServiceImpl implements SystemUserService {
@Resource
private SystemUserMapper userMapper;
@Override
public Long createUser(SystemUserCreateReqVO createReqVO) {
// 插入
SystemUserDO user = SystemUserConvert.INSTANCE.convert(createReqVO);
userMapper.insert(user);
// 返回
return user.getId();
}
@Override
public void updateUser(SystemUserUpdateReqVO updateReqVO) {
// 校验存在
validateUserExists(updateReqVO.getId());
// 更新
SystemUserDO updateObj = SystemUserConvert.INSTANCE.convert(updateReqVO);
userMapper.updateById(updateObj);
}
@Override
public void deleteUser(Long id) {
// 校验存在
validateUserExists(id);
// 删除
userMapper.deleteById(id);
}
private void validateUserExists(Long id) {
if (userMapper.selectById(id) == null) {
throw exception(USER_NOT_EXISTS);
}
}
@Override
public SystemUserDO getUser(Long id) {
return userMapper.selectById(id);
}
@Override
public List<SystemUserDO> getUserList(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return ListUtil.empty();
}
return userMapper.selectBatchIds(ids);
}
@Override
public PageResult<SystemUserDO> getUserPage(SystemUserPageReqVO pageReqVO) {
return userMapper.selectPage(pageReqVO);
}
@Override
public List<SystemUserDO> getUserList(SystemUserExportReqVO exportReqVO) {
return userMapper.selectList(exportReqVO);
}
}

View File

@ -0,0 +1,191 @@
package cn.iocoder.yudao.module.module.system.service.user;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import javax.annotation.Resource;
import cn.iocoder.yudao.module.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.module.system.controller.admin.user.vo.*;
import cn.iocoder.yudao.module.module.system.dal.dataobject.user.SystemUserDO;
import cn.iocoder.yudao.module.module.system.dal.mysql.user.SystemUserMapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import javax.annotation.Resource;
import org.springframework.context.annotation.Import;
import java.util.*;
import java.time.LocalDateTime;
import static cn.hutool.core.util.RandomUtil.*;
import static cn.iocoder.yudao.module.module.system.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.framework.test.core.util.AssertUtils.*;
import static cn.iocoder.yudao.module.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.*;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
/**
* {@link SystemUserServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(SystemUserServiceImpl.class)
public class SystemUserServiceImplTest extends BaseDbUnitTest {
@Resource
private SystemUserServiceImpl userService;
@Resource
private SystemUserMapper userMapper;
@Test
public void testCreateUser_success() {
// 准备参数
SystemUserCreateReqVO reqVO = randomPojo(SystemUserCreateReqVO.class);
// 调用
Long userId = userService.createUser(reqVO);
// 断言
assertNotNull(userId);
// 校验记录的属性是否正确
SystemUserDO user = userMapper.selectById(userId);
assertPojoEquals(reqVO, user);
}
@Test
public void testUpdateUser_success() {
// mock 数据
SystemUserDO dbUser = randomPojo(SystemUserDO.class);
userMapper.insert(dbUser);// @Sql: 先插入出一条存在的数据
// 准备参数
SystemUserUpdateReqVO reqVO = randomPojo(SystemUserUpdateReqVO.class, o -> {
o.setId(dbUser.getId()); // 设置更新的 ID
});
// 调用
userService.updateUser(reqVO);
// 校验是否更新正确
SystemUserDO user = userMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, user);
}
@Test
public void testUpdateUser_notExists() {
// 准备参数
SystemUserUpdateReqVO reqVO = randomPojo(SystemUserUpdateReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> userService.updateUser(reqVO), USER_NOT_EXISTS);
}
@Test
public void testDeleteUser_success() {
// mock 数据
SystemUserDO dbUser = randomPojo(SystemUserDO.class);
userMapper.insert(dbUser);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbUser.getId();
// 调用
userService.deleteUser(id);
// 校验数据不存在了
assertNull(userMapper.selectById(id));
}
@Test
public void testDeleteUser_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> userService.deleteUser(id), USER_NOT_EXISTS);
}
@Test
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
public void testGetUserPage() {
// mock 数据
SystemUserDO dbUser = randomPojo(SystemUserDO.class, o -> { // 等会查询到
o.setName(null);
o.setSex1(null);
o.setSex2(null);
o.setSex3(null);
o.setBirthday(null);
o.setCreateTime(null);
});
userMapper.insert(dbUser);
// 测试 name 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setName(null)));
// 测试 sex1 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSex1(null)));
// 测试 sex2 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSex2(null)));
// 测试 sex3 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSex3(null)));
// 测试 birthday 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setBirthday(null)));
// 测试 createTime 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setCreateTime(null)));
// 准备参数
SystemUserPageReqVO reqVO = new SystemUserPageReqVO();
reqVO.setName(null);
reqVO.setSex1(null);
reqVO.setSex2(null);
reqVO.setSex3(null);
reqVO.setBirthday(null);
reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
// 调用
PageResult<SystemUserDO> pageResult = userService.getUserPage(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbUser, pageResult.getList().get(0));
}
@Test
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
public void testGetUserList() {
// mock 数据
SystemUserDO dbUser = randomPojo(SystemUserDO.class, o -> { // 等会查询到
o.setName(null);
o.setSex1(null);
o.setSex2(null);
o.setSex3(null);
o.setBirthday(null);
o.setCreateTime(null);
});
userMapper.insert(dbUser);
// 测试 name 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setName(null)));
// 测试 sex1 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSex1(null)));
// 测试 sex2 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSex2(null)));
// 测试 sex3 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setSex3(null)));
// 测试 birthday 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setBirthday(null)));
// 测试 createTime 不匹配
userMapper.insert(cloneIgnoreId(dbUser, o -> o.setCreateTime(null)));
// 准备参数
SystemUserExportReqVO reqVO = new SystemUserExportReqVO();
reqVO.setName(null);
reqVO.setSex1(null);
reqVO.setSex2(null);
reqVO.setSex3(null);
reqVO.setBirthday(null);
reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
// 调用
List<SystemUserDO> list = userService.getUserList(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(dbUser, list.get(0));
}
}

View File

@ -0,0 +1,18 @@
package cn.iocoder.yudao.module.module.system.controller.admin.user.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import javax.validation.constraints.*;
@Schema(description = "管理后台 - 用户更新 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SystemUserUpdateReqVO extends SystemUserBaseVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
@NotNull(message = "编号不能为空")
private Long id;
}

View File

@ -0,0 +1,17 @@
-- 将该建表 SQL 语句,添加到 yudao-module-system-biz 模块的 test/resources/sql/create_tables.sql 文件里
CREATE TABLE IF NOT EXISTS "system_user" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"name" varchar NOT NULL,
"avatar" varchar,
"description" varchar,
"sex1" varchar,
"sex2" int,
"sex3" bit,
"birthday" varchar,
"memo" varchar,
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ("id")
) COMMENT '用户表';
-- 将该删表 SQL 语句,添加到 yudao-module-system-biz 模块的 test/resources/sql/clean.sql 文件里
DELETE FROM "system_user";

View File

@ -0,0 +1,55 @@
-- 菜单 SQL
INSERT INTO system_menu(
name, permission, type, sort, parent_id,
path, icon, component, status, component_name
)
VALUES (
'用户管理', '', 2, 0, 10,
'user', '', 'system/user/index', 0, 'SystemUser'
);
-- 按钮父菜单ID
-- 暂时只支持 MySQL。如果你是 Oracle、PostgreSQL、SQLServer 的话,需要手动修改 @parentId 的部分的代码
SELECT @parentId := LAST_INSERT_ID();
-- 按钮 SQL
INSERT INTO system_menu(
name, permission, type, sort, parent_id,
path, icon, component, status
)
VALUES (
'用户查询', 'system:user:query', 3, 1, @parentId,
'', '', '', 0
);
INSERT INTO system_menu(
name, permission, type, sort, parent_id,
path, icon, component, status
)
VALUES (
'用户创建', 'system:user:create', 3, 2, @parentId,
'', '', '', 0
);
INSERT INTO system_menu(
name, permission, type, sort, parent_id,
path, icon, component, status
)
VALUES (
'用户更新', 'system:user:update', 3, 3, @parentId,
'', '', '', 0
);
INSERT INTO system_menu(
name, permission, type, sort, parent_id,
path, icon, component, status
)
VALUES (
'用户删除', 'system:user:delete', 3, 4, @parentId,
'', '', '', 0
);
INSERT INTO system_menu(
name, permission, type, sort, parent_id,
path, icon, component, status
)
VALUES (
'用户导出', 'system:user:export', 3, 5, @parentId,
'', '', '', 0
);

View File

@ -0,0 +1,44 @@
import request from '@/config/axios'
export interface UserVO {
id: number
name: string
avatar: string
description: string
sex1: string
sex2: number
sex3: boolean
birthday: Date
memo: string
createTime: Date
}
// 查询用户列表
export const getUserPage = async (params) => {
return await request.get({ url: `/system/user/page`, params })
}
// 查询用户详情
export const getUser = async (id: number) => {
return await request.get({ url: `/system/user/get?id=` + id })
}
// 新增用户
export const createUser = async (data: UserVO) => {
return await request.post({ url: `/system/user/create`, data })
}
// 修改用户
export const updateUser = async (data: UserVO) => {
return await request.put({ url: `/system/user/update`, data })
}
// 删除用户
export const deleteUser = async (id: number) => {
return await request.delete({ url: `/system/user/delete?id=` + id })
}
// 导出用户 Excel
export const exportUser = async (params) => {
return await request.download({ url: `/system/user/export-excel`, params })
}

View File

@ -0,0 +1,162 @@
<template>
<Dialog :title="dialogTitle" v-model="dialogVisible">
<el-form
ref="formRef"
:model="formData"
:rules="formRules"
label-width="100px"
v-loading="formLoading"
>
<el-form-item label="名字" prop="name">
<el-input v-model="formData.name" placeholder="请输入名字" />
</el-form-item>
<el-form-item label="个人简介">
<Editor v-model="formData.description" height="150px" />
</el-form-item>
<el-form-item label="性别 1" prop="sex1">
<el-select v-model="formData.sex1" placeholder="请选择性别 1">
<el-option
v-for="dict in getStrDictOptions(DICT_TYPE.SYSTEM_SEX1)"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item label="性别 2" prop="sex2">
<el-checkbox-group v-model="formData.sex2">
<el-checkbox
v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_SEX2)"
:key="dict.value"
:label="dict.value"
>
{{ dict.label }}
</el-checkbox>
</el-checkbox-group>
</el-form-item>
<el-form-item label="性别 3" prop="sex3">
<el-radio-group v-model="formData.sex3">
<el-radio
v-for="dict in getBoolDictOptions(DICT_TYPE.SYSTEM_SEX3)"
:key="dict.value"
:label="dict.value"
>
{{ dict.label }}
</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="出生日期" prop="birthday">
<el-date-picker
v-model="formData.birthday"
type="date"
value-format="x"
placeholder="选择出生日期"
/>
</el-form-item>
<el-form-item label="备注" prop="memo">
<el-input v-model="formData.memo" type="textarea" placeholder="请输入备注" />
</el-form-item>
<el-form-item label="创建时间" prop="createTime">
<el-date-picker
v-model="formData.createTime"
type="date"
value-format="x"
placeholder="选择创建时间"
/>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
<el-button @click="dialogVisible = false">取 消</el-button>
</template>
</Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE, getStrDictOptions, getIntDictOptions, getBoolDictOptions } from '@/utils/dict'
import * as UserApi from '@/api/system/user'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中1修改时的数据加载2提交的按钮禁用
const formType = ref('') // 表单的类型create - 新增update - 修改
const formData = ref({
id: undefined,
name: undefined,
avatar: undefined,
description: undefined,
sex1: undefined,
sex2: [],
sex3: undefined,
birthday: undefined,
memo: undefined,
createTime: undefined
})
const formRules = reactive({
name: [{ required: true, message: '名字不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
dialogVisible.value = true
dialogTitle.value = t('action.' + type)
formType.value = type
resetForm()
// 修改时,设置数据
if (id) {
formLoading.value = true
try {
formData.value = await UserApi.getUser(id)
} finally {
formLoading.value = false
}
}
}
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
const submitForm = async () => {
// 校验表单
if (!formRef) return
const valid = await formRef.value.validate()
if (!valid) return
// 提交请求
formLoading.value = true
try {
const data = formData.value as unknown as UserApi.UserVO
if (formType.value === 'create') {
await UserApi.createUser(data)
message.success(t('common.createSuccess'))
} else {
await UserApi.updateUser(data)
message.success(t('common.updateSuccess'))
}
dialogVisible.value = false
// 发送操作成功的事件
emit('success')
} finally {
formLoading.value = false
}
}
/** 重置表单 */
const resetForm = () => {
formData.value = {
id: undefined,
name: undefined,
avatar: undefined,
description: undefined,
sex1: undefined,
sex2: [],
sex3: undefined,
birthday: undefined,
memo: undefined,
createTime: undefined
}
formRef.value?.resetFields()
}
</script>

View File

@ -0,0 +1,262 @@
<template>
<ContentWrap>
<!-- 搜索工作栏 -->
<el-form
class="-mb-15px"
:model="queryParams"
ref="queryFormRef"
:inline="true"
label-width="68px"
>
<el-form-item label="名字" prop="name">
<el-input
v-model="queryParams.name"
placeholder="请输入名字"
clearable
@keyup.enter="handleQuery"
class="!w-240px"
/>
</el-form-item>
<el-form-item label="性别 1" prop="sex1">
<el-select v-model="queryParams.sex1" placeholder="请选择性别 1" clearable class="!w-240px">
<el-option
v-for="dict in getStrDictOptions(DICT_TYPE.SYSTEM_SEX1)"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item label="性别 3" prop="sex3">
<el-select v-model="queryParams.sex3" placeholder="请选择性别 3" clearable class="!w-240px">
<el-option
v-for="dict in getBoolDictOptions(DICT_TYPE.SYSTEM_SEX3)"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item label="出生日期" prop="birthday">
<el-date-picker
v-model="queryParams.birthday"
value-format="YYYY-MM-DD"
type="date"
placeholder="选择出生日期"
clearable
class="!w-240px"
/>
</el-form-item>
<el-form-item label="创建时间" prop="createTime">
<el-date-picker
v-model="queryParams.createTime"
value-format="YYYY-MM-DD HH:mm:ss"
type="daterange"
start-placeholder="开始日期"
end-placeholder="结束日期"
:default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
class="!w-240px"
/>
</el-form-item>
<el-form-item>
<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
<el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<el-button type="primary" @click="openForm('create')" v-hasPermi="['system:user:create']">
<Icon icon="ep:plus" class="mr-5px" /> 新增
</el-button>
<el-button
type="success"
plain
@click="handleExport"
:loading="exportLoading"
v-hasPermi="['system:user:export']"
>
<Icon icon="ep:download" class="mr-5px" /> 导出
</el-button>
</el-form-item>
</el-form>
</ContentWrap>
<!-- 列表 -->
<ContentWrap>
<el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
<el-table-column label="编号" align="center" prop="id">
<template #default="scope">
<dict-tag :type="DICT_TYPE.$dictType.toUpperCase()" :value="scope.row.id" />
</template>
</el-table-column>
<el-table-column label="名字" align="center" prop="name">
<template #default="scope">
<dict-tag :type="DICT_TYPE.$dictType.toUpperCase()" :value="scope.row.name" />
</template>
</el-table-column>
<el-table-column label="头像" align="center" prop="avatar">
<template #default="scope">
<dict-tag :type="DICT_TYPE.$dictType.toUpperCase()" :value="scope.row.avatar" />
</template>
</el-table-column>
<el-table-column label="个人简介" align="center" prop="description">
<template #default="scope">
<dict-tag :type="DICT_TYPE.$dictType.toUpperCase()" :value="scope.row.description" />
</template>
</el-table-column>
<el-table-column label="性别 1" align="center" prop="sex1">
<template #default="scope">
<dict-tag :type="DICT_TYPE.SYSTEM_SEX1" :value="scope.row.sex1" />
</template>
</el-table-column>
<el-table-column label="性别 2" align="center" prop="sex2">
<template #default="scope">
<dict-tag :type="DICT_TYPE.SYSTEM_SEX2" :value="scope.row.sex2" />
</template>
</el-table-column>
<el-table-column label="性别 3" align="center" prop="sex3">
<template #default="scope">
<dict-tag :type="DICT_TYPE.SYSTEM_SEX3" :value="scope.row.sex3" />
</template>
</el-table-column>
<el-table-column
label="出生日期"
align="center"
prop="birthday"
:formatter="dateFormatter"
width="180px"
/>
<el-table-column label="备注" align="center" prop="memo">
<template #default="scope">
<dict-tag :type="DICT_TYPE.$dictType.toUpperCase()" :value="scope.row.memo" />
</template>
</el-table-column>
<el-table-column
label="创建时间"
align="center"
prop="createTime"
:formatter="dateFormatter"
width="180px"
/>
<el-table-column label="操作" align="center">
<template #default="scope">
<el-button
link
type="primary"
@click="openForm('update', scope.row.id)"
v-hasPermi="['system:user:update']"
>
编辑
</el-button>
<el-button
link
type="danger"
@click="handleDelete(scope.row.id)"
v-hasPermi="['system:user:delete']"
>
删除
</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页 -->
<Pagination
:total="total"
v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize"
@pagination="getList"
/>
</ContentWrap>
<!-- 表单弹窗:添加/修改 -->
<UserForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { DICT_TYPE, getStrDictOptions, getBoolDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as UserApi from '@/api/system/user'
import UserForm from './UserForm.vue'
defineOptions({ name: 'SystemUser' })
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
pageNo: 1,
pageSize: 10,
name: null,
sex1: null,
sex2: null,
sex3: null,
birthday: null,
birthday: [],
createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
/** 查询列表 */
const getList = async () => {
loading.value = true
try {
const data = await UserApi.getUserPage(queryParams)
list.value = data.list
total.value = data.total
} finally {
loading.value = false
}
}
/** 搜索按钮操作 */
const handleQuery = () => {
queryParams.pageNo = 1
getList()
}
/** 重置按钮操作 */
const resetQuery = () => {
queryFormRef.value.resetFields()
handleQuery()
}
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
formRef.value.open(type, id)
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
try {
// 删除的二次确认
await message.delConfirm()
// 发起删除
await UserApi.deleteUser(id)
message.success(t('common.delSuccess'))
// 刷新列表
await getList()
} catch {}
}
/** 导出按钮操作 */
const handleExport = async () => {
try {
// 导出的二次确认
await message.exportConfirm()
// 发起导出
exportLoading.value = true
const data = await UserApi.exportUser(queryParams)
download.excel(data, '用户.xls')
} catch {
} finally {
exportLoading.value = false
}
}
/** 初始化 **/
onMounted(() => {
getList()
})
</script>