diff --git a/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/convert/banner/BannerConvert.java b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/convert/banner/BannerConvert.java new file mode 100644 index 000000000..48c89ce45 --- /dev/null +++ b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/convert/banner/BannerConvert.java @@ -0,0 +1,30 @@ +package cn.iocoder.mall.promotionservice.convert.banner; + +import cn.iocoder.mall.promotionservice.dal.mysql.dataobject.banner.BannerDO; +import cn.iocoder.mall.promotionservice.service.banner.bo.BannerAddBO; +import cn.iocoder.mall.promotionservice.service.banner.bo.BannerBO; +import cn.iocoder.mall.promotionservice.service.banner.bo.BannerUpdateBO; +import org.mapstruct.Mapper; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +@Mapper +public interface BannerConvert { + + BannerConvert INSTANCE = Mappers.getMapper(BannerConvert.class); + + @Mappings({}) + BannerBO convertToBO(BannerDO banner); + + @Mappings({}) + List convertToBO(List bannerList); + + @Mappings({}) + BannerDO convert(BannerAddBO bannerAddDTO); + + @Mappings({}) + BannerDO convert(BannerUpdateBO bannerUpdateDTO); + +} \ No newline at end of file diff --git a/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/BannerService.java b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/BannerService.java new file mode 100644 index 000000000..f02e28f8f --- /dev/null +++ b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/BannerService.java @@ -0,0 +1,98 @@ +package cn.iocoder.mall.promotionservice.service.banner; + +import cn.iocoder.common.framework.enums.CommonStatusEnum; +import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil; +import cn.iocoder.mall.mybatis.core.enums.DeletedStatusEnum; +import cn.iocoder.mall.promotion.api.enums.PromotionActivityTypeEnum; +import cn.iocoder.mall.promotion.api.enums.PromotionErrorCodeEnum; +import cn.iocoder.mall.promotion.api.enums.RangeTypeEnum; +import cn.iocoder.mall.promotion.api.rpc.banner.dto.BannerPageDTO; +import cn.iocoder.mall.promotionservice.convert.activity.PromotionActivityConvert; +import cn.iocoder.mall.promotionservice.convert.banner.BannerConvert; +import cn.iocoder.mall.promotionservice.dal.mysql.dataobject.activity.PromotionActivityDO; +import cn.iocoder.mall.promotionservice.dal.mysql.dataobject.banner.BannerDO; +import cn.iocoder.mall.promotionservice.dal.mysql.mapper.activity.PromotionActivityMapper; +import cn.iocoder.mall.promotionservice.dal.mysql.mapper.banner.BannerMapper; +import cn.iocoder.mall.promotionservice.service.activity.bo.PromotionActivityPageBO; +import cn.iocoder.mall.promotionservice.service.banner.bo.BannerAddBO; +import cn.iocoder.mall.promotionservice.service.banner.bo.BannerBO; +import cn.iocoder.mall.promotionservice.service.banner.bo.BannerPageBO; +import cn.iocoder.mall.promotionservice.service.banner.bo.BannerUpdateBO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; +import org.springframework.validation.annotation.Validated; + +import java.util.*; + + +@Service +@Validated +public class BannerService { + + @Autowired + private BannerMapper bannerMapper; + + public List getBannerListByStatus(Integer status) { + List banners = bannerMapper.selectListByStatus(status); + return BannerConvert.INSTANCE.convertToBO(banners); + } + + public BannerPageBO getBannerPage(BannerPageDTO bannerPageDTO) { + BannerPageBO bannerPageBO = new BannerPageBO(); + // 查询分页数据 + int offset = (bannerPageDTO.getPageNo() - 1) * bannerPageDTO.getPageSize(); + bannerPageBO.setList(BannerConvert.INSTANCE.convertToBO(bannerMapper.selectListByTitleLike(bannerPageDTO.getTitle(), + offset, bannerPageDTO.getPageSize()))); + // 查询分页总数 + bannerPageBO.setTotal(bannerMapper.selectCountByTitleLike(bannerPageDTO.getTitle())); + return bannerPageBO; + } + + public BannerBO addBanner(Integer adminId, BannerAddBO bannerAddDTO) { + // 保存到数据库 + BannerDO banner = BannerConvert.INSTANCE.convert(bannerAddDTO).setStatus(CommonStatusEnum.ENABLE.getValue()); + banner.setDeleted(DeletedStatusEnum.DELETED_NO.getValue()).setCreateTime(new Date()); + bannerMapper.insert(banner); + // 返回成功 + return BannerConvert.INSTANCE.convertToBO(banner); + } + + public Boolean updateBanner(Integer adminId, BannerUpdateBO bannerUpdateDTO) { + // 校验 Banner 存在 + if (bannerMapper.selectById(bannerUpdateDTO.getId()) == null) { + throw ServiceExceptionUtil.exception(PromotionErrorCodeEnum.BANNER_NOT_EXISTS.getCode()); + } + // 更新到数据库 + BannerDO updateBanner = BannerConvert.INSTANCE.convert(bannerUpdateDTO); + bannerMapper.update(updateBanner); + // 返回成功 + return true; + } + + public Boolean updateBannerStatus(Integer adminId, Integer bannerId, Integer status) { + // 校验 Banner 存在 + if (bannerMapper.selectById(bannerId) == null) { + throw ServiceExceptionUtil.exception(PromotionErrorCodeEnum.BANNER_NOT_EXISTS.getCode()); + } + // 更新到数据库 + BannerDO updateBanner = new BannerDO().setId(bannerId).setStatus(status); + bannerMapper.update(updateBanner); + // 返回成功 + return true; + } + + public Boolean deleteBanner(Integer adminId, Integer bannerId) { + // 校验 Banner 存在 + if (bannerMapper.selectById(bannerId) == null) { + throw ServiceExceptionUtil.exception(PromotionErrorCodeEnum.BANNER_NOT_EXISTS.getCode()); + } + // 更新到数据库 + BannerDO updateBanner = new BannerDO().setId(bannerId); + updateBanner.setDeleted(DeletedStatusEnum.DELETED_YES.getValue()); + bannerMapper.update(updateBanner); + // 返回成功 + return true; + } + +} diff --git a/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerAddBO.java b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerAddBO.java new file mode 100644 index 000000000..c796ee88a --- /dev/null +++ b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerAddBO.java @@ -0,0 +1,35 @@ +package cn.iocoder.mall.promotionservice.service.banner.bo; + +import lombok.Data; +import lombok.experimental.Accessors; +import org.hibernate.validator.constraints.Length; +import org.hibernate.validator.constraints.URL; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +/** + * Banner 添加 DTO + */ +@Data +@Accessors(chain = true) +public class BannerAddBO implements Serializable { + + @NotEmpty(message = "标题不能为空") + @Length(min = 2, max = 32, message = "标题长度为 2-32 位") + private String title; + @NotEmpty(message = "跳转链接不能为空") + @URL(message = "跳转链接格式不正确") + @Length(max = 255, message = "跳转链接最大长度为 255 位") + private String url; + @NotEmpty(message = "图片链接不能为空") + @URL(message = "图片链接格式不正确") + @Length(max = 255, message = "图片链接最大长度为 255 位") + private String picUrl; + @NotNull(message = "排序不能为空") + private Integer sort; + @Length(max = 255, message = "备注最大长度为 255 位") + private String memo; + +} diff --git a/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerBO.java b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerBO.java new file mode 100644 index 000000000..68ae2062b --- /dev/null +++ b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerBO.java @@ -0,0 +1,49 @@ +package cn.iocoder.mall.promotionservice.service.banner.bo; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; + +/** + * Banner BO + */ +@Data +@Accessors(chain = true) +public class BannerBO implements Serializable { + + /** + * 编号 + */ + private Integer id; + /** + * 标题 + */ + private String title; + /** + * 跳转链接 + */ + private String url; + /** + * 图片链接 + */ + private String picUrl; + /** + * 排序 + */ + private Integer sort; + /** + * 状态 + */ + private Integer status; + /** + * 备注 + */ + private String memo; + /** + * 创建时间 + */ + private Date createTime; + +} diff --git a/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerPageBO.java b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerPageBO.java new file mode 100644 index 000000000..7db1b479b --- /dev/null +++ b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerPageBO.java @@ -0,0 +1,26 @@ +package cn.iocoder.mall.promotionservice.service.banner.bo; + +import cn.iocoder.mall.promotion.api.rpc.banner.dto.BannerRespDTO; +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.List; + +/** + * Banner 分页 DTO + */ +@Data +@Accessors(chain = true) +public class BannerPageBO implements Serializable { + + /** + * Banner 数组 + */ + private List list; + /** + * 总量 + */ + private Integer total; +} diff --git a/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerUpdateBO.java b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerUpdateBO.java new file mode 100644 index 000000000..e48d5262b --- /dev/null +++ b/promotion-service-project/promotion-service-app/src/main/java/cn/iocoder/mall/promotionservice/service/banner/bo/BannerUpdateBO.java @@ -0,0 +1,37 @@ +package cn.iocoder.mall.promotionservice.service.banner.bo; + +import lombok.Data; +import lombok.experimental.Accessors; +import org.hibernate.validator.constraints.Length; +import org.hibernate.validator.constraints.URL; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +/** + * Banner 更新 DTO + */ +@Data +@Accessors(chain = true) +public class BannerUpdateBO implements Serializable { + + @NotNull(message = "编号不能为空") + private Integer id; + @NotEmpty(message = "标题不能为空") + @Length(min = 2, max = 32, message = "标题长度为 2-32 位") + private String title; + @NotEmpty(message = "跳转链接不能为空") + @URL(message = "跳转链接格式不正确") + @Length(max = 255, message = "跳转链接最大长度为 255 位") + private String url; + @NotEmpty(message = "图片链接不能为空") + @URL(message = "图片链接格式不正确") + @Length(max = 255, message = "图片链接最大长度为 255 位") + private String picUrl; + @NotNull(message = "排序不能为空") + private Integer sort; + @Length(max = 255, message = "备注最大长度为 255 位") + private String memo; + +}