yujian
2019-09-27 41aedac31eb4c5b9f7b98bacec18c45b3926d69c
fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandInfoServiceImpl.java
@@ -1,14 +1,19 @@
package com.yeshi.fanli.service.impl.brand;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
import com.yeshi.fanli.dao.brand.BrandShopCaheDao;
import com.yeshi.fanli.dao.mybatis.brand.BrandInfoMapper;
@@ -27,6 +32,7 @@
import com.yeshi.fanli.service.inter.lable.QualityGoodsService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
import com.yeshi.fanli.vo.brand.BrandInfoVO;
@@ -63,15 +69,33 @@
   @Resource
   private BrandShopCaheService brandShopCaheService;
   @Resource(name = "taskExecutor")
   private TaskExecutor executor;
   @Override
   public void saveObject(BrandInfo record) throws BrandInfoException {
   public void saveObject(MultipartFile file, BrandInfo record) throws BrandInfoException {
      String name = record.getName();
      if (name == null || name.trim().length() == 0)
      if (StringUtil.isNullOrEmpty(name))
         throw new BrandInfoException(1, "名称不能为空");
      String searchKey = record.getSearchKey();
      if (StringUtil.isNullOrEmpty(searchKey))
         record.setSearchKey(name);
      Integer state = record.getState();
      if (state == null)
         record.setState(0);
      // 图片上传
      String picture = null;
      if (file != null) {
         try {
            picture = uploadPicture(file);
         } catch (Exception e) {
            throw new BrandInfoException(1, "图片上传失败");
         }
      }
      Long id = record.getId();
      if (id == null) {
@@ -83,12 +107,57 @@
         if (resultObj == null)
            throw new BrandInfoException(1, "修改内容已不存在");
         if (picture != null && picture.trim().length() > 0) {
            // 删除老图
            if (picture != null && picture.trim().length() > 0 && !Constant.IS_TEST) {
               COSManager.getInstance().deleteFile(picture);
            };
            // 存储新图
            record.setIcon(picture);
         } else {
            record.setIcon(resultObj.getIcon());
         }
         record.setGoodsTotal(resultObj.getGoodsTotal());
         record.setCreateTime(resultObj.getCreateTime());
         record.setUpdateTime(new Date());
         brandInfoMapper.updateByPrimaryKey(record);
      }
      if (state == 1) {
         executor.execute(new Runnable() {
            @Override
            public void run() {
               int goodsTotal = brandGoodsCaheService.addBrandGoods(record);
               record.setGoodsTotal(goodsTotal);
               brandInfoMapper.updateByPrimaryKeySelective(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/brand/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
      // 执行上传
      String fileLink= COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
      return fileLink;
   }
   @Override
   public int deleteBatchByPrimaryKey(List<Long> list) {
      return brandInfoMapper.deleteBatchByPrimaryKey(list);
@@ -163,8 +232,9 @@
         return;
      for (BrandInfo brandInfo : list) {
         String key = brandInfo.getName();
         if (StringUtil.isNullOrEmpty(key))
         String name = brandInfo.getName();
         String searchKey = brandInfo.getSearchKey();
         if (StringUtil.isNullOrEmpty(name) && StringUtil.isNullOrEmpty(searchKey))
            continue;
         // 添加商品