Banner:完善 review 提到的问题

This commit is contained in:
puhui999 2023-10-21 17:09:04 +08:00
parent 1bb2010dc0
commit f363c598d8
10 changed files with 150 additions and 32 deletions

View File

@ -0,0 +1,40 @@
package cn.iocoder.yudao.module.promotion.enums.banner;
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
import cn.iocoder.yudao.module.promotion.enums.bargain.BargainRecordStatusEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
/**
* Banner Position 枚举
*
* @author HUIHUI
*/
@AllArgsConstructor
@Getter
public enum BannerPositionEnum implements IntArrayValuable {
HOME_POSITION(1, "首页"),
SECKILL_POSITION(2, "秒杀活动页"),
COMBINATION_POSITION(3, "砍价活动页"),
DISCOUNT_POSITION(4, "限时折扣页"),
REWARD_POSITION(5, "满减送页");
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(BargainRecordStatusEnum::getStatus).toArray();
/**
*
*/
private final Integer position;
/**
* 名字
*/
private final String name;
@Override
public int[] array() {
return ARRAYS;
}
}

View File

@ -27,6 +27,10 @@ public class BannerBaseVO {
@NotNull(message = "图片地址不能为空")
private String picUrl;
@Schema(description = "position", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "position 不能为空")
private Integer position;
@Schema(description = "排序", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "排序不能为空")
private Integer sort;

View File

@ -25,7 +25,6 @@ public class BannerPageReqVO extends PageParam {
@Schema(description = "标题")
private String title;
@Schema(description = "状态")
@InEnum(CommonStatusEnum.class)
private Integer status;

View File

@ -2,18 +2,23 @@ package cn.iocoder.yudao.module.promotion.controller.app.banner;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.promotion.controller.app.banner.vo.AppBannerRespVO;
import cn.iocoder.yudao.module.promotion.convert.banner.BannerConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.banner.BannerDO;
import cn.iocoder.yudao.module.promotion.service.banner.BannerService;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache;
@RestController
@RequestMapping("/promotion/banner")
@ -21,22 +26,39 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Validated
public class AppBannerController {
@Resource
private BannerService bannerService;
/**
* {@link AppBannerRespVO} 缓存通过它异步刷新 {@link #getBannerList0(Integer)} 所要的首页数据
*/
private final LoadingCache<Integer, List<AppBannerRespVO>> bannerListCache = buildAsyncReloadingCache(Duration.ofSeconds(10L),
new CacheLoader<Integer, List<AppBannerRespVO>>() {
@Override
public List<AppBannerRespVO> load(Integer position) {
return getBannerList0(position);
}
});
@GetMapping("/list")
@Operation(summary = "获得 banner 列表")
// todo @芋艿swagger 注解待补全
// TODO @芋艿可以增加缓存提升性能
// TODO @芋艿position = 1 首页position = 10 拼团活动页
@Parameter(name = "position", description = "Banner position", example = "1")
public CommonResult<List<AppBannerRespVO>> getBannerList(@RequestParam("position") Integer position) {
List<AppBannerRespVO> bannerList = new ArrayList<>();
AppBannerRespVO banner1 = new AppBannerRespVO();
banner1.setUrl("https://www.example.com/link1");
banner1.setPicUrl("https://api.java.crmeb.net/crmebimage/public/content/2022/08/04/0f78716213f64bfa83f191d51a832cbf73f6axavoy.jpg");
bannerList.add(banner1);
AppBannerRespVO banner2 = new AppBannerRespVO();
banner2.setUrl("https://www.example.com/link2");
banner2.setPicUrl("https://api.java.crmeb.net/crmebimage/public/content/2023/01/11/be09e755268b43ee90b0db3a3e1b7132r7a6t2wvsm.jpg");
bannerList.add(banner2);
return success(bannerList);
return success(bannerListCache.getUnchecked(position));
}
private List<AppBannerRespVO> getBannerList0(Integer position) {
List<BannerDO> bannerList = bannerService.getBannerListByPosition(position);
return BannerConvert.INSTANCE.convertList01(bannerList);
}
@PutMapping("/add-browse-count")
@Operation(summary = "增加 Banner 点击量")
@Parameter(name = "id", description = "Banner 编号", example = "1024")
public CommonResult<Boolean> addBrowseCount(@RequestParam("id") Long id) {
bannerService.addBannerBrowseCount(id);
return success(true);
}
}

View File

@ -9,6 +9,13 @@ import javax.validation.constraints.NotNull;
@Data
public class AppBannerRespVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED)
private Long id;
@Schema(description = "标题", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "标题不能为空")
private String title;
@Schema(description = "跳转链接", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "跳转链接不能为空")
private String url;

View File

@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.controller.admin.banner.vo.BannerCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.banner.vo.BannerRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.banner.vo.BannerUpdateReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.banner.vo.AppBannerRespVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.banner.BannerDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@ -25,4 +26,6 @@ public interface BannerConvert {
BannerDO convert(BannerUpdateReqVO updateReqVO);
List<AppBannerRespVO> convertList01(List<BannerDO> bannerList);
}

View File

@ -1,6 +1,8 @@
package cn.iocoder.yudao.module.promotion.dal.dataobject.banner;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.promotion.enums.banner.BannerPositionEnum;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
@ -40,14 +42,25 @@ public class BannerDO extends BaseDO {
private Integer sort;
/**
* 状态 {@link cn.iocoder.yudao.framework.common.enums.CommonStatusEnum}
* 状态 {@link CommonStatusEnum}
*/
private Integer status;
/**
* 定位 {@link BannerPositionEnum}
*/
private Integer position;
/**
* 备注
*/
private String memo;
// TODO 芋艿 点击次数&& 其他数据相关
/**
* 点击次数
*/
private Integer browseCount;
// TODO 芋艿 其他数据相关
}

View File

@ -5,8 +5,11 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.promotion.controller.admin.banner.vo.BannerPageReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.banner.BannerDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* Banner Mapper
*
@ -23,4 +26,14 @@ public interface BannerMapper extends BaseMapperX<BannerDO> {
.orderByDesc(BannerDO::getSort));
}
default void updateBrowseCount(Long id) {
update(null, new LambdaUpdateWrapper<BannerDO>()
.eq(BannerDO::getId, id)
.setSql("browse_count = browse_count + 1"));
}
default List<BannerDO> selectBannerListByPosition(Integer position) {
return selectList(new LambdaQueryWrapperX<BannerDO>().eq(BannerDO::getPosition, position));
}
}

View File

@ -46,12 +46,6 @@ public interface BannerService {
*/
BannerDO getBanner(Long id);
/**
* 获得所有 Banner列表
* @return Banner列表
*/
List<BannerDO> getBannerList();
/**
* 获得 Banner 分页
*
@ -60,4 +54,19 @@ public interface BannerService {
*/
PageResult<BannerDO> getBannerPage(BannerPageReqVO pageReqVO);
/**
* 增加 Banner 点击量
*
* @param id Banner编号
*/
void addBannerBrowseCount(Long id);
/**
* 获得 Banner 列表
*
* @param position 定位
* @return Banner 列表
*/
List<BannerDO> getBannerListByPosition(Integer position);
}

View File

@ -65,14 +65,22 @@ public class BannerServiceImpl implements BannerService {
return bannerMapper.selectById(id);
}
@Override
public List<BannerDO> getBannerList() {
return bannerMapper.selectList();
}
@Override
public PageResult<BannerDO> getBannerPage(BannerPageReqVO pageReqVO) {
return bannerMapper.selectPage(pageReqVO);
}
@Override
public void addBannerBrowseCount(Long id) {
// 校验 Banner 是否存在
validateBannerExists(id);
// 增加点击次数
bannerMapper.updateBrowseCount(id);
}
@Override
public List<BannerDO> getBannerListByPosition(Integer position) {
return bannerMapper.selectBannerListByPosition(position);
}
}