406 lines
22 KiB
Java
406 lines
22 KiB
Java
package com.erp.service;
|
|
|
|
import com.erp.common.BaseService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.util.*;
|
|
|
|
/**
|
|
* Common Code Service
|
|
*
|
|
* commonCodeRoutes.ts 포팅.
|
|
* 테이블: code_category, code_info
|
|
*/
|
|
@Service
|
|
@Slf4j
|
|
public class CommonCodeService extends BaseService {
|
|
|
|
private static final String NS = "com.erp.mapper.CommonCodeMapper.";
|
|
|
|
private static final long DEFAULT_MENU_OBJID = 1757401858940L;
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 카테고리 목록
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
public Map<String, Object> getCommonCodeCategoryList(Map<String, Object> params) {
|
|
int page = toInt(params.get("page"), 1);
|
|
int size = toInt(params.get("size"), 20);
|
|
params.put("limit", size);
|
|
params.put("offset", (page - 1) * size);
|
|
|
|
Object isActiveRaw = params.get("isActive");
|
|
if (isActiveRaw != null) params.put("isActive", toBool(isActiveRaw));
|
|
|
|
List<Map<String, Object>> categories = sqlSession.selectList(NS + "getCommonCodeCategoryList", params);
|
|
Integer totalObj = sqlSession.selectOne(NS + "getCommonCodeCategoryListCnt", params);
|
|
int total = totalObj != null ? totalObj : 0;
|
|
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
|
result.put("data", categories);
|
|
result.put("total", total);
|
|
return result;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 카테고리 중복 확인
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
public Map<String, Object> checkCategoryDuplicate(String categoryCode, String companyCode) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("companyCode", companyCode);
|
|
Integer countObj = sqlSession.selectOne(NS + "getCommonCodeCategoryDuplicateCnt", params);
|
|
int count = countObj != null ? countObj : 0;
|
|
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
|
result.put("isDuplicate", count > 0);
|
|
return result;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 카테고리 생성
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
@Transactional
|
|
public Map<String, Object> insertCommonCodeCategory(Map<String, Object> body, String companyCode, String userId) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", body.get("categoryCode"));
|
|
params.put("categoryName", body.get("categoryName"));
|
|
params.put("categoryNameEng", body.getOrDefault("categoryNameEng", null));
|
|
params.put("description", body.getOrDefault("description", null));
|
|
params.put("sortOrder", body.getOrDefault("sortOrder", 0));
|
|
params.put("isActive", body.getOrDefault("isActive", true));
|
|
params.put("menuObjid", body.getOrDefault("menuObjid", DEFAULT_MENU_OBJID));
|
|
params.put("companyCode", companyCode);
|
|
params.put("createdBy", userId);
|
|
params.put("updatedBy", userId);
|
|
|
|
sqlSession.insert(NS + "insertCommonCodeCategory", params);
|
|
|
|
Map<String, Object> q = new HashMap<>();
|
|
q.put("categoryCode", params.get("categoryCode"));
|
|
q.put("companyCode", companyCode);
|
|
return sqlSession.selectOne(NS + "getCommonCodeCategoryInfo", q);
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 카테고리 수정
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
@Transactional
|
|
public Map<String, Object> updateCommonCodeCategory(String categoryCode, Map<String, Object> body,
|
|
String companyCode, String userId) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("companyCode", companyCode);
|
|
params.put("updatedBy", userId);
|
|
|
|
if (body.containsKey("categoryName")) params.put("categoryName", body.get("categoryName"));
|
|
if (body.containsKey("categoryNameEng")) params.put("categoryNameEng", body.get("categoryNameEng"));
|
|
if (body.containsKey("description")) params.put("description", body.get("description"));
|
|
if (body.containsKey("sortOrder")) params.put("sortOrder", body.get("sortOrder"));
|
|
if (body.containsKey("isActive")) params.put("isActive", body.get("isActive"));
|
|
|
|
int updated = sqlSession.update(NS + "updateCommonCodeCategory", params);
|
|
if (updated == 0) return null;
|
|
|
|
Map<String, Object> q = new HashMap<>();
|
|
q.put("categoryCode", categoryCode);
|
|
q.put("companyCode", companyCode);
|
|
return sqlSession.selectOne(NS + "getCommonCodeCategoryInfo", q);
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 카테고리 삭제
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
@Transactional
|
|
public void deleteCommonCodeCategory(String categoryCode, String companyCode) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("companyCode", companyCode);
|
|
int deleted = sqlSession.delete(NS + "deleteCommonCodeCategory", params);
|
|
if (deleted == 0) throw new IllegalArgumentException("카테고리를 찾을 수 없습니다.");
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 코드 목록 (snake_case + camelCase 이중 필드)
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
public Map<String, Object> getCommonCodeList(String categoryCode, Map<String, Object> params) {
|
|
int page = toInt(params.get("page"), 1);
|
|
int size = toInt(params.get("size"), 20);
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("limit", size);
|
|
params.put("offset", (page - 1) * size);
|
|
|
|
Object isActiveRaw = params.get("isActive");
|
|
if (isActiveRaw != null) params.put("isActive", toBool(isActiveRaw));
|
|
|
|
List<Map<String, Object>> rawList = sqlSession.selectList(NS + "getCommonCodeList", params);
|
|
Integer totalObj = sqlSession.selectOne(NS + "getCommonCodeListCnt", params);
|
|
int total = totalObj != null ? totalObj : 0;
|
|
|
|
List<Map<String, Object>> codes = new ArrayList<>();
|
|
for (Map<String, Object> raw : rawList) {
|
|
codes.add(transformCode(raw));
|
|
}
|
|
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
|
result.put("data", codes);
|
|
result.put("total", total);
|
|
return result;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 코드 중복 확인
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
public Map<String, Object> checkCodeDuplicate(String categoryCode, String codeValue, String companyCode) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("codeValue", codeValue);
|
|
params.put("companyCode", companyCode);
|
|
Integer countObj = sqlSession.selectOne(NS + "getCommonCodeDuplicateCnt", params);
|
|
int count = countObj != null ? countObj : 0;
|
|
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
|
result.put("isDuplicate", count > 0);
|
|
return result;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 코드 생성
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
@Transactional
|
|
public Map<String, Object> insertCommonCode(String categoryCode, Map<String, Object> body,
|
|
String companyCode, String userId) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("codeValue", body.get("codeValue"));
|
|
params.put("codeName", body.get("codeName"));
|
|
params.put("codeNameEng", body.getOrDefault("codeNameEng", null));
|
|
params.put("description", body.getOrDefault("description", null));
|
|
params.put("sortOrder", body.getOrDefault("sortOrder", 0));
|
|
params.put("isActive", body.getOrDefault("isActive", true));
|
|
params.put("menuObjid", body.getOrDefault("menuObjid", DEFAULT_MENU_OBJID));
|
|
params.put("companyCode", companyCode);
|
|
params.put("parentCodeValue", body.getOrDefault("parentCodeValue", null));
|
|
params.put("depth", body.getOrDefault("depth", 0));
|
|
params.put("createdBy", userId);
|
|
params.put("updatedBy", userId);
|
|
|
|
sqlSession.insert(NS + "insertCommonCode", params);
|
|
|
|
Map<String, Object> q = new HashMap<>();
|
|
q.put("categoryCode", categoryCode);
|
|
q.put("codeValue", params.get("codeValue"));
|
|
q.put("companyCode", companyCode);
|
|
Map<String, Object> raw = sqlSession.selectOne(NS + "getCommonCodeInfo", q);
|
|
return raw != null ? transformCode(raw) : null;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 코드 정렬 순서 변경
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
@Transactional
|
|
public void updateCommonCodeOrder(String categoryCode, List<Map<String, Object>> codes, String companyCode) {
|
|
for (Map<String, Object> code : codes) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("codeValue", code.get("codeValue"));
|
|
params.put("sortOrder", code.get("sortOrder"));
|
|
params.put("companyCode", companyCode);
|
|
sqlSession.update(NS + "updateCommonCodeSortOrder", params);
|
|
}
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 계층형 코드 목록
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
public List<Map<String, Object>> getCommonCodeHierarchicalList(String categoryCode, Map<String, Object> params) {
|
|
params.put("categoryCode", categoryCode);
|
|
|
|
Object isActiveRaw = params.get("isActive");
|
|
if (isActiveRaw != null) params.put("isActive", toBool(isActiveRaw));
|
|
else params.remove("isActive");
|
|
|
|
List<Map<String, Object>> rawList = sqlSession.selectList(NS + "getCommonCodeHierarchicalList", params);
|
|
List<Map<String, Object>> result = new ArrayList<>();
|
|
for (Map<String, Object> raw : rawList) {
|
|
result.add(transformCode(raw));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 트리 구조
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
public List<Map<String, Object>> getCommonCodeTree(String categoryCode, String companyCode) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("companyCode", companyCode);
|
|
|
|
List<Map<String, Object>> flatList = sqlSession.selectList(NS + "getCommonCodeTreeList", params);
|
|
return buildTree(flatList);
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 자식 존재 여부
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
public Map<String, Object> hasChildren(String categoryCode, String codeValue, String companyCode) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("codeValue", codeValue);
|
|
params.put("companyCode", companyCode);
|
|
Integer countObj = sqlSession.selectOne(NS + "getCommonCodeChildrenCnt", params);
|
|
int count = countObj != null ? countObj : 0;
|
|
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
|
result.put("hasChildren", count > 0);
|
|
return result;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 코드 수정
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
@Transactional
|
|
public Map<String, Object> updateCommonCode(String categoryCode, String codeValue,
|
|
Map<String, Object> body, String companyCode, String userId) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("codeValue", codeValue);
|
|
params.put("companyCode", companyCode);
|
|
params.put("updatedBy", userId);
|
|
|
|
if (body.containsKey("codeName")) params.put("codeName", body.get("codeName"));
|
|
if (body.containsKey("codeNameEng")) params.put("codeNameEng", body.get("codeNameEng"));
|
|
if (body.containsKey("description")) params.put("description", body.get("description"));
|
|
if (body.containsKey("sortOrder")) params.put("sortOrder", body.get("sortOrder"));
|
|
if (body.containsKey("isActive")) params.put("isActive", body.get("isActive"));
|
|
if (body.containsKey("parentCodeValue")) params.put("parentCodeValue", body.get("parentCodeValue"));
|
|
if (body.containsKey("depth")) params.put("depth", body.get("depth"));
|
|
|
|
int updated = sqlSession.update(NS + "updateCommonCode", params);
|
|
if (updated == 0) return null;
|
|
|
|
Map<String, Object> q = new HashMap<>();
|
|
q.put("categoryCode", categoryCode);
|
|
q.put("codeValue", codeValue);
|
|
q.put("companyCode", companyCode);
|
|
Map<String, Object> raw = sqlSession.selectOne(NS + "getCommonCodeInfo", q);
|
|
return raw != null ? transformCode(raw) : null;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 코드 삭제
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
@Transactional
|
|
public void deleteCommonCode(String categoryCode, String codeValue, String companyCode) {
|
|
Map<String, Object> params = new HashMap<>();
|
|
params.put("categoryCode", categoryCode);
|
|
params.put("codeValue", codeValue);
|
|
params.put("companyCode", companyCode);
|
|
int deleted = sqlSession.delete(NS + "deleteCommonCode", params);
|
|
if (deleted == 0) throw new IllegalArgumentException("코드를 찾을 수 없습니다.");
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// 코드 옵션 목록
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
public List<Map<String, Object>> getCommonCodeOptionList(String categoryCode, Map<String, Object> params) {
|
|
params.put("categoryCode", categoryCode);
|
|
|
|
Object isActiveRaw = params.get("isActive");
|
|
// 미지정 시 활성 코드만 반환 (드롭다운 기본 동작)
|
|
params.put("isActive", isActiveRaw != null ? toBool(isActiveRaw) : true);
|
|
|
|
List<Map<String, Object>> rawList = sqlSession.selectList(NS + "getCommonCodeOptionList", params);
|
|
List<Map<String, Object>> options = new ArrayList<>();
|
|
for (Map<String, Object> raw : rawList) {
|
|
Map<String, Object> opt = new LinkedHashMap<>();
|
|
opt.put("value", raw.get("code_value"));
|
|
opt.put("label", raw.get("code_name"));
|
|
opt.put("labelEng", raw.get("code_name_eng"));
|
|
options.add(opt);
|
|
}
|
|
return options;
|
|
}
|
|
|
|
// ══════════════════════════════════════════════════════════════
|
|
// Private helpers
|
|
// ══════════════════════════════════════════════════════════════
|
|
|
|
/** snake_case 원본 + camelCase 별칭을 모두 포함하는 Map 반환 */
|
|
private Map<String, Object> transformCode(Map<String, Object> raw) {
|
|
Map<String, Object> item = new LinkedHashMap<>(raw);
|
|
item.put("codeValue", raw.get("code_value"));
|
|
item.put("codeName", raw.get("code_name"));
|
|
item.put("codeNameEng", raw.get("code_name_eng"));
|
|
item.put("codeCategory", raw.get("code_category"));
|
|
item.put("sortOrder", raw.get("sort_order"));
|
|
item.put("isActive", raw.get("is_active"));
|
|
item.put("menuObjid", raw.get("menu_objid"));
|
|
item.put("companyCode", raw.get("company_code"));
|
|
item.put("parentCodeValue", raw.get("parent_code_value"));
|
|
item.put("createdBy", raw.get("created_by"));
|
|
item.put("updatedBy", raw.get("updated_by"));
|
|
item.put("createdDate", raw.get("created_date"));
|
|
item.put("updatedDate", raw.get("updated_date"));
|
|
return item;
|
|
}
|
|
|
|
/** 평탄 목록을 부모-자식 트리로 변환 */
|
|
@SuppressWarnings("unchecked")
|
|
private List<Map<String, Object>> buildTree(List<Map<String, Object>> codes) {
|
|
Map<String, Map<String, Object>> byValue = new LinkedHashMap<>();
|
|
for (Map<String, Object> code : codes) {
|
|
String val = objToStr(code.get("code_value"));
|
|
Map<String, Object> node = new LinkedHashMap<>(transformCode(code));
|
|
node.put("children", new ArrayList<Map<String, Object>>());
|
|
byValue.put(val, node);
|
|
}
|
|
|
|
List<Map<String, Object>> roots = new ArrayList<>();
|
|
for (Map<String, Object> code : codes) {
|
|
String val = objToStr(code.get("code_value"));
|
|
Object parentRaw = code.get("parent_code_value");
|
|
String parentVal = (parentRaw != null) ? parentRaw.toString() : null;
|
|
|
|
Map<String, Object> node = byValue.get(val);
|
|
if (parentVal == null || parentVal.isEmpty() || !byValue.containsKey(parentVal)) {
|
|
roots.add(node);
|
|
} else {
|
|
((List<Map<String, Object>>) byValue.get(parentVal).get("children")).add(node);
|
|
}
|
|
}
|
|
return roots;
|
|
}
|
|
|
|
private int toInt(Object val, int defaultVal) {
|
|
if (val == null) return defaultVal;
|
|
try { return Integer.parseInt(val.toString()); }
|
|
catch (NumberFormatException e) { return defaultVal; }
|
|
}
|
|
|
|
private boolean toBool(Object val) {
|
|
if (val instanceof Boolean b) return b;
|
|
return "true".equalsIgnoreCase(val.toString());
|
|
}
|
|
|
|
private String objToStr(Object val) {
|
|
return val != null ? val.toString() : "";
|
|
}
|
|
}
|