admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/brand/BrandInfoServiceImpl.java
@@ -1,535 +1,529 @@
package com.yeshi.fanli.service.impl.brand;
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 com.yeshi.fanli.entity.SystemEnum;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
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;
import com.yeshi.fanli.dto.ConfigParamsDTO;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.entity.brand.BrandClass;
import com.yeshi.fanli.entity.brand.BrandGoodsCahe;
import com.yeshi.fanli.entity.brand.BrandInfo;
import com.yeshi.fanli.entity.brand.BrandShopCahe;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoShop;
import com.yeshi.fanli.exception.brand.BrandInfoException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.brand.BrandClassShopService;
import com.yeshi.fanli.service.inter.brand.BrandGoodsCaheService;
import com.yeshi.fanli.service.inter.brand.BrandInfoService;
import com.yeshi.fanli.service.inter.brand.BrandShopCaheService;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.service.inter.lable.QualityGoodsService;
import com.yeshi.fanli.service.inter.order.OrderHongBaoMoneyComputeService;
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.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
import com.yeshi.fanli.vo.brand.BrandInfoVO;
import com.yeshi.fanli.vo.brand.TaoBaoShopVO;
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
@Service
public class BrandInfoServiceImpl implements BrandInfoService {
   @Resource
   private BrandInfoMapper brandInfoMapper;
   @Resource
   private OrderHongBaoMoneyComputeService orderHongBaoMoneyComputeService;
   @Resource
   private BrandClassShopService brandClassShopService;
   @Resource
   private BrandGoodsCaheService brandGoodsCaheService;
   @Resource
   private BrandShopCaheService brandShopCaheService;
   @Resource(name = "taskExecutor")
   private TaskExecutor executor;
   @Override
   public void saveObject(MultipartFile file, BrandInfo record) throws BrandInfoException {
      String name = record.getName();
      if (StringUtil.isNullOrEmpty(name))
         throw new BrandInfoException(1, "名称不能为空");
      BrandClass brandClass = record.getBrandClass();
      if (brandClass == null || brandClass.getId() == null)
         throw new BrandInfoException(1, "分类不能为空");
      BrandInfo existBrand = brandInfoMapper.selectByUniqueName(name.trim(), brandClass.getId(), record.getId());
      if (existBrand != null)
         throw new BrandInfoException(1, "该分类中已存在:" + name.trim());
      Integer state = record.getState();
      if (state == null) {
         state = 0;
         record.setState(state);
      }
      String picture = null;
      if (file != null) {
         try { // 图片上传
            picture = uploadPicture(file);
         } catch (Exception e) {
            throw new BrandInfoException(1, "图片上传失败");
         }
      }
      String searchKey = record.getSearchKey();
      if (!StringUtil.isNullOrEmpty(searchKey)) {
         if (StringUtil.isNullOrEmpty(record.getSearchKeyJD()))
            record.setSearchKeyJD(searchKey);
         if (StringUtil.isNullOrEmpty(record.getSearchKeyPDD()))
            record.setSearchKeyPDD(searchKey);
      }
      String shopKey = record.getShopKey();
      if (!StringUtil.isNullOrEmpty(shopKey)) {
         if (StringUtil.isNullOrEmpty(record.getShopKeyJD()))
            record.setShopKeyJD(shopKey);
         if (StringUtil.isNullOrEmpty(record.getShopKeyPDD()))
            record.setShopKeyPDD(shopKey);
      }
      record.setName(name.trim());
      record.setUpdateTime(new Date());
      Long id = record.getId();
      if (id == null) {
         record.setIcon(picture);
         record.setWeight(500.00);
         record.setGoodsTotal(0);
         record.setShopTotal(0);
         record.setCreateTime(new Date());
         brandInfoMapper.insert(record);
         BrandInfo resultObj = new BrandInfo();
         resultObj.setId(record.getId());
         resultObj.setOrder(Integer.valueOf(record.getId().toString()));
         brandInfoMapper.updateByPrimaryKeySelective(resultObj);
      } else {
         BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
         if (resultObj == null)
            throw new BrandInfoException(1, "修改内容已不存在");
         if (picture != null && picture.trim().length() > 0) {
            // 删除老图
            if (resultObj.getIcon() != null && resultObj.getIcon().trim().length() > 0 && !Constant.IS_TEST) {
               COSManager.getInstance().deleteFile(resultObj.getIcon());
            }
            ;
            // 存储新图
            record.setIcon(picture);
         } else {
            record.setIcon(resultObj.getIcon());
         }
         if (state == 0) {
            record.setGoodsTotal(0);
            record.setShopTotal(0);
         } else {
            record.setGoodsTotal(resultObj.getGoodsTotal());
            record.setShopTotal(resultObj.getShopTotal());
         }
         record.setOrder(resultObj.getOrder());
         record.setWeight(resultObj.getWeight());
         record.setCreateTime(resultObj.getCreateTime());
         brandInfoMapper.updateByPrimaryKey(record);
         if (state == 0) {
            executor.execute(new Runnable() {
               @Override
               public void run() {
                  brandGoodsCaheService.removeByBrandId(id);
                  brandShopCaheService.removeByBrandId(id);
               }
            });
         }
      }
      if (state == 1) {
         executor.execute(new Runnable() {
            @Override
            public void run() {
               BrandInfo numInfo = brandGoodsCaheService.addBrandGoods(record, SystemEnum.blks);
               BrandInfo update = new BrandInfo();
               update.setId(record.getId());
               update.setGoodsTotal(numInfo.getGoodsTotal());
               update.setShopTotal(numInfo.getShopTotal());
               brandInfoMapper.updateByPrimaryKeySelective(update);
            }
         });
      }
   }
   /**
    * 上传图片
    *
    * @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.brand.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
      // 执行上传
      return COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
   }
   @Override
   public void switchState(Long id) throws BrandInfoException {
      if (id == null) {
         throw new BrandInfoException(1, "请传递正确参数");
      }
      BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
      if (resultObj == null) {
         throw new BrandInfoException(1, "此内容已不存在");
      }
      Integer state = resultObj.getState();
      if (state == null || state == 0) {
         state = 1;
      } else {
         state = 0;
      }
      BrandInfo updateObj = new BrandInfo();
      updateObj.setId(id);
      updateObj.setState(state);
      brandInfoMapper.updateByPrimaryKeySelective(updateObj);
   }
   @Override
   public int deleteBatchByPrimaryKey(List<Long> list) {
      executor.execute(new Runnable() {
         @Override
         public void run() {
            for (Long id : list) {
               brandGoodsCaheService.removeByBrandId(id);
               brandShopCaheService.removeByBrandId(id);
            }
         }
      });
      return brandInfoMapper.deleteBatchByPrimaryKey(list);
   }
   @Override
   public void updateOrder(Long id, Integer moveType) throws BrandInfoException {
      if (moveType == null || (!moveType.equals(1) && !moveType.equals(-1)))
         throw new BrandInfoException(1, "传递的类型不正确");
      if (id == null)
         throw new BrandInfoException(1, "ID不能为空");
      BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
      if (resultObj == null)
         throw new BrandInfoException(1, "操作数据已不存在");
      Integer oldOrder = resultObj.getOrder();
      BrandInfo changeObj = brandInfoMapper.getByAdjoinOrder(resultObj.getBrandClass().getId(), oldOrder, moveType);
      if (changeObj == null)
         throw new BrandInfoException(1, "已经在最边缘,无可交换的位置");
      BrandInfo changeObj1 = new BrandInfo();
      changeObj1.setId(id);
      changeObj1.setOrder(changeObj.getOrder());
      BrandInfo changeObj2 = new BrandInfo();
      changeObj2.setId(changeObj.getId());
      changeObj2.setOrder(oldOrder);
      brandInfoMapper.updateByPrimaryKeySelective(changeObj1);
      brandInfoMapper.updateByPrimaryKeySelective(changeObj2);
   }
   @Override
   public List<BrandInfo> listQuery(long start, int count, List<String> keys, Long cid, Integer state,
         Integer showState) {
      return brandInfoMapper.listQuery(start, count, keys, cid, state, showState);
   }
   @Override
   public long countQuery(List<String> keys, Long cid, Integer state, Integer showState) {
      return brandInfoMapper.countQuery(keys, cid, state, showState);
   }
   @Override
   @Cacheable(value = "brandCache", key = "'listValidBrandInfoCache-'+#cid")
   public List<BrandInfo> listValidBrandInfoCache(Long cid,SystemEnum system) {
      List<BrandInfo> listInfo = new ArrayList<BrandInfo>();
      List<TaoBaoShop> listShop = brandClassShopService.listEffectiveClassShop(cid,system);
      if (listShop == null || listShop.size() == 0)
         return listInfo;
      for (TaoBaoShop taoBaoShop : listShop) {
         BrandInfo brandInfo = new BrandInfo();
         brandInfo.setId(taoBaoShop.getId());
         brandInfo.setName(taoBaoShop.getShopName());
         brandInfo.setIcon(taoBaoShop.getShopIcon());
         listInfo.add(brandInfo);
      }
      return listInfo;
   }
   @Override
   @Cacheable(value = "brandCache", key = "'listBrandInfoCache-'+#start+'-'+#start +'-'+#cid+'-'+#platform+'-'+#version+'-'+#system")
   public List<BrandInfoVO> listBrandInfoCache(long start, int count, Long cid, String platform, String version,SystemEnum system) {
      List<BrandInfoVO> listInfo = new ArrayList<BrandInfoVO>();
      List<TaoBaoShopVO> listShop = brandClassShopService.listEffectiveShop(start, count, cid, platform, version,system);
      if (listShop == null || listShop.size() == 0)
         return listInfo;
      for (TaoBaoShopVO taoBaoShopVO : listShop) {
         String shopNameCustom = taoBaoShopVO.getShopNameCustom();
         if (!StringUtil.isNullOrEmpty(shopNameCustom)) {
            taoBaoShopVO.setShopName(shopNameCustom);
         }
         String shopIconCustom = taoBaoShopVO.getShopIconCustom();
         if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
            taoBaoShopVO.setShopIcon(shopIconCustom);
         }
         BrandInfoVO brandInfoVO = new BrandInfoVO();
         brandInfoVO.setId(taoBaoShopVO.getId());
         brandInfoVO.setName(taoBaoShopVO.getShopName());
         brandInfoVO.setIcon(taoBaoShopVO.getShopIcon());
         brandInfoVO.setListGoods(taoBaoShopVO.getListGoodsVO());
         listInfo.add(brandInfoVO);
      }
      return listInfo;
   }
   @Override
   public long countBrandInfo(Long cid) {
      return brandClassShopService.countBrandShopinfo(cid);
   }
   @Override
   public void removeAgoByDate(Date date) {
      // 删除商品
      brandGoodsCaheService.removeAgoByDate(date);
      // 删除店铺
      brandShopCaheService.removeAgoByDate(date);
      long count = brandInfoMapper.countValidByCid(null);
      if (count == 0)
         return;
      for (int page = 0; page < (count / 100) + 1; page++) {
         updateTotalGoods(page * 100, 100);
      }
   }
   /**
    * 更新商品数量
    *
    * @param start
    * @param count
    */
   private void updateTotalGoods(long start, int count) {
      List<BrandInfo> list = brandInfoMapper.listValidAll(start, count);
      if (list == null || list.size() == 0)
         return;
      for (BrandInfo brandInfo : list) {
         long goodsTotal = brandGoodsCaheService.countByBrandId(brandInfo.getId());
         BrandInfo updateInfo = new BrandInfo();
         updateInfo.setId(brandInfo.getId());
         updateInfo.setGoodsTotal((int) goodsTotal);
         List<BrandShopCahe> listshop = brandShopCaheService.getByBrandId(brandInfo.getId());
         if (listshop == null) {
            updateInfo.setShopTotal(0);
         } else {
            updateInfo.setShopTotal(listshop.size());
         }
         updateInfo.setWeight(Math.random() * 1000);
         brandInfoMapper.updateByPrimaryKeySelective(updateInfo);
      }
   }
   @Override
   public void addShopAndGoods(long start, int count) {
      List<BrandInfo> list = brandInfoMapper.listValidAll(start, count);
      addShopAndGoods(list);
   }
   @Override
   public void addShopAndGoods(List<BrandInfo> list) {
      if (list == null || list.size() == 0)
         return;
      // long startTime = java.lang.System.currentTimeMillis();
      for (BrandInfo brandInfo : list) {
         try {
            String name = brandInfo.getName();
            String searchKey = brandInfo.getSearchKey();
            if (StringUtil.isNullOrEmpty(name) && StringUtil.isNullOrEmpty(searchKey))
               continue;
            // 添加商品
            BrandInfo numInfo = brandGoodsCaheService.addBrandGoods(brandInfo, SystemEnum.blks);
            BrandInfo updateInfo = new BrandInfo();
            updateInfo.setId(brandInfo.getId());
            updateInfo.setWeight(Math.random() * 1000);
            updateInfo.setGoodsTotal(numInfo.getGoodsTotal());
            updateInfo.setShopTotal(numInfo.getShopTotal());
            updateInfo.setUpdateTime(new Date());
            brandInfoMapper.updateByPrimaryKeySelective(updateInfo);
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
         }
      }
      // long endTime = java.lang.System.currentTimeMillis();
   }
   @Override
   public long countValidByCid(Long cid) {
      return brandInfoMapper.countValidByCid(cid);
   }
   @Override
   public long countValidByCidToApp(Long cid, SystemEnum system) {
      return brandInfoMapper.countValidByCidToApp(cid,system);
   }
   @Override
   @Cacheable(value = "brandCache", key = "'listValidByCidToApp-'+#cid+'-'+#system")
   public List<BrandInfo> listValidByCidToApp(Long cid, SystemEnum system) {
      if (cid == null)
         return null;
      return brandInfoMapper.listValidByCidToApp(cid,system);
   }
   @Cacheable(value = "brandCache", key = "'listValidToApp-'+#start+'-'+#cid+'-'+#system")
   @Override
   public List<BrandInfoVO> listValidToApp(long start, int count, Long cid, String platform, String version,SystemEnum system) {
      List<BrandInfoVO> list = brandInfoMapper.listBrandInfoVO(start, count, cid,system);
      if (list == null || list.size() == 0)
         return null;
      ConfigParamsDTO configParamsDTO = orderHongBaoMoneyComputeService.getShowComputeRate(platform, version,system);
      List<BrandInfoVO> listInfo = new ArrayList<BrandInfoVO>();
      for (int i = 0; i < list.size(); i++) {
         BrandInfoVO brand = list.get(i);
         if (brand == null)
            continue;
         List<BrandGoodsCahe> listGoods = brandGoodsCaheService.getByBrandId(1, 3, brand.getId());
         if (listGoods == null || listGoods.size() < 3)
            continue;
         List<GoodsDetailVO> listGoodsVO = new ArrayList<GoodsDetailVO>();
         for (BrandGoodsCahe brandGoods : listGoods) {
            JDGoods goodsJD = brandGoods.getGoodsJD();
            if (goodsJD != null) {
               listGoodsVO.add(GoodsDetailVOFactory.convertJDGoods(goodsJD, configParamsDTO));
               continue;
            }
            TaoBaoGoodsBrief goodsTB = brandGoods.getGoodsTB();
            if (goodsTB != null) {
               listGoodsVO.add(GoodsDetailVOFactory.convertTaoBao(goodsTB, configParamsDTO));
               continue;
            }
            PDDGoodsDetail goodsPDD = brandGoods.getGoodsPDD();
            if (goodsPDD != null) {
               listGoodsVO.add(GoodsDetailVOFactory.convertPDDGoods(goodsPDD, configParamsDTO));
               continue;
            }
         }
         if (listGoodsVO.size() >= 3) {
            brand.setListGoods(listGoodsVO);
            listInfo.add(brand);
         }
      }
      return listInfo;
   }
   @Override
   @Cacheable(value = "brandCache", key = "'listByAlikeName-'+#key+'-'+#platform+'-'+#version+'-'+#system")
   public BrandInfoVO listByAlikeName(String key, String platform, String version,SystemEnum system) {
      if (StringUtil.isNullOrEmpty(key))
         return null;
      List<BrandInfoVO> list = brandInfoMapper.listByAlikeName(key);
      if (list == null || list.size() == 0)
         return null;
      ConfigParamsDTO configParamsDTO = orderHongBaoMoneyComputeService.getShowComputeRate(platform, version, system);
      BrandInfoVO brand = list.get(0);
      List<BrandGoodsCahe> listGoods = brandGoodsCaheService.getByBrandId(1, 3, brand.getId());
      if (listGoods == null || listGoods.size() < 3)
         return null;
      List<GoodsDetailVO> listGoodsVO = new ArrayList<GoodsDetailVO>();
      for (BrandGoodsCahe brandGoods : listGoods) {
         JDGoods goodsJD = brandGoods.getGoodsJD();
         if (goodsJD != null) {
            listGoodsVO.add(GoodsDetailVOFactory.convertJDGoods(goodsJD, configParamsDTO));
            continue;
         }
         TaoBaoGoodsBrief goodsTB = brandGoods.getGoodsTB();
         if (goodsTB != null) {
            listGoodsVO.add(GoodsDetailVOFactory.convertTaoBao(goodsTB, configParamsDTO));
            continue;
         }
         PDDGoodsDetail goodsPDD = brandGoods.getGoodsPDD();
         if (goodsPDD != null) {
            listGoodsVO.add(GoodsDetailVOFactory.convertPDDGoods(goodsPDD, configParamsDTO));
            continue;
         }
      }
      if (listGoodsVO.size() < 3)
         return null;
      brand.setListGoods(listGoodsVO);
      return brand;
   }
   @Override
   public List<BrandInfo> listValidOrderByUpdateTime(int page, int pageSize) {
      return brandInfoMapper.listValidAll((page - 1) * pageSize, pageSize);
   }
}
package com.yeshi.fanli.service.impl.brand;
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 com.yeshi.fanli.entity.SystemEnum;
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.mybatis.brand.BrandInfoMapper;
import com.yeshi.fanli.dto.GoodsMoneyConfigParamsDTO;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.entity.brand.BrandClass;
import com.yeshi.fanli.entity.brand.BrandGoodsCahe;
import com.yeshi.fanli.entity.brand.BrandInfo;
import com.yeshi.fanli.entity.brand.BrandShopCahe;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.goods.facade.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.common.entity.taobao.TaoBaoShop;
import com.yeshi.fanli.exception.brand.BrandInfoException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.brand.BrandClassShopService;
import com.yeshi.fanli.service.inter.brand.BrandGoodsCaheService;
import com.yeshi.fanli.service.inter.brand.BrandInfoService;
import com.yeshi.fanli.service.inter.brand.BrandShopCaheService;
import com.yeshi.fanli.service.inter.order.OrderHongBaoMoneyComputeService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
import com.yeshi.fanli.vo.brand.BrandInfoVO;
import com.yeshi.fanli.vo.brand.TaoBaoShopVO;
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
@Service
public class BrandInfoServiceImpl implements BrandInfoService {
   @Resource
   private BrandInfoMapper brandInfoMapper;
   @Resource
   private OrderHongBaoMoneyComputeService orderHongBaoMoneyComputeService;
   @Resource
   private BrandClassShopService brandClassShopService;
   @Resource
   private BrandGoodsCaheService brandGoodsCaheService;
   @Resource
   private BrandShopCaheService brandShopCaheService;
   @Resource(name = "taskExecutor")
   private TaskExecutor executor;
   @Override
   public void saveObject(MultipartFile file, BrandInfo record) throws BrandInfoException {
      String name = record.getName();
      if (StringUtil.isNullOrEmpty(name))
         throw new BrandInfoException(1, "名称不能为空");
      BrandClass brandClass = record.getBrandClass();
      if (brandClass == null || brandClass.getId() == null)
         throw new BrandInfoException(1, "分类不能为空");
      BrandInfo existBrand = brandInfoMapper.selectByUniqueName(name.trim(), brandClass.getId(), record.getId());
      if (existBrand != null)
         throw new BrandInfoException(1, "该分类中已存在:" + name.trim());
      Integer state = record.getState();
      if (state == null) {
         state = 0;
         record.setState(state);
      }
      String picture = null;
      if (file != null) {
         try { // 图片上传
            picture = uploadPicture(file);
         } catch (Exception e) {
            throw new BrandInfoException(1, "图片上传失败");
         }
      }
      String searchKey = record.getSearchKey();
      if (!StringUtil.isNullOrEmpty(searchKey)) {
         if (StringUtil.isNullOrEmpty(record.getSearchKeyJD()))
            record.setSearchKeyJD(searchKey);
         if (StringUtil.isNullOrEmpty(record.getSearchKeyPDD()))
            record.setSearchKeyPDD(searchKey);
      }
      String shopKey = record.getShopKey();
      if (!StringUtil.isNullOrEmpty(shopKey)) {
         if (StringUtil.isNullOrEmpty(record.getShopKeyJD()))
            record.setShopKeyJD(shopKey);
         if (StringUtil.isNullOrEmpty(record.getShopKeyPDD()))
            record.setShopKeyPDD(shopKey);
      }
      record.setName(name.trim());
      record.setUpdateTime(new Date());
      Long id = record.getId();
      if (id == null) {
         record.setIcon(picture);
         record.setWeight(500.00);
         record.setGoodsTotal(0);
         record.setShopTotal(0);
         record.setCreateTime(new Date());
         brandInfoMapper.insert(record);
         BrandInfo resultObj = new BrandInfo();
         resultObj.setId(record.getId());
         resultObj.setOrder(Integer.valueOf(record.getId().toString()));
         brandInfoMapper.updateByPrimaryKeySelective(resultObj);
      } else {
         BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
         if (resultObj == null)
            throw new BrandInfoException(1, "修改内容已不存在");
         if (picture != null && picture.trim().length() > 0) {
            // 删除老图
            if (resultObj.getIcon() != null && resultObj.getIcon().trim().length() > 0 && !Constant.IS_TEST) {
               COSManager.getInstance().deleteFile(resultObj.getIcon());
            }
            ;
            // 存储新图
            record.setIcon(picture);
         } else {
            record.setIcon(resultObj.getIcon());
         }
         if (state == 0) {
            record.setGoodsTotal(0);
            record.setShopTotal(0);
         } else {
            record.setGoodsTotal(resultObj.getGoodsTotal());
            record.setShopTotal(resultObj.getShopTotal());
         }
         record.setOrder(resultObj.getOrder());
         record.setWeight(resultObj.getWeight());
         record.setCreateTime(resultObj.getCreateTime());
         brandInfoMapper.updateByPrimaryKey(record);
         if (state == 0) {
            executor.execute(new Runnable() {
               @Override
               public void run() {
                  brandGoodsCaheService.removeByBrandId(id);
                  brandShopCaheService.removeByBrandId(id);
               }
            });
         }
      }
      if (state == 1) {
         executor.execute(new Runnable() {
            @Override
            public void run() {
               BrandInfo numInfo = brandGoodsCaheService.addBrandGoods(record, SystemEnum.blks);
               BrandInfo update = new BrandInfo();
               update.setId(record.getId());
               update.setGoodsTotal(numInfo.getGoodsTotal());
               update.setShopTotal(numInfo.getShopTotal());
               brandInfoMapper.updateByPrimaryKeySelective(update);
            }
         });
      }
   }
   /**
    * 上传图片
    *
    * @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.brand.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
      // 执行上传
      return COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
   }
   @Override
   public void switchState(Long id) throws BrandInfoException {
      if (id == null) {
         throw new BrandInfoException(1, "请传递正确参数");
      }
      BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
      if (resultObj == null) {
         throw new BrandInfoException(1, "此内容已不存在");
      }
      Integer state = resultObj.getState();
      if (state == null || state == 0) {
         state = 1;
      } else {
         state = 0;
      }
      BrandInfo updateObj = new BrandInfo();
      updateObj.setId(id);
      updateObj.setState(state);
      brandInfoMapper.updateByPrimaryKeySelective(updateObj);
   }
   @Override
   public int deleteBatchByPrimaryKey(List<Long> list) {
      executor.execute(new Runnable() {
         @Override
         public void run() {
            for (Long id : list) {
               brandGoodsCaheService.removeByBrandId(id);
               brandShopCaheService.removeByBrandId(id);
            }
         }
      });
      return brandInfoMapper.deleteBatchByPrimaryKey(list);
   }
   @Override
   public void updateOrder(Long id, Integer moveType) throws BrandInfoException {
      if (moveType == null || (!moveType.equals(1) && !moveType.equals(-1)))
         throw new BrandInfoException(1, "传递的类型不正确");
      if (id == null)
         throw new BrandInfoException(1, "ID不能为空");
      BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
      if (resultObj == null)
         throw new BrandInfoException(1, "操作数据已不存在");
      Integer oldOrder = resultObj.getOrder();
      BrandInfo changeObj = brandInfoMapper.getByAdjoinOrder(resultObj.getBrandClass().getId(), oldOrder, moveType);
      if (changeObj == null)
         throw new BrandInfoException(1, "已经在最边缘,无可交换的位置");
      BrandInfo changeObj1 = new BrandInfo();
      changeObj1.setId(id);
      changeObj1.setOrder(changeObj.getOrder());
      BrandInfo changeObj2 = new BrandInfo();
      changeObj2.setId(changeObj.getId());
      changeObj2.setOrder(oldOrder);
      brandInfoMapper.updateByPrimaryKeySelective(changeObj1);
      brandInfoMapper.updateByPrimaryKeySelective(changeObj2);
   }
   @Override
   public List<BrandInfo> listQuery(long start, int count, List<String> keys, Long cid, Integer state,
         Integer showState) {
      return brandInfoMapper.listQuery(start, count, keys, cid, state, showState);
   }
   @Override
   public long countQuery(List<String> keys, Long cid, Integer state, Integer showState) {
      return brandInfoMapper.countQuery(keys, cid, state, showState);
   }
   @Override
   @Cacheable(value = "brandCache", key = "'listValidBrandInfoCache-'+#cid")
   public List<BrandInfo> listValidBrandInfoCache(Long cid,SystemEnum system) {
      List<BrandInfo> listInfo = new ArrayList<BrandInfo>();
      List<TaoBaoShop> listShop = brandClassShopService.listEffectiveClassShop(cid,system);
      if (listShop == null || listShop.size() == 0)
         return listInfo;
      for (TaoBaoShop taoBaoShop : listShop) {
         BrandInfo brandInfo = new BrandInfo();
         brandInfo.setId(taoBaoShop.getId());
         brandInfo.setName(taoBaoShop.getShopName());
         brandInfo.setIcon(taoBaoShop.getShopIcon());
         listInfo.add(brandInfo);
      }
      return listInfo;
   }
   @Override
   @Cacheable(value = "brandCache", key = "'listBrandInfoCache-'+#start+'-'+#start +'-'+#cid+'-'+#platform+'-'+#version+'-'+#system")
   public List<BrandInfoVO> listBrandInfoCache(long start, int count, Long cid, String platform, String version,SystemEnum system) {
      List<BrandInfoVO> listInfo = new ArrayList<BrandInfoVO>();
      List<TaoBaoShopVO> listShop = brandClassShopService.listEffectiveShop(start, count, cid, platform, version,system);
      if (listShop == null || listShop.size() == 0)
         return listInfo;
      for (TaoBaoShopVO taoBaoShopVO : listShop) {
         String shopNameCustom = taoBaoShopVO.getShopNameCustom();
         if (!StringUtil.isNullOrEmpty(shopNameCustom)) {
            taoBaoShopVO.setShopName(shopNameCustom);
         }
         String shopIconCustom = taoBaoShopVO.getShopIconCustom();
         if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
            taoBaoShopVO.setShopIcon(shopIconCustom);
         }
         BrandInfoVO brandInfoVO = new BrandInfoVO();
         brandInfoVO.setId(taoBaoShopVO.getId());
         brandInfoVO.setName(taoBaoShopVO.getShopName());
         brandInfoVO.setIcon(taoBaoShopVO.getShopIcon());
         brandInfoVO.setListGoods(taoBaoShopVO.getListGoodsVO());
         listInfo.add(brandInfoVO);
      }
      return listInfo;
   }
   @Override
   public long countBrandInfo(Long cid) {
      return brandClassShopService.countBrandShopinfo(cid);
   }
   @Override
   public void removeAgoByDate(Date date) {
      // 删除商品
      brandGoodsCaheService.removeAgoByDate(date);
      // 删除店铺
      brandShopCaheService.removeAgoByDate(date);
      long count = brandInfoMapper.countValidByCid(null);
      if (count == 0)
         return;
      for (int page = 0; page < (count / 100) + 1; page++) {
         updateTotalGoods(page * 100, 100);
      }
   }
   /**
    * 更新商品数量
    *
    * @param start
    * @param count
    */
   private void updateTotalGoods(long start, int count) {
      List<BrandInfo> list = brandInfoMapper.listValidAll(start, count);
      if (list == null || list.size() == 0)
         return;
      for (BrandInfo brandInfo : list) {
         long goodsTotal = brandGoodsCaheService.countByBrandId(brandInfo.getId());
         BrandInfo updateInfo = new BrandInfo();
         updateInfo.setId(brandInfo.getId());
         updateInfo.setGoodsTotal((int) goodsTotal);
         List<BrandShopCahe> listshop = brandShopCaheService.getByBrandId(brandInfo.getId());
         if (listshop == null) {
            updateInfo.setShopTotal(0);
         } else {
            updateInfo.setShopTotal(listshop.size());
         }
         updateInfo.setWeight(Math.random() * 1000);
         brandInfoMapper.updateByPrimaryKeySelective(updateInfo);
      }
   }
   @Override
   public void addShopAndGoods(long start, int count) {
      List<BrandInfo> list = brandInfoMapper.listValidAll(start, count);
      addShopAndGoods(list);
   }
   @Override
   public void addShopAndGoods(List<BrandInfo> list) {
      if (list == null || list.size() == 0)
         return;
      // long startTime = java.lang.System.currentTimeMillis();
      for (BrandInfo brandInfo : list) {
         try {
            String name = brandInfo.getName();
            String searchKey = brandInfo.getSearchKey();
            if (StringUtil.isNullOrEmpty(name) && StringUtil.isNullOrEmpty(searchKey))
               continue;
            // 添加商品
            BrandInfo numInfo = brandGoodsCaheService.addBrandGoods(brandInfo, SystemEnum.blks);
            BrandInfo updateInfo = new BrandInfo();
            updateInfo.setId(brandInfo.getId());
            updateInfo.setWeight(Math.random() * 1000);
            updateInfo.setGoodsTotal(numInfo.getGoodsTotal());
            updateInfo.setShopTotal(numInfo.getShopTotal());
            updateInfo.setUpdateTime(new Date());
            brandInfoMapper.updateByPrimaryKeySelective(updateInfo);
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
         }
      }
      // long endTime = java.lang.System.currentTimeMillis();
   }
   @Override
   public long countValidByCid(Long cid) {
      return brandInfoMapper.countValidByCid(cid);
   }
   @Override
   public long countValidByCidToApp(Long cid, SystemEnum system) {
      return brandInfoMapper.countValidByCidToApp(cid,system);
   }
   @Override
   @Cacheable(value = "brandCache", key = "'listValidByCidToApp-'+#cid+'-'+#system")
   public List<BrandInfo> listValidByCidToApp(Long cid, SystemEnum system) {
      if (cid == null)
         return null;
      return brandInfoMapper.listValidByCidToApp(cid,system);
   }
   @Cacheable(value = "brandCache", key = "'listValidToApp-'+#start+'-'+#cid+'-'+#system")
   @Override
   public List<BrandInfoVO> listValidToApp(long start, int count, Long cid, String platform, String version,SystemEnum system) {
      List<BrandInfoVO> list = brandInfoMapper.listBrandInfoVO(start, count, cid,system);
      if (list == null || list.size() == 0)
         return null;
      GoodsMoneyConfigParamsDTO configParamsDTO = orderHongBaoMoneyComputeService.getShowComputeRate(platform, version,system);
      List<BrandInfoVO> listInfo = new ArrayList<BrandInfoVO>();
      for (int i = 0; i < list.size(); i++) {
         BrandInfoVO brand = list.get(i);
         if (brand == null)
            continue;
         List<BrandGoodsCahe> listGoods = brandGoodsCaheService.getByBrandId(1, 3, brand.getId());
         if (listGoods == null || listGoods.size() < 3)
            continue;
         List<GoodsDetailVO> listGoodsVO = new ArrayList<GoodsDetailVO>();
         for (BrandGoodsCahe brandGoods : listGoods) {
            JDGoods goodsJD = brandGoods.getGoodsJD();
            if (goodsJD != null) {
               listGoodsVO.add(GoodsDetailVOFactory.convertJDGoods(goodsJD, configParamsDTO));
               continue;
            }
            TaoBaoGoodsBrief goodsTB = brandGoods.getGoodsTB();
            if (goodsTB != null) {
               listGoodsVO.add(GoodsDetailVOFactory.convertTaoBao(goodsTB, configParamsDTO));
               continue;
            }
            PDDGoodsDetail goodsPDD = brandGoods.getGoodsPDD();
            if (goodsPDD != null) {
               listGoodsVO.add(GoodsDetailVOFactory.convertPDDGoods(goodsPDD, configParamsDTO));
               continue;
            }
         }
         if (listGoodsVO.size() >= 3) {
            brand.setListGoods(listGoodsVO);
            listInfo.add(brand);
         }
      }
      return listInfo;
   }
   @Override
   @Cacheable(value = "brandCache", key = "'listByAlikeName-'+#key+'-'+#platform+'-'+#version+'-'+#system")
   public BrandInfoVO listByAlikeName(String key, String platform, String version,SystemEnum system) {
      if (StringUtil.isNullOrEmpty(key))
         return null;
      List<BrandInfoVO> list = brandInfoMapper.listByAlikeName(key);
      if (list == null || list.size() == 0)
         return null;
      GoodsMoneyConfigParamsDTO configParamsDTO = orderHongBaoMoneyComputeService.getShowComputeRate(platform, version, system);
      BrandInfoVO brand = list.get(0);
      List<BrandGoodsCahe> listGoods = brandGoodsCaheService.getByBrandId(1, 3, brand.getId());
      if (listGoods == null || listGoods.size() < 3)
         return null;
      List<GoodsDetailVO> listGoodsVO = new ArrayList<GoodsDetailVO>();
      for (BrandGoodsCahe brandGoods : listGoods) {
         JDGoods goodsJD = brandGoods.getGoodsJD();
         if (goodsJD != null) {
            listGoodsVO.add(GoodsDetailVOFactory.convertJDGoods(goodsJD, configParamsDTO));
            continue;
         }
         TaoBaoGoodsBrief goodsTB = brandGoods.getGoodsTB();
         if (goodsTB != null) {
            listGoodsVO.add(GoodsDetailVOFactory.convertTaoBao(goodsTB, configParamsDTO));
            continue;
         }
         PDDGoodsDetail goodsPDD = brandGoods.getGoodsPDD();
         if (goodsPDD != null) {
            listGoodsVO.add(GoodsDetailVOFactory.convertPDDGoods(goodsPDD, configParamsDTO));
            continue;
         }
      }
      if (listGoodsVO.size() < 3)
         return null;
      brand.setListGoods(listGoodsVO);
      return brand;
   }
   @Override
   public List<BrandInfo> listValidOrderByUpdateTime(int page, int pageSize) {
      return brandInfoMapper.listValidAll((page - 1) * pageSize, pageSize);
   }
}