:Qrge branch 'jskim-node' of http://39.117.244.52:3000/kjs/ERP-node into jskim-node
This commit is contained in:
@@ -181,20 +181,92 @@ export async function getDistinctColumnValues(req: AuthenticatedRequest, res: Re
|
||||
? `WHERE ${whereConditions.join(" AND ")}`
|
||||
: "";
|
||||
|
||||
// DISTINCT 쿼리 실행
|
||||
const query = `
|
||||
// 1단계: DISTINCT 값 조회
|
||||
const distinctQuery = `
|
||||
SELECT DISTINCT "${columnName}" as value, "${effectiveLabelColumn}" as label
|
||||
FROM "${tableName}"
|
||||
${whereClause}
|
||||
ORDER BY "${effectiveLabelColumn}" ASC
|
||||
LIMIT 500
|
||||
`;
|
||||
const result = await pool.query(distinctQuery, params);
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
// 2단계: 카테고리/코드 라벨 변환 (값이 있을 때만)
|
||||
if (result.rows.length > 0) {
|
||||
const rawValues = result.rows.map((r: any) => r.value);
|
||||
const labelMap: Record<string, string> = {};
|
||||
|
||||
// category_values에서 라벨 조회
|
||||
try {
|
||||
const cvCompanyCondition = companyCode !== "*"
|
||||
? `AND (company_code = $4 OR company_code = '*')`
|
||||
: "";
|
||||
const cvParams = companyCode !== "*"
|
||||
? [tableName, columnName, rawValues, companyCode]
|
||||
: [tableName, columnName, rawValues];
|
||||
|
||||
const cvResult = await pool.query(
|
||||
`SELECT value_code, value_label FROM category_values
|
||||
WHERE table_name = $1 AND column_name = $2
|
||||
AND value_code = ANY($3) AND is_active = true
|
||||
${cvCompanyCondition}`,
|
||||
cvParams
|
||||
);
|
||||
cvResult.rows.forEach((r: any) => {
|
||||
labelMap[r.value_code] = r.value_label;
|
||||
});
|
||||
} catch (e) {
|
||||
// category_values 조회 실패 시 무시
|
||||
}
|
||||
|
||||
// code_info에서 라벨 조회 (code_category 기반)
|
||||
try {
|
||||
const ttcResult = await pool.query(
|
||||
`SELECT code_category FROM table_type_columns
|
||||
WHERE table_name = $1 AND column_name = $2 AND code_category IS NOT NULL
|
||||
LIMIT 1`,
|
||||
[tableName, columnName]
|
||||
);
|
||||
const codeCategory = ttcResult.rows[0]?.code_category;
|
||||
|
||||
if (codeCategory) {
|
||||
const ciCompanyCondition = companyCode !== "*"
|
||||
? `AND (company_code = $3 OR company_code = '*')`
|
||||
: "";
|
||||
const ciParams = companyCode !== "*"
|
||||
? [codeCategory, rawValues, companyCode]
|
||||
: [codeCategory, rawValues];
|
||||
|
||||
const ciResult = await pool.query(
|
||||
`SELECT code_value, code_name FROM code_info
|
||||
WHERE code_category = $1 AND code_value = ANY($2) AND is_active = 'Y'
|
||||
${ciCompanyCondition}`,
|
||||
ciParams
|
||||
);
|
||||
ciResult.rows.forEach((r: any) => {
|
||||
if (!labelMap[r.code_value]) {
|
||||
labelMap[r.code_value] = r.code_name;
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// code_info 조회 실패 시 무시
|
||||
}
|
||||
|
||||
// 라벨 매핑 적용
|
||||
if (Object.keys(labelMap).length > 0) {
|
||||
result.rows.forEach((row: any) => {
|
||||
if (labelMap[row.value]) {
|
||||
row.label = labelMap[row.value];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("컬럼 DISTINCT 값 조회 성공", {
|
||||
tableName,
|
||||
columnName,
|
||||
columnInputType: columnInputType || "none",
|
||||
labelColumn: effectiveLabelColumn,
|
||||
companyCode,
|
||||
hasFilters: !!filtersParam,
|
||||
|
||||
Reference in New Issue
Block a user