前端
activity.js
直接替换现有的,最求速度了,后面在详细一个个记录,不在过多解释了。
import request from '@/utils/request' /** * 查询活动列表 * @param query 查询条件参数 * @returns {*} 查询结果 */ export function listActivity(query) { return request({ url: '/tienchin/activity/list', method: 'get', params: query }) } /** * 查询渠道列表 * @returns {*} 查询结果 */ export function listChannel() { return request({ url: '/tienchin/activity/channel/list', method: 'get' }) } /** * 查询活动详细 * @param activityId 活动ID * @returns {*} 查询结果 */ export function getInfo(activityId) { return request({ url: '/tienchin/activity/' + activityId, method: 'get' }) } // 查询活动详细 export function getActivity(activityId) { return request({ url: '/tienchin/activity/' + activityId, method: 'get' }) } // 新增活动 export function addActivity(data) { return request({ url: '/tienchin/activity', method: 'post', data: data }) } // 修改活动 export function updateActivity(data) { return request({ url: '/tienchin/activity', method: 'put', data: data }) } // 删除活动 export function delActivity(activityIds) { return request({ url: '/tienchin/activity/' + activityIds, method: 'delete' }) }
后端
我们在进行数据修改的时候,有的数据在新增的时候是需要校验必填项的,但是在编辑的时候却不需要,这个时候可以使用校验分组的方式进行区分实现方式如下。
新建一个 validator
包,在当中分别创建,CreateGroup,EditGroup:
/** * @author BNTang * @version 1.0 * @description 添加活动校验分组 * @since 2023-09-10 **/ public interface CreateGroup { }
/** * @author BNTang * @version 1.0 * @description 编辑活动校验分组 * @since 2023-09-10 **/ public interface EditGroup { }
ActivityController.java
@PreAuthorize("hasPermission('tienchin:activity:create')") @Log(title = "活动管理", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@Validated(CreateGroup.class) @RequestBody ActivityVO activityVO) { return AjaxResult.success(iActivityService.addActivity(activityVO) > 0 ? "添加成功" : "添加失败"); }
/** * 修改活动 */ @PreAuthorize("hasPermission('tienchin:activity:edit')") @Log(title = "活动管理", businessType = BusinessType.UPDATE) @PutMapping AjaxResult edit(@Validated(EditGroup.class) @RequestBody ActivityVO activityVO) { return AjaxResult.success(iActivityService.updateActivity(activityVO) > 0 ? "更新成功" : "更新失败"); }
ValidationMessages.properties
添加一个活动id不能为空的校验信息:
activity.id.notnull=活动ID不能为空
ActivityVO
/** * 活动ID */ @NotNull(message = "{activity.id.notnull}", groups = {EditGroup.class}) private Integer activityId; /** * 活动名称 */ @NotBlank(message = "{activity.name.notBlank}", groups = {EditGroup.class, CreateGroup.class}) @Size(max = 20, message = "{activity.name.size}", groups = {EditGroup.class, CreateGroup.class}) private String activityName; /** * 渠道ID */ @NotNull(message = "{activity.channelId.notnull}", groups = {EditGroup.class, CreateGroup.class}) private Integer channelId; /** * 活动简介 */ @NotBlank(message = "{activity.info.notBlank}", groups = {EditGroup.class, CreateGroup.class}) @Size(max = 255, message = "{activity.info.size}", groups = {EditGroup.class, CreateGroup.class}) private String activityInfo; /** * 活动类型,1.折扣券、2.代金券 */ @NotNull(message = "{activity.type.notnull}", groups = {EditGroup.class, CreateGroup.class}) private Integer activityType; /** * 活动状态,0.表示禁用、1.表示正常 */ @Max(value = 1, message = "{activity.status.max}", groups = {EditGroup.class, CreateGroup.class}) @Min(value = 0, message = "{activity.status.min}", groups = {EditGroup.class, CreateGroup.class}) private Integer activityStatus; /** * 折扣券 */ @Max(value = 10, message = "{activity.discount.max}", groups = {EditGroup.class, CreateGroup.class}) @Min(value = 0, message = "{activity.discount.min}", groups = {EditGroup.class, CreateGroup.class}) private Double discount; /** * 代金券 */ @Min(value = 0, message = "{activity.voucher.min}", groups = {EditGroup.class, CreateGroup.class}) private Double voucher; /** * 活动开始时间 */ @NotNull(message = "{activity.beginTime.notnull}", groups = {EditGroup.class, CreateGroup.class}) @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai") private LocalDateTime beginTime; /** * 活动结束时间 */ @NotNull(message = "{activity.endTime.notnull}", groups = {EditGroup.class, CreateGroup.class}) @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai") private LocalDateTime endTime;
IActivityService
/** * 修改活动 * * @param activityVO 活动信息 * @return 结果 */ int updateActivity(ActivityVO activityVO);
ActivityServiceImpl
@Override public int updateActivity(ActivityVO activityVO) { Activity activity = new Activity(); BeanUtils.copyProperties(activityVO, activity); activity.setUpdateBy(SecurityUtils.getUsername()); activity.setUpdateTime(LocalDateTime.now()); // 将来更新的时候,不修改为 null 的字段 activity.setCreateBy(null); activity.setCreateTime(null); activity.setDelFlag(null); return updateById(activity) ? 1 : 0; }
!> 将实体类都统一放入到 domain 包当中自己改下