boutiqueCustomerDailyDisplay.helpers.js 2.21 KB
import { formatActualDeliveryDateForDisplay } from './actualDeliveryDate.helpers.js'

export const BOUTIQUE_CUSTOMER_TAGS = [
  { value: '1', label: '已触客' },
  { value: '2', label: '已拉群' },
  { value: '3', label: '已交付引荐' },
  { value: '4', label: '首保维保客户' },
  { value: '5', label: '事故车客户' }
]

const CUSTOMER_TAG_ORDER = BOUTIQUE_CUSTOMER_TAGS.map(item => item.value)

function toTagValues(value) {
  if (Array.isArray(value)) return value
  if (value === undefined || value === null || value === '') return []
  return String(value).split(',')
}

export function parseCustomerTags(value, allowedValues = CUSTOMER_TAG_ORDER) {
  const allowed = new Set((allowedValues || []).map(String))
  const selected = new Set(
    toTagValues(value)
      .map(item => String(item).trim())
      .filter(item => allowed.has(item))
  )
  return CUSTOMER_TAG_ORDER.filter(item => selected.has(item))
}

export function serializeCustomerTags(value, allowedValues = CUSTOMER_TAG_ORDER) {
  return parseCustomerTags(value, allowedValues).join(',')
}

export function getCustomerTagLabels(value) {
  const selected = new Set(parseCustomerTags(value))
  return BOUTIQUE_CUSTOMER_TAGS.filter(item => selected.has(item.value)).map(
    item => item.label
  )
}

export function formatCustomerTags(value) {
  return getCustomerTagLabels(value).join('/')
}

export function buildBoutiqueCustomerDailyDisplay(customer = {}) {
  const source = customer || {}
  const customerTagLabels = getCustomerTagLabels(source.customerTags)
  const actualDeliveryDate = formatActualDeliveryDateForDisplay(
    source.actualDeliveryDate
  )
  return {
    showCustomerTags: customerTagLabels.length > 0,
    showActualDeliveryDate: !!actualDeliveryDate,
    customerTagLabels,
    customerTagsText: customerTagLabels.join('/'),
    actualDeliveryDate
  }
}

export function buildBoutiqueCustomerDailyListDisplay(orderItem = {}) {
  const source = orderItem || {}
  const nestedCustomer = source.customer || {}
  return buildBoutiqueCustomerDailyDisplay({
    ...nestedCustomer,
    customerTags: nestedCustomer.customerTags || source.customerTags || '',
    actualDeliveryDate:
      nestedCustomer.actualDeliveryDate || source.actualDeliveryDate || ''
  })
}