147 lines
6.3 KiB
Java
147 lines
6.3 KiB
Java
package com.erp.service;
|
|
|
|
import com.erp.common.BaseService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.*;
|
|
|
|
@Service
|
|
@Slf4j
|
|
public class EntityReferenceService extends BaseService {
|
|
|
|
private static final String NS = "com.erp.mapper.EntityReferenceMapper.";
|
|
|
|
public Map<String, Object> getEntityReferenceData(Map<String, Object> params) {
|
|
String tableName = (String) params.get("tableName");
|
|
String columnName = (String) params.get("columnName");
|
|
String companyCode = (String) params.get("companyCode");
|
|
int limit = toInt(params.getOrDefault("limit", 100));
|
|
Object search = params.get("search");
|
|
|
|
Map<String, Object> columnInfo = sqlSession.selectOne(NS + "selectColumnInfo",
|
|
Map.of("tableName", tableName, "columnName", columnName));
|
|
|
|
if (columnInfo == null) {
|
|
throw new IllegalArgumentException(
|
|
"컬럼 정보를 찾을 수 없습니다: " + tableName + "." + columnName);
|
|
}
|
|
|
|
String inputType = (String) columnInfo.get("inputType");
|
|
if (!"entity".equals(inputType)) {
|
|
throw new IllegalStateException(
|
|
"컬럼 '" + tableName + "." + columnName + "'은 entity 타입이 아닙니다. inputType: " + inputType);
|
|
}
|
|
|
|
String referenceTable = (String) columnInfo.get("referenceTable");
|
|
String referenceColumn = (String) columnInfo.get("referenceColumn");
|
|
String displayColumn = columnInfo.get("displayColumn") != null
|
|
? (String) columnInfo.get("displayColumn") : "name";
|
|
|
|
if (referenceTable == null || referenceColumn == null) {
|
|
throw new IllegalStateException(
|
|
"Entity 타입 컬럼 '" + tableName + "." + columnName
|
|
+ "'에 참조 테이블 정보가 설정되지 않았습니다.");
|
|
}
|
|
|
|
String safeTable = sanitizeIdentifier(referenceTable);
|
|
String safeRefCol = sanitizeIdentifier(referenceColumn);
|
|
String safeDispCol = sanitizeIdentifier(displayColumn);
|
|
|
|
if (safeTable.isEmpty() || safeRefCol.isEmpty() || safeDispCol.isEmpty()) {
|
|
throw new IllegalArgumentException("잘못된 참조 테이블/컬럼 이름입니다.");
|
|
}
|
|
|
|
Integer tableExistsObj = sqlSession.selectOne(NS + "checkTableExistsInSchema",
|
|
Map.of("tableName", safeTable));
|
|
int tableExists = tableExistsObj != null ? tableExistsObj : 0;
|
|
if (tableExists == 0) {
|
|
throw new IllegalStateException("참조 테이블 '" + safeTable + "'이 존재하지 않습니다.");
|
|
}
|
|
|
|
Integer hasCompanyCodeObj = sqlSession.selectOne(NS + "checkTableHasCompanyCode",
|
|
Map.of("tableName", safeTable));
|
|
int hasCompanyCode = hasCompanyCodeObj != null ? hasCompanyCodeObj : 0;
|
|
|
|
Map<String, Object> queryParams = new HashMap<>();
|
|
queryParams.put("tableName", safeTable);
|
|
queryParams.put("referenceColumn", safeRefCol);
|
|
queryParams.put("displayColumn", safeDispCol);
|
|
queryParams.put("companyCode", companyCode);
|
|
queryParams.put("hasCompanyCode", hasCompanyCode > 0);
|
|
queryParams.put("limit", limit);
|
|
if (search != null && !search.toString().isBlank()) {
|
|
queryParams.put("searchLike", "%" + search + "%");
|
|
}
|
|
|
|
log.info("엔티티 참조 데이터 조회: {}.{} -> {}.{} (display: {}), company={}",
|
|
tableName, columnName, safeTable, safeRefCol, safeDispCol, companyCode);
|
|
|
|
List<Map<String, Object>> rows = sqlSession.selectList(NS + "selectReferenceData", queryParams);
|
|
|
|
List<Map<String, Object>> options = new ArrayList<>();
|
|
for (Map<String, Object> row : rows) {
|
|
Object val = row.get("refValue");
|
|
Object disp = row.get("displayName");
|
|
Map<String, Object> opt = new LinkedHashMap<>();
|
|
opt.put("value", val != null ? val.toString() : "");
|
|
opt.put("label", disp != null ? disp.toString() : (val != null ? val.toString() : ""));
|
|
options.add(opt);
|
|
}
|
|
|
|
Map<String, Object> referenceInfo = new LinkedHashMap<>();
|
|
referenceInfo.put("referenceTable", safeTable);
|
|
referenceInfo.put("referenceColumn", safeRefCol);
|
|
referenceInfo.put("displayColumn", safeDispCol);
|
|
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
|
result.put("options", options);
|
|
result.put("referenceInfo", referenceInfo);
|
|
return result;
|
|
}
|
|
|
|
public Map<String, Object> getCodeData(Map<String, Object> params) {
|
|
String codeCategory = (String) params.get("codeCategory");
|
|
String companyCode = (String) params.get("companyCode");
|
|
int limit = toInt(params.getOrDefault("limit", 100));
|
|
Object search = params.get("search");
|
|
|
|
Map<String, Object> queryParams = new HashMap<>();
|
|
queryParams.put("codeCategory", codeCategory);
|
|
queryParams.put("companyCode", companyCode);
|
|
queryParams.put("limit", limit);
|
|
if (search != null && !search.toString().isBlank()) {
|
|
queryParams.put("searchLike", "%" + search + "%");
|
|
}
|
|
|
|
log.info("공통 코드 데이터 조회: category={}, company={}", codeCategory, companyCode);
|
|
|
|
List<Map<String, Object>> rows = sqlSession.selectList(NS + "selectCodeData", queryParams);
|
|
|
|
List<Map<String, Object>> options = new ArrayList<>();
|
|
for (Map<String, Object> row : rows) {
|
|
Map<String, Object> opt = new LinkedHashMap<>();
|
|
opt.put("value", String.valueOf(row.get("codeValue")));
|
|
opt.put("label", String.valueOf(row.get("codeName")));
|
|
options.add(opt);
|
|
}
|
|
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
|
result.put("options", options);
|
|
result.put("codeCategory", codeCategory);
|
|
return result;
|
|
}
|
|
|
|
private String sanitizeIdentifier(String name) {
|
|
if (name == null) return "";
|
|
return name.replaceAll("[^a-zA-Z0-9_]", "");
|
|
}
|
|
|
|
private int toInt(Object val) {
|
|
if (val == null) return 0;
|
|
if (val instanceof Number) return ((Number) val).intValue();
|
|
try { return Integer.parseInt(val.toString()); }
|
|
catch (NumberFormatException e) { return 0; }
|
|
}
|
|
}
|