|
@@ -0,0 +1,222 @@
|
|
|
+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.HomePageDataInfo;
|
|
|
+import com.miaxis.carousel.mapper.HomePageDataInfoMapper;
|
|
|
+import com.miaxis.carousel.service.IHomePageDataInfoService;
|
|
|
+import com.miaxis.carousel.vo.HomePageDataInfoVo;
|
|
|
+import com.miaxis.common.constant.Constants;
|
|
|
+import com.miaxis.common.core.domain.Response;
|
|
|
+import com.miaxis.common.enums.HomePageDataType;
|
|
|
+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.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 首页数据Service业务层处理
|
|
|
+ *
|
|
|
+ * @author miaxis
|
|
|
+ * @date 2021-03-11
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class HomePageDataInfoServiceImpl extends ServiceImpl<HomePageDataInfoMapper, HomePageDataInfo> implements IHomePageDataInfoService {
|
|
|
+
|
|
|
+ private final HomePageDataInfoMapper homePageDataInfoMapper;
|
|
|
+
|
|
|
+ private final RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+ private final IFileInfoService fileInfoService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询首页数据列表
|
|
|
+ *
|
|
|
+ * @param homePageDataInfo 首页数据
|
|
|
+ * @return 首页数据
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<HomePageDataInfoVo> selectHomePageDataInfoList(HomePageDataInfo homePageDataInfo){
|
|
|
+ return homePageDataInfoMapper.selectHomePageDataInfoList(homePageDataInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增首页数据
|
|
|
+ * @param homePageDataInfo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Response saveHomePageDataInfo(HomePageDataInfo homePageDataInfo) {
|
|
|
+ try{
|
|
|
+ HomePageDataInfo chartInfo = new HomePageDataInfo();
|
|
|
+ BeanUtils.copyProperties(homePageDataInfo,chartInfo);
|
|
|
+ homePageDataInfoMapper.insert(chartInfo);
|
|
|
+ //更新缓存
|
|
|
+ updateHomePageDataRedis(chartInfo);
|
|
|
+ return Response.success();
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new CustomException("系统异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改首页数据
|
|
|
+ * @param homePageDataInfo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Response<Integer> updateHomePageDataInfoById(HomePageDataInfo homePageDataInfo) {
|
|
|
+ try {
|
|
|
+ homePageDataInfoMapper.updateById(homePageDataInfo);
|
|
|
+ if ("1".equals(homePageDataInfo.getStatus())){
|
|
|
+ //删除缓存
|
|
|
+ redisTemplate.delete(Constants.HOME_PAGE_DATA_KEY+homePageDataInfo.getDataType()+":"+ homePageDataInfo.getId());
|
|
|
+ }else{
|
|
|
+ //更新缓存
|
|
|
+ updateHomePageDataRedis(homePageDataInfo);
|
|
|
+ }
|
|
|
+ return Response.success();
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new CustomException("系统异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取首页数据详细信息
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Response<HomePageDataInfoVo> getHomePageDataById(Long id) {
|
|
|
+ return Response.success(homePageDataInfoMapper.getHomePageDataById(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除首页数据(伪删除)
|
|
|
+ * @param idTypes id-type
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @SneakyThrows
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Response removeCarouselByIds(String[] idTypes) {
|
|
|
+
|
|
|
+ for (String idType : idTypes) {
|
|
|
+ String[] split = idType.split("-");
|
|
|
+ this.update(new UpdateWrapper<HomePageDataInfo>().set("status","1").eq("id",split[0]));
|
|
|
+ //删除缓存
|
|
|
+ redisTemplate.delete(Constants.HOME_PAGE_DATA_KEY + split[1] + ":" + split[0]);
|
|
|
+ }
|
|
|
+ return Response.success();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * applet
|
|
|
+ * 获取首页数据列表
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Response getHomePageDataList() {
|
|
|
+
|
|
|
+ Map<String, List<HomePageDataInfoVo>> map = new HashMap<>();
|
|
|
+
|
|
|
+ List<HomePageDataInfoVo> carouselChartInfoVos = new ArrayList<>(); //轮播图list
|
|
|
+ List<HomePageDataInfoVo> couponInfos = new ArrayList<>(); //优惠劵list
|
|
|
+ List<HomePageDataInfoVo> menuInfos = new ArrayList<>(); //首页菜单list
|
|
|
+
|
|
|
+ //-------------------从缓存获取轮播图-------------------
|
|
|
+ Set carouselChartkeys = redisTemplate.keys(Constants.HOME_PAGE_DATA_KEY + HomePageDataType.CAROUSElCHART.getDataType()+":*");
|
|
|
+ if (!carouselChartkeys.isEmpty()){
|
|
|
+ for (Object key : carouselChartkeys) {
|
|
|
+ String jsonStr = (String) redisTemplate.opsForValue().get(key);
|
|
|
+ HomePageDataInfoVo homePageDataInfoVo = JSONObject.parseObject(jsonStr).toJavaObject(HomePageDataInfoVo.class);
|
|
|
+ carouselChartInfoVos.add(homePageDataInfoVo);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //数据库获取(此处可不做查询!)
|
|
|
+ HomePageDataInfo homePageDataInfo = new HomePageDataInfo();
|
|
|
+ homePageDataInfo.setDataType(HomePageDataType.CAROUSElCHART.getDataType());
|
|
|
+ homePageDataInfo.setStatus("0");//启用状态
|
|
|
+ carouselChartInfoVos = homePageDataInfoMapper.selectHomePageDataInfoList(homePageDataInfo);
|
|
|
+ }
|
|
|
+ //排序
|
|
|
+ List<HomePageDataInfoVo> chartInfoVos = carouselChartInfoVos.stream()
|
|
|
+ .sorted(Comparator.comparing(HomePageDataInfoVo::getWeight).reversed())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ //-------------------从缓存获取优惠劵-------------------
|
|
|
+ Set couponKeys = redisTemplate.keys(Constants.HOME_PAGE_DATA_KEY + HomePageDataType.COUPON.getDataType()+":*");
|
|
|
+ if (!couponKeys.isEmpty()){
|
|
|
+ for (Object key : couponKeys) {
|
|
|
+ String jsonStr = (String) redisTemplate.opsForValue().get(key);
|
|
|
+ HomePageDataInfoVo homePageDataInfoVo = JSONObject.parseObject(jsonStr).toJavaObject(HomePageDataInfoVo.class);
|
|
|
+ couponInfos.add(homePageDataInfoVo);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //数据库获取(此处可不做查询!)
|
|
|
+ HomePageDataInfo homePageDataInfo = new HomePageDataInfo();
|
|
|
+ homePageDataInfo.setDataType(HomePageDataType.COUPON.getDataType());
|
|
|
+ homePageDataInfo.setStatus("0");//启用状态
|
|
|
+ couponInfos = homePageDataInfoMapper.selectHomePageDataInfoList(homePageDataInfo);
|
|
|
+ }
|
|
|
+ //排序
|
|
|
+ List<HomePageDataInfoVo> couponInfoVos = couponInfos.stream()
|
|
|
+ .sorted(Comparator.comparing(HomePageDataInfoVo::getWeight).reversed())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ //----------------从缓存获取首页菜单-------------------
|
|
|
+ Set menuKeys = redisTemplate.keys(Constants.HOME_PAGE_DATA_KEY + HomePageDataType.MENU.getDataType()+":*");
|
|
|
+ if (!menuKeys.isEmpty()){
|
|
|
+ for (Object key : menuKeys) {
|
|
|
+ String jsonStr = (String) redisTemplate.opsForValue().get(key);
|
|
|
+ HomePageDataInfoVo homePageDataInfoVo = JSONObject.parseObject(jsonStr).toJavaObject(HomePageDataInfoVo.class);
|
|
|
+ menuInfos.add(homePageDataInfoVo);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //数据库获取(此处可不做查询!)
|
|
|
+ HomePageDataInfo homePageDataInfo = new HomePageDataInfo();
|
|
|
+ homePageDataInfo.setDataType(HomePageDataType.MENU.getDataType());
|
|
|
+ homePageDataInfo.setStatus("0");//启用状态
|
|
|
+ menuInfos = homePageDataInfoMapper.selectHomePageDataInfoList(homePageDataInfo);
|
|
|
+ }
|
|
|
+ //排序
|
|
|
+ List<HomePageDataInfoVo> menuInfoVos = menuInfos.stream()
|
|
|
+ .sorted(Comparator.comparing(HomePageDataInfoVo::getWeight).reversed())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ map.put(HomePageDataType.CAROUSElCHART.getDataType(),chartInfoVos);
|
|
|
+ map.put(HomePageDataType.COUPON.getDataType(),couponInfoVos);
|
|
|
+ map.put(HomePageDataType.MENU.getDataType(),menuInfoVos);
|
|
|
+
|
|
|
+ return Response.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新首页数据缓存
|
|
|
+ * @param homePageDataInfo
|
|
|
+ */
|
|
|
+ private void updateHomePageDataRedis(HomePageDataInfo homePageDataInfo){
|
|
|
+ //更新缓存
|
|
|
+ FileInfo fileInfo = fileInfoService.getById(homePageDataInfo.getFileId());
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(homePageDataInfo));
|
|
|
+ jsonObject.put("fileUrl",fileInfo.getFileUrl());
|
|
|
+ //同时更新缓存(根据type拼接)
|
|
|
+ redisTemplate.opsForValue().set(Constants.HOME_PAGE_DATA_KEY +homePageDataInfo.getDataType()+":"+ homePageDataInfo.getId(),JSONObject.toJSONString(jsonObject));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|