初始化短信模板的基础后端代码

This commit is contained in:
YunaiV 2021-04-05 22:32:05 +08:00
parent cb1f9a5808
commit 58d368cfdc
17 changed files with 717 additions and 65 deletions

View File

@ -1,14 +0,0 @@
package cn.iocoder.dashboard.modules.system.controller.sms;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api("认证 API")
@RestController
@RequestMapping("/sms/template")
public class SmsTemplateController {
}

View File

@ -23,7 +23,7 @@ import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
@Api(tags = "短信渠道") @Api(tags = "短信渠道")
@RestController @RestController
@RequestMapping("system/sms-channel") @RequestMapping("system/sms-channel")
public class SmsChannelController { public class SysSmsChannelController {
@Resource @Resource
private SysSmsChannelService smsChannelService; private SysSmsChannelService smsChannelService;

View File

@ -0,0 +1,87 @@
package cn.iocoder.dashboard.modules.system.controller.sms;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.excel.core.util.ExcelUtils;
import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.*;
import cn.iocoder.dashboard.modules.system.convert.sms.SysSmsTemplateConvert;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
import static cn.iocoder.dashboard.framework.logger.operatelog.core.enums.OperateTypeEnum.EXPORT;
@Api("短信模板")
@RestController
@RequestMapping("/system/sms-template")
public class SysSmsTemplateController {
@Resource
private SysSmsTemplateService smsTemplateService;
@PostMapping("/create")
@ApiOperation("创建短信模板")
@PreAuthorize("@ss.hasPermission('system:sms-template:create')")
public CommonResult<Long> createSmsTemplate(@Valid @RequestBody SysSmsTemplateCreateReqVO createReqVO) {
return success(smsTemplateService.createSmsTemplate(createReqVO));
}
@PutMapping("/update")
@ApiOperation("更新短信模板")
@PreAuthorize("@ss.hasPermission('system:sms-template:update')")
public CommonResult<Boolean> updateSmsTemplate(@Valid @RequestBody SysSmsTemplateUpdateReqVO updateReqVO) {
smsTemplateService.updateSmsTemplate(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@ApiOperation("删除短信模板")
@ApiImplicitParam(name = "id", value = "编号", required = true)
@PreAuthorize("@ss.hasPermission('system:sms-template:delete')")
public CommonResult<Boolean> deleteSmsTemplate(@RequestParam("id") Long id) {
smsTemplateService.deleteSmsTemplate(id);
return success(true);
}
@GetMapping("/get")
@ApiOperation("获得短信模板")
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('system:sms-template:query')")
public CommonResult<SysSmsTemplateRespVO> getSmsTemplate(@RequestParam("id") Long id) {
SysSmsTemplateDO smsTemplate = smsTemplateService.getSmsTemplate(id);
return success(SysSmsTemplateConvert.INSTANCE.convert(smsTemplate));
}
@GetMapping("/page")
@ApiOperation("获得短信模板分页")
@PreAuthorize("@ss.hasPermission('system:sms-template:query')")
public CommonResult<PageResult<SysSmsTemplateRespVO>> getSmsTemplatePage(@Valid SysSmsTemplatePageReqVO pageVO) {
PageResult<SysSmsTemplateDO> pageResult = smsTemplateService.getSmsTemplatePage(pageVO);
return success(SysSmsTemplateConvert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/export-excel")
@ApiOperation("导出短信模板 Excel")
@PreAuthorize("@ss.hasPermission('system:sms-template:export')")
@OperateLog(type = EXPORT)
public void exportSmsTemplateExcel(@Valid SysSmsTemplateExportReqVO exportReqVO,
HttpServletResponse response) throws IOException {
List<SysSmsTemplateDO> list = smsTemplateService.getSmsTemplateList(exportReqVO);
// 导出 Excel
List<SysSmsTemplateExcelVO> datas = SysSmsTemplateConvert.INSTANCE.convertList02(list);
ExcelUtils.write(response, "短信模板.xls", "数据", SysSmsTemplateExcelVO.class, datas);
}
}

View File

@ -0,0 +1,46 @@
package cn.iocoder.dashboard.modules.system.controller.sms.vo.template;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* 短信模板 Base VO提供给添加修改详细的子 VO 使用
* 如果子 VO 存在差异的字段请不要添加到这里影响 Swagger 文档生成
*/
@Data
public class SysSmsTemplateBaseVO {
@ApiModelProperty(value = "短信签名", required = true, example = "1", notes = "参见 SysSmsTemplateTypeEnum 枚举类")
@NotNull(message = "短信签名不能为空")
private Integer type;
@ApiModelProperty(value = "开启状态", required = true, example = "1", notes = "参见 CommonStatusEnum 枚举类")
@NotNull(message = "开启状态不能为空")
private Integer status;
@ApiModelProperty(value = "模板编码", required = true, example = "test_01")
@NotNull(message = "模板编码不能为空")
private String code;
@ApiModelProperty(value = "模板名称", required = true, example = "yudao")
@NotNull(message = "模板名称不能为空")
private String name;
@ApiModelProperty(value = "模板内容", required = true, example = "你好,{name}。你长的太{like}啦!")
@NotNull(message = "模板内容不能为空")
private String content;
@ApiModelProperty(value = "备注", example = "哈哈哈")
private String remark;
@ApiModelProperty(value = "短信 API 的模板编号", required = true, example = "4383920")
@NotNull(message = "短信 API 的模板编号不能为空")
private String apiTemplateId;
@ApiModelProperty(value = "短信渠道编号", required = true, example = "10")
@NotNull(message = "短信渠道编号不能为空")
private Long channelId;
}

View File

@ -0,0 +1,14 @@
package cn.iocoder.dashboard.modules.system.controller.sms.vo.template;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@ApiModel("短信模板创建 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SysSmsTemplateCreateReqVO extends SysSmsTemplateBaseVO {
}

View File

@ -0,0 +1,56 @@
package cn.iocoder.dashboard.modules.system.controller.sms.vo.template;
import cn.iocoder.dashboard.framework.excel.core.annotations.DictFormat;
import cn.iocoder.dashboard.framework.excel.core.convert.DictConvert;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.util.Date;
import static cn.iocoder.dashboard.modules.system.enums.dict.SysDictTypeEnum.*;
/**
* 短信模板 Excel VO
*
* @author 芋道源码
*/
@Data
public class SysSmsTemplateExcelVO {
@ExcelProperty("编号")
private Long id;
@ExcelProperty(value = "短信签名", converter = DictConvert.class)
@DictFormat(SYS_SMS_TEMPLATE_TYPE)
private Integer type;
@ExcelProperty(value = "开启状态", converter = DictConvert.class)
@DictFormat(SYS_COMMON_STATUS)
private Integer status;
@ExcelProperty("模板编码")
private String code;
@ExcelProperty("模板名称")
private String name;
@ExcelProperty("模板内容")
private String content;
@ExcelProperty("备注")
private String remark;
@ExcelProperty("短信 API 的模板编号")
private String apiTemplateId;
@ExcelProperty("短信渠道编号")
private Long channelId;
@ExcelProperty(value = "短信渠道编码", converter = DictConvert.class)
@DictFormat(SYS_SMS_CHANNEL_CODE)
private String channelCode;
@ExcelProperty("创建时间")
private Date createTime;
}

View File

@ -0,0 +1,42 @@
package cn.iocoder.dashboard.modules.system.controller.sms.vo.template;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@ApiModel(value = "短信模板 Excel 导出 Request VO", description = "参数和 SysSmsTemplatePageReqVO 是一致的")
@Data
public class SysSmsTemplateExportReqVO {
@ApiModelProperty(value = "短信签名", example = "1")
private Integer type;
@ApiModelProperty(value = "开启状态", example = "1")
private Integer status;
@ApiModelProperty(value = "模板编码", example = "test_01", notes = "模糊匹配")
private String code;
@ApiModelProperty(value = "模板内容", example = "你好,{name}。你长的太{like}啦!", notes = "模糊匹配")
private String content;
@ApiModelProperty(value = "短信 API 的模板编号", example = "4383920", notes = "模糊匹配")
private String apiTemplateId;
@ApiModelProperty(value = "短信渠道编号", example = "10")
private Long channelId;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ApiModelProperty(value = "开始创建时间")
private Date beginCreateTime;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ApiModelProperty(value = "结束创建时间")
private Date endCreateTime;
}

View File

@ -0,0 +1,47 @@
package cn.iocoder.dashboard.modules.system.controller.sms.vo.template;
import cn.iocoder.dashboard.common.pojo.PageParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@ApiModel("短信模板分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SysSmsTemplatePageReqVO extends PageParam {
@ApiModelProperty(value = "短信签名", example = "1")
private Integer type;
@ApiModelProperty(value = "开启状态", example = "1")
private Integer status;
@ApiModelProperty(value = "模板编码", example = "test_01", notes = "模糊匹配")
private String code;
@ApiModelProperty(value = "模板内容", example = "你好,{name}。你长的太{like}啦!", notes = "模糊匹配")
private String content;
@ApiModelProperty(value = "短信 API 的模板编号", example = "4383920", notes = "模糊匹配")
private String apiTemplateId;
@ApiModelProperty(value = "短信渠道编号", example = "10")
private Long channelId;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ApiModelProperty(value = "开始创建时间")
private Date beginCreateTime;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ApiModelProperty(value = "结束创建时间")
private Date endCreateTime;
}

View File

@ -0,0 +1,26 @@
package cn.iocoder.dashboard.modules.system.controller.sms.vo.template;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.util.Date;
@ApiModel("短信模板 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SysSmsTemplateRespVO extends SysSmsTemplateBaseVO {
@ApiModelProperty(value = "编号", required = true, example = "1024")
private Long id;
@ApiModelProperty(value = "短信渠道编码", required = true, example = "ALIYUN")
private String channelCode;
@ApiModelProperty(value = "创建时间", required = true)
private Date createTime;
}

View File

@ -0,0 +1,21 @@
package cn.iocoder.dashboard.modules.system.controller.sms.vo.template;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.constraints.NotNull;
@ApiModel("短信模板更新 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SysSmsTemplateUpdateReqVO extends SysSmsTemplateBaseVO {
@ApiModelProperty(value = "编号", required = true, example = "1024")
@NotNull(message = "编号不能为空")
private Long id;
}

View File

@ -1,26 +0,0 @@
package cn.iocoder.dashboard.modules.system.convert.sms;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.SmsTemplateVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface SmsTemplateConvert {
SmsTemplateConvert INSTANCE = Mappers.getMapper(SmsTemplateConvert.class);
@Mapping(source = "records", target = "list")
PageResult<SysSmsChannelDO> convertPage(IPage<SysSmsChannelDO> page);
List<SmsTemplateVO> convert(List<SysSmsTemplateDO> bean);
SmsTemplateVO convert(SysSmsTemplateDO bean);
}

View File

@ -0,0 +1,31 @@
package cn.iocoder.dashboard.modules.system.convert.sms;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateExcelVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateRespVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface SysSmsTemplateConvert {
SysSmsTemplateConvert INSTANCE = Mappers.getMapper(SysSmsTemplateConvert.class);
SysSmsTemplateDO convert(SysSmsTemplateCreateReqVO bean);
SysSmsTemplateDO convert(SysSmsTemplateUpdateReqVO bean);
SysSmsTemplateRespVO convert(SysSmsTemplateDO bean);
List<SysSmsTemplateRespVO> convertList(List<SysSmsTemplateDO> list);
PageResult<SysSmsTemplateRespVO> convertPage(PageResult<SysSmsTemplateDO> page);
List<SysSmsTemplateExcelVO> convertList02(List<SysSmsTemplateDO> list);
}

View File

@ -1,8 +1,11 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.sms; package cn.iocoder.dashboard.modules.system.dal.mysql.sms;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateExportReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplatePageReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.List; import java.util.List;
@ -14,29 +17,28 @@ public interface SysSmsTemplateMapper extends BaseMapperX<SysSmsTemplateDO> {
return selectOne("code", code); return selectOne("code", code);
} }
/** default PageResult<SysSmsTemplateDO> selectPage(SysSmsTemplatePageReqVO reqVO) {
* 根据短信渠道id查询短信模板集合 return selectPage(reqVO, new QueryWrapperX<SysSmsTemplateDO>()
* .eqIfPresent("type", reqVO.getType())
* @param channelId 渠道id .eqIfPresent("status", reqVO.getStatus())
* @return 模板集合 .likeIfPresent("code", reqVO.getCode())
*/ .likeIfPresent("content", reqVO.getContent())
default List<SysSmsTemplateDO> selectListByChannelId(Long channelId) { .likeIfPresent("api_template_id", reqVO.getApiTemplateId())
return selectList(new LambdaQueryWrapper<SysSmsTemplateDO>() .eqIfPresent("channel_id", reqVO.getChannelId())
.eq(SysSmsTemplateDO::getChannelId, channelId) .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
.eq(SysSmsTemplateDO::getStatus, CommonStatusEnum.ENABLE.getStatus()) .orderByDesc("id"));
.orderByAsc(SysSmsTemplateDO::getId)
);
} }
/** default List<SysSmsTemplateDO> selectList(SysSmsTemplateExportReqVO reqVO) {
* 查询有效短信模板集合 return selectList(new QueryWrapperX<SysSmsTemplateDO>()
* .eqIfPresent("type", reqVO.getType())
* @return 有效短信模板集合 .eqIfPresent("status", reqVO.getStatus())
*/ .likeIfPresent("code", reqVO.getCode())
default List<SysSmsTemplateDO> selectEnabledList() { .likeIfPresent("content", reqVO.getContent())
return selectList(new LambdaQueryWrapper<SysSmsTemplateDO>() .likeIfPresent("api_template_id", reqVO.getApiTemplateId())
.eq(SysSmsTemplateDO::getStatus, CommonStatusEnum.ENABLE.getStatus()) .eqIfPresent("channel_id", reqVO.getChannelId())
.orderByAsc(SysSmsTemplateDO::getId) .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
); .orderByDesc("id"));
} }
} }

View File

@ -19,6 +19,7 @@ public enum SysDictTypeEnum {
SYS_CONFIG_TYPE("sys_config_type"), // 参数配置类型 SYS_CONFIG_TYPE("sys_config_type"), // 参数配置类型
SYS_BOOLEAN_STRING("sys_boolean_string"), // Boolean 是否类型 SYS_BOOLEAN_STRING("sys_boolean_string"), // Boolean 是否类型
SYS_SMS_CHANNEL_CODE("sys_sms_channel_code"), // 短信渠道编码 SYS_SMS_CHANNEL_CODE("sys_sms_channel_code"), // 短信渠道编码
SYS_SMS_TEMPLATE_TYPE("sys_sms_template_type"), // 短信模板类型
INF_REDIS_TIMEOUT_TYPE("inf_redis_timeout_type"), // Redis 超时类型 INF_REDIS_TIMEOUT_TYPE("inf_redis_timeout_type"), // Redis 超时类型
INF_JOB_STATUS("inf_job_status"), // 定时任务状态的枚举 INF_JOB_STATUS("inf_job_status"), // 定时任务状态的枚举

View File

@ -1,7 +1,15 @@
package cn.iocoder.dashboard.modules.system.service.sms; package cn.iocoder.dashboard.modules.system.service.sms;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateExportReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplatePageReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -29,4 +37,58 @@ public interface SysSmsTemplateService {
*/ */
String formatSmsTemplateContent(String content, Map<String, Object> params); String formatSmsTemplateContent(String content, Map<String, Object> params);
/**
* 创建短信模板
*
* @param createReqVO 创建信息
* @return 编号
*/
Long createSmsTemplate(@Valid SysSmsTemplateCreateReqVO createReqVO);
/**
* 更新短信模板
*
* @param updateReqVO 更新信息
*/
void updateSmsTemplate(@Valid SysSmsTemplateUpdateReqVO updateReqVO);
/**
* 删除短信模板
*
* @param id 编号
*/
void deleteSmsTemplate(Long id);
/**
* 获得短信模板
*
* @param id 编号
* @return 短信模板
*/
SysSmsTemplateDO getSmsTemplate(Long id);
/**
* 获得短信模板列表
*
* @param ids 编号
* @return 短信模板列表
*/
List<SysSmsTemplateDO> getSmsTemplateList(Collection<Long> ids);
/**
* 获得短信模板分页
*
* @param pageReqVO 分页查询
* @return 短信模板分页
*/
PageResult<SysSmsTemplateDO> getSmsTemplatePage(SysSmsTemplatePageReqVO pageReqVO);
/**
* 获得短信模板列表, 用于 Excel 导出
*
* @param exportReqVO 查询条件
* @return 短信模板分页
*/
List<SysSmsTemplateDO> getSmsTemplateList(SysSmsTemplateExportReqVO exportReqVO);
} }

View File

@ -1,14 +1,25 @@
package cn.iocoder.dashboard.modules.system.service.sms.impl; package cn.iocoder.dashboard.modules.system.service.sms.impl;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateExportReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplatePageReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateUpdateReqVO;
import cn.iocoder.dashboard.modules.system.convert.sms.SysSmsTemplateConvert;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsTemplateMapper; import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsTemplateMapper;
import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService; import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map; import java.util.Map;
import static cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_TEMPLATE_NOT_EXISTS;
/** /**
* 短信模板Service实现类 * 短信模板Service实现类
* *
@ -31,4 +42,56 @@ public class SysSmsTemplateServiceImpl implements SysSmsTemplateService {
return StrUtil.format(content, params); return StrUtil.format(content, params);
} }
@Override
public Long createSmsTemplate(SysSmsTemplateCreateReqVO createReqVO) {
// 插入
SysSmsTemplateDO smsTemplate = SysSmsTemplateConvert.INSTANCE.convert(createReqVO);
smsTemplateMapper.insert(smsTemplate);
// 返回
return smsTemplate.getId();
}
@Override
public void updateSmsTemplate(SysSmsTemplateUpdateReqVO updateReqVO) {
// 校验存在
this.validateSmsTemplateExists(updateReqVO.getId());
// 更新
SysSmsTemplateDO updateObj = SysSmsTemplateConvert.INSTANCE.convert(updateReqVO);
smsTemplateMapper.updateById(updateObj);
}
@Override
public void deleteSmsTemplate(Long id) {
// 校验存在
this.validateSmsTemplateExists(id);
// 更新
smsTemplateMapper.deleteById(id);
}
private void validateSmsTemplateExists(Long id) {
if (smsTemplateMapper.selectById(id) == null) {
throw exception(SMS_TEMPLATE_NOT_EXISTS);
}
}
@Override
public SysSmsTemplateDO getSmsTemplate(Long id) {
return smsTemplateMapper.selectById(id);
}
@Override
public List<SysSmsTemplateDO> getSmsTemplateList(Collection<Long> ids) {
return smsTemplateMapper.selectBatchIds(ids);
}
@Override
public PageResult<SysSmsTemplateDO> getSmsTemplatePage(SysSmsTemplatePageReqVO pageReqVO) {
return smsTemplateMapper.selectPage(pageReqVO);
}
@Override
public List<SysSmsTemplateDO> getSmsTemplateList(SysSmsTemplateExportReqVO exportReqVO) {
return smsTemplateMapper.selectList(exportReqVO);
}
} }

View File

@ -0,0 +1,194 @@
package cn.iocoder.dashboard.modules.system.service.sms;
import cn.iocoder.dashboard.BaseDbUnitTest;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateExportReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplatePageReqVO;
import cn.iocoder.dashboard.modules.system.controller.sms.vo.template.SysSmsTemplateUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsTemplateMapper;
import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsTemplateServiceImpl;
import cn.iocoder.dashboard.util.object.ObjectUtils;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import java.util.List;
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_TEMPLATE_NOT_EXISTS;
import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException;
import static cn.iocoder.dashboard.util.RandomUtils.randomLongId;
import static cn.iocoder.dashboard.util.RandomUtils.randomPojo;
import static org.junit.jupiter.api.Assertions.*;
/**
* {@link SysSmsTemplateServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(SysSmsTemplateServiceImpl.class)
public class SysSmsTemplateServiceTest extends BaseDbUnitTest {
@Resource
private SysSmsTemplateServiceImpl smsTemplateService;
@Resource
private SysSmsTemplateMapper smsTemplateMapper;
@Test
public void testCreateSmsTemplate_success() {
// 准备参数
SysSmsTemplateCreateReqVO reqVO = randomPojo(SysSmsTemplateCreateReqVO.class);
// 调用
Long smsTemplateId = smsTemplateService.createSmsTemplate(reqVO);
// 断言
assertNotNull(smsTemplateId);
// 校验记录的属性是否正确
SysSmsTemplateDO smsTemplate = smsTemplateMapper.selectById(smsTemplateId);
assertPojoEquals(reqVO, smsTemplate);
}
@Test
public void testUpdateSmsTemplate_success() {
// mock 数据
SysSmsTemplateDO dbSmsTemplate = randomPojo(SysSmsTemplateDO.class);
smsTemplateMapper.insert(dbSmsTemplate);// @Sql: 先插入出一条存在的数据
// 准备参数
SysSmsTemplateUpdateReqVO reqVO = randomPojo(SysSmsTemplateUpdateReqVO.class, o -> {
o.setId(dbSmsTemplate.getId()); // 设置更新的 ID
});
// 调用
smsTemplateService.updateSmsTemplate(reqVO);
// 校验是否更新正确
SysSmsTemplateDO smsTemplate = smsTemplateMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, smsTemplate);
}
@Test
public void testUpdateSmsTemplate_notExists() {
// 准备参数
SysSmsTemplateUpdateReqVO reqVO = randomPojo(SysSmsTemplateUpdateReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> smsTemplateService.updateSmsTemplate(reqVO), SMS_TEMPLATE_NOT_EXISTS);
}
@Test
public void testDeleteSmsTemplate_success() {
// mock 数据
SysSmsTemplateDO dbSmsTemplate = randomPojo(SysSmsTemplateDO.class);
smsTemplateMapper.insert(dbSmsTemplate);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbSmsTemplate.getId();
// 调用
smsTemplateService.deleteSmsTemplate(id);
// 校验数据不存在了
assertNull(smsTemplateMapper.selectById(id));
}
@Test
public void testDeleteSmsTemplate_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> smsTemplateService.deleteSmsTemplate(id), SMS_TEMPLATE_NOT_EXISTS);
}
@Test // TODO 请修改 null 为需要的值
public void testGetSmsTemplatePage() {
// mock 数据
SysSmsTemplateDO dbSmsTemplate = randomPojo(SysSmsTemplateDO.class, o -> { // 等会查询到
o.setType(null);
o.setStatus(null);
o.setCode(null);
o.setContent(null);
o.setApiTemplateId(null);
o.setChannelId(null);
o.setCreateTime(null);
});
smsTemplateMapper.insert(dbSmsTemplate);
// 测试 type 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setType(null)));
// 测试 status 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setStatus(null)));
// 测试 code 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setCode(null)));
// 测试 content 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setContent(null)));
// 测试 apiTemplateId 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setApiTemplateId(null)));
// 测试 channelId 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setChannelId(null)));
// 测试 createTime 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setCreateTime(null)));
// 准备参数
SysSmsTemplatePageReqVO reqVO = new SysSmsTemplatePageReqVO();
reqVO.setType(null);
reqVO.setStatus(null);
reqVO.setCode(null);
reqVO.setContent(null);
reqVO.setApiTemplateId(null);
reqVO.setChannelId(null);
reqVO.setBeginCreateTime(null);
reqVO.setEndCreateTime(null);
// 调用
PageResult<SysSmsTemplateDO> pageResult = smsTemplateService.getSmsTemplatePage(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbSmsTemplate, pageResult.getList().get(0));
}
@Test // TODO 请修改 null 为需要的值
public void testGetSmsTemplateList() {
// mock 数据
SysSmsTemplateDO dbSmsTemplate = randomPojo(SysSmsTemplateDO.class, o -> { // 等会查询到
o.setType(null);
o.setStatus(null);
o.setCode(null);
o.setContent(null);
o.setApiTemplateId(null);
o.setChannelId(null);
o.setCreateTime(null);
});
smsTemplateMapper.insert(dbSmsTemplate);
// 测试 type 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setType(null)));
// 测试 status 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setStatus(null)));
// 测试 code 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setCode(null)));
// 测试 content 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setContent(null)));
// 测试 apiTemplateId 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setApiTemplateId(null)));
// 测试 channelId 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setChannelId(null)));
// 测试 createTime 不匹配
smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setCreateTime(null)));
// 准备参数
SysSmsTemplateExportReqVO reqVO = new SysSmsTemplateExportReqVO();
reqVO.setType(null);
reqVO.setStatus(null);
reqVO.setCode(null);
reqVO.setContent(null);
reqVO.setApiTemplateId(null);
reqVO.setChannelId(null);
reqVO.setBeginCreateTime(null);
reqVO.setEndCreateTime(null);
// 调用
List<SysSmsTemplateDO> list = smsTemplateService.getSmsTemplateList(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(dbSmsTemplate, list.get(0));
}
}