feat(gating): 后管新增品牌Gating库管理页
菜单权限键 admin_brand_gating(admin_group_data 组),路由 asin-center/gating;
统计卡+五字段筛选+13 列表格+新增/删除/启停用/批量导入(粘文本 { text }
整段交后端,前端同款 parse_batch 规则只做行数预览);companyScoped=false
(biz_brand_gating 全平台共享表口径)。补 ops-cell 操作列宽登记与操作提示条。
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
/** 品牌Gating库页面模型(biz_brand_gating 并入管理后台):类型、字典、筛选态与响应解析。
|
||||
* 纯逻辑、零依赖(tests/gating-model.test.ts 用 node --test 直连本文件,别 import 带 @ 别名/axios 的模块)。 */
|
||||
|
||||
export interface GatingItem {
|
||||
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 GatingPageResult {
|
||||
rows: GatingItem[]
|
||||
total: number
|
||||
}
|
||||
|
||||
/** 分组统计单行(GET stats 的 byType/byCountry/bySource)。 */
|
||||
export interface GatingNameCount {
|
||||
name: string
|
||||
count: number
|
||||
}
|
||||
|
||||
export interface GatingStats {
|
||||
total: number
|
||||
active: number
|
||||
disabled: number
|
||||
brands: number
|
||||
byType: GatingNameCount[]
|
||||
byCountry: GatingNameCount[]
|
||||
bySource: GatingNameCount[]
|
||||
}
|
||||
|
||||
export interface GatingFilterState {
|
||||
/** 品牌/ASIN/备注 LIKE 关键词。 */
|
||||
keyword: string
|
||||
country: string
|
||||
gatingType: string
|
||||
/** '' = 全部;'1' 生效;'0' 停用。 */
|
||||
status: string
|
||||
source: string
|
||||
}
|
||||
|
||||
/** 国家字典:与桌面版 gating_web_admin 下拉一致;'全部' 是表内真实取值(不限国家时用空串)。 */
|
||||
export const GATING_COUNTRIES = ['全部', '英国', '德国', '法国', '西班牙', '意大利'] as const
|
||||
export const GATING_TYPES = ['品牌gating', '类目gating', '账户不符合'] as const
|
||||
export const GATING_SOURCES = ['AUTO', 'MANUAL'] as const
|
||||
/** 每页条数档位(默认 10,可选 20/50)。 */
|
||||
export const GATING_PAGE_SIZES = [10, 20, 50] as const
|
||||
export const GATING_DEFAULT_PAGE_SIZE = 10
|
||||
|
||||
export function createGatingFilterState(): GatingFilterState {
|
||||
return { keyword: '', country: '', gatingType: '', status: '', source: '' }
|
||||
}
|
||||
|
||||
function text(value: unknown): string {
|
||||
return typeof value === 'string' ? value.trim() : ''
|
||||
}
|
||||
|
||||
function intOr(value: unknown, fallback: number): number {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : fallback
|
||||
}
|
||||
|
||||
/** 解析单条记录;缺 id 视为无效。兼容 camel 与 snake 字段名。 */
|
||||
export function toGatingItem(raw: unknown): GatingItem | null {
|
||||
if (!raw || typeof raw !== 'object') return null
|
||||
const record = raw as Record<string, unknown>
|
||||
const id = intOr(record.id, 0)
|
||||
if (id <= 0) return null
|
||||
const item: GatingItem = {
|
||||
id,
|
||||
brand: text(record.brand),
|
||||
asin: text(record.asin),
|
||||
country: text(record.country),
|
||||
gatingType: text(record.gatingType ?? record.gating_type),
|
||||
status: intOr(record.status, 0),
|
||||
source: text(record.source),
|
||||
shopName: text(record.shopName ?? record.shop_name),
|
||||
remark: text(record.remark),
|
||||
}
|
||||
const createdAt = text(record.createdAt ?? record.created_at)
|
||||
const updatedAt = text(record.updatedAt ?? record.updated_at)
|
||||
const disabledAt = text(record.disabledAt ?? record.disabled_at)
|
||||
if (createdAt) item.createdAt = createdAt
|
||||
if (updatedAt) item.updatedAt = updatedAt
|
||||
if (disabledAt) item.disabledAt = disabledAt
|
||||
return item
|
||||
}
|
||||
|
||||
/** 解析列表负载({ total, rows };rows 兼容 items 别名),空负载回空页。 */
|
||||
export function parseGatingPage(payload: unknown): GatingPageResult {
|
||||
const out: GatingPageResult = { rows: [], total: 0 }
|
||||
if (!payload || typeof payload !== 'object') return out
|
||||
const record = payload as Record<string, unknown>
|
||||
const rawRows = Array.isArray(record.rows) ? record.rows : Array.isArray(record.items) ? record.items : []
|
||||
out.rows = rawRows
|
||||
.map((raw) => toGatingItem(raw))
|
||||
.filter((item): item is GatingItem => item !== null)
|
||||
out.total = intOr(record.total, out.rows.length)
|
||||
return out
|
||||
}
|
||||
|
||||
/** 分组统计单组:数组 [{name,count}] 或桌面版遗留 dict({品牌gating: 3})都收。 */
|
||||
export function parseGatingNameCountList(raw: unknown): GatingNameCount[] {
|
||||
if (Array.isArray(raw)) {
|
||||
const out: GatingNameCount[] = []
|
||||
for (const item of raw) {
|
||||
if (!item || typeof item !== 'object') continue
|
||||
const record = item as Record<string, unknown>
|
||||
const name = text(record.name)
|
||||
if (!name) continue
|
||||
out.push({ name, count: intOr(record.count, 0) })
|
||||
}
|
||||
return out
|
||||
}
|
||||
if (raw && typeof raw === 'object') {
|
||||
const out: GatingNameCount[] = []
|
||||
for (const [name, count] of Object.entries(raw as Record<string, unknown>)) {
|
||||
if (!name) continue
|
||||
out.push({ name, count: intOr(count, 0) })
|
||||
}
|
||||
return out
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
/** 解析统计负载;缺省字段回 0/空数组。 */
|
||||
export function parseGatingStats(payload: unknown): GatingStats {
|
||||
const out: GatingStats = {
|
||||
total: 0,
|
||||
active: 0,
|
||||
disabled: 0,
|
||||
brands: 0,
|
||||
byType: [],
|
||||
byCountry: [],
|
||||
bySource: [],
|
||||
}
|
||||
if (!payload || typeof payload !== 'object') return out
|
||||
const record = payload as Record<string, unknown>
|
||||
out.total = intOr(record.total, 0)
|
||||
out.active = intOr(record.active, 0)
|
||||
out.disabled = intOr(record.disabled, 0)
|
||||
out.brands = intOr(record.brands, 0)
|
||||
out.byType = parseGatingNameCountList(record.byType ?? record.by_type)
|
||||
out.byCountry = parseGatingNameCountList(record.byCountry ?? record.by_country)
|
||||
out.bySource = parseGatingNameCountList(record.bySource ?? record.by_source)
|
||||
return out
|
||||
}
|
||||
|
||||
/** 筛选态 + 分页 → 列表查询参数(page/size/keyword/country/gatingType/status/source);空白值不下发。 */
|
||||
export function toGatingListParams(
|
||||
state: GatingFilterState,
|
||||
page: number,
|
||||
pageSize: number,
|
||||
): Record<string, string | number> {
|
||||
const params: Record<string, string | number> = {
|
||||
page: page >= 1 ? Math.floor(page) : 1,
|
||||
size: pageSize >= 1 ? Math.floor(pageSize) : GATING_DEFAULT_PAGE_SIZE,
|
||||
}
|
||||
const keyword = (state.keyword || '').trim()
|
||||
const country = (state.country || '').trim()
|
||||
const gatingType = (state.gatingType || '').trim()
|
||||
const status = (state.status || '').trim()
|
||||
const source = (state.source || '').trim()
|
||||
if (keyword) params.keyword = keyword
|
||||
if (country) params.country = country
|
||||
if (gatingType) params.gatingType = gatingType
|
||||
if (status === '1' || status === '0') params.status = status
|
||||
if (source) params.source = source
|
||||
return params
|
||||
}
|
||||
|
||||
/** 状态 → 展示文案;非 1 一律按停用。 */
|
||||
export function gatingStatusLabel(status: unknown): string {
|
||||
return status === 1 ? '生效' : '停用'
|
||||
}
|
||||
Reference in New Issue
Block a user