BannerServiceImpl.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.ylx.banner.service.impl;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.ylx.banner.domain.vo.BannerVO;
  7. import com.ylx.common.exception.ServiceException;
  8. import com.ylx.common.utils.uuid.IdUtils;
  9. import com.ylx.banner.domain.Banner;
  10. import com.ylx.giftCard.domain.GiftCard;
  11. import com.ylx.giftCard.domain.vo.GiftCardVO;
  12. import com.ylx.massage.domain.dto.TLbtStatusDTO;
  13. import com.ylx.banner.mapper.BannerMapper;
  14. import com.ylx.banner.service.BannerService;
  15. import com.ylx.common.utils.DateUtils;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.springframework.stereotype.Service;
  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. /**
  21. * 轮播图 服务实现类
  22. */
  23. @Service
  24. public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {
  25. private static final int STATUS_HIDE = 0;
  26. private static final int STATUS_SHOW = 1;
  27. private static final long MAX_SHOW_COUNT = 5L;
  28. private static final long MIN_SHOW_COUNT = 1L;
  29. private static final int NOT_DELETE = 0;
  30. @Override
  31. public Boolean addOrUpdate(Banner lbt) {
  32. if (StringUtils.isBlank(lbt.getImgUrl())) {
  33. throw new ServiceException("图片不能为空");
  34. }
  35. if (lbt.getSort() == null) {
  36. throw new ServiceException("序号不能为空");
  37. }
  38. Integer i = lbt.getSort();
  39. LambdaQueryWrapper<Banner> query = new LambdaQueryWrapper<Banner>().eq(Banner::getSort, i);
  40. Banner one = getOne(query);
  41. if (StringUtils.isBlank(lbt.getId())) {
  42. //新增
  43. lbt.setId(IdUtils.simpleUUID());
  44. if (one != null) {
  45. batchLbt(i);
  46. }
  47. } else {
  48. //修改
  49. query.ne(Banner::getId, lbt.getId());
  50. if (getOne(query) != null) {
  51. batchLbt(i);
  52. }
  53. }
  54. return this.saveOrUpdate(lbt);
  55. }
  56. /**
  57. *
  58. * @param i
  59. */
  60. private void batchLbt(Integer i) {
  61. LambdaQueryWrapper<Banner> query1 = new LambdaQueryWrapper<Banner>().ge(Banner::getSort, i);
  62. List<Banner> lbts = list(query1);
  63. lbts.forEach(tLbt1 -> tLbt1.setSort(tLbt1.getSort() + 1));
  64. updateBatchById(lbts);
  65. }
  66. @Override
  67. public Boolean del(Banner banner) {
  68. if (banner == null) {
  69. throw new ServiceException("参数不能为空");
  70. }
  71. if (StringUtils.isBlank(banner.getId())) {
  72. throw new ServiceException("ID不能为空");
  73. }
  74. Banner exists = baseMapper.selectLbtById(banner.getId());
  75. if (exists == null) {
  76. throw new ServiceException("轮播图不存在");
  77. }
  78. if (Integer.valueOf(STATUS_SHOW).equals(exists.getStatus())) {
  79. throw new ServiceException("当前banner已显示,不可删除!");
  80. }
  81. boolean removed = this.removeById(banner.getId());
  82. if (!removed) {
  83. throw new ServiceException("轮播图删除失败");
  84. }
  85. return true;
  86. }
  87. @Override
  88. public Boolean updateStatus(TLbtStatusDTO dto) {
  89. if (dto == null) {
  90. throw new ServiceException("参数不能为空");
  91. }
  92. if (StringUtils.isBlank(dto.getId())) {
  93. throw new ServiceException("ID不能为空");
  94. }
  95. if (dto.getStatus() == null || (dto.getStatus() != STATUS_HIDE && dto.getStatus() != STATUS_SHOW)) {
  96. throw new ServiceException("显示状态值不正确");
  97. }
  98. Banner exists = baseMapper.selectLbtById(dto.getId());
  99. if (exists == null) {
  100. throw new ServiceException("轮播图不存在");
  101. }
  102. if (dto.getStatus().equals(exists.getStatus())) {
  103. return true;
  104. }
  105. Long showCount = baseMapper.countByStatus(STATUS_SHOW);
  106. if (STATUS_SHOW == dto.getStatus() && showCount >= MAX_SHOW_COUNT) {
  107. throw new ServiceException("当前显示数量已达5条上限!");
  108. }
  109. if (STATUS_HIDE == dto.getStatus() && showCount <= MIN_SHOW_COUNT) {
  110. throw new ServiceException("当前banner已是最后一条显示,不可关闭!");
  111. }
  112. Banner update = new Banner();
  113. update.setId(dto.getId());
  114. update.setStatus(dto.getStatus());
  115. update.setUpdateTime(DateUtils.getNowDate());
  116. int rows = baseMapper.updateLbtById(update);
  117. if (rows <= 0) {
  118. throw new ServiceException("轮播图显示状态修改失败");
  119. }
  120. return true;
  121. }
  122. @Override
  123. public Page<BannerVO> page(Page<Banner> page) {
  124. LambdaQueryWrapper<Banner> wrapper = new LambdaQueryWrapper<>();
  125. wrapper.eq(Banner::getIsDelete, NOT_DELETE);
  126. wrapper.eq(Banner::getStatus, STATUS_SHOW);
  127. wrapper.orderByDesc(Banner::getSort);
  128. Page<Banner> bannerPage = this.baseMapper.selectPage(page, wrapper);
  129. Page<BannerVO> pageData = new Page<>(
  130. bannerPage.getCurrent(),
  131. bannerPage.getSize(),
  132. bannerPage.getTotal()
  133. );
  134. if (CollectionUtil.isNotEmpty(bannerPage.getRecords())) {
  135. List<BannerVO> voList = bannerPage.getRecords().stream()
  136. .map(BannerVO::new)
  137. .collect(Collectors.toList());
  138. pageData.setRecords(voList);
  139. }
  140. return pageData;
  141. }
  142. }