yujian
2020-01-18 f4a0f2acc63d7785eab108419a4e16f5f688cb95
fanli/src/main/java/com/yeshi/fanli/service/impl/shop/BanLiShopGoodsServiceImpl.java
@@ -1,12 +1,16 @@
package com.yeshi.fanli.service.impl.shop;
import java.io.InputStream;
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.shop.BanLiShopGoodsMapper;
import com.yeshi.fanli.entity.shop.BanLiShopGoods;
@@ -18,6 +22,7 @@
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsImgService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetService;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
@Service
@@ -127,12 +132,100 @@
   }
   @Override
   public void saveObject(MultipartFile file, MultipartFile file2, BanLiShopGoods record) throws BanLiShopGoodsException, Exception{
      if (record.getGoodsClass() == null || record.getGoodsClass().getId() == null)
         throw new BanLiShopGoodsException(1, "请指定商品分类");
      if (StringUtil.isNullOrEmpty(record.getTitle()))
         throw new BanLiShopGoodsException(1, "缺少标题");
      if (file != null)
         record.setPicture(uploadPicture(file));
      if (file2 != null)
         record.setSquarePicture(uploadPicture(file2));
      if (record.getState() == null)
         record.setState(BanLiShopGoods.STATE_ONLINE);
      if (record.getSalesCount() == null)
         record.setSalesCount(0L);
      record.setUpdateTime(new Date());
      if (record.getId() == null) {
         if (StringUtil.isNullOrEmpty(record.getPicture()))
            throw new BanLiShopGoodsException(1, "缺少封面图");
         record.setCreateTime(new Date());
         banLiShopGoodsMapper.insertSelective(record);
      } else {
         BanLiShopGoods resultObj = banLiShopGoodsMapper.selectDetailByPrimaryKey(record.getId());
         if (resultObj == null)
            throw new BanLiShopGoodsException(1, "修改内容已不存在");
         if (StringUtil.isNullOrEmpty(record.getPicture())) {
            record.setPicture(resultObj.getPicture());
         } else {
            removePicture(resultObj.getPicture());
         }
         if (StringUtil.isNullOrEmpty(record.getSquarePicture())) {
            record.setSquarePicture(resultObj.getSquarePicture());
         } else {
            removePicture(resultObj.getSquarePicture());
         }
         if (StringUtil.isNullOrEmpty(record.getPicture()))
            throw new BanLiShopGoodsException(1, "缺少封面图");
         record.setCreateTime(resultObj.getCreateTime());
         banLiShopGoodsMapper.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=FilePathEnum.banLiShopGoods.getPath() +UUID.randomUUID().toString().replace("-", "") + "." + type;
      // 执行上传
      String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
      return fileLink;
   }
   /**
    * 删除图片-不更新数据库
    * @param record
    * @throws Exception
    */
   public void removePicture(String picture) throws Exception {
      if (picture != null && picture.trim().length() > 0) {
         COSManager.getInstance().deleteFile(picture);
      }
   }
   @Override
   public void updateSelectiveByPrimaryKey(BanLiShopGoods goods) {
      if (goods == null || goods.getId() == null)
         return;
      if (goods.getUpdateTime() == null)
         goods.setUpdateTime(new Date());
      banLiShopGoodsMapper.updateByPrimaryKeySelective(goods);
   }
   @Transactional
@@ -154,4 +247,16 @@
            banLiShopGoodsSetService.deleteByPrimaryKey(set.getId());
   }
   @Override
   public void addSalesCount(Long id, int count) {
      BanLiShopGoods goods = selectByPrimaryKey(id);
      if (goods != null) {
         BanLiShopGoods update = new BanLiShopGoods(id);
         update.setSalesCount(goods.getSalesCount() + count);
         update.setUpdateTime(new Date());
         updateSelectiveByPrimaryKey(goods);
      }
   }
}