/**
   * 重置订单优惠信息(主要用于京东订单)
   *
   * @param originalOrderList
   */
  public void resetDiscountInfo(List<OriginalOrder> originalOrderList) {
    if (CollectionUtils.isEmpty(originalOrderList)) {
      return;
    }

    for (OriginalOrder originalOrder : originalOrderList) {
      originalOrder.setDiscountFee(getDiscountFee(originalOrder.getPromotionInfoList()));
      originalOrder.setSelfDiscountFee(getSelfDiscountFee(originalOrder.getPromotionInfoList()));

      if (CollectionUtils.isEmpty(originalOrder.getOriginalOrderItemList())) {
        continue;
      }

      Money totalPayableFee = getTotalPayableFee(originalOrder);

      for (OriginalOrderItem originalOrderItem : originalOrder.getOriginalOrderItemList()) {
        originalOrderItem.setPartMjzDiscount(
            getItemMjzDiscountFee(
                originalOrderItem.getPayableFee(),
                totalPayableFee,
                originalOrder.getDiscountFee()));
        originalOrderItem.setSelfPartMjzDiscount(
            getItemMjzDiscountFee(
                originalOrderItem.getPayableFee(),
                totalPayableFee,
                originalOrder.getSelfDiscountFee()));
        saveOriginalOrderItem(originalOrderItem);
      }

      saveOriginalOrder(originalOrder);
    }
  }
  /**
   * 获得订单总应付金额
   *
   * @param originalOrder
   * @return
   */
  public Money getTotalPayableFee(OriginalOrder originalOrder) {
    Money totalPayableFee = Money.valueOf(0);
    if (originalOrder == null
        || CollectionUtils.isEmpty(originalOrder.getOriginalOrderItemList())) {
      return totalPayableFee;
    }

    for (OriginalOrderItem originalOrderItem : originalOrder.getOriginalOrderItemList()) {
      totalPayableFee = totalPayableFee.add(originalOrderItem.getPayableFee());
    }
    return totalPayableFee;
  }
  /**
   * 根据条件查询唯一的原始订单
   *
   * @param originalOrder
   * @return
   */
  @Transactional(readOnly = true)
  public OriginalOrder getOriginalOrderByCondition(OriginalOrder originalOrder) {
    Search search = new Search(OriginalOrder.class);
    if (originalOrder != null) {
      if (StringUtils.isNotBlank(originalOrder.getPlatformOrderNo())) {
        search.addFilterEqual("platformOrderNo", originalOrder.getPlatformOrderNo());
      }
    }

    List<OriginalOrder> originalOrderList = generalDAO.search(search);

    return CollectionUtils.isEmpty(originalOrderList) ? null : originalOrderList.get(0);
  }
  /**
   * 保存指定状态的原始订单至数据库
   *
   * @param originalOrderList
   */
  public void saveOriginalOrders(List<OriginalOrder> originalOrderList) {

    if (CollectionUtils.isEmpty(originalOrderList)) {
      return;
    }

    for (OriginalOrder originalOrder : originalOrderList) {

      if (!(StringUtils.equalsIgnoreCase(
              originalOrder.getStatus(), OriginalOrderStatus.WAIT_SELLER_SEND_GOODS.toString())
          || StringUtils.equalsIgnoreCase(
              originalOrder.getStatus(), OriginalOrderStatus.WAIT_BUYER_CONFIRM_GOODS.toString())
          || StringUtils.equalsIgnoreCase(
              originalOrder.getStatus(), OriginalOrderStatus.TRADE_FINISHED.toString()))) {
        continue;
      }
      // 保存原始订单
      saveOriginalOrder(originalOrder);
      for (OriginalOrderItem originalOrderItem : originalOrder.getOriginalOrderItemList()) {
        // 保存原始订单项
        originalOrderItem.setOriginalOrderId(originalOrder.getId());
        saveOriginalOrderItem(originalOrderItem);
      }
      for (PromotionInfo promotionInfo : originalOrder.getPromotionInfoList()) {
        // 保存订单优惠记录
        promotionInfo.setOriginalOrderId(originalOrder.getId());
        savePromotionInfo(promotionInfo);
      }
    }
  }
 /**
  * 删除原始订单与品牌的关系记录
  *
  * @param originalOrder 如果为null,删除表中所有记录
  */
 private void deleteOriginalOrderBrands(OriginalOrder originalOrder) {
   String sql = "delete from t_original_order_brand ";
   if (originalOrder != null) {
     sql += " where original_order_id = " + originalOrder.getId();
   }
   generalDAO.getSession().createSQLQuery(sql).executeUpdate();
 }
 private void createOriginalOrderBrand(
     OriginalOrder originalOrder, OriginalOrderItem originalOrderItem, Integer brandId) {
   OriginalOrderBrand originalOrderBrand = new OriginalOrderBrand();
   originalOrderBrand.setOriginalOrderId(originalOrder.getId());
   originalOrderBrand.setOriginalOrderItemId(originalOrderItem.getId());
   originalOrderBrand.setBrandId(brandId);
   generalDAO.saveOrUpdate(originalOrderBrand);
 }
 @Transactional
 public void createAnalyzeLog(OriginalOrder originalOrder, boolean processed, String errorMsg) {
   OrderAnalyzeLog log = new OrderAnalyzeLog();
   log.setCreateTime(new Date());
   log.setOriginalOrderId(originalOrder.getId());
   log.setProcessed(processed);
   log.setMessage(errorMsg);
   generalDAO.saveOrUpdate(log);
 }
  /**
   * 获取订单、订单项、订单优惠信息
   *
   * @param originalOrder
   * @param startTime
   * @param endTime
   * @return
   */
  @Transactional(readOnly = true)
  public List<OriginalOrder> findOriginalOrderFullinfos(
      OriginalOrder originalOrder, Date startTime, Date endTime) {
    List<OriginalOrder> originalOrderList = getOriginalOrder(originalOrder, startTime, endTime);

    if (CollectionUtils.isNotEmpty(originalOrderList)) {
      for (OriginalOrder originalOrderOri : originalOrderList) {
        OriginalOrderItem originalOrderItem = new OriginalOrderItem();
        originalOrderItem.setOriginalOrderId(originalOrderOri.getId());
        List<OriginalOrderItem> originalOrderItemList = findOriginalOrderItem(originalOrderItem);
        if (CollectionUtils.isNotEmpty(originalOrderItemList)) {
          originalOrderOri.setOriginalOrderItemList(originalOrderItemList);
        }

        PromotionInfo promotionInfo = new PromotionInfo();
        promotionInfo.setOriginalOrderId(originalOrderOri.getId());
        List<PromotionInfo> promotionInfoList = findPromotionInfos(promotionInfo);
        if (CollectionUtils.isNotEmpty(promotionInfoList)) {
          originalOrderOri.setPromotionInfoList(promotionInfoList);
        }
      }
    }
    return originalOrderList;
  }
  /**
   * 猜测原始订单对应的品牌
   *
   * @param originalOrder
   */
  @Transactional
  public void guessOriginalOrderBrand(OriginalOrder originalOrder, boolean deleteBeforeGuess) {
    try {
      if (deleteBeforeGuess) {
        // 先删除原始猜测的记录
        deleteOriginalOrderBrands(originalOrder);
      }

      List<OriginalOrderItem> originalOrderItemList = originalOrder.getOriginalOrderItemList();
      if (originalOrderItemList == null || originalOrderItemList.isEmpty()) {
        return;
      }
      for (OriginalOrderItem originalOrderItem : originalOrderItemList) {
        String sku = originalOrderItem.getSku();
        if (StringUtils.isBlank(sku)) continue;
        Mealset mealset = mealSetService.findBySku(sku);
        if (mealset != null) {
          for (MealsetItem mealsetItem : mealset.getMealsetItemList()) {
            Product product = mealsetItem.getProduct();
            if (product == null || product.getBrandId() == null) {
              continue;
            }
            createOriginalOrderBrand(originalOrder, originalOrderItem, product.getBrandId());
          }
        } else {
          Product product = productService.findProductBySKU(sku);
          if (product == null || product.getBrandId() == null) {
            continue;
          }
          createOriginalOrderBrand(originalOrder, originalOrderItem, product.getBrandId());
        }
      }
    } catch (Exception e) {
      logger.error("猜测原始订单对应品牌的时候发生错误", e);
    }
  }
 /**
  * 查询
  *
  * @param originalOrder
  * @return
  */
 @Transactional(readOnly = true)
 public List<OriginalOrder> getOriginalOrder(
     OriginalOrder originalOrder, Date startTime, Date endTime) {
   if (logger.isInfoEnabled()) {
     logger.info(
         String.format(
             "getOriginalOrder方法参数为originalOrder[{}],startTime[{}],endTime[{}]",
             originalOrder,
             startTime,
             endTime));
   }
   Search search = new Search(OriginalOrder.class);
   if (originalOrder != null) {
     if (StringUtils.isNotBlank(originalOrder.getPlatformOrderNo())) {
       search.addFilterLike(
           "platformOrderNo", "%" + originalOrder.getPlatformOrderNo().trim() + "%");
     }
     if (!NumberUtil.isNullOrZero(originalOrder.getShopId())) {
       search.addFilterEqual("shopId", originalOrder.getShopId());
     }
     if (!NumberUtil.isNullOrZero(originalOrder.getProcessed())) {
       search.addFilterEqual("processed", originalOrder.getProcessed());
     }
     if (!NumberUtil.isNullOrZero(originalOrder.getOutShopId())) {
       search.addFilterEqual("outShopId", originalOrder.getOutShopId());
     }
     if (!NumberUtil.isNullOrZero(originalOrder.getStatus())) {
       search.addFilterEqual("status", originalOrder.getStatus());
     }
     if (originalOrder.getPlatformType() != null) {
       search.addFilterEqual("platformType", originalOrder.getPlatformType());
     }
     if (startTime != null) {
       search.addFilterGreaterOrEqual("buyTime", startTime);
     }
     if (endTime != null) {
       search.addFilterLessOrEqual("buyTime", endTime);
     }
   }
   return generalDAO.search(search);
 }