23c1402a75
V164 建菜单、V166 回滚后,按 2026-10-25 决定用 V174 恢复独立菜单, 不再作为「店铺数据重复检查」的第三页签;后端权限键同步改为 admin_brand_gating。
178 lines
5.3 KiB
TypeScript
178 lines
5.3 KiB
TypeScript
/** 品牌Gating库模型(独立菜单页):与 /api/admin/brand-gating 的 camel 契约对齐,纯逻辑。 */
|
|
|
|
export const GATING_COUNTRIES = ['全部', '英国', '德国', '法国', '西班牙', '意大利'] as const
|
|
export const GATING_TYPES = ['品牌gating', '类目gating', '账户不符合'] as const
|
|
export const GATING_SOURCES = ['AUTO', 'MANUAL'] as const
|
|
|
|
export type GatingStatusFilter = '' | '1' | '0'
|
|
|
|
export interface BrandGatingFilter {
|
|
keyword: string
|
|
country: string
|
|
gatingType: string
|
|
status: GatingStatusFilter
|
|
source: string
|
|
}
|
|
|
|
export interface BrandGatingForm {
|
|
brand: string
|
|
country: string
|
|
gatingType: string
|
|
asin: string
|
|
remark: string
|
|
}
|
|
|
|
export interface BrandGatingRow {
|
|
id: number
|
|
brand: string
|
|
asin: string
|
|
country: string
|
|
gatingType: string
|
|
/** 1=生效 0=已解除。 */
|
|
status: number
|
|
source: string
|
|
shopName: string
|
|
remark: string
|
|
createdAt: string
|
|
updatedAt: string
|
|
disabledAt: string
|
|
}
|
|
|
|
export interface BrandGatingNameCount {
|
|
name: string
|
|
count: number
|
|
}
|
|
|
|
export interface BrandGatingStats {
|
|
total: number
|
|
active: number
|
|
disabled: number
|
|
brands: number
|
|
byType: BrandGatingNameCount[]
|
|
byCountry: BrandGatingNameCount[]
|
|
bySource: BrandGatingNameCount[]
|
|
}
|
|
|
|
export interface BrandGatingPage {
|
|
total: number
|
|
rows: BrandGatingRow[]
|
|
}
|
|
|
|
export interface BrandGatingImportResult {
|
|
added: number
|
|
skipped: number
|
|
}
|
|
|
|
export function createBrandGatingFilter(): BrandGatingFilter {
|
|
return { keyword: '', country: '', gatingType: '', status: '', source: '' }
|
|
}
|
|
|
|
export function createBrandGatingForm(): BrandGatingForm {
|
|
return { brand: '', country: '全部', gatingType: '品牌gating', asin: '', remark: '' }
|
|
}
|
|
|
|
/** 列表查询参数:空值与「全部」哨兵不下发,其余原样交给后端(与桌面版 list_gating 同口径)。 */
|
|
export function toBrandGatingQuery(
|
|
filter: BrandGatingFilter,
|
|
page: number,
|
|
pageSize: number,
|
|
): Record<string, string | number> {
|
|
const query: Record<string, string | number> = {
|
|
page: Math.max(1, Math.trunc(page) || 1),
|
|
size: Math.max(1, Math.trunc(pageSize) || 10),
|
|
}
|
|
const keyword = filter.keyword.trim()
|
|
if (keyword) query.keyword = keyword
|
|
if (filter.country && filter.country !== '全部') query.country = filter.country
|
|
if (filter.gatingType && filter.gatingType !== '全部') query.gatingType = filter.gatingType
|
|
if (filter.status === '1' || filter.status === '0') query.status = filter.status
|
|
if (filter.source && filter.source !== '全部') query.source = filter.source
|
|
return query
|
|
}
|
|
|
|
/** 单条新增前置校验:只拦空品牌,其余字段由后端落桌面版同款默认值。 */
|
|
export function validateBrandGatingForm(form: BrandGatingForm): string {
|
|
if (!form.brand.trim()) return '请填写品牌'
|
|
return ''
|
|
}
|
|
|
|
function asRecord(value: unknown): Record<string, unknown> | null {
|
|
return value && typeof value === 'object' ? (value as Record<string, unknown>) : null
|
|
}
|
|
|
|
function textOf(value: unknown): string {
|
|
return value == null ? '' : String(value).trim()
|
|
}
|
|
|
|
function countOf(value: unknown): number {
|
|
const count = Number(value)
|
|
return Number.isFinite(count) ? Math.max(0, Math.trunc(count)) : 0
|
|
}
|
|
|
|
function nameCountsOf(value: unknown): BrandGatingNameCount[] {
|
|
if (!Array.isArray(value)) return []
|
|
return value
|
|
.map((item) => {
|
|
const record = asRecord(item)
|
|
if (!record) return null
|
|
const name = textOf(record.name)
|
|
return name ? { name, count: countOf(record.count) } : null
|
|
})
|
|
.filter((item): item is BrandGatingNameCount => item !== null)
|
|
}
|
|
|
|
export function parseBrandGatingRow(raw: unknown): BrandGatingRow | null {
|
|
const record = asRecord(raw)
|
|
if (!record) return null
|
|
const id = Number(record.id)
|
|
if (!Number.isFinite(id)) return null
|
|
return {
|
|
id,
|
|
brand: textOf(record.brand),
|
|
asin: textOf(record.asin),
|
|
country: textOf(record.country),
|
|
gatingType: textOf(record.gatingType),
|
|
status: countOf(record.status) === 1 ? 1 : 0,
|
|
source: textOf(record.source),
|
|
shopName: textOf(record.shopName),
|
|
remark: textOf(record.remark),
|
|
createdAt: textOf(record.createdAt),
|
|
updatedAt: textOf(record.updatedAt),
|
|
disabledAt: textOf(record.disabledAt),
|
|
}
|
|
}
|
|
|
|
export function parseBrandGatingPage(raw: unknown): BrandGatingPage {
|
|
const record = asRecord(raw)
|
|
if (!record) return { total: 0, rows: [] }
|
|
const rows = Array.isArray(record.rows)
|
|
? record.rows
|
|
.map(parseBrandGatingRow)
|
|
.filter((row): row is BrandGatingRow => row !== null)
|
|
: []
|
|
return { total: countOf(record.total), rows }
|
|
}
|
|
|
|
export function parseBrandGatingStats(raw: unknown): BrandGatingStats {
|
|
const record = asRecord(raw)
|
|
return {
|
|
total: countOf(record?.total),
|
|
active: countOf(record?.active),
|
|
disabled: countOf(record?.disabled),
|
|
brands: countOf(record?.brands),
|
|
byType: nameCountsOf(record?.byType),
|
|
byCountry: nameCountsOf(record?.byCountry),
|
|
bySource: nameCountsOf(record?.bySource),
|
|
}
|
|
}
|
|
|
|
/** 单条新增回包:added=1 新增成功,0 表示撞唯一键(品牌+国家已存在)。 */
|
|
export function parseBrandGatingAddResult(raw: unknown): boolean {
|
|
return countOf(asRecord(raw)?.added) > 0
|
|
}
|
|
|
|
export function parseBrandGatingImportResult(raw: unknown): BrandGatingImportResult {
|
|
const record = asRecord(raw)
|
|
return { added: countOf(record?.added), skipped: countOf(record?.skipped) }
|
|
}
|