marketingFlag.js 5.45 KB
// 活动标识:marketingFlag 为 1 时显示活动 icon
const SHOW_MARKETING_VALUE = 1

function findActiveMarketingInList(list) {
  if (!Array.isArray(list)) return undefined
  const activeGoods = list.find(
    g => g.marketingFlag == SHOW_MARKETING_VALUE || g.marketingFlag === String(SHOW_MARKETING_VALUE)
  )
  return activeGoods?.marketingFlag
}

function findActiveActivityInList(list) {
  if (!Array.isArray(list)) return undefined
  const activeGoods = list.find(
    g => g.marketingFlag == SHOW_MARKETING_VALUE || g.marketingFlag === String(SHOW_MARKETING_VALUE)
  )
  const activityId = activeGoods?.activityId
  if (activityId === undefined || activityId === null || activityId === '') return undefined
  return activityId
}

function hasActivityValue(value) {
  return value != null && value !== ''
}

export function getMarketingFlag(item) {
  if (!item) return undefined
  if (item.marketingFlag !== undefined && item.marketingFlag !== null && item.marketingFlag !== '') {
    return item.marketingFlag
  }
  const nested =
    item.goods?.marketingFlag ??
    item.packageGoods?.marketingFlag ??
    item.multipleSpecificationsGoods?.marketingFlag ??
    item.multipleSpecificationsGoods?.goods?.marketingFlag
  if (nested !== undefined && nested !== null && nested !== '') {
    return nested
  }
  const activeFromPackageGoods =
    findActiveMarketingInList(item.packageGoods?.goodsList) ??
    findActiveMarketingInList(item.packageGoodsOrderList) ??
    findActiveMarketingInList(item.goodsList)
  if (activeFromPackageGoods !== undefined) return activeFromPackageGoods
  return undefined
}

export function shouldShowMarketingIcon(item, enabled = false) {
  if (!enabled) return false
  const flag = getMarketingFlag(item)
  return flag == SHOW_MARKETING_VALUE || flag === String(SHOW_MARKETING_VALUE)
}

export function isMarketingGoods(goods, orderItem) {
  const flag = getMarketingFlag(orderItem || { goods }) ?? goods?.marketingFlag
  return flag == SHOW_MARKETING_VALUE || flag === String(SHOW_MARKETING_VALUE)
}

export function getActivityId(item) {
  if (!item) return undefined
  if (hasActivityValue(item.activityId)) {
    return item.activityId
  }
  const nested =
    item.goods?.activityId ??
    item.packageGoods?.activityId ??
    item.multipleSpecificationsGoods?.activityId ??
    item.multipleSpecificationsGoods?.goods?.activityId
  if (hasActivityValue(nested)) {
    return nested
  }
  const activeFromPackageGoods =
    findActiveActivityInList(item.packageGoods?.goodsList) ??
    findActiveActivityInList(item.packageGoodsOrderList) ??
    findActiveActivityInList(item.goodsList)
  if (activeFromPackageGoods !== undefined) return activeFromPackageGoods
  return undefined
}

export function getSubmitMarketingFlag(orderItem, goods) {
  const flag = getMarketingFlag(orderItem) ?? goods?.marketingFlag
  return flag == SHOW_MARKETING_VALUE || flag === String(SHOW_MARKETING_VALUE) ? 1 : 0
}

export function getSubmitActivityId(orderItem, goods) {
  if (getSubmitMarketingFlag(orderItem, goods) !== 1) return undefined
  const activityId = goods?.activityId ?? getActivityId(orderItem)
  if (!hasActivityValue(activityId)) return undefined
  return activityId
}

// 商品加入订单时快照商品列表 sellPrice,供活动价展示和 modifyFlag 对比
export function snapshotOriginalPrice(goods) {
  if (!goods) return
  if (goods.oldPrice != null && goods.oldPrice !== '') return
  if (goods.sellPrice == null || goods.sellPrice === '') return
  goods.oldPrice = goods.sellPrice
}

function findCatalogPackageGoods(catalogItem, merchantGoodsId) {
  const list = catalogItem?.packageGoods?.goodsList
  if (!Array.isArray(list) || merchantGoodsId == null) return undefined
  return list.find(g => g.merchantGoodsId == merchantGoodsId)
}

// 订单回显时,从商品列表恢复活动价快照(不覆盖用户已改动的 sellPrice)
export function snapshotActivityPriceFromCatalog(goods, catalogItem) {
  if (!goods || !catalogItem) return
  if (goods.oldPrice != null && goods.oldPrice !== '') return

  let catalogSellPrice
  if (catalogItem.goodsFlag == 1 && catalogItem.goods) {
    catalogSellPrice = catalogItem.goods.sellPrice
  } else if (catalogItem.goodsFlag == 2) {
    catalogSellPrice = catalogItem.multipleSpecificationsGoods?.goods?.sellPrice
  } else if (catalogItem.goodsFlag == 3) {
    catalogSellPrice = findCatalogPackageGoods(catalogItem, goods.merchantGoodsId)?.sellPrice
  }

  if (catalogSellPrice == null || catalogSellPrice === '') return
  goods.oldPrice = catalogSellPrice
}

export function snapshotOrderItemOriginalPrice(item) {
  if (!item) return
  if (item.goodsFlag == 1 && item.goods) {
    snapshotOriginalPrice(item.goods)
  } else if (item.goodsFlag == 2 && item.multipleSpecificationsGoods?.goods) {
    snapshotOriginalPrice(item.multipleSpecificationsGoods.goods)
  } else if (item.goodsFlag == 3 && item.packageGoods?.goodsList) {
    item.packageGoods.goodsList.forEach(snapshotOriginalPrice)
  }
}

export function snapshotOrderItemsOriginalPrice(items) {
  if (!Array.isArray(items)) return
  items.forEach(snapshotOrderItemOriginalPrice)
}

export function getSubmitModifyFlag(goods) {
  if (!goods) return 0
  const current = parseFloat(goods.sellPrice ?? 0)
  const originalSource = goods.oldPrice ?? goods.upgradePrice ?? goods.price
  if (originalSource == null || originalSource === '') return 0
  const original = parseFloat(originalSource)
  if (Number.isNaN(current) || Number.isNaN(original)) return 0
  return Math.abs(current - original) > 0.001 ? 1 : 0
}