Files
shanxia2.0/web-admin/src/utils/crm-product-brand.ts
T

249 lines
7.3 KiB
TypeScript

/** CRM 产品行:展示名 / 品牌约定(报价、合同共用) */
export type BrandOption = { id: string; brandName: string }
export function buildProductNameOptions(
standardName?: string | null,
aliases?: Array<{ aliasName?: string } | null> | null
): string[] {
const names = [String(standardName || '').trim()]
for (const a of aliases || []) {
const n = String(a?.aliasName || '').trim()
if (n) names.push(n)
}
return [...new Set(names.filter(Boolean))]
}
/** 仅别名列表(不含标准名) */
export function buildProductAliasOptions(
aliases?: Array<{ aliasName?: string } | null> | null
): string[] {
const names: string[] = []
for (const a of aliases || []) {
const n = String(a?.aliasName || '').trim()
if (n) names.push(n)
}
return [...new Set(names)]
}
export function resolveLineProductName(
standardName?: string | null,
aliasName?: string | null
): string {
const alias = String(aliasName || '').trim()
if (alias) return alias
return String(standardName || '').trim()
}
/**
* 从已存 productName 反推别名列。
* 等于标准名或不在别名列表 → undefined(脏数据时别名列空,提交保留原 productName)。
*/
export function resolveAliasFromSavedName(
savedProductName?: string | null,
standardName?: string | null,
aliasOptions?: string[] | null
): string | undefined {
const saved = String(savedProductName || '').trim()
const standard = String(standardName || '').trim()
if (!saved || saved === standard) return undefined
if ((aliasOptions || []).includes(saved)) return saved
return undefined
}
export function formatBrandConstraintLabel(
brandMode?: string | null,
brandName?: string | null
): string {
if (!brandMode) return '—'
if (brandMode === 'required') return String(brandName || '').trim() || '—'
if (brandMode === 'any') return '—'
return '—'
}
/** 打印表格:指定品牌显示名称,不限/无品牌显示 - */
export function formatBrandForPrint(
brandMode?: string | null,
brandName?: string | null
): string {
if (brandMode === 'required') {
const name = String(brandName || '').trim()
return name || '-'
}
return '-'
}
export function initLineBrandMetaFromProduct(product: {
productName?: string
brands?: Array<{ id?: string; brandName?: string; status?: string }>
aliases?: Array<{ aliasName?: string }>
}) {
const brands = (product.brands || []).filter((b) => (b.status || 'on') === 'on' && b.id && b.brandName)
const standardProductName = String(product.productName || '').trim()
const aliasOptions = buildProductAliasOptions(product.aliases)
return {
standardProductName,
aliasOptions,
aliasName: undefined as string | undefined,
nameOptions: buildProductNameOptions(standardProductName, product.aliases),
brandOptions: brands.map((b) => ({
id: String(b.id),
brandName: String(b.brandName)
})) as BrandOption[],
brandMode: brands.length ? ('any' as const) : undefined,
brandId: undefined as string | undefined,
brandName: undefined as string | undefined
}
}
export function applyBrandModeChange(
row: {
brandMode?: string
brandId?: string
brandName?: string
},
mode: string | undefined
) {
row.brandMode = mode
if (mode !== 'required') {
row.brandId = undefined
row.brandName = undefined
}
}
/** 单一品牌下拉:有值=指定,清空=不限(有品牌选项时) */
export function applyBrandSelectChange(
row: { brandId?: string; brandName?: string; brandMode?: string },
brandId: string | undefined | null,
options: BrandOption[]
) {
const id = brandId ? String(brandId) : ''
if (!id) {
row.brandId = undefined
row.brandName = undefined
row.brandMode = options.length ? 'any' : undefined
return
}
row.brandId = id
row.brandMode = 'required'
const hit = options.find((o) => o.id === id)
row.brandName = hit?.brandName
}
type LineAliasBrand = {
productId?: string | number
productName?: string
standardProductName?: string
aliasName?: string
aliasOptions?: string[]
brandMode?: string
brandId?: string
brandName?: string
brandOptions?: BrandOption[]
}
/** 提交前:折叠别名→productName,折叠品牌下拉→brandMode */
export function prepareLineAliasAndBrandForSubmit(line: LineAliasBrand) {
const standard = String(line.standardProductName || '').trim()
const aliasOpts = line.aliasOptions || []
const saved = String(line.productName || '').trim()
const alias = String(line.aliasName || '').trim()
if (alias) {
line.productName = alias
} else if (
saved &&
standard &&
saved !== standard &&
!aliasOpts.includes(saved)
) {
// 脏数据:别名列空且已存名既非标准名也不在别名列表 → 保留原 productName
} else {
line.productName = resolveLineProductName(standard || saved, undefined)
}
const brandOptions = line.brandOptions || []
if (brandOptions.length) {
if (line.brandId) {
applyBrandSelectChange(line, line.brandId, brandOptions)
} else {
line.brandMode = 'any'
line.brandId = undefined
line.brandName = undefined
}
} else {
line.brandMode = undefined
line.brandId = undefined
line.brandName = undefined
}
}
/** 查看态:用产品主数据回填标准名与别名展示字段 */
export function applyAliasDisplayMeta(
line: {
productName?: string
standardProductName?: string
aliasName?: string
},
product: {
productName?: string
aliases?: Array<{ aliasName?: string } | null> | null
}
) {
const standard = String(product.productName || '').trim()
const aliasOptions = buildProductAliasOptions(product.aliases)
line.standardProductName = standard || String(line.productName || '').trim()
line.aliasName = resolveAliasFromSavedName(
line.productName,
line.standardProductName,
aliasOptions
)
}
/** 主数据别名/品牌维护成功后:同步同 productId 各行的 options,并校正已选值 */
export function applyManagedProductMetaToLines(
lines: Array<LineAliasBrand & { productId?: string | number }>,
productId: string,
product: {
productName?: string
brands?: Array<{ id?: string; brandName?: string; status?: string }>
aliases?: Array<{ aliasName?: string }>
}
) {
const pid = String(productId || '')
if (!pid) return
const meta = initLineBrandMetaFromProduct(product)
for (const line of lines || []) {
if (String(line.productId || '') !== pid) continue
line.standardProductName = meta.standardProductName
line.aliasOptions = meta.aliasOptions
const alias = String(line.aliasName || '').trim()
if (alias && !meta.aliasOptions.includes(alias)) {
line.aliasName = undefined
}
line.brandOptions = meta.brandOptions
if (meta.brandOptions.length) {
if (line.brandMode === 'required' && line.brandId) {
const still = meta.brandOptions.some((o) => o.id === String(line.brandId))
if (still) {
applyBrandSelectChange(line, line.brandId, meta.brandOptions)
} else {
line.brandMode = 'any'
line.brandId = undefined
line.brandName = undefined
}
} else {
line.brandMode = 'any'
line.brandId = undefined
line.brandName = undefined
}
} else {
line.brandMode = undefined
line.brandId = undefined
line.brandName = undefined
}
}
}