yujian
2020-01-18 f4a0f2acc63d7785eab108419a4e16f5f688cb95
fanli/src/main/java/com/yeshi/fanli/service/impl/homemodule/SpecialCardServiceImpl.java
@@ -1,12 +1,27 @@
package com.yeshi.fanli.service.impl.homemodule;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
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.homemodule.SpecialCardMapper;
import com.yeshi.fanli.entity.bus.homemodule.SpecialCard;
import com.yeshi.fanli.entity.bus.homemodule.SpecialPlace;
import com.yeshi.fanli.exception.homemodule.SpecialCardException;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.service.inter.homemodule.SpecialCardService;
import com.yeshi.fanli.service.inter.homemodule.SpecialPlaceService;
import com.yeshi.fanli.service.inter.homemodule.SpecialService;
import com.yeshi.fanli.util.FilePathEnum;
@Service
@@ -15,35 +30,211 @@
   @Resource
   private SpecialCardMapper specialCardMapper;
   @Resource
   private BusinessSystemService businessSystemService;
   @Resource
   private SpecialService specialService;
   @Resource
   private  SpecialPlaceService specialPlaceService;
   
   @Override
   public int deleteByPrimaryKey(Long id) {
      return specialCardMapper.deleteByPrimaryKey(id);
   public void saveObject(MultipartFile file, SpecialCard record) throws SpecialCardException, Exception{
      String name = record.getName();
      if (name == null || name.trim().length() == 0) {
         throw new SpecialCardException(1, "名称不能为空");
      }
      Long placeId = record.getPlaceId();
      if (placeId == null || placeId == 0) {
         throw new SpecialCardException(1, "使用位置不能为空");
      }
      // 时间任务控制
      conversionTime(record);
      // 图片上传
      String picture = null;
      if (file != null) {
         picture = uploadPicture(file);
      }
      Integer state = record.getState();
      if (state == null) {
         record.setState(0);
      }
      Long id = record.getId();
      if (id == null) {
         record.setBottomPicture(picture);
         record.setCreatetime(new Date());
         record.setUpdatetime(new Date());
         specialCardMapper.insert(record);
      } else {
         // 修改
         SpecialCard resultObj = specialCardMapper.selectByPrimaryKey(id);
         if (resultObj == null) {
            throw new SpecialCardException(1, "修改内容已不存在");
         }
         // 删除图片
         Boolean delPicture = record.getDelPicture();
         if (delPicture != null && delPicture) {
            removePicture(resultObj.getBottomPicture());
            resultObj.setBottomPicture(null);
         }
         if (picture != null && picture.trim().length() > 0) {
            // 删除老图
            removePicture(resultObj.getBottomPicture());
            // 存储新图
            record.setBottomPicture(picture);
         } else {
            record.setBottomPicture(resultObj.getBottomPicture());
         }
         record.setCreatetime(resultObj.getCreatetime());
         record.setUpdatetime(new Date());
         specialCardMapper.updateByPrimaryKey(record);
      }
   }
   /**
    * web段时间转换
    * @param record
    */
   public void conversionTime(SpecialCard record) throws SpecialCardException, Exception {
      // 是否时间控制
      if(!record.isTimeTask()) {
         record.setStartTime(null);
         record.setEndTime(null);
      } else {
         String startTime_str = record.getStartTime_str();
         String endTime_str = record.getEndTime_str();
         if ((startTime_str == null|| startTime_str.trim().length() == 0)
               && (endTime_str == null || endTime_str.trim().length() == 0)) {
            throw new SpecialCardException(1, "请输入控制时间");
         } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            if (startTime_str != null && startTime_str.trim().length() > 0) {
               startTime_str = startTime_str.replaceAll("T", " ");
               record.setStartTime(format.parse(startTime_str));
            }
            if (endTime_str != null && endTime_str.trim().length() > 0) {
               endTime_str = endTime_str.replaceAll("T", " ");
               record.setEndTime(format.parse(endTime_str));
            }
         }
      }
   }
   /**
    * 上传图片
    * @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=FilePathEnum.specialCard.getPath() +UUID.randomUUID().toString().replace("-", "") + "." + type;
      // 执行上传
      String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
      return fileLink;
   }
   @Override
   public int insert(SpecialCard record) {
      return specialCardMapper.insert(record);
   /**
    * 删除图片-不更新数据库
    * @param record
    * @throws Exception
    */
   public void removePicture(String picture) throws Exception {
      if (picture != null && picture.trim().length() > 0) {
         COSManager.getInstance().deleteFile(picture);
      }
   }
   @Override
   public int insertSelective(SpecialCard record) {
      return specialCardMapper.insertSelective(record);
   @Transactional(rollbackFor=Exception.class)
   public int deleteBatchByPrimaryKey(List<Long> list) throws Exception{
      specialService.deleteBatchByCardID(list);
      return specialCardMapper.deleteBatchByPrimaryKey(list);
   }
   @Override
   public SpecialCard selectByPrimaryKey(Long id) {
      return specialCardMapper.selectByPrimaryKey(id);
   public List<SpecialCard> listQuery(long start, int count, String key, Integer sort) {
      List<SpecialCard> listObj = specialCardMapper.listQuery(start, count, key);
      if (listObj == null || listObj.size() == 0) {
         return null;
      }
      for (SpecialCard specialCard: listObj) {
         long totalSpecial= specialService.countlistQueryByCard(specialCard.getId(), null, null);
         specialCard.setTotalSpecial(totalSpecial);
         Date startTime = specialCard.getStartTime();
         Date endTime = specialCard.getEndTime();
         if (startTime == null && endTime == null) {
            specialCard.setTimeTask(false);
            specialCard.setStartTime_str("");
            specialCard.setEndTime_str("");
         } else {
            specialCard.setTimeTask(true);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
            if (startTime == null) {
               specialCard.setStartTime_str("");
            } else {
               specialCard.setStartTime_str(sdf.format(startTime));
            }
            if (endTime == null) {
               specialCard.setEndTime_str("");
            } else {
               specialCard.setEndTime_str(sdf.format(endTime));
            }
         }
         Long placeId = specialCard.getPlaceId();
         if (placeId != null) {
            SpecialPlace specialPlace = specialPlaceService.selectByPrimaryKey(placeId);
            if (specialPlace != null) {
               specialCard.setPlaceName(specialPlace.getName());
            }
         }
      }
      return listObj;
   }
   @Override
   public int updateByPrimaryKeySelective(SpecialCard record) {
      return specialCardMapper.updateByPrimaryKeySelective(record);
   public long countlistQuery(String key) {
      return specialCardMapper.countlistQuery(key);
   }
   @Override
   public int updateByPrimaryKey(SpecialCard record) {
      return specialCardMapper.updateByPrimaryKey(record);
   public String getbottomPicture(String placeKey) {
      return specialCardMapper.getbottomPicture(placeKey);
   }
}