yujian
2020-04-02 0ec22dcf4fd9c4496e6f681e7fab89f56c6e4e8a
fanli/src/main/java/com/yeshi/fanli/service/impl/help/HelpClassServiceImpl.java
@@ -2,20 +2,34 @@
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.annotation.Resource;
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.help.HelpClassMapper;
import com.yeshi.fanli.entity.AppVersionInfo;
import com.yeshi.fanli.entity.bus.help.HelpClass;
import com.yeshi.fanli.exception.HelpClassException;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl;
import com.yeshi.fanli.entity.bus.homemodule.FloatAD;
import com.yeshi.fanli.entity.bus.homemodule.SwiperPicture;
import com.yeshi.fanli.entity.bus.homemodule.AdActivityVersionControl.AdActivityType;
import com.yeshi.fanli.exception.banner.SwiperPictureException;
import com.yeshi.fanli.exception.config.HelpClassException;
import com.yeshi.fanli.exception.homemodule.FloatADException;
import com.yeshi.fanli.service.inter.help.HelpClassService;
import com.yeshi.fanli.service.inter.homemodule.AdActivityVersionControlService;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
@Service
@@ -24,6 +38,9 @@
   @Resource
   private HelpClassMapper helpClassMapper;
   @Resource
   private AdActivityVersionControlService adActivityVersionControlService;
   @Override
   public int deleteByPrimaryKeyBatch(List<Long> list) {
      return helpClassMapper.deleteByPrimaryKeyBatch(list);
@@ -35,32 +52,71 @@
   }
   
   @Override
   public void save(HelpClass helpClass) throws HelpClassException {
      if (helpClass == null) {
   public void save(HelpClass record, MultipartFile file) throws HelpClassException, Exception{
      if (record == null)
         throw new HelpClassException(1, "参数不能为空");
      }
      
      if (helpClass.getId() == null) {
         String name = helpClass.getName();
         if (StringUtil.isNullOrEmpty(name)) {
            throw new HelpClassException(1, "名称不能为空");
      String name = record.getName();
      if (StringUtil.isNullOrEmpty(name))
         throw new HelpClassException(1, "名称不能为空");
      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));
         }
         
         // 默认不显示
         helpClass.setShowState(0);
         // 排序-末尾
         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 picture = null;
      if (file != null) {
         InputStream inputStream = file.getInputStream();
         String contentType = file.getContentType();
         String type = contentType.substring(contentType.indexOf("/") + 1);
         String filePath =FilePathEnum.helpClass.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
         picture = COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
      }
      Integer showState = record.getShowState();
      if (showState == null)
         record.setShowState(0);
      record.setUpdateTime(new Date());
      if (record.getId() == null) {
         int maxOrder = helpClassMapper.getMaxOrder();
         helpClass.setSort(maxOrder + 1);
         helpClass.setCreateTime(new Date());
         helpClass.setUpdateTime(new Date());
         helpClassMapper.insertSelective(helpClass);
         record.setSort(maxOrder + 1);
         record.setCreateTime(new Date());
         helpClassMapper.insertSelective(record);
      } else {
         helpClass.setUpdateTime(new Date());
         helpClassMapper.updateByPrimaryKeySelective(helpClass);
         // 修改
         HelpClass resultObj = helpClassMapper.selectByPrimaryKey(record.getId());
         if (resultObj == null)
            throw new FloatADException(1, "修改内容已不存在");
         if (picture != null && picture.trim().length() > 0) {
            String pictureOld = resultObj.getPicture();
            if (pictureOld != null && pictureOld.trim().length() > 0) {
               COSManager.getInstance().deleteFile(pictureOld);
            }
            // 存储新图
            record.setPicture(picture);
         } else {
            record.setPicture(resultObj.getPicture());
         }
         record.setSort(resultObj.getSort());
         record.setCreateTime(resultObj.getCreateTime());
         helpClassMapper.updateByPrimaryKey(record);
      }
   }
   
@@ -139,7 +195,7 @@
      String contentType = file.getContentType();
      String type = contentType.substring(contentType.indexOf("/") + 1);
      
      String filePath ="/img/HelpClass/" + UUID.randomUUID().toString().replace("-", "") + "." + type;
      String filePath =FilePathEnum.helpClass.getPath()+ UUID.randomUUID().toString().replace("-", "") + "." + type;
      
      /*  修改图片时,先删除已存在图片  */
      String picture = helpClass.getPicture();
@@ -180,4 +236,52 @@
   }
   
   
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void setVersions(Long id, List<Long> versions) throws HelpClassException {
      HelpClass helpClass = helpClassMapper.selectByPrimaryKey(id);
      if (helpClass == null) {
         throw new HelpClassException(1, "此信息不存在,请刷新重试");
      }
      Set<Long> oldSet = new HashSet<>();
      List<AdActivityVersionControl> versionList = adActivityVersionControlService
            .listByTypeAndSourceId(AdActivityType.helpClass, id);
      if (versionList != null) {
         for (AdActivityVersionControl control : versionList)
            oldSet.add(control.getVersion().getId());
      }
      Set<Long> newSet = new HashSet<>();
      for (Long version : versions) {
         newSet.add(version);
      }
      Set<Long> delSet = new HashSet<>();
      delSet.addAll(oldSet);
      delSet.removeAll(newSet);
      for (Long versionId : delSet) {
         adActivityVersionControlService.deleteBySourceAndVersion(id, AdActivityType.helpClass, versionId);
      }
      Set<Long> addSet = new HashSet<>();
      addSet.addAll(newSet);
      addSet.removeAll(oldSet);
      // 添加映射
      for (Long versionId : addSet) {
         AdActivityVersionControl control = new AdActivityVersionControl();
         control.setCreateTime(new Date());
         control.setSourceId(id);
         control.setType(AdActivityType.helpClass);
         control.setVersion(new AppVersionInfo(versionId));
         try {
            adActivityVersionControlService.addVersionControl(control);
         } catch (Exception e) {
            throw new HelpClassException(2, e.getMessage());
         }
      }
   }
}