yujian
2019-08-27 d8359ddb48dab5cc797a9d552e11fde571f4920c
fanli/src/main/java/com/yeshi/fanli/service/impl/homemodule/SwiperPictureServiceImpl.java
@@ -1,24 +1,27 @@
package com.yeshi.fanli.service.impl.homemodule;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
import com.yeshi.fanli.dao.mybatis.homemodule.SwiperPictureMapper;
import com.yeshi.fanli.entity.bus.homemodule.SwiperPicture;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.exception.banner.SwiperPictureException;
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
import com.yeshi.fanli.service.inter.homemodule.SwiperPictureService;
import com.yeshi.fanli.util.StringUtil;
import org.yeshi.utils.tencentcloud.COSManager;
@Service
public class SwiperPictureServiceImpl implements SwiperPictureService {
@@ -26,33 +29,28 @@
   @Resource
   private SwiperPictureMapper swiperPictureMapper;
   
   @Override
   public int deleteByPrimaryKey(Long id) throws SwiperPictureException{
      return swiperPictureMapper.deleteByPrimaryKey(id);
   }
   @Resource
   private JumpDetailV2Service jumpDetailV2Service;
   @Override
   public int insert(SwiperPicture record) throws SwiperPictureException{
      return swiperPictureMapper.insert(record);
   }
   @Override
   public int insertSelective(SwiperPicture record) throws SwiperPictureException{
   public int insertSelective(SwiperPicture record) {
      return swiperPictureMapper.insertSelective(record);
   }
   @Override
   public SwiperPicture selectByPrimaryKey(Long id) throws SwiperPictureException{
   public SwiperPicture selectByPrimaryKey(Long id) {
      return swiperPictureMapper.selectByPrimaryKey(id);
   }
   @Override
   public int updateByPrimaryKeySelective(SwiperPicture record) throws SwiperPictureException{
   public int updateByPrimaryKeySelective(SwiperPicture record) {
      return swiperPictureMapper.updateByPrimaryKeySelective(record);
   }
   @Override
   public int updateByPrimaryKey(SwiperPicture record) throws SwiperPictureException{
   public int updateByPrimaryKey(SwiperPicture record){
      return swiperPictureMapper.updateByPrimaryKey(record);
   }
@@ -66,6 +64,143 @@
      return swiperPictureMapper.countQueryByBannerID(bannerId);
   }
   @Override
   public void saveObject(MultipartFile file, SwiperPicture record, String jumpType) throws SwiperPictureException, Exception{
      if (record == null) {
         throw new SwiperPictureException(1, "参数不能为空");
      }
      Long bannerId = record.getBannerId();
      if (bannerId == null) {
         throw new SwiperPictureException(1, "标识管理ID不能为空");
      }
      String params = record.getParams();
      if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params) ) {
         record.setParams(null);
      } else if (!StringUtil.isJson(params)) {
         throw new SwiperPictureException(1, "跳转参数非JSON格式");
      }
      if (!StringUtil.isNullOrEmpty(jumpType)) {
         List<JumpDetailV2> listByType = jumpDetailV2Service.listByType(jumpType);
         if (listByType !=null && listByType.size() > 0) {
            record.setJumpDetail(listByType.get(0));
         }
      }
      try {
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
         String startTime_str = record.getStartTime_str();
         if (startTime_str != null && startTime_str.trim().length() > 0) {
            startTime_str = startTime_str.replaceAll("T", " ");
            record.setStartTime(format.parse(startTime_str));
         }
         String endTime_str = record.getEndTime_str();
         if (endTime_str != null && endTime_str.trim().length() > 0) {
            endTime_str = endTime_str.replaceAll("T", " ");
            record.setEndTime(format.parse(endTime_str));
         }
      } catch (ParseException e) {
         e.printStackTrace();
      }
      String remark = record.getRemark();
      if (remark != null && (remark.trim().length() == 0 || remark.equalsIgnoreCase("null"))) {
         record.setRemark(null);
      }
      String picture = null;
      if (file != null) {
         picture = uploadPicture(file);
      }
      Long id = record.getId();
      if (id == null) {
         int maxOrder = swiperPictureMapper.getMaxOrderByBannerID(bannerId);
         record.setOrder(maxOrder + 1);
         Integer state = record.getState();
         // 默认停用
         if (state == null) {
            record.setState(1);
         }
         Integer autoControl = record.getAutoControl();
         if (autoControl == null) {
            // 默认非系统控制
            record.setAutoControl(1);
         }
         record.setCreatetime(new Date());
         record.setUpdatetime(new Date());
         record.setSrc(picture);
         swiperPictureMapper.insert(record);
      } else {
         // 修改
         SwiperPicture resultObj = swiperPictureMapper.selectByPrimaryKey(id);
         if (resultObj == null) {
            throw new SwiperPictureException(1, "参数不能为空");
         }
         if (picture != null && picture.trim().length() > 0) {
            // 删除已存在图片
            removePicture(resultObj);
            record.setSrc(picture);
         } else {
            record.setSrc(resultObj.getSrc());
         }
         record.setOrder(resultObj.getOrder());
         record.setCreatetime(resultObj.getCreatetime());
         record.setUpdatetime(new Date());
         swiperPictureMapper.updateByPrimaryKey(record);
      }
   }
   /**
    * 上传图片
    * @param file
    * @return
    * @throws Exception
    */
   public String uploadPicture(MultipartFile file) throws Exception {
      // 文件解析
      InputStream inputStream = file.getInputStream();
      String contentType = file.getContentType();
      String type = contentType.substring(contentType.indexOf("/") + 1);
      // 文件路径
      String filePath="/img/swiperPic/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
      // 执行上传
      String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
      return fileLink;
   }
   /**
    * 删除图片-不更新数据库
    * @param record
    * @throws Exception
    */
   public void removePicture(SwiperPicture record) throws Exception {
      String picture = record.getSrc();
      if (picture != null && picture.trim().length() > 0) {
         COSManager.getInstance().deleteFile(picture);
      }
   }
   @Override
   @Transactional
   public int deleteBatchByPrimaryKey(List<Long> list) throws SwiperPictureException{
@@ -91,33 +226,9 @@
      return swiperPictureMapper.queryByListBannerID(list);
   }
   @Override
   public void uploadPicture(MultipartFile file, SwiperPicture record) throws Exception {
      InputStream inputStream = file.getInputStream();
      String contentType = file.getContentType();
      String type = contentType.substring(contentType.indexOf("/") + 1);
      // 上传文件相对位置
      String fileUrl="swiperPic/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
      /*  修改图片时,先删除已存在图片  */
      String src = record.getSrc();
      if (!StringUtil.isNullOrEmpty(src)) {
         COSManager.getInstance().deleteFile(src);
      }
      String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
      /*  更新数据库信息  */
      if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
         record.setSrc(uploadFilePath);
         record.setUpdatetime(new Date());
         updateByPrimaryKeySelective(record);
      }
   }
   @Override
   public List<SwiperPicture> getOrderByBannerID(Long bannerId, Integer type, Integer order) throws SwiperPictureException {
   public List<SwiperPicture> getOrderByBannerID(Long bannerId, Integer type, Integer order) {
      return swiperPictureMapper.getOrderByBannerID(bannerId, type, order);
   }
@@ -128,8 +239,26 @@
   
   @Override
   @Cacheable(value = "bannerCache", key = "'getByBannerCard-'+#card")
   public List<SwiperPicture> getByBannerCard(String card) throws SwiperPictureException {
      return swiperPictureMapper.getByBannerCard(card);
   public List<SwiperPicture> getByBannerCard(String card) {
      List<SwiperPicture> list = swiperPictureMapper.getByBannerCard(card);
      if (list != null && list.size() > 0) {
         for (SwiperPicture swiperPicture: list) {
            boolean needLogin = swiperPicture.isJumpNeedLogin();
            JumpDetailV2 jumpDetail = swiperPicture.getJumpDetail();
            if (jumpDetail != null) {
               jumpDetail.setNeedLogin(needLogin);
               swiperPicture.setJumpDetail(jumpDetail);
            }
         }
      }
      return list;
   }
   @Override
   @Cacheable(value = "bannerCache", key = "'getByBannerId-'+#bannerId")
   public List<SwiperPicture> getByBannerId(Long bannerId) {
      return swiperPictureMapper.getByBannerId(bannerId);
   }
   
}