admin
2019-01-23 e179d8660bf4837455a38c24e74320143e0d2e17
fanli/src/main/java/com/yeshi/fanli/service/impl/lable/QualityFactoryServiceImpl.java
@@ -28,6 +28,7 @@
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.QualityFactoryException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsUpdateException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.service.inter.lable.LabelService;
@@ -71,23 +72,8 @@
   private TaoBaoGoodsUpdateService taoBaoGoodsUpdateService;
   @Override
   public int insert(QualityFactory record) throws QualityFactoryException {
      return qualityFactoryMapper.insert(record);
   }
   @Override
   public int updateByPrimaryKey(QualityFactory record) throws QualityFactoryException {
      return qualityFactoryMapper.updateByPrimaryKey(record);
   }
   @Override
   public int updateByPrimaryKeySelective(QualityFactory record) throws QualityFactoryException {
      return qualityFactoryMapper.updateByPrimaryKeySelective(record);
   }
   @Override
   public int deleteByPrimaryKey(Long id) throws QualityFactoryException {
      return qualityFactoryMapper.deleteByPrimaryKey(id);
   }
   @Override
@@ -162,17 +148,6 @@
   @Override
   @Transactional
   public void deleteBatchByPrimaryKey(List<String> ids) throws QualityFactoryException {
      if (ids != null) {
         for (String id : ids) {
            qualityFactoryMapper.deleteByPrimaryKey(Long.parseLong(id));
         }
      }
   }
   @Override
   @Transactional
   public void deleteByGoodsId(List<String> ids) throws QualityFactoryException {
      if (ids != null && ids.size() > 0) {
@@ -180,11 +155,6 @@
            qualityFactoryMapper.deleteByGoodsId(Long.parseLong(goodsId));
         }
      }
   }
   @Override
   public int removeStorageGoods(String doTime, Long gcids) throws QualityFactoryException {
      return qualityFactoryMapper.removeStorageGoods(doTime, gcids);
   }
   @Override
@@ -279,11 +249,7 @@
         }
      }
      List<String> strList = new ArrayList<String>();
      for (Long id : auctionIdList) {
         strList.add(id + "");
      }
      List<TaoBaoGoodsBrief> existList = taoBaoGoodsBriefRecordMapper.queryGoodsByAuctionId(strList);
      List<TaoBaoGoodsBrief> existList = taoBaoGoodsBriefRecordMapper.queryByAuctionIdList(auctionIdList);
      List<QualityFactory> addList = new ArrayList<QualityFactory>();
      List<LabelGoods> addLabelList = new ArrayList<LabelGoods>();
@@ -320,10 +286,10 @@
            TaoBaoGoodsBrief currentGoodsBrief = taoBaoGoodsBriefList.get(0);
            goodsBrief.setId(currentGoodsBrief.getId());
            // 非空项 处理
            setTaoBaoGoodsBriefDefault(goodsBrief);
            // 更新商品最新信息
            taoBaoGoodsBriefService.updateByPrimaryKey(goodsBrief);
            taoBaoGoodsUpdateService.updateTaoBaoGoods(goodsBrief);
         } else {
            // 插入本地数据库
@@ -361,7 +327,7 @@
            // 更新商品
            QualityFactory selectionGoods = selectionList.get(0);
            selectionGoods.setUpdatetime(new Date());
            qualityFactoryMapper.updateByPrimaryKey(selectionGoods);
            qualityFactoryMapper.updateByPrimaryKeySelective(selectionGoods);
         }
         if (listLabs.size() > 0) {
@@ -419,7 +385,7 @@
         }
         // 修改权重
         // selectionGoods.setUpdateUser(admin);
         selectionGoods.setUpdateUser(admin);
         selectionGoods.setWeight(resultWeight);
         qualityFactoryMapper.updateByPrimaryKeySelective(selectionGoods);
      }
@@ -443,25 +409,27 @@
   @Override
   public List<Long> queryNeedUpdate(long start, int count, int hour) {
      return qualityFactoryMapper.queryNeedUpdate(0, count, hour);
      return qualityFactoryMapper.queryNeedUpdate(start, count, hour);
   }
   /**
    * 获取缓存
    * @param key 键
    * @param title 标签名称
    *
    * @param key
    *            键
    * @param title
    *            标签名称
    * @return
    */
   public String getLabelIdByRedis (String key, String title) throws Exception {
   public String getLabelIdByRedis(String key, String title) throws Exception {
      String value = redisManager.getCommonString(key);
      if (StringUtil.isNullOrEmpty(value)) {
         // id为空时系统进行查询
         List<Label> list9K9 = labelService.selectByTitle(title);
         if (list9K9 != null && list9K9.size() > 0) {
            Long id = list9K9.get(0).getId();
            if (id != null) {
@@ -471,10 +439,10 @@
            }
         }
      }
      return value;
   }
   @Override
   @Transactional
   public void updateGoodsFactory(List<Long> listId) {
@@ -488,9 +456,11 @@
         return;
      }
      // 删除精选库
      List<Long> listDeleteQuality = new ArrayList<Long>();
      // 删除标签
      List<Long> listDeleteLabel = new ArrayList<Long>();
      List<QualityFactory> listUpdateQuality = new ArrayList<QualityFactory>();
      List<TaoBaoGoodsBrief> listUpdateGoodsBrief = new ArrayList<TaoBaoGoodsBrief>();
@@ -500,37 +470,37 @@
      List<LabelGoods> listLabelAdd = new ArrayList<LabelGoods>();
      try {
         List<Long> list9Class = new ArrayList<Long>();
         Map<String, Label> map = new HashMap<String, Label>();
         if (Constant.IS_OUTNET) {
            String value9K9 = getLabelIdByRedis("lab-9K9", "9.9");
            String value19K9 = getLabelIdByRedis("lab-19K9", "19.9");
            String value29K9 = getLabelIdByRedis("lab-29K9", "29.9");
            String value49K9 = getLabelIdByRedis("lab-49K9", "49.9");
            if (!StringUtil.isNullOrEmpty(value9K9)) {
               map.put("9.9", new Label(Long.parseLong(value9K9)));
               list9Class.add(Long.parseLong(value9K9));
               list9Class.add(Long.parseLong(value9K9));
            }
            if (!StringUtil.isNullOrEmpty(value19K9)) {
               map.put("19.9", new Label(Long.parseLong(value19K9)));
               list9Class.add(Long.parseLong(value19K9));
               list9Class.add(Long.parseLong(value19K9));
            }
            if (!StringUtil.isNullOrEmpty(value29K9)) {
               map.put("29.9", new Label(Long.parseLong(value29K9)));
               list9Class.add(Long.parseLong(value29K9));
               list9Class.add(Long.parseLong(value29K9));
            }
            if (!StringUtil.isNullOrEmpty(value49K9)) {
               map.put("49.9", new Label(Long.parseLong(value49K9)));
               list9Class.add(Long.parseLong(value49K9));
               list9Class.add(Long.parseLong(value49K9));
            }
         }
         listQuality.parallelStream().forEach(quality -> {
@@ -567,6 +537,8 @@
                  /* 无券信息 移除精选库 */
                  listDeleteQuality.add(quality.getId());
                  // 移除标签
                  listDeleteLabel.add(goods.getId());
               } else {
                  /* 无券信息 移除精选库 */
@@ -575,13 +547,17 @@
                     // 移除精选库
                     listDeleteQuality.add(quality.getId());
                     // 移除标签
                     listDeleteLabel.add(goods.getId());
                  } else {
                     /* 更新商品信息 */
                     TaoBaoGoodsBrief updateGoods = taoBaoGoodsUpdateService
                           .getUpdateTaoBaoGoodsBrief(newGoodsBrief);
                     updateGoods.setId(id);
                     taoBaoGoodsBriefService.setGoodsBriefDefault(updateGoods);
                     listUpdateGoodsBrief.add(updateGoods);
                     /* 更新精选库 信息时间 */
@@ -619,7 +595,7 @@
         // 更新商品信息
         if (listUpdateGoodsBrief.size() > 0) {
            taoBaoGoodsBriefService.updateBatchSelective(listUpdateGoodsBrief);
            taoBaoGoodsUpdateService.updateTaoBaoGoods(listUpdateGoodsBrief);
         }
         // 更新精选库
@@ -630,6 +606,13 @@
         // 批量删除精选库
         if (listDeleteQuality.size() > 0) {
            qualityFactoryMapper.deleteBatchByPrimaryKey(listDeleteQuality);
            // 批量删除限时抢购
            qualityFlashSaleMapper.deleteBatchByQualityID(listDeleteQuality);
         }
         // 删除对应标签
         if (listDeleteLabel.size() > 0) {
            labelGoodsMapper.deleteBatchByGoodsId(listDeleteLabel);
         }
         // 添加标签
@@ -648,140 +631,6 @@
         } catch (Exception e1) {
            e1.printStackTrace();
         }
      }
   }
   @Override
   @Transactional
   public void updateBatchQualityFactory(List<QualityFactory> listQuality, List<Long> list9Class,
         Map<String, Label> map) throws Exception {
      if (listQuality == null || listQuality.size() == 0) {
         return;
      }
      List<Long> listDeleteQuality = new ArrayList<Long>();
      List<QualityFactory> listUpdateQuality = new ArrayList<QualityFactory>();
      List<Long> listGid = new ArrayList<Long>();
      List<TaoBaoGoodsBrief> listUpdateGoodsBrief = new ArrayList<TaoBaoGoodsBrief>();
      List<Long> list9ClassDelete = new ArrayList<Long>();
      // 添加商品标签列表
      List<LabelGoods> listLabelAdd = new ArrayList<LabelGoods>();
      listQuality.parallelStream().forEach(quality -> {
         TaoBaoGoodsBrief goods = quality.getTaoBaoGoodsBrief();
         if (goods == null) {
            // 移除精选库
            listDeleteQuality.add(quality.getId());
         } else {
            Long id = goods.getId();
            Long auctionId = goods.getAuctionId();
            try {
               // 获取详情
               TaoBaoGoodsBrief taoBaoGoodsBrief = TaoKeApiUtil.searchGoodsDetail(auctionId);
               if (taoBaoGoodsBrief == null) {
                  /* 无券信息 移除精选库 */
                  listGid.add(id);
               } else {
                  /* 无券信息 移除精选库 */
                  if (StringUtil.isNullOrEmpty(taoBaoGoodsBrief.getCouponInfo())
                        && StringUtil.isNullOrEmpty(taoBaoGoodsBrief.getCouponActivityId())) { // 无券信息
                     // 移除精选库
                     listGid.add(id);
                     /* 更新商品信息 */
                     TaoBaoGoodsBrief updateGoods = taoBaoGoodsUpdateService
                           .getUpdateTaoBaoGoodsBrief(taoBaoGoodsBrief);
                     updateGoods.setId(id);
                     taoBaoGoodsBriefService.setGoodsBriefDefault(updateGoods);
                     listUpdateGoodsBrief.add(updateGoods);
                  } else {
                     /* 更新商品信息 */
                     TaoBaoGoodsBrief updateGoods = taoBaoGoodsUpdateService
                           .getUpdateTaoBaoGoodsBrief(taoBaoGoodsBrief);
                     updateGoods.setId(id);
                     taoBaoGoodsBriefService.setGoodsBriefDefault(updateGoods);
                     listUpdateGoodsBrief.add(updateGoods);
                     /* 更新精选库 信息时间 */
                     Integer biz30day = updateGoods.getBiz30day();
                     if (biz30day < 1000) {
                        // 销量小于1000 重新设置权重
                        int weight = 10 + (int) (Math.random() * 2000);
                        quality.setWeight(weight);
                     }
                     quality.setUpdatetime(new Date());
                     listUpdateQuality.add(quality);
                     /* 判断是否已经贴上标签 9k9标签 对应价格变化 */
                     if (list9Class.size() > 0) {
                        List<LabelGoods> listGlid = labelGoodsMapper.listQuery9k9Class(id, list9Class);
                        if (listGlid != null && listGlid.size() > 0) {
                           /* 删除原标签 9k9 */
                           for (LabelGoods labelGoods : listGlid) {
                              list9ClassDelete.add(labelGoods.getId());
                           }
                           /* 贴上新标签 9k9 */
                           LabelGoods labelGds = screen9K9(updateGoods, map);
                           if (labelGds != null) {
                              listLabelAdd.add(labelGds);
                           }
                        }
                     }
                  }
               }
            } catch (TaobaoGoodsDownException e1) {
               // 商品下架 移除数据库
               taoBaoGoodsUpdateService.deleteTaoBaoGoods(auctionId);
            } catch (Exception e) {
               try {
                  LogHelper.errorDetailInfo(e);
               } catch (Exception e1) {
                  e1.printStackTrace();
               }
            }
         }
      });
      // 更新商品信息
      if (listUpdateGoodsBrief.size() > 0) {
         taoBaoGoodsBriefService.updateBatchSelective(listUpdateGoodsBrief);
      }
      // 更新精选库
      if (listUpdateQuality.size() > 0) {
         qualityFactoryMapper.updateBatchSelective(listUpdateQuality);
      }
      // 批量移除精选库
      if (listGid.size() > 0) {
         deleteBatchByTaoBaoGoodsId(listGid);
      }
      // 批量删除精选库
      if (listDeleteQuality.size() > 0) {
         qualityFactoryMapper.deleteBatchByPrimaryKey(listDeleteQuality);
      }
      // 添加标签
      if (listLabelAdd.size() > 0) {
         labelGoodsMapper.insertBatch(listLabelAdd);
      }
      // 删除标签
      if (list9ClassDelete.size() > 0) {
         labelGoodsMapper.deleteBatchByPK(list9ClassDelete);
      }
   }
@@ -820,8 +669,6 @@
         }
      }
      // 商品id集合
      List<Long> listSystemTBid = new ArrayList<Long>();
@@ -837,7 +684,6 @@
      List<QualityFactory> listAddInsert = new ArrayList<QualityFactory>();
      // 进入精选商品列表
      List<QualityFactory> listAddUpdate = new ArrayList<QualityFactory>();
      // 插入商品淘宝ID集合
      List<Long> listAuctionId = new ArrayList<Long>();
@@ -898,10 +744,10 @@
         /* 商品已存在数据库 */
         if (isExist && systemId != null) {
            // 用于确定是否存在精选库
            listSystemTBid.add(systemId);
            // 更新商品信息
            TaoBaoGoodsBrief updateGoods = taoBaoGoodsUpdateService.getUpdateTaoBaoGoodsBrief(newGoodsBrief);
            // 设置主键id
@@ -911,41 +757,42 @@
            updateGoods.setCreatetime(date);
            updateGoods.setUpdatetime(date);
            taoBaoGoodsBriefService.setGoodsBriefDefault(updateGoods);
            listGoodsUpdate.add(updateGoods);
            Long has9k9ID = null;
            /* 贴上标签   */
            /* 贴上标签 */
            if (listLabs != null && listLabs.size() > 0) {
               for (Label label : listLabs) {
                  //  包含 9.9/19.9/29.9/49.9 、同时来源淘宝推荐: 由于获取详情与推荐接口的券信息存在出入  以最后详情为准
                  if (list9Class.contains(label.getId()) && autoRule.isCalss9k9() && QualityFactory.SOURCE_TAOBAO_MATERIAL == autoRule.getGoodsSource()) {
                     /* 获取商品详情后计算是否属于 9k9系列  加入对应的标签*/
                  // 包含 9.9/19.9/29.9/49.9 、同时来源淘宝推荐: 由于获取详情与推荐接口的券信息存在出入
                  // 以最后详情为准
                  if (list9Class.contains(label.getId()) && autoRule.isCalss9k9()
                        && QualityFactory.SOURCE_TAOBAO_MATERIAL == autoRule.getGoodsSource()) {
                     /* 获取商品详情后计算是否属于 9k9系列 加入对应的标签 */
                     LabelGoods labelGds = screen9K9(updateGoods, map);
                     if (labelGds != null) {
                        Label label9k = labelGds.getLabel();
                        Long id = label9k.getId();
                        long existence = labelGoodsMapper.isExistence(systemId, id);
                        if (existence <= 0) {
                           // 不存在对应标签
                           listLabelAdd.add(labelGds);
                        } else {
                           // 已存在
                           has9k9ID = id;
                           has9k9ID = id;
                        }
                     }
                  } else {
                     long existence = labelGoodsMapper.isExistence(systemId, label.getId());
                     if (existence <= 0) {
                        // 贴上新标签
@@ -957,54 +804,52 @@
                        listLabelAdd.add(lg);
                     } else {
                        if(list9Class.contains(label.getId())){
                        if (list9Class.contains(label.getId())) {
                           // 已存在
                           has9k9ID = label.getId();
                           has9k9ID = label.getId();
                        }
                     }
                  }
               }
            }
            /* 判断是否已经贴上标签 9k9标签 */
            if (list9Class.size() > 0) {
               List<LabelGoods> listGlid = labelGoodsMapper.listQuery9k9Class(systemId, list9Class);
               if (listGlid != null && listGlid.size() > 0) {
                  if (has9k9ID == null && !autoRule.isCalss9k9()) {
                     // 非9k9入库规则  若商品信息变化 对应的标签根据价格 修改
                     // 非9k9入库规则 若商品信息变化 对应的标签根据价格 修改
                     LabelGoods labelGds = screen9K9(updateGoods, map);
                     if (labelGds != null) {
                        Label label9k = labelGds.getLabel();
                        Long id = label9k.getId();
                        long existence = labelGoodsMapper.isExistence(systemId, id);
                        if (existence <= 0) {
                           // 不存在对应标签
                           listLabelAdd.add(labelGds);
                        } else {
                           // 已存在
                           has9k9ID = id;
                           has9k9ID = id;
                        }
                     }
                  }
                  // 清空 9K9系列标签
                  for (LabelGoods labelGoods : listGlid) {
                     Label label = labelGoods.getLabel();
                     if (has9k9ID != null && has9k9ID.equals(label.getId())) {
                        continue;
                     }
                     list9ClassDelete.add(labelGoods.getId());
                  }
               }
            }
         } else {
            newGoodsBrief.setId(null);
            newGoodsBrief.setState(0); // 状态启用
@@ -1020,19 +865,20 @@
            // 贴上标签
            if (listLabs != null && listLabs.size() > 0) {
               for (Label label : listLabs) {
                  // 包含 9.9/19.9/29.9/49.9 、同时来源淘宝推荐: 由于获取详情与推荐接口的券信息存在出入  以最后详情为准
                  if (list9Class.contains(label.getId()) && autoRule.isCalss9k9() &&
                        QualityFactory.SOURCE_TAOBAO_MATERIAL == autoRule.getGoodsSource()) {
                     /*  加入对应的9.9/19.9/29.9/49.9 标签*/
                  // 包含 9.9/19.9/29.9/49.9 、同时来源淘宝推荐: 由于获取详情与推荐接口的券信息存在出入
                  // 以最后详情为准
                  if (list9Class.contains(label.getId()) && autoRule.isCalss9k9()
                        && QualityFactory.SOURCE_TAOBAO_MATERIAL == autoRule.getGoodsSource()) {
                     /* 加入对应的9.9/19.9/29.9/49.9 标签 */
                     LabelGoods labelGds = screen9K9(newGoodsBrief, map);
                     if (labelGds != null) {
                        listLabelAdd.add(labelGds);
                     }
                  } else {
                     // 贴上新标签
                     LabelGoods lg = new LabelGoods();
@@ -1042,9 +888,9 @@
                     lg.setTaoBaoGoodsBrief(newGoodsBrief);
                     listLabelAdd.add(lg);
                  }
               }
            }
         }
      }
@@ -1104,8 +950,11 @@
            }
         }
         taoBaoGoodsBriefService.updateBatchSelective(listGoodsUpdate);
         try {
            taoBaoGoodsUpdateService.updateTaoBaoGoods(listGoodsUpdate);
         } catch (TaobaoGoodsUpdateException e) {
            System.out.println(e.getMsg());
         }
      }
      // 淘宝商品插入数据库
@@ -1172,29 +1021,29 @@
      // 查询已存在显示限时抢购的商品
      List<QualityFlashSale> listQuery = qualityFlashSaleService.listQueryByQualityID(listQFId);
      if (listQuery == null || listQuery.size() == 0) {
         // 全部新增入库
         int i = 0;
         for (Long qfId : listQFId) {
            listFlashSale.add(getNewQualityFlashSale(qfId, i, startWeight, endWeight, goodsSource));
         }
      } else {
         int i = 0;
         boolean exist = false;
         for (Long qfId : listQFId) {
            for (QualityFlashSale flashSale : listQuery) {
               QualityFactory qualityFactory = flashSale.getQualityFactory();
               if (qfId.equals(qualityFactory.getId())) {
                  exist = true;
                  //即将准备时间段时间
                  //int nowType = qualityFlashSaleService.getQueryType();
                  //flashSale.setType(nowType);
                  // 即将准备时间段时间
                  // int nowType = qualityFlashSaleService.getQueryType();
                  // flashSale.setType(nowType);
                  double weight = startWeight + (Math.random() * ((endWeight - startWeight) + 1));
                  flashSale.setWeight(weight);
@@ -1209,7 +1058,7 @@
               listFlashSale.add(getNewQualityFlashSale(qfId, i, startWeight, endWeight, goodsSource));
            }
         }
      }
      // 插入限时抢购
@@ -1247,8 +1096,8 @@
         flashSale.setWeight(weight);
      }
      //int nowType = qualityFlashSaleService.getNowType();
      //flashSale.setType(nowType);
      // int nowType = qualityFlashSaleService.getNowType();
      // flashSale.setType(nowType);
      flashSale.setCreatetime(new Date());
      flashSale.setUpdatetime(new Date());
@@ -1460,4 +1309,10 @@
      return lg;
   }
   @Override
   public List<QualityFactory> listByMinSalesCountOrderByCreateTimeDesc(int salesCount, int page, int pageSize) {
      return qualityFactoryMapper.listByMinSalesCountOrderByCreateTimeDesc(salesCount, (page - 1) * pageSize,
            pageSize);
   }
}