yujian
2019-08-27 d8359ddb48dab5cc797a9d552e11fde571f4920c
fanli/src/main/java/com/yeshi/fanli/service/impl/goods/GoodsSubClassServiceImpl.java
@@ -1,23 +1,32 @@
package com.yeshi.fanli.service.impl.goods;
import java.io.InputStream;
import java.util.ArrayList;
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.GoodsSubClassMapper;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.entity.bus.clazz.GoodsSubClass;
import com.yeshi.fanli.entity.bus.clazz.GoodsSubClassLabel;
import com.yeshi.fanli.entity.bus.clazz.GoodsSubClassLabelMap;
import com.yeshi.fanli.exception.GoodsSubClassException;
import com.yeshi.fanli.service.inter.clazz.GoodsSubClassLabelService;
import com.yeshi.fanli.service.inter.goods.GoodsSubClassService;
import com.yeshi.fanli.service.inter.goods.TaoBaoClassService;
import com.yeshi.fanli.service.inter.lable.LabelClassService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import org.yeshi.utils.tencentcloud.COSManager;
@Service
public class GoodsSubClassServiceImpl implements GoodsSubClassService {
@@ -28,7 +37,12 @@
   private GoodsSubClassService goodsSubClassService;
   @Resource
   private LabelClassService labelClassService;
   @Resource
   private TaoBaoClassService taoBaoClassService;
   @Resource
   private GoodsSubClassLabelService goodsSubClassLabelService;
   @Override
   public int deleteByPrimaryKey(Long id) {
@@ -56,22 +70,17 @@
   }
   @Override
   public int updateByPrimaryKey(GoodsSubClass record) {
      return goodsSubClassMapper.updateByPrimaryKey(record);
   }
   @Override
   @Transactional
   public void deleteByRootId(Long id) throws Exception {
      List<GoodsSubClass> subClassList = goodsSubClassMapper.queryByRootId(id, null);
      List<GoodsSubClass> subClassList = goodsSubClassMapper.queryByRootId(id, null, null);
      if (subClassList != null && subClassList.size() > 0) {
         for (GoodsSubClass goodsSubClass : subClassList) {
               deleteSub(goodsSubClass.getId());
            deleteSub(goodsSubClass.getId());
         }
      }
   }
   @Override
   @Transactional
   public void deleteByPrimaryKeyBatch(List<String> recordIds) throws Exception {
@@ -81,13 +90,10 @@
         }
      }
   }
   @Override
   @Transactional
   public void deleteSub(Long recordId) throws Exception {
      // TODO Auto-generated method stub
      GoodsSubClass goodsSubClass = goodsSubClassMapper.selectByPrimaryKey(recordId);
      if (goodsSubClass == null)
@@ -99,6 +105,12 @@
         COSManager.getInstance().deleteFile(picture);
      }
      /* 删除网络图片 */
      String pictureSecond = goodsSubClass.getPictureSecond();
      if (!StringUtil.isNullOrEmpty(pictureSecond)) {
         COSManager.getInstance().deleteFile(pictureSecond);
      }
      /* 删除所有关联子类 */
      List<GoodsSubClass> subList = goodsSubClassMapper.queryByPid(recordId, null);
      if (subList != null && subList.size() > 0) {
@@ -108,59 +120,194 @@
            deleteSub(id);
         }
      }
      /* 删除关联标签 */
      labelClassService.deleteBySubClassId(recordId);
      /* 删除数据 */
      goodsSubClassMapper.deleteByPrimaryKey(recordId);
   }
   @Override
   public int save(GoodsSubClass record, MultipartFile file) throws Exception {
   public void saveObject(MultipartFile file, MultipartFile file2, GoodsSubClass record, Integer type, Long pid,
         Long labelId) throws GoodsSubClassException, Exception {
      int result = 0;
      result = goodsSubClassMapper.insertSelective(record);
      // 上传图片
      if (file != null) {
         result = goodsSubClassService.uploadPicture(record, file);
      String name = record.getName();
      if (name == null || name.trim().length() == 0) {
         throw new GoodsSubClassException(1, "分类名称不能为空");
      }
      return result;
      if (StringUtil.isNullOrEmpty(record.getMonth()))
         record.setMonth(null);
      // 图片上传
      String picture = null;
      if (file != null) {
         picture = uploadPicture(file);
      }
      // 图片上传
      String pictureSecond = null;
      if (file2 != null) {
         pictureSecond = uploadPicture(file2);
      }
      String params = record.getSearchParam();
      if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params)) {
         record.setSearchJson(null);
      } else if (!StringUtil.isJson(params)) {
         throw new GoodsSubClassException(1, "筛选条件非JSON格式");
      } else {
         record.setSearchJson(params);
      }
      Long id = record.getId();
      if (id == null) {
         if (type == null) {
            throw new GoodsSubClassException(1, "等级不能为空");
         }
         if (type > 5) {
            throw new GoodsSubClassException(1, "等级不能超过五级");
         }
         if (pid == null) {
            throw new GoodsSubClassException(1, "上级id为空");
         }
         if (type == 2) {
            record.setRootClass(new GoodsClass(pid));
            int weight = goodsSubClassMapper.getMaxWeightByRootId(pid);
            record.setWeight(weight + 1);
         } else {
            record.setParent(new GoodsSubClass(pid));
            int weight = goodsSubClassMapper.getMaxWeightByPid(pid);
            record.setWeight(weight + 1);
         }
         String key = record.getKey();
         if (StringUtil.isNullOrEmpty(key)) {
            record.setKey(name.trim());
         }
         record.setLevel(type);
         record.setPicture(picture);
         record.setPictureSecond(pictureSecond);
         record.setState(0);
         record.setAndroidClick(0L);
         record.setIosClick(0L);
         record.setCreatetime(new Date());
         record.setUpdatetime(new Date());
         if (labelId != null)
            record.setClassLabel(new GoodsSubClassLabel(labelId));
         if (params == null || params.trim().length() == 0 || "null".equalsIgnoreCase(params)) {
            // 搜索条件:有券、在售价20-200、牛皮癣轻微
            record.setSearchJson("{\"quan\":1,\"endPrice\":220,\"includeGoodRate\":true}");
         }
         goodsSubClassMapper.insert(record);
         if (labelId != null) {
            GoodsSubClassLabelMap map = new GoodsSubClassLabelMap();
            map.setGoodsSubClass(record);
            map.setLabel(new GoodsSubClassLabel(labelId));
            goodsSubClassLabelService.addSubClassLabelMap(map);
         }
      } else {
         // 修改
         GoodsSubClass resultObj = goodsSubClassMapper.selectByPrimaryKey(id);
         if (resultObj == null) {
            throw new GoodsSubClassException(1, "修改内容已不存在");
         }
         if (picture != null && picture.trim().length() > 0) {
            // 删除老图
            if (!Constant.IS_TEST)
               removePicture(resultObj.getPicture());
            // 存储新图
            record.setPicture(picture);
         } else {
            record.setPicture(resultObj.getPicture());
         }
         if (pictureSecond != null && pictureSecond.trim().length() > 0) {
            // 删除老图
            if (!Constant.IS_TEST)
               removePicture(resultObj.getPictureSecond());
            // 存储新图
            record.setPictureSecond(pictureSecond);
         } else {
            record.setPictureSecond(resultObj.getPictureSecond());
         }
         record.setLevel(resultObj.getLevel());
         record.setRootClass(resultObj.getRootClass());
         record.setWeight(resultObj.getWeight());
         record.setIosClick(resultObj.getIosClick());
         record.setAndroidClick(resultObj.getAndroidClick());
         record.setCreatetime(resultObj.getCreatetime());
         record.setUpdatetime(new Date());
         if (labelId != null) {
            GoodsSubClassLabelMap map = new GoodsSubClassLabelMap();
            map.setGoodsSubClass(record);
            map.setLabel(new GoodsSubClassLabel(labelId));
            try {
               goodsSubClassLabelService.addSubClassLabelMap(map);
            } catch (Exception e) {
            }
         }
         goodsSubClassMapper.updateByPrimaryKey(record);
      }
      // 保存淘宝商品分类id
      String taobaoCids = record.getTaobaoCids();
      List<Long> tbCidList = new ArrayList<>();
      if (!StringUtil.isNullOrEmpty(taobaoCids) && !"null".equalsIgnoreCase(taobaoCids)) {
         String[] sts = taobaoCids.split(",");
         for (String st : sts) {
            String cid = st.split("-")[st.split("-").length - 1];
            tbCidList.add(Long.parseLong(cid));
         }
      }
      taoBaoClassService.saveSub(record.getId(), tbCidList);
   }
   @Override
   public int uploadPicture(GoodsSubClass record, MultipartFile file) throws Exception {
   /**
    * 上传图片
    *
    * @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 fileUrl = "ClassImg/" + UUID.randomUUID().toString().replace("-", "") + "." + type;
      boolean deleteFile = true;
      // 文件路径
      String filePath = "/img/GoodsSubClass/" + UUID.randomUUID().toString().replace("-", "") + "." + type;
      // 执行上传
      String fileLink = COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
      /* 修改图片时,先删除已存在图片 */
      String picture = record.getPicture();
      if (!StringUtil.isNullOrEmpty(picture))
         deleteFile = COSManager.getInstance().deleteFile(picture);
      return fileLink;
   }
      String uploadFilePath = null;
      /* 上传新图片 */
      if (deleteFile) {
         uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
   /**
    * 删除图片
    *
    * @param record
    * @throws Exception
    */
   public void removePicture(String picture) throws Exception {
      if (picture != null && picture.trim().length() > 0) {
         COSManager.getInstance().deleteFile(picture);
      }
      /* 更新数据库信息 */
      int result = 0;
      if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
         record.setPicture(uploadFilePath);
         result = goodsSubClassMapper.updateByPrimaryKeySelective(record);
      }
      return result;
   }
   /**
@@ -191,7 +338,7 @@
   @Override
   public List<GoodsSubClass> queryByRootId(Long rootId, Integer state) throws Exception {
      return goodsSubClassMapper.queryByRootId(rootId, state);
      return goodsSubClassMapper.queryByRootId(rootId, state, null);
   }
   @Override
@@ -199,80 +346,57 @@
      return goodsSubClassMapper.queryByPid(pid, state);
   }
   @Override
   public List<GoodsSubClass> queryByRootIdAndWeight(Long rootId,int type, int weight) throws Exception {
   public List<GoodsSubClass> queryByRootIdAndWeight(Long rootId, int type, int weight) throws Exception {
      return goodsSubClassMapper.queryByRootIdAndWeight(rootId, type, weight);
   }
   @Override
   public List<GoodsSubClass> queryByPidAndWeight(Long pid,int type, int weight) throws Exception {
   public List<GoodsSubClass> queryByPidAndWeight(Long pid, int type, int weight) throws Exception {
      return goodsSubClassMapper.queryByPidAndWeight(pid, type, weight);
   }
   @Override
   public List<GoodsSubClass> getGoodsSecondClass(Long rootId, Integer state) throws Exception {
      return goodsSubClassMapper.queryByRootId(rootId, state);
   public List<GoodsSubClass> getGoodsSecondClass(Long rootId, Integer state, Integer month) throws Exception {
      return goodsSubClassMapper.queryByRootId(rootId, state, month);
   }
   @Override
   @Cacheable(value="classCache",key="'getSubClassCache-'+#rootId +'-'+#state")
   public List<GoodsSubClass> getSubClassCache(Long rootId, Integer state) throws Exception {
      return getGoodsSecondClass(rootId, state);
   @Cacheable(value = "classCache", key = "'getSubClassCache-'+#rootId +'-'+#state")
   public List<GoodsSubClass> getSubClassCache(Long rootId, Integer state, Integer month) throws Exception {
      return getGoodsSecondClass(rootId, state, month);
   }
   @Override
   @Cacheable(value="classCache",key="'getSubClassByPrimaryKeyCache-'+#id")
   @Cacheable(value = "classCache", key = "'getSubClassByPrimaryKeyCache-'+#id")
   public GoodsSubClass getSubClassByPrimaryKeyCache(Long id) throws Exception {
      return selectByPrimaryKey(id);
   }
   /**
    *  统计一级之下的所有二级分类
    * @param rootId  一级id
    * 统计一级之下的所有二级分类
    *
    * @param rootId
    *            一级id
    * @returnL
    */
   @Override
   public int countByRootId(Long rootId) {
      return goodsSubClassMapper.countByRootId(rootId);
   }
   /**
    *  统计二级分类之下其他分类
    * @param rootId  一级id
    * 统计二级分类之下其他分类
    *
    * @param rootId
    *            一级id
    * @return
    */
   @Override
   public int countByPid(Long pid){
   public int countByPid(Long pid) {
      return goodsSubClassMapper.countByPid(pid);
   }
   /**
    *  统计一级之下的所有二级分类最大权重
    * @param rootId  一级id
    * @returnL
    */
   @Override
   public int getMaxWeightByRootId(Long rootId) {
      return goodsSubClassMapper.getMaxWeightByRootId(rootId);
   }
   /**
    *  统计二级分类之下其他分类最大权重
    * @param rootId  一级id
    * @return
    */
   @Override
   public int getMaxWeightByPid(Long pid){
      return goodsSubClassMapper.getMaxWeightByPid(pid);
   }
   @Override
   public void countClick(AcceptData acceptData, GoodsSubClass record) {
      if ("android".equalsIgnoreCase(acceptData.getPlatform())) {
@@ -292,4 +416,10 @@
      }
      goodsSubClassService.updateByPrimaryKeySelective(record);
   }
   @Override
   public List<GoodsSubClass> queryByListCid(List<Long> list) {
      return goodsSubClassMapper.queryByListCid(list);
   }
}