orderMerchantContext.helpers.js 3.3 KB
export const LOW_PRICE_FLAG = 1

const CHILD_LIST_KEYS = [
  'shopMerchantList',
  'shopList',
  'shopWithYqList',
  'merchantList',
  'storeList',
  'shopMerchantInfoList',
  'children'
]

function firstDefined(...values) {
  return values.find(value => value !== undefined && value !== null && value !== '')
}

function unwrapList(value) {
  if (Array.isArray(value)) return value
  if (!value || typeof value !== 'object') return []
  if (value.data !== undefined) return unwrapList(value.data)
  for (const key of ['list', 'records', 'rows', 'result']) {
    if (Array.isArray(value[key])) return value[key]
  }
  return []
}

function childListOf(item) {
  for (const key of CHILD_LIST_KEYS) {
    if (Array.isArray(item?.[key])) return item[key]
  }
  return []
}

function toOption(item, parent) {
  const hasExplicitShop = item?.shopMerchantId !== undefined
  const merchantId = firstDefined(
    item?.shopMerchantId,
    item?.merchantId,
    item?.shopId,
    item?.id
  )
  const merchantName = firstDefined(
    item?.shopMerchantName,
    item?.merchantName,
    item?.shopName,
    item?.name,
    ''
  )
  const yqMerchantId = firstDefined(
    item?.yqMerchantId,
    item?.sellerMerchantId,
    item?.parkMerchantId,
    parent?.yqMerchantId,
    parent?.sellerMerchantId,
    parent?.parkMerchantId,
    parent?.merchantId,
    hasExplicitShop ? item?.merchantId : undefined,
    merchantId
  )
  const yqMerchantName = firstDefined(
    item?.yqMerchantName,
    item?.sellerMerchantName,
    item?.parkMerchantName,
    parent?.yqMerchantName,
    parent?.sellerMerchantName,
    parent?.parkMerchantName,
    parent?.merchantName,
    hasExplicitShop ? item?.merchantName : undefined,
    merchantName
  )

  if (merchantId === undefined || merchantId === null || merchantId === '') {
    return null
  }

  return {
    merchantId,
    merchantName,
    yqMerchantId,
    yqMerchantName,
    key: `${yqMerchantId}:${merchantId}`
  }
}

/**
 * 将 shopWithYqList 的园区/4S 店混合返回结构统一为可选 4S 店。
 */
export function normalizeOrderMerchantOptions(payload) {
  const result = []
  const seen = new Set()

  unwrapList(payload).forEach(parent => {
    const children = childListOf(parent)
    const rows = children.length ? children : [parent]
    rows.forEach(row => {
      const option = toOption(row, children.length ? parent : null)
      if (!option || seen.has(option.key)) return
      seen.add(option.key)
      result.push(option)
    })
  })

  return result
}

export function filterOrderMerchantOptions(options, keyword) {
  const normalizedKeyword = String(keyword || '').trim().toLowerCase()
  if (!normalizedKeyword) return [...(options || [])]
  return (options || []).filter(item =>
    `${item.merchantName || ''} ${item.yqMerchantName || ''}`
      .toLowerCase()
      .includes(normalizedKeyword)
  )
}

export function buildLowPriceMerchantContext(option = {}) {
  const yqMerchantId = firstDefined(
    option.yqMerchantId,
    option.sellerMerchantId
  )
  const yqMerchantName = firstDefined(
    option.yqMerchantName,
    option.sellerMerchantName,
    ''
  )

  return {
    lowPriceFlag: LOW_PRICE_FLAG,
    merchantId: option.merchantId,
    merchantName: option.merchantName || '',
    yqMerchantId,
    yqMerchantName,
    sellerMerchantId: yqMerchantId,
    sellerMerchantName: yqMerchantName
  }
}