admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
fanli/src/main/java/com/yeshi/fanli/service/impl/homemodule/SwiperPictureServiceImpl.java
@@ -12,6 +12,7 @@
import javax.annotation.Resource;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -22,15 +23,23 @@
import com.yeshi.fanli.entity.AppVersionInfo;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl.AdActivityType;
import com.yeshi.fanli.entity.bus.homemodule.Special;
import com.yeshi.fanli.entity.bus.homemodule.CommonShareInfo;
import com.yeshi.fanli.entity.bus.homemodule.CommonShareInfo.CommonShareInfoEnum;
import com.yeshi.fanli.entity.bus.homemodule.SwiperPicture;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.exception.banner.SwiperPictureException;
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
import com.yeshi.fanli.service.inter.config.AppVersionService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.homemodule.AdActivityVersionControlService;
import com.yeshi.fanli.service.inter.homemodule.CommonShareInfoService;
import com.yeshi.fanli.service.inter.homemodule.SwiperPictureService;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.vo.homemodule.BannerVO;
import net.sf.json.JSONObject;
@Service
public class SwiperPictureServiceImpl implements SwiperPictureService {
@@ -46,6 +55,13 @@
   @Resource
   private AppVersionService appVersionService;
   @Resource
   private CommonShareInfoService commonShareInfoService;
   @Resource
   private ConfigService configService;
   @Override
   public int insertSelective(SwiperPicture record) {
@@ -68,8 +84,9 @@
   }
   @Override
   public List<SwiperPicture> queryByBannerID(long start, int count, Long bannerId) throws SwiperPictureException {
      return swiperPictureMapper.queryByBannerID(start, count, bannerId);
   public List<BannerVO> queryByBannerID(long start, int count, Long bannerId) throws SwiperPictureException {
      List<SwiperPicture> list = swiperPictureMapper.queryByBannerID(start, count, bannerId);
      return transformVO(list);
   }
   @Override
@@ -78,9 +95,8 @@
   }
   @Override
   public void saveObject(MultipartFile file, SwiperPicture record, String jumpType)
   public void saveObject(MultipartFile file, SwiperPicture record, String jumpType, CommonShareInfo shareInfo)
         throws SwiperPictureException, Exception {
      if (record == null) {
         throw new SwiperPictureException(1, "参数不能为空");
      }
@@ -95,6 +111,8 @@
         record.setParams(null);
      } else if (!StringUtil.isJson(params)) {
         throw new SwiperPictureException(1, "跳转参数非JSON格式");
      } else {
         record.setParams(params.trim());
      }
      if (!StringUtil.isNullOrEmpty(jumpType)) {
@@ -137,7 +155,6 @@
      if (id == null) {
         int maxOrder = swiperPictureMapper.getMaxOrderByBannerID(bannerId);
         record.setOrder(maxOrder + 1);
         Integer state = record.getState();
         // 默认停用
         if (state == null) {
@@ -152,8 +169,9 @@
         record.setCreatetime(new Date());
         record.setUpdatetime(new Date());
         record.setSrc(picture);
         swiperPictureMapper.insert(record);
         shareInfo.setPid(record.getId());
      } else {
         // 修改
         SwiperPicture resultObj = swiperPictureMapper.selectByPrimaryKey(id);
@@ -164,7 +182,6 @@
         if (picture != null && picture.trim().length() > 0) {
            // 删除已存在图片
            removePicture(resultObj);
            record.setSrc(picture);
         } else {
            record.setSrc(resultObj.getSrc());
@@ -173,9 +190,14 @@
         record.setOrder(resultObj.getOrder());
         record.setCreatetime(resultObj.getCreatetime());
         record.setUpdatetime(new Date());
         swiperPictureMapper.updateByPrimaryKey(record);
         shareInfo.setPid(record.getId());
      }
      // 添加补充信息
      shareInfo.setType(CommonShareInfoEnum.banner);
      commonShareInfoService.save(shareInfo);
   }
   /**
@@ -193,7 +215,7 @@
      String type = contentType.substring(contentType.indexOf("/") + 1);
      // 文件路径
      String filePath = "/img/swiperPic/" + UUID.randomUUID().toString().replace("-", "") + "." + type;
      String filePath = FilePathEnum.swiper.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
      // 执行上传
      String fileLink = COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
@@ -212,17 +234,44 @@
         COSManager.getInstance().deleteFile(picture);
      }
   }
   @Override
   public void switchState(Long id) throws SwiperPictureException {
      if (id == null) {
         throw new SwiperPictureException(1, "请传递正确参数");
      }
      SwiperPicture resultObj = swiperPictureMapper.selectByPrimaryKey(id);
      if (resultObj == null) {
         throw new SwiperPictureException(1, "此内容已不存在");
      }
      Integer state = resultObj.getState();
      if (state == null || state == 0) {
         state = 1;
      } else {
         state = 0;
      }
      SwiperPicture updateObj = new SwiperPicture();
      updateObj.setId(id);
      updateObj.setState(state);
      updateObj.setJumpNeedLogin(resultObj.isJumpNeedLogin());
      swiperPictureMapper.updateByPrimaryKeySelective(updateObj);
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   public int deleteBatchByPrimaryKey(List<Long> list) throws SwiperPictureException {
      List<SwiperPicture> listSwiper = swiperPictureMapper.queryByListPrimaryKey(list);
      for (SwiperPicture swiperPicture : listSwiper) {
         String src = swiperPicture.getSrc();
         if (!StringUtil.isNullOrEmpty(src)) {
            COSManager.getInstance().deleteFile(src);
         }
         commonShareInfoService.deleteByPidAndType(swiperPicture.getId(), CommonShareInfoEnum.banner.name());
      }
      if (listSwiper != null)
@@ -238,6 +287,11 @@
   @Override
   public int deleteBatchByBannerID(List<Long> list) throws SwiperPictureException {
      if (list != null) {
         for (Long id : list) {
            commonShareInfoService.deleteByPidAndType(id, CommonShareInfoEnum.banner.name());
         }
      }
      return swiperPictureMapper.deleteBatchByBannerID(list);
   }
@@ -258,7 +312,7 @@
   @Override
   @Cacheable(value = "bannerCache", key = "'getByBannerCard-'+#card")
   public List<SwiperPicture> getByBannerCard(String card) {
   public List<BannerVO> getByBannerCard(String card) {
      List<SwiperPicture> list = swiperPictureMapper.getByBannerCard(card);
      if (list != null && list.size() > 0) {
         for (SwiperPicture swiperPicture : list) {
@@ -270,7 +324,7 @@
            }
         }
      }
      return list;
      return transformVO(list);
   }
   /**
@@ -306,25 +360,32 @@
   @Cacheable(value = "bannerCache", key = "'getByBannerCardAndVersion-'+#card+'-'+#platform+'-'+#version")
   @Override
   public List<SwiperPicture> getByBannerCardAndVersion(String card, String platform, int version) {
   public List<BannerVO> getByBannerCardAndVersion(String card, String platform, int version) {
      List<SwiperPicture> list = swiperPictureMapper.getByBannerCard(card);
      filterSwipePicture(list, platform, version);
      return list;
      for (SwiperPicture picture : list) {
         if (picture.isJumpNeedLogin() && picture.getJumpDetail() != null) {
            picture.getJumpDetail().setNeedLogin(true);
         }
      }
      return transformVO(list);
   }
   @Override
   @Cacheable(value = "bannerCache", key = "'getByBannerId-'+#bannerId")
   public List<SwiperPicture> getByBannerId(Long bannerId) {
      return swiperPictureMapper.getByBannerId(bannerId);
   public List<BannerVO> getByBannerId(Long bannerId) {
      List<SwiperPicture>  pictureList = swiperPictureMapper.getByBannerId(bannerId);
      return transformVO(pictureList);
   }
   @Cacheable(value = "bannerCache", key = "'getByBannerId-'+#bannerId+'-'+#platform+'-'+#version")
   @Override
   public List<SwiperPicture> getByBannerId(Long bannerId, String platform, int version)
   public List<BannerVO> getByBannerId(Long bannerId, String platform, int version)
         throws SwiperPictureException {
      List<SwiperPicture> pictureList = swiperPictureMapper.getByBannerId(bannerId);
      filterSwipePicture(pictureList, platform, version);
      return pictureList;
      return transformVO(pictureList);
   }
   @Transactional(rollbackFor = Exception.class)
@@ -375,7 +436,45 @@
            throw new SwiperPictureException(2, e.getMessage());
         }
      }
   }
   /**
    * 转换VO
    * @param list
    * @return
    */
   private List<BannerVO> transformVO(List<SwiperPicture> list) {
      List<BannerVO> listVo = new ArrayList<BannerVO>();
      if (list != null && list.size() > 0) {
         for (SwiperPicture swiperPicture: list) {
            BannerVO bannerVO = new BannerVO();
            try {
               PropertyUtils.copyProperties(bannerVO, swiperPicture);
            } catch (Exception e) {
               e.printStackTrace();
               continue;
            }
            // 跳转活动过渡页
            CommonShareInfo info = commonShareInfoService.getByPidAndType(bannerVO.getId(),
                  CommonShareInfoEnum.banner.name());
            if (info != null && !StringUtil.isNullOrEmpty(info.getComment())) {
               String link = configService.get(ConfigKeyEnum.activityDetailLink.getKey()) + "?type=%s&id=%s";
               link = String.format(link, CommonShareInfoEnum.banner.name(),bannerVO.getId());
               JSONObject params = new JSONObject();
               params.put("url", link);
               bannerVO.setComment(info.getComment());
               bannerVO.setParams(params.toString());
               bannerVO.setJumpDetail(jumpDetailV2Service.getByTypeCache("web"));
            }
            listVo.add(bannerVO);
         }
      }
      return listVo;
   }
}