diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/banner/BannerPositionEnum.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/banner/BannerPositionEnum.java new file mode 100644 index 000000000..5d99fc1b4 --- /dev/null +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/banner/BannerPositionEnum.java @@ -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; + } + +} diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/banner/vo/BannerBaseVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/banner/vo/BannerBaseVO.java index ff90cb7a0..f840254cc 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/banner/vo/BannerBaseVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/banner/vo/BannerBaseVO.java @@ -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; diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/banner/vo/BannerPageReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/banner/vo/BannerPageReqVO.java index b97008ccd..4aafb9f46 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/banner/vo/BannerPageReqVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/banner/vo/BannerPageReqVO.java @@ -25,7 +25,6 @@ public class BannerPageReqVO extends PageParam { @Schema(description = "标题") private String title; - @Schema(description = "状态") @InEnum(CommonStatusEnum.class) private Integer status; diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/banner/AppBannerController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/banner/AppBannerController.java index 3a4ff8a78..4947e113c 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/banner/AppBannerController.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/banner/AppBannerController.java @@ -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> bannerListCache = buildAsyncReloadingCache(Duration.ofSeconds(10L), + new CacheLoader>() { + + @Override + public List 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> getBannerList(@RequestParam("position") Integer position) { - List 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 getBannerList0(Integer position) { + List 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 addBrowseCount(@RequestParam("id") Long id) { + bannerService.addBannerBrowseCount(id); + return success(true); } } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/banner/vo/AppBannerRespVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/banner/vo/AppBannerRespVO.java index 7656a431d..cc36d87d4 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/banner/vo/AppBannerRespVO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/banner/vo/AppBannerRespVO.java @@ -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; diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/banner/BannerConvert.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/banner/BannerConvert.java index 3e2afeb49..d2d75362e 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/banner/BannerConvert.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/convert/banner/BannerConvert.java @@ -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 convertList01(List bannerList); + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/banner/BannerDO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/banner/BannerDO.java index 585462b95..0a2065e77 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/banner/BannerDO.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/dataobject/banner/BannerDO.java @@ -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 芋艿 其他数据相关 } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/banner/BannerMapper.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/banner/BannerMapper.java index d98375365..74bd3c7da 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/banner/BannerMapper.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/banner/BannerMapper.java @@ -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 { .orderByDesc(BannerDO::getSort)); } + default void updateBrowseCount(Long id) { + update(null, new LambdaUpdateWrapper() + .eq(BannerDO::getId, id) + .setSql("browse_count = browse_count + 1")); + } + + default List selectBannerListByPosition(Integer position) { + return selectList(new LambdaQueryWrapperX().eq(BannerDO::getPosition, position)); + } + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/banner/BannerService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/banner/BannerService.java index d541211be..404f7f5b2 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/banner/BannerService.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/banner/BannerService.java @@ -46,12 +46,6 @@ public interface BannerService { */ BannerDO getBanner(Long id); - /** - * 获得所有 Banner列表 - * @return Banner列表 - */ - List getBannerList(); - /** * 获得 Banner 分页 * @@ -60,4 +54,19 @@ public interface BannerService { */ PageResult getBannerPage(BannerPageReqVO pageReqVO); + /** + * 增加 Banner 点击量 + * + * @param id Banner编号 + */ + void addBannerBrowseCount(Long id); + + /** + * 获得 Banner 列表 + * + * @param position 定位 + * @return Banner 列表 + */ + List getBannerListByPosition(Integer position); + } diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/banner/BannerServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/banner/BannerServiceImpl.java index 013ae8992..46c22f0e2 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/banner/BannerServiceImpl.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/banner/BannerServiceImpl.java @@ -65,14 +65,22 @@ public class BannerServiceImpl implements BannerService { return bannerMapper.selectById(id); } - @Override - public List getBannerList() { - return bannerMapper.selectList(); - } - @Override public PageResult getBannerPage(BannerPageReqVO pageReqVO) { return bannerMapper.selectPage(pageReqVO); } + @Override + public void addBannerBrowseCount(Long id) { + // 校验 Banner 是否存在 + validateBannerExists(id); + // 增加点击次数 + bannerMapper.updateBrowseCount(id); + } + + @Override + public List getBannerListByPosition(Integer position) { + return bannerMapper.selectBannerListByPosition(position); + } + }