优化物品规则分页加载
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,
|
||||
|
||||
Reference in New Issue
Block a user