promotion:优化砍价活动的分页结果

This commit is contained in:
YunaiV 2023-10-05 02:18:14 +08:00
parent 16e436a0f5
commit cd45be1fe2
15 changed files with 191 additions and 44 deletions

View File

@ -5,13 +5,13 @@ import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityUpdateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.*;
import cn.iocoder.yudao.module.promotion.convert.bargain.BargainActivityConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainActivityDO;
import cn.iocoder.yudao.module.promotion.enums.bargain.BargainRecordStatusEnum;
import cn.iocoder.yudao.module.promotion.service.bargain.BargainActivityService;
import cn.iocoder.yudao.module.promotion.service.bargain.BargainHelpService;
import cn.iocoder.yudao.module.promotion.service.bargain.BargainRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@ -21,7 +21,9 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@ -33,7 +35,12 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.
public class BargainActivityController {
@Resource
private BargainActivityService activityService;
private BargainActivityService bargainActivityService;
@Resource
private BargainRecordService bargainRecordService;
@Resource
private BargainHelpService bargainHelpService;
@Resource
private ProductSpuApi spuApi;
@ -41,14 +48,14 @@ public class BargainActivityController {
@Operation(summary = "创建砍价活动")
@PreAuthorize("@ss.hasPermission('promotion:bargain-activity:create')")
public CommonResult<Long> createBargainActivity(@Valid @RequestBody BargainActivityCreateReqVO createReqVO) {
return success(activityService.createBargainActivity(createReqVO));
return success(bargainActivityService.createBargainActivity(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新砍价活动")
@PreAuthorize("@ss.hasPermission('promotion:bargain-activity:update')")
public CommonResult<Boolean> updateBargainActivity(@Valid @RequestBody BargainActivityUpdateReqVO updateReqVO) {
activityService.updateBargainActivity(updateReqVO);
bargainActivityService.updateBargainActivity(updateReqVO);
return success(true);
}
@ -57,7 +64,7 @@ public class BargainActivityController {
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('promotion:bargain-activity:delete')")
public CommonResult<Boolean> deleteBargainActivity(@RequestParam("id") Long id) {
activityService.deleteBargainActivity(id);
bargainActivityService.deleteBargainActivity(id);
return success(true);
}
@ -66,23 +73,30 @@ public class BargainActivityController {
@Parameter(name = "id", description = "编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('promotion:bargain-activity:query')")
public CommonResult<BargainActivityRespVO> getBargainActivity(@RequestParam("id") Long id) {
return success(BargainActivityConvert.INSTANCE.convert(activityService.getBargainActivity(id)));
return success(BargainActivityConvert.INSTANCE.convert(bargainActivityService.getBargainActivity(id)));
}
@GetMapping("/page")
@Operation(summary = "获得砍价活动分页")
@PreAuthorize("@ss.hasPermission('promotion:bargain-activity:query')")
public CommonResult<PageResult<BargainActivityRespVO>> getBargainActivityPage(
public CommonResult<PageResult<BargainActivityPageItemRespVO>> getBargainActivityPage(
@Valid BargainActivityPageReqVO pageVO) {
// 查询砍价活动
PageResult<BargainActivityDO> pageResult = activityService.getBargainActivityPage(pageVO);
PageResult<BargainActivityDO> pageResult = bargainActivityService.getBargainActivityPage(pageVO);
if (CollUtil.isEmpty(pageResult.getList())) {
return success(PageResult.empty(pageResult.getTotal()));
}
// 拼接数据
List<ProductSpuRespDTO> spuList = spuApi.getSpuList(convertList(pageResult.getList(), BargainActivityDO::getSpuId));
return success(BargainActivityConvert.INSTANCE.convertPage(pageResult, spuList));
// 统计数据
Collection<Long> activityIds = convertList(pageResult.getList(), BargainActivityDO::getId);
Map<Long, Integer> recordUserCountMap = bargainRecordService.getBargainRecordUserCountMap(activityIds, null);
Map<Long, Integer> recordSuccessUserCountMap = bargainRecordService.getBargainRecordUserCountMap(activityIds,
BargainRecordStatusEnum.SUCCESS.getStatus());
Map<Long, Integer> helpUserCountMap = bargainHelpService.getBargainHelpUserCountMap(activityIds);
return success(BargainActivityConvert.INSTANCE.convertPage(pageResult, spuList,
recordUserCountMap, recordSuccessUserCountMap, helpUserCountMap));
}
}

View File

@ -36,7 +36,7 @@ public class BargainActivityBaseVO {
@Schema(description = "砍价底价", requiredMode = Schema.RequiredMode.REQUIRED, example = "23")
@NotNull(message = "砍价底价不能为空")
private Integer bargainPrice;
private Integer bargainMinPrice;
@Schema(description = "活动库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "23")
@NotNull(message = "活动库存不能为空")

View File

@ -0,0 +1,46 @@
package cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
@Schema(description = "管理后台 - 砍价活动的分页项 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class BargainActivityPageItemRespVO extends BargainActivityBaseVO {
@Schema(description = "活动编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "22901")
private Long id;
@Schema(description = "商品名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "618大促")
private String spuName;
@Schema(description = "商品主图", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/xx.png")
private String picUrl;
@Schema(description = "活动状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
@NotNull(message = "活动状态不能为空")
private Integer status;
@Schema(description = "活动总库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "23")
private Integer totalStock;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2022-07-01 23:59:59")
private LocalDateTime createTime;
// ========== 统计字段 ==========
@Schema(description = "总砍价的用户数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "999")
private Integer recordUserCount;
@Schema(description = "成功砍价的用户数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "500")
private Integer recordSuccessUserCount;
@Schema(description = "帮助砍价的用户数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "888")
private Integer helpUserCount;
}

View File

@ -5,7 +5,6 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
@Schema(description = "管理后台 - 砍价活动 Response VO")
@Data
@ -13,23 +12,13 @@ import java.time.LocalDateTime;
@ToString(callSuper = true)
public class BargainActivityRespVO extends BargainActivityBaseVO {
@Schema(description = "商品名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "618大促")
private String spuName;
@Schema(description = "商品主图", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/xx.png")
private String picUrl;
@Schema(description = "活动编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "22901")
private Long id;
@Schema(description = "活动状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
private Integer status;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "2022-07-01 23:59:59")
private LocalDateTime createTime;
@Schema(description = "砍价成功数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "999")
private Integer successCount;
@Schema(description = "活动状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
@NotNull(message = "活动状态不能为空")
private Integer status;
}

View File

@ -22,6 +22,7 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Validated
public class AppActivityController {
// TODO @puhui999可以实现下
@GetMapping("/list-by-spu-id")
@Operation(summary = "获得单个商品,近期参与的每个活动") // 每种活动只返回一个
@Parameter(name = "spuId", description = "商品编号", required = true)
@ -41,6 +42,7 @@ public class AppActivityController {
return success(randomList);
}
// TODO @puhui999可以实现下
@GetMapping("/list-by-spu-ids")
@Operation(summary = "获得多个商品,近期参与的每个活动") // 每种活动只返回一个key SPU 编号
@Parameter(name = "spuIds", description = "商品编号数组", required = true)

View File

@ -32,6 +32,7 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.
@RequestMapping("/promotion/bargain-activity")
@Validated
public class AppBargainActivityController {
@Resource
private BargainActivityService bargainActivityService;
@Resource

View File

@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.dict.core.util.DictFrameworkUtils;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.product.enums.DictTypeConstants;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityBaseVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityPageItemRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.bargain.vo.BargainActivityUpdateReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.activity.AppBargainActivityDetailRespVO;
@ -38,19 +39,23 @@ public interface BargainActivityConvert {
List<BargainActivityRespVO> convertList(List<BargainActivityDO> list);
PageResult<BargainActivityRespVO> convertPage(PageResult<BargainActivityDO> page);
PageResult<BargainActivityPageItemRespVO> convertPage(PageResult<BargainActivityDO> page);
default PageResult<BargainActivityRespVO> convertPage(PageResult<BargainActivityDO> page, List<ProductSpuRespDTO> spuList) {
PageResult<BargainActivityRespVO> result = convertPage(page);
default PageResult<BargainActivityPageItemRespVO> convertPage(PageResult<BargainActivityDO> page, List<ProductSpuRespDTO> spuList,
Map<Long, Integer> recordUserCountMap, Map<Long, Integer> recordSuccessUserCountMap,
Map<Long, Integer> helpUserCountMap) {
PageResult<BargainActivityPageItemRespVO> result = convertPage(page);
// 拼接关联属性
Map<Long, ProductSpuRespDTO> spuMap = convertMap(spuList, ProductSpuRespDTO::getId);
List<BargainActivityRespVO> list = CollectionUtils.convertList(result.getList(), item -> {
result.getList().forEach(item -> {
findAndThen(spuMap, item.getSpuId(), spu -> {
item.setPicUrl(spu.getPicUrl()).setSpuName(spu.getName());
});
return item;
// 设置统计字段
item.setRecordUserCount(recordUserCountMap.getOrDefault(item.getId(), 0))
.setRecordSuccessUserCount(recordSuccessUserCountMap.getOrDefault(item.getId(), 0))
.setHelpUserCount(helpUserCountMap.getOrDefault(item.getId(), 0));
});
result.setList(list);
return result;
}

View File

@ -67,10 +67,15 @@ public class BargainActivityDO extends BaseDO {
* 砍价底价单位
*/
private Integer bargainMinPrice;
/**
* 砍价活动库存
* 砍价库存(剩余库存砍价时扣减)
*/
private Integer stock;
/**
* 砍价总库存
*/
private Integer totalStock;
/**
* 砍价人数
@ -99,11 +104,4 @@ public class BargainActivityDO extends BaseDO {
*/
private Integer randomMaxPrice;
// ========== 统计字段 ==========
/**
* 砍价成功数量
*/
private Integer successCount;
}

View File

@ -1,10 +1,19 @@
package cn.iocoder.yudao.module.promotion.dal.mysql.bargain;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainHelpDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@Mapper
public interface BargainHelpMapper extends BaseMapperX<BargainHelpDO> {
@ -24,4 +33,20 @@ public interface BargainHelpMapper extends BaseMapperX<BargainHelpDO> {
.eq(BargainHelpDO::getRecordId, recordId));
}
default Map<Long, Integer> selectCountByActivityId(Collection<Long> activityIds) {
// SQL count 查询
List<Map<String, Object>> result = selectMaps(new QueryWrapper<BargainHelpDO>()
.select("COUNT(DISTINCT(user_id)) AS userCount, activity_id AS activityId")
.in("activity_id", activityIds)
.groupBy("activity_id"));
if (CollUtil.isEmpty(result)) {
return Collections.emptyMap();
}
// 转换数据
return CollectionUtils.convertMap(result,
record -> MapUtil.getLong(record, "activityId"),
record -> MapUtil.getInt(record, "userCount" ));
}
}

View File

@ -1,11 +1,18 @@
package cn.iocoder.yudao.module.promotion.dal.mysql.bargain;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainRecordDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* 砍价记录 Mapper
@ -42,4 +49,20 @@ public interface BargainRecordMapper extends BaseMapperX<BargainRecordDO> {
.eq(BargainRecordDO::getBargainPrice, whereBargainPrice));
}
default Map<Long, Integer> selectCountByActivityIdsAndStatus(Collection<Long> activityIds, Integer status) {
// SQL count 查询
List<Map<String, Object>> result = selectMaps(new QueryWrapper<BargainRecordDO>()
.select("COUNT(DISTINCT(user_id)) AS userCount, activity_id AS activityId")
.in("activity_id", activityIds)
.eq(status != null, "status", status)
.groupBy("activity_id"));
if (CollUtil.isEmpty(result)) {
return Collections.emptyMap();
}
// 转换数据
return CollectionUtils.convertMap(result,
record -> MapUtil.getLong(record, "activityId"),
record -> MapUtil.getInt(record, "userCount" ));
}
}

View File

@ -52,7 +52,8 @@ public class BargainActivityServiceImpl implements BargainActivityService {
// 插入砍价活动
BargainActivityDO activityDO = BargainActivityConvert.INSTANCE.convert(createReqVO)
.setStatus(CommonStatusEnum.ENABLE.getStatus()).setSuccessCount(0);
.setTotalStock(createReqVO.getStock())
.setStatus(CommonStatusEnum.ENABLE.getStatus());
bargainActivityMapper.insert(activityDO);
return activityDO.getId();
}
@ -61,9 +62,9 @@ public class BargainActivityServiceImpl implements BargainActivityService {
@Transactional(rollbackFor = Exception.class)
public void updateBargainActivity(BargainActivityUpdateReqVO updateReqVO) {
// 校验存在
BargainActivityDO activityDO = validateBargainActivityExists(updateReqVO.getId());
BargainActivityDO activity = validateBargainActivityExists(updateReqVO.getId());
// 校验状态
if (ObjectUtil.equal(activityDO.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
if (ObjectUtil.equal(activity.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
throw exception(BARGAIN_ACTIVITY_STATUS_DISABLE);
}
// 校验商品冲突
@ -73,6 +74,9 @@ public class BargainActivityServiceImpl implements BargainActivityService {
// 更新
BargainActivityDO updateObj = BargainActivityConvert.INSTANCE.convert(updateReqVO);
if (updateObj.getStock() > activity.getTotalStock()) { // 如果更新的库存大于原来的库存则更新总库存
updateObj.setTotalStock(updateObj.getStock());
}
bargainActivityMapper.updateById(updateObj);
}

View File

@ -3,6 +3,9 @@ package cn.iocoder.yudao.module.promotion.service.bargain;
import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.help.AppBargainHelpCreateReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainHelpDO;
import java.util.Collection;
import java.util.Map;
/**
* 砍价助力 Service 接口
*
@ -19,4 +22,12 @@ public interface BargainHelpService {
*/
BargainHelpDO createBargainHelp(Long userId, AppBargainHelpCreateReqVO reqVO);
/**
* 获得助力人数 Map
*
* @param activityIds 活动编号
* @return 助力人数 Map
*/
Map<Long, Integer> getBargainHelpUserCountMap(Collection<Long> activityIds);
}

View File

@ -15,6 +15,9 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
@ -99,4 +102,9 @@ public class BargainHelpServiceImpl implements BargainHelpService {
return reducePrice;
}
@Override
public Map<Long, Integer> getBargainHelpUserCountMap(Collection<Long> activityIds) {
return bargainHelpMapper.selectCountByActivityId(activityIds);
}
}

View File

@ -5,6 +5,10 @@ import cn.iocoder.yudao.module.promotion.api.bargain.dto.BargainValidateJoinResp
import cn.iocoder.yudao.module.promotion.controller.app.bargain.vo.record.AppBargainRecordCreateReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.bargain.BargainRecordDO;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Map;
/**
* 砍价记录 service 接口
*
@ -55,4 +59,13 @@ public interface BargainRecordService {
*/
BargainRecordDO getBargainRecord(Long id);
/**
* 获得砍价人数 Map
*
* @param activityIds 活动编号
* @param status 砍价记录状态
* @return 砍价人数 Map
*/
Map<Long, Integer> getBargainRecordUserCountMap(Collection<Long> activityIds, @Nullable Integer status);
}

View File

@ -12,7 +12,10 @@ import cn.iocoder.yudao.module.promotion.enums.bargain.BargainRecordStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -92,4 +95,9 @@ public class BargainRecordServiceImpl implements BargainRecordService {
return bargainRecordMapper.selectById(id);
}
@Override
public Map<Long, Integer> getBargainRecordUserCountMap(Collection<Long> activityIds, @Nullable Integer status) {
return bargainRecordMapper.selectCountByActivityIdsAndStatus(activityIds, status);
}
}