优化物品规则分页加载
This commit is contained in:
@@ -262,8 +262,11 @@ export type WorkOrderStatistics = {
|
||||
}
|
||||
|
||||
export type ProductRuleListInput = {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
enabled?: boolean | null
|
||||
keyword?: string
|
||||
categoryId?: number
|
||||
}
|
||||
|
||||
export type WalletLedgerListInput = {
|
||||
|
||||
@@ -548,8 +548,9 @@ export async function deleteWorkCategory(
|
||||
export async function listWorkProductRules({
|
||||
enabled = null,
|
||||
keyword = '',
|
||||
categoryId = 0,
|
||||
}: ProductRuleListInput = {}): Promise<WorkProductRuleRow[]> {
|
||||
const { whereClause, params } = buildWorkProductRuleWhere({ enabled, keyword })
|
||||
const { whereClause, params } = buildWorkProductRuleWhere({ enabled, keyword, categoryId })
|
||||
const result = await query<WorkProductRuleRow>(
|
||||
`${WORK_PRODUCT_RULE_SELECT}
|
||||
${whereClause}
|
||||
@@ -559,6 +560,56 @@ export async function listWorkProductRules({
|
||||
return result.rows
|
||||
}
|
||||
|
||||
export async function listWorkProductRulesPage({
|
||||
page = 1,
|
||||
pageSize = 20,
|
||||
enabled = null,
|
||||
keyword = '',
|
||||
categoryId = 0,
|
||||
}: ProductRuleListInput): Promise<{ items: WorkProductRuleRow[]; total: number }> {
|
||||
const { whereClause, params } = buildWorkProductRuleWhere({ enabled, keyword, categoryId })
|
||||
const totalResult = await query<{ total: number }>(
|
||||
`
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM work_product_rules wpr
|
||||
${whereClause}
|
||||
`,
|
||||
params,
|
||||
)
|
||||
const offset = (page - 1) * pageSize
|
||||
const listParams = [...params, pageSize, offset]
|
||||
const result = await query<WorkProductRuleRow>(
|
||||
`${WORK_PRODUCT_RULE_SELECT}
|
||||
${whereClause}
|
||||
ORDER BY wpr.sort_order ASC, wpr.id DESC
|
||||
LIMIT $${listParams.length - 1} OFFSET $${listParams.length}`,
|
||||
listParams,
|
||||
)
|
||||
return { items: result.rows, total: Number(totalResult.rows[0]?.total || 0) }
|
||||
}
|
||||
|
||||
export async function countWorkProductRulesByCategory(
|
||||
input: {
|
||||
enabled?: boolean | null
|
||||
keyword?: string
|
||||
} = {},
|
||||
): Promise<Array<{ categoryId: number | null; total: number }>> {
|
||||
const { whereClause, params } = buildWorkProductRuleWhere(input)
|
||||
const result = await query<{ category_id: number | null; total: number }>(
|
||||
`
|
||||
SELECT wpr.category_id, COUNT(*)::int AS total
|
||||
FROM work_product_rules wpr
|
||||
${whereClause}
|
||||
GROUP BY wpr.category_id
|
||||
`,
|
||||
params,
|
||||
)
|
||||
return result.rows.map((row) => ({
|
||||
categoryId: row.category_id ? Number(row.category_id) : null,
|
||||
total: Number(row.total || 0),
|
||||
}))
|
||||
}
|
||||
|
||||
export async function getWorkProductRuleByKey(ruleKey: string): Promise<WorkProductRuleRow | null> {
|
||||
const result = await query<WorkProductRuleRow>(
|
||||
`${WORK_PRODUCT_RULE_SELECT} WHERE wpr.rule_key = $1 LIMIT 1`,
|
||||
@@ -2803,7 +2854,11 @@ function buildWorkOrderWhere({
|
||||
}
|
||||
}
|
||||
|
||||
function buildWorkProductRuleWhere({ enabled = null, keyword = '' }: ProductRuleListInput) {
|
||||
function buildWorkProductRuleWhere({
|
||||
enabled = null,
|
||||
keyword = '',
|
||||
categoryId = 0,
|
||||
}: ProductRuleListInput) {
|
||||
const filters: string[] = []
|
||||
const params: unknown[] = []
|
||||
if (enabled !== null && enabled !== undefined) {
|
||||
@@ -2818,6 +2873,10 @@ function buildWorkProductRuleWhere({ enabled = null, keyword = '' }: ProductRule
|
||||
OR wpr.product_name ILIKE $${params.length}
|
||||
)`)
|
||||
}
|
||||
if (categoryId) {
|
||||
params.push(categoryId)
|
||||
filters.push(`wpr.category_id = $${params.length}`)
|
||||
}
|
||||
return {
|
||||
whereClause: filters.length > 0 ? `WHERE ${filters.join(' AND ')}` : '',
|
||||
params,
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
countWorkerActiveOrders,
|
||||
countWorkCategoryUsages,
|
||||
countWorkerLevelUsages,
|
||||
countWorkProductRulesByCategory,
|
||||
countWorkOrderPendingSharingSubmissions,
|
||||
countTimeoutEventsByWorkerIds,
|
||||
createWorkOrder,
|
||||
@@ -31,6 +32,7 @@ import {
|
||||
listWorkOrders,
|
||||
listWorkOrderEventsByOrderId,
|
||||
listWorkProductRules,
|
||||
listWorkProductRulesPage,
|
||||
listWorkerLevels,
|
||||
listWorkerUsers,
|
||||
listWorkerWithdrawalAccounts,
|
||||
@@ -232,15 +234,32 @@ export async function deleteAdminWorkerLevel(levelId: number | string) {
|
||||
|
||||
export async function listAdminWorkProductRules(query: JsonObject = {}) {
|
||||
await ensureWorkerPlatformDefaults()
|
||||
const page = normalizePage(query.page)
|
||||
const pageSize = normalizePageSize(query.pageSize)
|
||||
const enabledValue = String(query.enabled ?? '').trim()
|
||||
const enabled = enabledValue
|
||||
? ['true', '1', 'enabled', 'active'].includes(enabledValue.toLowerCase())
|
||||
: null
|
||||
const items = await listWorkProductRules({
|
||||
enabled,
|
||||
keyword: String(query.keyword || '').trim(),
|
||||
})
|
||||
return { items: items.map(mapWorkProductRule) }
|
||||
const keyword = String(query.keyword || '').trim()
|
||||
const categoryId = normalizeOptionalId(query.categoryId ?? query.category_id) || 0
|
||||
const [{ items, total }, categoryRows] = await Promise.all([
|
||||
listWorkProductRulesPage({
|
||||
page,
|
||||
pageSize,
|
||||
enabled,
|
||||
keyword,
|
||||
categoryId,
|
||||
}),
|
||||
countWorkProductRulesByCategory({ enabled, keyword }),
|
||||
])
|
||||
return {
|
||||
items: items.map(mapWorkProductRule),
|
||||
pagination: { page, pageSize, total },
|
||||
categoryCounts: categoryRows.map((row) => ({
|
||||
categoryId: row.categoryId,
|
||||
total: row.total,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveAdminWorkProductRule(payload: JsonObject = {}) {
|
||||
|
||||
Reference in New Issue
Block a user