mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 15:21:53 +08:00
Banner:完善 review 提到的问题
This commit is contained in:
parent
1bb2010dc0
commit
f363c598d8
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
@ -25,7 +25,6 @@ public class BannerPageReqVO extends PageParam {
|
||||
@Schema(description = "标题")
|
||||
private String title;
|
||||
|
||||
|
||||
@Schema(description = "状态")
|
||||
@InEnum(CommonStatusEnum.class)
|
||||
private Integer status;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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 芋艿 其他数据相关
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user