完成秒杀时段todo,秒杀时段商品添加timeIds

This commit is contained in:
halfninety 2022-12-03 15:42:07 +08:00
parent abbb076859
commit 6808d3c163
12 changed files with 75 additions and 86 deletions

View File

@ -7,7 +7,6 @@ import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
@ -26,7 +25,6 @@ public class SeckillActivityCreateReqVO extends SeckillActivityBaseVO {
@NotNull(message = "排序不能为空")
private Integer sort;
// TODO halfninety直接使用数组接口timeIds
@ApiModelProperty(value = "秒杀时段id", required = true)
@NotEmpty(message = "参与场次不能为空")
private List<Long> timeIds;

View File

@ -75,8 +75,9 @@ public interface SeckillActivityConvert {
}
default List<SeckillProductDO> convertList(List<SeckillActivityBaseVO.Product> products, Long seckillActivityId, List<Long> timeIds) {
default List<SeckillProductDO> convertList(List<SeckillActivityBaseVO.Product> products, SeckillActivityDO seckillActivity) {
return CollectionUtils.convertList(products, product -> convert(product)
.setActivityId(seckillActivityId).setTimeIds(timeIds));
.setActivityId(seckillActivity.getId()).setTimeIds(seckillActivity.getTimeIds()));
}
}

View File

@ -1,13 +1,16 @@
package cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.seckillactivity;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.mybatis.core.type.LongListTypeHandler;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionActivityStatusEnum;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionActivityStatusEnum;
import lombok.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@ -16,14 +19,11 @@ import java.util.List;
*
* @author halfninety
*/
@TableName("promotion_seckill_activity")
@TableName(value = "promotion_seckill_activity", autoResultMap = true)
@KeySequence("promotion_seckill_activity_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SeckillActivityDO extends BaseDO {
/**
@ -37,7 +37,7 @@ public class SeckillActivityDO extends BaseDO {
private String name;
/**
* 活动状态
*
* <p>
* 枚举 {@link PromotionActivityStatusEnum 对应的类}
*/
private Integer status;
@ -60,6 +60,7 @@ public class SeckillActivityDO extends BaseDO {
/**
* 秒杀时段 id
*/
@TableField(typeHandler = LongListTypeHandler.class)
private List<Long> timeIds;
/**
* 付款订单数

View File

@ -1,17 +1,13 @@
package cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.seckillactivity;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import cn.iocoder.yudao.framework.mybatis.core.type.LongListTypeHandler;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.util.List;
/**
* 秒杀参与商品
@ -19,8 +15,11 @@ import lombok.Data;
* @author halfninety
* @TableName promotion_seckill_product
*/
@TableName(value = "promotion_seckill_product")
@TableName(value = "promotion_seckill_product", autoResultMap = true)
@KeySequence("promotion_seckill_product_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SeckillProductDO extends BaseDO {
/**
* 秒杀参与商品编号
@ -36,6 +35,7 @@ public class SeckillProductDO extends BaseDO {
/**
* 秒杀时段id
*/
@TableField(typeHandler = LongListTypeHandler.class)
private List<Long> timeIds;
/**

View File

@ -4,7 +4,9 @@ import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.time.LocalTime;
@ -18,9 +20,6 @@ import java.time.LocalTime;
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SeckillTimeDO extends BaseDO {
/**

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
@ -19,8 +20,7 @@ public interface SeckillActivityMapper extends BaseMapperX<SeckillActivityDO> {
.likeIfPresent(SeckillActivityDO::getName, reqVO.getName())
.eqIfPresent(SeckillActivityDO::getStatus, reqVO.getStatus())
.betweenIfPresent(SeckillActivityDO::getCreateTime, reqVO.getCreateTime())
.apply("FIND_IN_SET(" + reqVO.getTimeId() + ",time_id) > 0")
.apply(ObjectUtil.isNotNull(reqVO.getTimeId()),"FIND_IN_SET(" + reqVO.getTimeId() + ",time_ids) > 0")
.orderByDesc(SeckillActivityDO::getId));
}
}

View File

@ -1,7 +1,9 @@
package cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.seckillactivity.SeckillProductDO;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
@ -23,4 +25,11 @@ public interface SeckillProductMapper extends BaseMapperX<SeckillProductDO> {
return selectList(SeckillProductDO::getSkuId, skuIds);
}
default void updateTimeIdsByActivityId(Long id, List<Long> timeIds) {
new LambdaUpdateChainWrapper<>(this)
.set(SeckillProductDO::getTimeIds, CollUtil.join(timeIds, ","))
.eq(SeckillProductDO::getActivityId, id)
.update();
}
}

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckilltime;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.seckilltime.SeckillTimeDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@ -29,25 +28,10 @@ public interface SeckillTimeMapper extends BaseMapperX<SeckillTimeDO> {
.le(SeckillTimeDO::getEndTime, endTime));
}
// TODO @halfninetyupdateActivityCount + -可以执使用一个方法实现哈多传递一个参数
default void sekillActivityCountAdd(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
new LambdaUpdateChainWrapper<SeckillTimeDO>(this)
default void updateActivityCount(Collection<Long> ids, String type, Integer count) {
new LambdaUpdateChainWrapper<>(this)
.in(SeckillTimeDO::getId, ids)
.setSql("`seckill_activity_count` = `seckill_activity_count` + 1 ")
.setSql("`seckill_activity_count` = `seckill_activity_count` " + type + count)
.update();
}
default void sekillActivityCountReduce(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
new LambdaUpdateChainWrapper<SeckillTimeDO>(this)
.in(SeckillTimeDO::getId, ids)
.setSql("`seckill_activity_count` = `seckill_activity_count` - 1 ")
.update();
}
}

View File

@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.promotion.service.seckill.seckillactivity;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.string.StrUtils;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityBaseVO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.seckill.vo.activity.SeckillActivityPageReqVO;
@ -35,13 +34,10 @@ import static java.util.Arrays.asList;
@Service
@Validated
public class SeckillActivityServiceImpl implements SeckillActivityService {
// TODO: 2022/12/2 halfninety 当前修改时忘记秒杀商品中的秒杀时段id的设置了需要全部修改
// TODO: 2022/12/2 halfninety 将活动数量改为原来的商品数量
@Resource
private SeckillActivityMapper seckillActivityMapper;
@Resource
private SeckillProductMapper seckillProductMapper;
@Resource
private SeckillTimeService seckillTimeService;
@ -49,20 +45,18 @@ public class SeckillActivityServiceImpl implements SeckillActivityService {
public Long createSeckillActivity(SeckillActivityCreateReqVO createReqVO) {
// 校验商品是否冲突
validateSeckillActivityProductConflicts(null, createReqVO.getProducts());
// 校验秒杀时段是否存在
seckillTimeService.validateSeckillTimeExists(createReqVO.getTimeIds());
// 插入秒杀活动
SeckillActivityDO seckillActivity = SeckillActivityConvert.INSTANCE.convert(createReqVO)
.setStatus(PromotionUtils.calculateActivityStatus(createReqVO.getStartTime(), createReqVO.getEndTime()));
seckillActivityMapper.insert(seckillActivity);
// 插入商品
List<SeckillProductDO> productDOS = SeckillActivityConvert.INSTANCE
.convertList(createReqVO.getProducts(),seckillActivity.getId(),seckillActivity.getTimeIds());
List<SeckillProductDO> productDOS = SeckillActivityConvert.INSTANCE.convertList(createReqVO.getProducts(), seckillActivity);
seckillProductMapper.insertBatch(productDOS);
// 更新秒杀时段的秒杀活动数量
seckillTimeService.sekillActivityCountAdd(createReqVO.getTimeIds());
seckillTimeService.sekillActivityCountIncr(createReqVO.getTimeIds());
return seckillActivity.getId();
}
@ -76,17 +70,16 @@ public class SeckillActivityServiceImpl implements SeckillActivityService {
// 校验商品是否冲突
validateSeckillActivityProductConflicts(updateReqVO.getId(), updateReqVO.getProducts());
// 更新秒杀时段的秒杀活动数量
updateSeckillTimeActivityCount(seckillActivity, updateReqVO.getTimeIds());
// 更新活动
SeckillActivityDO updateObj = SeckillActivityConvert.INSTANCE.convert(updateReqVO)
.setStatus(PromotionUtils.calculateActivityStatus(updateReqVO.getStartTime(), updateReqVO.getEndTime()));
seckillActivityMapper.updateById(updateObj);
// 更新商品
updateSeckillProduct(updateReqVO);
// 更新秒杀时段的秒杀活动数量
updateSeckillTimeActivityCount(seckillActivity, updateReqVO.getTimeIds());
}
// TODO @halfninety注释写全哈
/**
* 更新秒杀时段的秒杀活动数量
@ -98,16 +91,15 @@ public class SeckillActivityServiceImpl implements SeckillActivityService {
// 查询出 timeIds
List<Long> existsTimeIds = seckillActivity.getTimeIds();
// 需要减少的时间段
// TODO @halfninety可以使用 CollUtil.filterNew()
Collection<Long> reduceIds = CollUtil.filterNew(existsTimeIds, existsTimeId -> !updateTimeIds.contains(existsTimeId));
// 需要添加的时间段
updateTimeIds.removeIf(existsTimeIds::contains);
// 更新减少时间段和增加时间段
if (CollUtil.isNotEmpty(updateTimeIds)) {
seckillTimeService.sekillActivityCountAdd(updateTimeIds);
seckillTimeService.sekillActivityCountIncr(updateTimeIds);
}
if (CollUtil.isNotEmpty(reduceIds)) {
seckillTimeService.sekillActivityCountReduce(reduceIds);
seckillTimeService.sekillActivityCountDecr(reduceIds);
}
}
@ -116,6 +108,9 @@ public class SeckillActivityServiceImpl implements SeckillActivityService {
* 后台查出的数据和前台查出的数据进行遍历
* 1. 对前台数据进行遍历如果不存在于后台的 sku 中需要新增
* 2. 对后台数据进行遍历如果不存在于前台的 sku 中需要删除
* 3. 最后对当前活动商品全部更新更新秒杀时段id列表
*
* @param updateReqVO 更新的请求VO
*/
private void updateSeckillProduct(SeckillActivityUpdateReqVO updateReqVO) {
List<SeckillProductDO> seckillProductDOS = seckillProductMapper.selectListByActivityId(updateReqVO.getId());
@ -137,6 +132,9 @@ public class SeckillActivityServiceImpl implements SeckillActivityService {
if (CollUtil.isNotEmpty(newSeckillProductDOs)) {
seckillProductMapper.insertBatch(newSeckillProductDOs);
}
//全量更新当前活动商品的秒杀时段id列表timeIds
seckillProductMapper.updateTimeIdsByActivityId(updateReqVO.getId(), updateReqVO.getTimeIds());
}
/**
@ -192,7 +190,7 @@ public class SeckillActivityServiceImpl implements SeckillActivityService {
throw exception(SECKILL_ACTIVITY_DELETE_FAIL_STATUS_NOT_CLOSED_OR_END);
}
// 更新秒杀时段的秒杀活动数量
seckillTimeService.sekillActivityCountReduce(seckillActivity.getTimeIds());
seckillTimeService.sekillActivityCountDecr(seckillActivity.getTimeIds());
// 删除
seckillActivityMapper.deleteById(id);
}

View File

@ -64,7 +64,7 @@ public interface SeckillTimeService {
*
* @param ids 秒杀时段id列表
*/
void sekillActivityCountAdd(Collection<Long> ids);
void sekillActivityCountIncr(Collection<Long> ids);
/**
@ -72,5 +72,5 @@ public interface SeckillTimeService {
*
* @param ids 秒杀时段id列表
*/
void sekillActivityCountReduce(Collection<Long> ids);
void sekillActivityCountDecr(Collection<Long> ids);
}

View File

@ -104,22 +104,21 @@ public class SeckillTimeServiceImpl implements SeckillTimeService {
@Override
public void validateSeckillTimeExists(Collection<Long> timeIds) {
if (CollUtil.isEmpty(timeIds)) {
return;
throw exception(SECKILL_TIME_NOT_EXISTS);
}
if (seckillTimeMapper.selectBatchIds(timeIds).size() != timeIds.size()) {
throw exception(SECKILL_TIME_NOT_EXISTS);
}
}
// TODO @halfninetyupdateActivityCount + -可以执使用一个方法实现哈多传递一个参数
@Override
public void sekillActivityCountAdd(Collection<Long> ids) {
seckillTimeMapper.sekillActivityCountAdd(ids);
public void sekillActivityCountIncr(Collection<Long> ids) {
seckillTimeMapper.updateActivityCount(ids, "+", 1);
}
@Override
public void sekillActivityCountReduce(Collection<Long> ids) {
seckillTimeMapper.sekillActivityCountReduce(ids);
public void sekillActivityCountDecr(Collection<Long> ids) {
seckillTimeMapper.updateActivityCount(ids, "-", 1);
}
}

View File

@ -51,10 +51,10 @@
<dict-tag :type="DICT_TYPE.PROMOTION_ACTIVITY_STATUS" :value="scope.row.status" />
</template>
</el-table-column>
<el-table-column label="参与场次" prop="timeId" width="250">
<el-table-column label="参与场次" prop="timeIds" width="250">
<template slot-scope="scope">
<span v-for="item in seckillTimeList" :key="item.id"
v-if="scope.row.timeId.indexOf(item.id) !== -1">
v-if="scope.row.timeIds.includes(item.id)">
<el-tag style="margin:4px;" size="small">{{ item.name }}</el-tag>
</span>
</template>
@ -107,7 +107,7 @@
<el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
</el-form-item>
<el-form-item label="场次选择">
<el-select v-model="form.timeId" placeholder="请选择参与场次" clearable size="small" multiple filterable
<el-select v-model="form.timeIds" placeholder="请选择参与场次" clearable size="small" multiple filterable
style="width: 880px">
<el-option v-for="item in seckillTimeList" :key="item.id" :label="item.name" :value="item.id">
<span style="float: left">{{ item.name + ': { ' }} {{ item.startTime }} -- {{ item.endTime +
@ -223,7 +223,7 @@ export default {
form: {
skuIds: [], // SKU
products: [], //
timeId: [], //id
timeIds: [], //id
},
// SKU
productSkus: [],
@ -233,7 +233,7 @@ export default {
status: [{ required: true, message: "活动状态不能为空", trigger: "blur" }],
startAndEndTime: [{ required: true, message: "活动时间不能为空", trigger: "blur" }],
sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
timeId: [{ required: true, message: "秒杀场次不能为空", trigger: "blur" }],
timeIds: [{ required: true, message: "秒杀场次不能为空", trigger: "blur" }],
totalPrice: [{ required: true, message: "订单实付金额,单位:分不能为空", trigger: "blur" }],
}
};
@ -247,6 +247,7 @@ export default {
methods: {
/** 查询列表 */
getList() {
// timeId
const timeId = this.$route.params && this.$route.params.timeId;
if (timeId) {
this.queryParams.timeId = timeId
@ -254,11 +255,13 @@ export default {
this.loading = true;
//
getSeckillActivityPage(this.queryParams).then(response => {
console.log(response.data.list, "查询出的值");
this.list = response.data.list;
this.total = response.data.total;
this.loading = false;
});
if (timeId) {
//
this.$route.params.timeId = undefined
}
// SKU
@ -285,7 +288,7 @@ export default {
startTime: undefined,
endTime: undefined,
sort: undefined,
timeId: [],
timeIds: [],
totalPrice: undefined,
skuIds: [],
products: [],
@ -317,11 +320,9 @@ export default {
this.reset();
const id = row.id;
getSeckillActivity(id).then(response => {
var timeIdList = response.data.timeId.split(',')
this.form = response.data;
//
this.form.startAndEndTime = [response.data.startTime, response.data.endTime];
this.form.timeId = timeIdList.map(item => parseInt(item))
this.form.skuIds = response.data.products.map(item => item.skuId);
this.form.products.forEach(product => {
// SKU
@ -353,7 +354,6 @@ export default {
const data = deepClone(this.form);
data.startTime = this.form.startAndEndTime[0];
data.endTime = this.form.startAndEndTime[1];
data.timeId = data.timeId.toString();
data.products.forEach(product => {
product.stock = product.seckillStock;
product.seckillPrice = product.seckillPrice !== undefined ? product.seckillPrice * 100 : undefined;