9166656673
背景:列表页 `ORDER BY id DESC LIMIT offset,size`,带筛选且选择性低时每页都要 对命中集做一次 filesort;深翻页 offset 也白扫索引。 改法(保持跳页/回退/改每页的原有行为): - 后端:page 接口新增可选 `last_id` 游标 —— 传了就 `id < lastId` + `LIMIT size`(无 offset), 响应新增 `nextLastId`(本页最后一行 id);不传仍是原 OFFSET 分页 - 管理前端:只有"下一页"用游标(上一页响应带回),跳页/改每页/筛选清空游标走 OFFSET - 测试:新增 2 个后端契约测试(keyset 无 offset + nextLastId;无游标保持 LIMIT 30,15) 验证:mvn test 2897 全绿;admin-frontend-vue vue-tsc 通过 + 1619 测试全绿; 已部署 JAR 2b9ab774c60c3f5d802c9a2cee549008(双节点 health=200)与 admin-vue-20260914-112055。
138 lines
5.4 KiB
TypeScript
138 lines
5.4 KiB
TypeScript
/** 去重汇总列表查询模型(任务 62):解析 DedupeTotalDataPageVo(camel),纯逻辑。 */
|
|
import { unwrap } from '../../api/envelope.ts'
|
|
import { ASIN_PAGE_DEFAULT_SIZE, type AsinListParams } from './asin-filter.ts'
|
|
|
|
export interface DedupeTotalItem {
|
|
id: number
|
|
dataValue: string
|
|
country: string
|
|
groupId: number | null
|
|
groupName: string
|
|
uploaderUserId: number | null
|
|
username: string
|
|
createdAt?: string
|
|
}
|
|
|
|
export interface DedupeTotalPageResult {
|
|
items: DedupeTotalItem[]
|
|
total: number
|
|
page: number
|
|
pageSize: number
|
|
/** 顺序翻页游标:本页最后一行 id;下一页回传它即可走 keyset。 */
|
|
nextLastId?: number
|
|
}
|
|
|
|
export function emptyDedupeTotalPage(): DedupeTotalPageResult {
|
|
return { items: [], total: 0, page: 1, pageSize: ASIN_PAGE_DEFAULT_SIZE }
|
|
}
|
|
|
|
function text(value: unknown): string {
|
|
return typeof value === 'string' ? value.trim() : ''
|
|
}
|
|
|
|
function numberOrNull(value: unknown): number | null {
|
|
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
|
|
}
|
|
|
|
/** 解析单条去重总数据;缺 id 视为无效。 */
|
|
export function toDedupeTotalItem(raw: unknown): DedupeTotalItem | null {
|
|
if (!raw || typeof raw !== 'object') return null
|
|
const record = raw as Record<string, unknown>
|
|
const id = numberOrNull(record.id)
|
|
if (id === null) return null
|
|
const item: DedupeTotalItem = {
|
|
id,
|
|
dataValue: text(record.dataValue ?? record.data_value),
|
|
country: text(record.country),
|
|
groupId: numberOrNull(record.groupId ?? record.group_id),
|
|
groupName: text(record.groupName ?? record.group_name),
|
|
uploaderUserId: numberOrNull(record.uploaderUserId ?? record.uploader_user_id),
|
|
username: text(record.username),
|
|
}
|
|
const createdAt = text(record.createdAt ?? record.created_at)
|
|
if (createdAt) item.createdAt = createdAt
|
|
return item
|
|
}
|
|
|
|
/** 归一化分页负载(信封或已解包 VO)为前端结果;缺省字段回默认。 */
|
|
export function parseDedupeTotalPage(payload: unknown): DedupeTotalPageResult {
|
|
const out = emptyDedupeTotalPage()
|
|
const core = unwrap<unknown>(payload)
|
|
if (!core || typeof core !== 'object') return out
|
|
const record = core as Record<string, unknown>
|
|
if (Array.isArray(record.items)) {
|
|
out.items = record.items
|
|
.map((raw) => toDedupeTotalItem(raw))
|
|
.filter((item): item is DedupeTotalItem => item !== null)
|
|
}
|
|
if (typeof record.total === 'number') out.total = Math.floor(record.total)
|
|
if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page)
|
|
const rawNextLastId = record.nextLastId ?? record.next_last_id
|
|
if (typeof rawNextLastId === 'number' && rawNextLastId > 0) out.nextLastId = Math.floor(rawNextLastId)
|
|
const rawSize = record.pageSize ?? record.page_size
|
|
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
|
|
return out
|
|
}
|
|
|
|
/** 构造缺省分页查询参数(供初始加载使用)。 */
|
|
export function emptyDedupePageParams(): Partial<AsinListParams> {
|
|
return { page: 1, pageSize: ASIN_PAGE_DEFAULT_SIZE }
|
|
}
|
|
|
|
/** 数据权限分组选项(GET /api/admin/shop-manages/groups items)。 */
|
|
export interface DedupeGroupOption {
|
|
id: number
|
|
name: string
|
|
leaderUserId?: number | null
|
|
}
|
|
|
|
/** 归一化分组选项负载(信封 items 或裸数组),缺 id 行丢弃;解析组长 id 供汇总卡判定组长/组员。 */
|
|
export function parseDedupeGroupOptionList(payload: unknown): DedupeGroupOption[] {
|
|
const core = unwrap<unknown>(payload)
|
|
const rawList = Array.isArray(core)
|
|
? core
|
|
: core && typeof core === 'object'
|
|
? (core as Record<string, unknown>).items
|
|
: []
|
|
if (!Array.isArray(rawList)) return []
|
|
const options: DedupeGroupOption[] = []
|
|
for (const raw of rawList) {
|
|
if (!raw || typeof raw !== 'object') continue
|
|
const record = raw as Record<string, unknown>
|
|
const id = typeof record.id === 'number' ? Math.floor(record.id) : Number(record.group_id)
|
|
if (!Number.isFinite(id) || id <= 0) continue
|
|
const nameRaw = record.groupName ?? record.group_name ?? record.name
|
|
const name = typeof nameRaw === 'string' ? nameRaw.trim() : ''
|
|
const option: DedupeGroupOption = { id, name: name || `分组${id}` }
|
|
const leaderRaw = record.leader_user_id ?? record.leaderUserId
|
|
const leaderUserId = typeof leaderRaw === 'number' ? Math.floor(leaderRaw) : Number(leaderRaw)
|
|
if (Number.isFinite(leaderUserId) && leaderUserId > 0) option.leaderUserId = leaderUserId
|
|
options.push(option)
|
|
}
|
|
return options
|
|
}
|
|
|
|
/** 页顶数据权限分组汇总(对齐 admin.js renderDedupeGroupSummary)。 */
|
|
export type DedupeGroupSummary =
|
|
| { kind: 'all'; count: number }
|
|
| { kind: 'none' }
|
|
| { kind: 'chips'; chips: Array<{ name: string; relation: string }>; extra: number }
|
|
|
|
export function dedupeGroupSummaryOf(
|
|
groups: DedupeGroupOption[],
|
|
currentUserId: number | null,
|
|
operatorRole: string,
|
|
): DedupeGroupSummary {
|
|
if ((operatorRole || '').toLowerCase() === 'super_admin') {
|
|
return { kind: 'all', count: groups.length }
|
|
}
|
|
if (!groups.length) return { kind: 'none' }
|
|
const visible = groups.slice(0, 4)
|
|
const currentKey = currentUserId != null ? String(currentUserId) : ''
|
|
const chips = visible.map((group) => ({
|
|
name: group.name,
|
|
relation: String(group.leaderUserId ?? '') === currentKey ? '组长' : '组员',
|
|
}))
|
|
return { kind: 'chips', chips, extra: Math.max(groups.length - visible.length, 0) }
|
|
}
|