|
@@ -1,18 +1,27 @@
|
|
|
package com.miaxis.carousel.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.miaxis.carousel.domain.CarouselChartInfo;
|
|
|
import com.miaxis.carousel.mapper.CarouselChartInfoMapper;
|
|
|
import com.miaxis.carousel.service.ICarouselChartInfoService;
|
|
|
import com.miaxis.carousel.vo.CarouselChartInfoVo;
|
|
|
+import com.miaxis.common.constant.Constants;
|
|
|
import com.miaxis.common.core.domain.Response;
|
|
|
+import com.miaxis.common.exception.CustomException;
|
|
|
+import com.miaxis.common.utils.bean.BeanUtils;
|
|
|
+import com.miaxis.file.domain.FileInfo;
|
|
|
+import com.miaxis.file.service.IFileInfoService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* 轮播图Service业务层处理
|
|
@@ -26,6 +35,10 @@ public class CarouselChartInfoServiceImpl extends ServiceImpl<CarouselChartInfoM
|
|
|
|
|
|
private final CarouselChartInfoMapper carouselChartInfoMapper;
|
|
|
|
|
|
+ private final RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+ private final IFileInfoService fileInfoService;
|
|
|
+
|
|
|
/**
|
|
|
* 查询轮播图列表
|
|
|
*
|
|
@@ -37,6 +50,44 @@ public class CarouselChartInfoServiceImpl extends ServiceImpl<CarouselChartInfoM
|
|
|
return carouselChartInfoMapper.selectCarouselChartInfoList(carouselChartInfo);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 新增轮播图
|
|
|
+ * @param carouselChartInfo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Response saveCarouselChartInfo(CarouselChartInfo carouselChartInfo) {
|
|
|
+ try{
|
|
|
+ CarouselChartInfo chartInfo = new CarouselChartInfo();
|
|
|
+ BeanUtils.copyProperties(carouselChartInfo,chartInfo);
|
|
|
+ carouselChartInfoMapper.insert(chartInfo);
|
|
|
+ //更新缓存
|
|
|
+ updateCarouselChartRedis(chartInfo);
|
|
|
+ return Response.success();
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new CustomException("系统异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改轮播图
|
|
|
+ * @param carouselChartInfo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Response<Integer> updateCarouselChartInfoById(CarouselChartInfo carouselChartInfo) {
|
|
|
+ try {
|
|
|
+ carouselChartInfoMapper.updateById(carouselChartInfo);
|
|
|
+ //更新缓存
|
|
|
+ updateCarouselChartRedis(carouselChartInfo);
|
|
|
+ return Response.success();
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new CustomException("系统异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 删除轮播图(伪删除)
|
|
|
* @param ids
|
|
@@ -48,8 +99,51 @@ public class CarouselChartInfoServiceImpl extends ServiceImpl<CarouselChartInfoM
|
|
|
public Response removeCarouselByIds(Long[] ids) {
|
|
|
for (Long id : ids) {
|
|
|
this.update(new UpdateWrapper<CarouselChartInfo>().set("status",1).eq("id",id));
|
|
|
+ //删除缓存
|
|
|
+ redisTemplate.delete(Constants.CAROUSEL_CHART_KEY + id);
|
|
|
}
|
|
|
return Response.success();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * applet
|
|
|
+ * 获取轮播图列表
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<CarouselChartInfoVo> getCarouselChartList() {
|
|
|
+ List<CarouselChartInfoVo> infoVos = new ArrayList<>();
|
|
|
+ //从缓存获取
|
|
|
+ Set keys = redisTemplate.keys(Constants.CAROUSEL_CHART_KEY + "*");
|
|
|
+ if (!keys.isEmpty()){
|
|
|
+ for (Object key : keys) {
|
|
|
+ String jsonStr = (String) redisTemplate.opsForValue().get(key);
|
|
|
+ CarouselChartInfoVo carouselChartInfoVo = JSONObject.parseObject(jsonStr).toJavaObject(CarouselChartInfoVo.class);
|
|
|
+ infoVos.add(carouselChartInfoVo);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //数据库获取(此处可不做查询,防止缓存挂掉)
|
|
|
+ CarouselChartInfo carouselChartInfo = new CarouselChartInfo();
|
|
|
+ carouselChartInfo.setStatus(0);//启用状态
|
|
|
+ infoVos = carouselChartInfoMapper.selectCarouselChartInfoList(carouselChartInfo);
|
|
|
+ }
|
|
|
+ return infoVos;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新轮播图缓存
|
|
|
+ * @param carouselChartInfo
|
|
|
+ */
|
|
|
+ private void updateCarouselChartRedis(CarouselChartInfo carouselChartInfo){
|
|
|
+ //更新缓存
|
|
|
+ FileInfo fileInfo = fileInfoService.getById(carouselChartInfo.getFileId());
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(carouselChartInfo));
|
|
|
+ jsonObject.put("fileUrl",fileInfo.getFileUrl());
|
|
|
+ //同时更新缓存
|
|
|
+ redisTemplate.opsForValue().set(Constants.CAROUSEL_CHART_KEY + carouselChartInfo.getId(),JSONObject.toJSONString(jsonObject));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|