forked from sfms3.0/sfms3.0
14 changed files with 646 additions and 84 deletions
@ -1,79 +1,99 @@ |
|||||
package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; |
package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; |
||||
|
|
||||
|
import org.springframework.stereotype.Service; |
||||
|
import javax.annotation.Resource; |
||||
|
import org.springframework.validation.annotation.Validated; |
||||
|
|
||||
import java.util.*; |
import java.util.*; |
||||
import javax.validation.*; |
|
||||
import ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}.vo.*; |
import ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}.vo.*; |
||||
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; |
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; |
||||
import ${PageResultClassName}; |
import ${PageResultClassName}; |
||||
|
|
||||
|
import ${basePackage}.module.${table.moduleName}.convert.${table.businessName}.${table.className}Convert; |
||||
|
import ${basePackage}.module.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; |
||||
|
|
||||
|
import static ${ServiceExceptionUtilClassName}.exception; |
||||
|
import static ${basePackage}.module.${table.moduleName}.enums.ErrorCodeConstants.*; |
||||
|
|
||||
/** |
/** |
||||
* ${table.classComment} Service 接口 |
* ${table.classComment} Service |
||||
* |
* |
||||
* @author ${table.author} |
* @author ${table.author} |
||||
*/ |
*/ |
||||
public interface ${table.className}Service { |
@Service |
||||
|
@Validated |
||||
/** |
public class ${table.className}Service { |
||||
* 创建${table.classComment} |
|
||||
* |
@Resource |
||||
* @param createReqVO 创建信息 |
private ${table.className}Mapper ${classNameVar}Mapper; |
||||
* @return 编号 |
|
||||
*/ |
|
||||
${primaryColumn.javaType} create${simpleClassName}(@Valid ${sceneEnum.prefixClass}${table.className}CreateReqVO createReqVO); |
public ${primaryColumn.javaType} create${simpleClassName}(${sceneEnum.prefixClass}${table.className}CreateReqVO createReqVO) { |
||||
|
// 插入 |
||||
/** |
${table.className}DO ${classNameVar} = ${table.className}Convert.INSTANCE.convert(createReqVO); |
||||
* 更新${table.classComment} |
${classNameVar}Mapper.insert(${classNameVar}); |
||||
* |
// 返回 |
||||
* @param updateReqVO 更新信息 |
return ${classNameVar}.getId(); |
||||
*/ |
} |
||||
void update${simpleClassName}(@Valid ${sceneEnum.prefixClass}${table.className}UpdateReqVO updateReqVO); |
|
||||
|
public void update${simpleClassName}(${sceneEnum.prefixClass}${table.className}UpdateReqVO updateReqVO) { |
||||
/** |
// 校验存在 |
||||
* 删除${table.classComment} |
validate${simpleClassName}Exists(updateReqVO.getId()); |
||||
* |
// 更新 |
||||
* @param id 编号 |
${table.className}DO updateObj = ${table.className}Convert.INSTANCE.convert(updateReqVO); |
||||
*/ |
${classNameVar}Mapper.updateById(updateObj); |
||||
void delete${simpleClassName}(${primaryColumn.javaType} id); |
} |
||||
|
|
||||
/** |
public void delete${simpleClassName}(${primaryColumn.javaType} id) { |
||||
* 获得${table.classComment} |
// 校验存在 |
||||
* |
validate${simpleClassName}Exists(id); |
||||
* @param id 编号 |
// 删除 |
||||
* @return ${table.classComment} |
${classNameVar}Mapper.deleteById(id); |
||||
*/ |
} |
||||
${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id); |
|
||||
|
private void validate${simpleClassName}Exists(${primaryColumn.javaType} id) { |
||||
/** |
if (${classNameVar}Mapper.selectById(id) == null) { |
||||
* 获得${table.classComment}列表 |
throw exception(${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); |
||||
* |
} |
||||
* @param ids 编号 |
} |
||||
* @return ${table.classComment}列表 |
|
||||
*/ |
public ${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id) { |
||||
List<${table.className}DO> get${simpleClassName}List(Collection<${primaryColumn.javaType}> ids); |
return ${classNameVar}Mapper.selectById(id); |
||||
|
} |
||||
/** |
|
||||
* 获得${table.classComment}分页 |
public List<${table.className}DO> get${simpleClassName}List(Collection<${primaryColumn.javaType}> ids) { |
||||
* |
return ${classNameVar}Mapper.selectBatchIds(ids); |
||||
* @param pageReqVO 分页查询 |
} |
||||
* @return ${table.classComment}分页 |
|
||||
*/ |
public PageResult<${table.className}DO> get${simpleClassName}Page(${sceneEnum.prefixClass}${table.className}PageReqVO pageReqVO) { |
||||
PageResult<${table.className}DO> get${simpleClassName}Page(${sceneEnum.prefixClass}${table.className}PageReqVO pageReqVO); |
return ${classNameVar}Mapper.selectPage(pageReqVO); |
||||
|
} |
||||
/** |
|
||||
* 获得${table.classComment}列表, 用于 Excel 导出 |
public List<${table.className}DO> get${simpleClassName}List(${sceneEnum.prefixClass}${table.className}ExportReqVO exportReqVO) { |
||||
* |
return ${classNameVar}Mapper.selectList(exportReqVO); |
||||
* @param exportReqVO 查询条件 |
} |
||||
* @return ${table.classComment}列表 |
|
||||
*/ |
public List<${table.className}ExcelVO> import${table.className}List(List<${table.className}ExcelVO> datas, Integer mode, boolean updatePart) { |
||||
List<${table.className}DO> get${simpleClassName}List(${sceneEnum.prefixClass}${table.className}ExportReqVO exportReqVO); |
if (CollUtil.isEmpty(datas)) { |
||||
|
throw exception(${simpleClassName_underlineCase.toUpperCase()}_IMPORT_LIST_IS_EMPTY); |
||||
/** |
} |
||||
* 导入${table.classComment}主信息 |
|
||||
* |
List<${table.className}ExcelVO> errorList = new ArrayList<>(); |
||||
* @param datas 导入${table.classComment}主信息列表 |
datas.forEach(item -> { |
||||
* @param mode 导入模式1更新2追加3覆盖 |
if(errorList == null){ |
||||
* @param updatePart 是否支持更新 |
// 判断如果不存在,在进行插入 |
||||
* @return 导入结果 |
${table.className}DO obj = ${classNameVar}Mapper.selectByCode(item.getCode()); |
||||
*/ |
if (obj == null&& mode != 3) { |
||||
public List<${table.className}ExcelVO> import${table.className}List(List<${table.className}ExcelVO> datas, Integer mode, boolean updatePart); |
${classNameVar}Mapper.insert(${table.className}Convert.INSTANCE.convert(item)); |
||||
|
} |
||||
|
else if (obj != null && mode != 2) {// 如果存在,判断是否允许更新 |
||||
|
${table.className}DO ${classNameVar}DO = ${table.className}Convert.INSTANCE.convert(item); |
||||
|
${classNameVar}DO.setId(obj.getId()); |
||||
|
${classNameVar}Mapper.updateById(obj); |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
return errorList; |
||||
|
} |
||||
} |
} |
||||
|
@ -0,0 +1,79 @@ |
|||||
|
package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; |
||||
|
|
||||
|
import java.util.*; |
||||
|
import javax.validation.*; |
||||
|
import ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}.vo.*; |
||||
|
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; |
||||
|
import ${PageResultClassName}; |
||||
|
|
||||
|
/** |
||||
|
* ${table.classComment} Service 接口 |
||||
|
* |
||||
|
* @author ${table.author} |
||||
|
*/ |
||||
|
public interface ${table.className}Service { |
||||
|
|
||||
|
/** |
||||
|
* 创建${table.classComment} |
||||
|
* |
||||
|
* @param createReqVO 创建信息 |
||||
|
* @return 编号 |
||||
|
*/ |
||||
|
${primaryColumn.javaType} create${simpleClassName}(@Valid ${sceneEnum.prefixClass}${table.className}CreateReqVO createReqVO); |
||||
|
|
||||
|
/** |
||||
|
* 更新${table.classComment} |
||||
|
* |
||||
|
* @param updateReqVO 更新信息 |
||||
|
*/ |
||||
|
void update${simpleClassName}(@Valid ${sceneEnum.prefixClass}${table.className}UpdateReqVO updateReqVO); |
||||
|
|
||||
|
/** |
||||
|
* 删除${table.classComment} |
||||
|
* |
||||
|
* @param id 编号 |
||||
|
*/ |
||||
|
void delete${simpleClassName}(${primaryColumn.javaType} id); |
||||
|
|
||||
|
/** |
||||
|
* 获得${table.classComment} |
||||
|
* |
||||
|
* @param id 编号 |
||||
|
* @return ${table.classComment} |
||||
|
*/ |
||||
|
${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id); |
||||
|
|
||||
|
/** |
||||
|
* 获得${table.classComment}列表 |
||||
|
* |
||||
|
* @param ids 编号 |
||||
|
* @return ${table.classComment}列表 |
||||
|
*/ |
||||
|
List<${table.className}DO> get${simpleClassName}List(Collection<${primaryColumn.javaType}> ids); |
||||
|
|
||||
|
/** |
||||
|
* 获得${table.classComment}分页 |
||||
|
* |
||||
|
* @param pageReqVO 分页查询 |
||||
|
* @return ${table.classComment}分页 |
||||
|
*/ |
||||
|
PageResult<${table.className}DO> get${simpleClassName}Page(${sceneEnum.prefixClass}${table.className}PageReqVO pageReqVO); |
||||
|
|
||||
|
/** |
||||
|
* 获得${table.classComment}列表, 用于 Excel 导出 |
||||
|
* |
||||
|
* @param exportReqVO 查询条件 |
||||
|
* @return ${table.classComment}列表 |
||||
|
*/ |
||||
|
List<${table.className}DO> get${simpleClassName}List(${sceneEnum.prefixClass}${table.className}ExportReqVO exportReqVO); |
||||
|
|
||||
|
/** |
||||
|
* 导入${table.classComment}主信息 |
||||
|
* |
||||
|
* @param datas 导入${table.classComment}主信息列表 |
||||
|
* @param mode 导入模式1更新2追加3覆盖 |
||||
|
* @param updatePart 是否支持更新 |
||||
|
* @return 导入结果 |
||||
|
*/ |
||||
|
public List<${table.className}ExcelVO> import${table.className}List(List<${table.className}ExcelVO> datas, Integer mode, boolean updatePart); |
||||
|
} |
@ -0,0 +1,165 @@ |
|||||
|
package ${basePackage}.module.${table.moduleName}.service.${table.businessName}; |
||||
|
|
||||
|
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 ${baseFrameworkPackage}.test.core.ut.BaseDbUnitTest; |
||||
|
|
||||
|
import ${basePackage}.module.${table.moduleName}.controller.${sceneEnum.basePackage}.${table.businessName}.vo.*; |
||||
|
import ${basePackage}.module.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; |
||||
|
import ${basePackage}.module.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; |
||||
|
import ${PageResultClassName}; |
||||
|
|
||||
|
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 ${basePackage}.module.${table.moduleName}.enums.ErrorCodeConstants.*; |
||||
|
import static ${baseFrameworkPackage}.test.core.util.AssertUtils.*; |
||||
|
import static ${baseFrameworkPackage}.test.core.util.RandomUtils.*; |
||||
|
import static ${LocalDateTimeUtilsClassName}.*; |
||||
|
import static ${ObjectUtilsClassName}.*; |
||||
|
import static ${DateUtilsClassName}.*; |
||||
|
import static org.junit.jupiter.api.Assertions.*; |
||||
|
import static org.mockito.Mockito.*; |
||||
|
|
||||
|
## 字段模板 |
||||
|
#macro(getPageCondition $VO) |
||||
|
// mock 数据 |
||||
|
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class, o -> { // 等会查询到 |
||||
|
#foreach ($column in $columns) |
||||
|
#if (${column.listOperation}) |
||||
|
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 |
||||
|
o.set$JavaField(null); |
||||
|
#end |
||||
|
#end |
||||
|
}); |
||||
|
${classNameVar}Mapper.insert(db${simpleClassName}); |
||||
|
#foreach ($column in $columns) |
||||
|
#if (${column.listOperation}) |
||||
|
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 |
||||
|
// 测试 ${column.javaField} 不匹配 |
||||
|
${classNameVar}Mapper.insert(cloneIgnoreId(db${simpleClassName}, o -> o.set$JavaField(null))); |
||||
|
#end |
||||
|
#end |
||||
|
// 准备参数 |
||||
|
${sceneEnum.prefixClass}${table.className}${VO} reqVO = new ${sceneEnum.prefixClass}${table.className}${VO}(); |
||||
|
#foreach ($column in $columns) |
||||
|
#if (${column.listOperation}) |
||||
|
#set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 |
||||
|
#if (${column.listOperationCondition} == "BETWEEN")## BETWEEN 的情况 |
||||
|
reqVO.set${JavaField}(buildBetweenTime(2023, 2, 1, 2023, 2, 28)); |
||||
|
#else |
||||
|
reqVO.set$JavaField(null); |
||||
|
#end |
||||
|
#end |
||||
|
#end |
||||
|
#end |
||||
|
/** |
||||
|
* {@link ${table.className}ServiceImpl} 的单元测试类 |
||||
|
* |
||||
|
* @author ${table.author} |
||||
|
*/ |
||||
|
@Import(${table.className}ServiceImpl.class) |
||||
|
public class ${table.className}ServiceImplTest extends BaseDbUnitTest { |
||||
|
|
||||
|
@Resource |
||||
|
private ${table.className}ServiceImpl ${classNameVar}Service; |
||||
|
|
||||
|
@Resource |
||||
|
private ${table.className}Mapper ${classNameVar}Mapper; |
||||
|
|
||||
|
@Test |
||||
|
public void testCreate${simpleClassName}_success() { |
||||
|
// 准备参数 |
||||
|
${sceneEnum.prefixClass}${table.className}CreateReqVO reqVO = randomPojo(${sceneEnum.prefixClass}${table.className}CreateReqVO.class); |
||||
|
|
||||
|
// 调用 |
||||
|
${primaryColumn.javaType} ${classNameVar}Id = ${classNameVar}Service.create${simpleClassName}(reqVO); |
||||
|
// 断言 |
||||
|
assertNotNull(${classNameVar}Id); |
||||
|
// 校验记录的属性是否正确 |
||||
|
${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(${classNameVar}Id); |
||||
|
assertPojoEquals(reqVO, ${classNameVar}); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testUpdate${simpleClassName}_success() { |
||||
|
// mock 数据 |
||||
|
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class); |
||||
|
${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据 |
||||
|
// 准备参数 |
||||
|
${sceneEnum.prefixClass}${table.className}UpdateReqVO reqVO = randomPojo(${sceneEnum.prefixClass}${table.className}UpdateReqVO.class, o -> { |
||||
|
o.setId(db${simpleClassName}.getId()); // 设置更新的 ID |
||||
|
}); |
||||
|
|
||||
|
// 调用 |
||||
|
${classNameVar}Service.update${simpleClassName}(reqVO); |
||||
|
// 校验是否更新正确 |
||||
|
${table.className}DO ${classNameVar} = ${classNameVar}Mapper.selectById(reqVO.getId()); // 获取最新的 |
||||
|
assertPojoEquals(reqVO, ${classNameVar}); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testUpdate${simpleClassName}_notExists() { |
||||
|
// 准备参数 |
||||
|
${sceneEnum.prefixClass}${table.className}UpdateReqVO reqVO = randomPojo(${sceneEnum.prefixClass}${table.className}UpdateReqVO.class); |
||||
|
|
||||
|
// 调用, 并断言异常 |
||||
|
assertServiceException(() -> ${classNameVar}Service.update${simpleClassName}(reqVO), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDelete${simpleClassName}_success() { |
||||
|
// mock 数据 |
||||
|
${table.className}DO db${simpleClassName} = randomPojo(${table.className}DO.class); |
||||
|
${classNameVar}Mapper.insert(db${simpleClassName});// @Sql: 先插入出一条存在的数据 |
||||
|
// 准备参数 |
||||
|
${primaryColumn.javaType} id = db${simpleClassName}.getId(); |
||||
|
|
||||
|
// 调用 |
||||
|
${classNameVar}Service.delete${simpleClassName}(id); |
||||
|
// 校验数据不存在了 |
||||
|
assertNull(${classNameVar}Mapper.selectById(id)); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDelete${simpleClassName}_notExists() { |
||||
|
// 准备参数 |
||||
|
${primaryColumn.javaType} id = random${primaryColumn.javaType}Id(); |
||||
|
|
||||
|
// 调用, 并断言异常 |
||||
|
assertServiceException(() -> ${classNameVar}Service.delete${simpleClassName}(id), ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 |
||||
|
public void testGet${simpleClassName}Page() { |
||||
|
#getPageCondition("PageReqVO") |
||||
|
|
||||
|
// 调用 |
||||
|
PageResult<${table.className}DO> pageResult = ${classNameVar}Service.get${simpleClassName}Page(reqVO); |
||||
|
// 断言 |
||||
|
assertEquals(1, pageResult.getTotal()); |
||||
|
assertEquals(1, pageResult.getList().size()); |
||||
|
assertPojoEquals(db${simpleClassName}, pageResult.getList().get(0)); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
@Disabled // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解 |
||||
|
public void testGet${simpleClassName}List() { |
||||
|
#getPageCondition("ExportReqVO") |
||||
|
|
||||
|
// 调用 |
||||
|
List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(reqVO); |
||||
|
// 断言 |
||||
|
assertEquals(1, list.size()); |
||||
|
assertPojoEquals(db${simpleClassName}, list.get(0)); |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue