Просмотр исходного кода

优化轮播图相关接口的代码

jinshihui 5 дней назад
Родитель
Сommit
d63f13ab1c

+ 22 - 0
nightFragrance-admin/src/main/java/com/ylx/web/controller/massage/TLbtController.java

@@ -9,6 +9,7 @@ import com.ylx.common.core.domain.R;
 import com.ylx.common.enums.BusinessType;
 import com.ylx.common.exception.ServiceException;
 import com.ylx.massage.domain.TLbt;
+import com.ylx.massage.domain.dto.TLbtStatusDTO;
 import com.ylx.massage.service.TLbtService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -119,6 +120,27 @@ public class TLbtController {
         }
     }
 
+    /**
+     * 打开或者关闭轮播图显示。
+     *
+     * @param dto 显示状态修改参数
+     * @return R
+     */
+    @Log(title = "轮播图显示状态", businessType = BusinessType.UPDATE)
+    @RequestMapping(value = "/status", method = RequestMethod.PUT)
+    @ApiOperation("打开或者关闭轮播图显示")
+    public R updateStatus(@RequestBody TLbtStatusDTO dto) {
+        try {
+            return R.ok(lbtService.updateStatus(dto));
+        } catch (ServiceException s) {
+            log.error(s.toString());
+            return R.fail(s.getMessage());
+        } catch (Exception e) {
+            log.error(e.toString());
+            return R.fail("系统异常");
+        }
+    }
+
     /**
      * 根据id查询轮播图
      *

+ 25 - 0
nightFragrance-massage/src/main/java/com/ylx/massage/domain/dto/TLbtStatusDTO.java

@@ -0,0 +1,25 @@
+package com.ylx.massage.domain.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * 轮播图显示状态修改参数。
+ */
+@Data
+@ApiModel(value = "TLbtStatusDTO", description = "轮播图显示状态修改参数")
+public class TLbtStatusDTO {
+
+    /**
+     * 轮播图ID。
+     */
+    @ApiModelProperty("轮播图ID")
+    private String id;
+
+    /**
+     * 显示状态:0-隐藏,1-显示。
+     */
+    @ApiModelProperty("显示状态:0-隐藏,1-显示")
+    private Integer status;
+}

+ 30 - 0
nightFragrance-massage/src/main/java/com/ylx/massage/mapper/TLbtMapper.java

@@ -1,5 +1,6 @@
 package com.ylx.massage.mapper;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Mapper;
 import com.ylx.massage.domain.TLbt;
@@ -10,4 +11,33 @@ import com.ylx.massage.domain.TLbt;
 @Mapper
 public interface TLbtMapper extends BaseMapper<TLbt> {
 
+    /**
+     * 根据ID查询轮播图。
+     *
+     * @param id 轮播图ID
+     * @return 轮播图
+     */
+    default TLbt selectLbtById(String id) {
+        return selectById(id);
+    }
+
+    /**
+     * 统计指定显示状态的轮播图数量。
+     *
+     * @param status 显示状态
+     * @return 数量
+     */
+    default Long countByStatus(Integer status) {
+        return selectCount(new LambdaQueryWrapper<TLbt>().eq(TLbt::getStatus, status));
+    }
+
+    /**
+     * 根据ID更新轮播图。
+     *
+     * @param lbt 轮播图
+     * @return 影响行数
+     */
+    default int updateLbtById(TLbt lbt) {
+        return updateById(lbt);
+    }
 }

+ 3 - 0
nightFragrance-massage/src/main/java/com/ylx/massage/service/TLbtService.java

@@ -2,6 +2,7 @@ package com.ylx.massage.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.ylx.massage.domain.TLbt;
+import com.ylx.massage.domain.dto.TLbtStatusDTO;
 
 /**
  * <p>
@@ -16,4 +17,6 @@ public interface TLbtService extends IService<TLbt> {
     Boolean addOrUpdate(TLbt lbt);
 
     Boolean del(TLbt tLbt);
+
+    Boolean updateStatus(TLbtStatusDTO dto);
 }

+ 45 - 0
nightFragrance-massage/src/main/java/com/ylx/massage/service/impl/TLbtServiceImpl.java

@@ -5,8 +5,10 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ylx.common.exception.ServiceException;
 import com.ylx.common.utils.uuid.IdUtils;
 import com.ylx.massage.domain.TLbt;
+import com.ylx.massage.domain.dto.TLbtStatusDTO;
 import com.ylx.massage.mapper.TLbtMapper;
 import com.ylx.massage.service.TLbtService;
+import com.ylx.common.utils.DateUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
 
@@ -17,6 +19,10 @@ import java.util.List;
  */
 @Service
 public class TLbtServiceImpl extends ServiceImpl<TLbtMapper, TLbt> implements TLbtService {
+    private static final int STATUS_HIDE = 0;
+    private static final int STATUS_SHOW = 1;
+    private static final long MAX_SHOW_COUNT = 5L;
+    private static final long MIN_SHOW_COUNT = 1L;
 
     @Override
     public Boolean addOrUpdate(TLbt lbt) {
@@ -63,4 +69,43 @@ public class TLbtServiceImpl extends ServiceImpl<TLbtMapper, TLbt> implements TL
         }
         return this.removeById(tLbt.getId());
     }
+
+    @Override
+    public Boolean updateStatus(TLbtStatusDTO dto) {
+        if (dto == null) {
+            throw new ServiceException("参数不能为空");
+        }
+        if (StringUtils.isBlank(dto.getId())) {
+            throw new ServiceException("ID不能为空");
+        }
+        if (dto.getStatus() == null || (dto.getStatus() != STATUS_HIDE && dto.getStatus() != STATUS_SHOW)) {
+            throw new ServiceException("显示状态值不正确");
+        }
+
+        TLbt exists = baseMapper.selectLbtById(dto.getId());
+        if (exists == null) {
+            throw new ServiceException("轮播图不存在");
+        }
+        if (dto.getStatus().equals(exists.getStatus())) {
+            return true;
+        }
+
+        Long showCount = baseMapper.countByStatus(STATUS_SHOW);
+        if (STATUS_SHOW == dto.getStatus() && showCount >= MAX_SHOW_COUNT) {
+            throw new ServiceException("当前显示数量已达5条上限!");
+        }
+        if (STATUS_HIDE == dto.getStatus() && showCount <= MIN_SHOW_COUNT) {
+            throw new ServiceException("当前banner已是最后一条显示,不可关闭!");
+        }
+
+        TLbt update = new TLbt();
+        update.setId(dto.getId());
+        update.setStatus(dto.getStatus());
+        update.setUpdateTime(DateUtils.getNowDate());
+        int rows = baseMapper.updateLbtById(update);
+        if (rows <= 0) {
+            throw new ServiceException("轮播图显示状态修改失败");
+        }
+        return true;
+    }
 }