260225_01_ENH: 최초 구현 완료

This commit is contained in:
2026-02-25 16:59:53 +09:00
commit 511e2ef765
49 changed files with 2933 additions and 0 deletions

3
.gitattributes vendored Normal file
View File

@@ -0,0 +1,3 @@
/gradlew text eol=lf
*.bat text eol=crlf
*.jar binary

37
.gitignore vendored Normal file
View File

@@ -0,0 +1,37 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/

65
build.gradle Normal file
View File

@@ -0,0 +1,65 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.5.7'
id 'io.spring.dependency-management' version '1.1.7'
}
group = 'com.kri.lge'
version = '0.0.1-SNAPSHOT'
description = 'lge-penetration-back'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
def giteaUser = findProperty("giteaUser") ?: ""
def giteaToken = findProperty("giteaToken") ?: ""
repositories {
mavenCentral()
maven {
name = "gitea"
url = uri("https://gitea.kric.com/api/packages/KRi/maven")
//allowInsecureProtocol = true
credentials {
username = giteaUser
password = giteaToken
}
}
}
dependencies {
implementation "com.kri.common:starter:beta.1.0.66"
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.11"
//JPA
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
//Entity Json <-> Class 파싱용
implementation "io.hypersistence:hypersistence-utils-hibernate-62:3.9.4"
//MySQL
runtimeOnly "com.mysql:mysql-connector-j"
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.apache.poi:poi-ooxml:5.2.5'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.5'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}

BIN
gradle/wrapper/gradle-wrapper.jar vendored Normal file

Binary file not shown.

View File

@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.0-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

248
gradlew vendored Normal file
View File

@@ -0,0 +1,248 @@
#!/bin/sh
#
# Copyright © 2015 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

93
gradlew.bat vendored Normal file
View File

@@ -0,0 +1,93 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:execute
@rem Setup the command line
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

1
settings.gradle Normal file
View File

@@ -0,0 +1 @@
rootProject.name = 'lge-penetration-back'

View File

@@ -0,0 +1,33 @@
package com.kri.lge.penetration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@Slf4j
@SpringBootApplication
public class LgePenetrationBackApplication {
public static void main(String[] args) {
SpringApplication.run(LgePenetrationBackApplication.class, args);
}
@Bean
ApplicationRunner dsCheck(ApplicationContext ctx) {
return args -> {
String[] names = ctx.getBeanNamesForType(javax.sql.DataSource.class);
log.info("DataSource beans = {}", java.util.Arrays.toString(names));
for (String n : names) {
Object ds = ctx.getBean(n);
log.info(" - {} = {}", n, ds.getClass().getName());
if (ds instanceof com.zaxxer.hikari.HikariDataSource hk) {
log.info(" jdbcUrl=" + hk.getJdbcUrl());
log.info(" driver=" + hk.getDriverClassName());
}
}
};
}
}

View File

@@ -0,0 +1,78 @@
package com.kri.lge.penetration.common.cache;
import com.kri.lge.penetration.persistence.entity.TStatisticsCacheEntity;
import com.kri.lge.penetration.persistence.repository.StatisticsCacheRepository;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
public class StatisticsDbCache {
private final StatisticsCacheRepository statisticsCacheRepository;
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void clearStatisticsCache() {
statisticsCacheRepository.deleteAll();
}
@Transactional(readOnly = true)
public TStatisticsCacheEntity getStatisticsCache(String cacheKey) {
return statisticsCacheRepository.findById(cacheKey).orElse(null);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveStatisticsCache(List<TStatisticsCacheEntity> cacheList) {
if (cacheList == null || cacheList.isEmpty()) return;
statisticsCacheRepository.saveAll(cacheList);
}
public String makeResultCacheKey(String requestKey, Map<String, Collection<Integer>> filterCondition) {
String rk = (requestKey == null) ? "" : requestKey;
if (filterCondition == null || filterCondition.isEmpty()) {
return rk;
}
// Map 순회 순서(예: HashMap)가 불안정하면 캐시 키가 흔들리니 key/value 모두 정렬해서 고정
List<String> keys = new ArrayList<>(filterCondition.keySet());
Collections.sort(keys);
StringBuilder sb = new StringBuilder(rk.length() + 64);
sb.append(rk).append('_');
boolean firstFilter = true;
for (String k : keys) {
if (!firstFilter) sb.append('|');
firstFilter = false;
sb.append(k).append('#');
Collection<Integer> values = filterCondition.get(k);
if (values == null || values.isEmpty()) {
continue;
}
int[] sorted = values.stream()
.filter(Objects::nonNull)
.mapToInt(Integer::intValue)
.sorted()
.toArray();
for (int i = 0; i < sorted.length; i++) {
if (i > 0) sb.append(',');
sb.append(sorted[i]);
}
}
return sb.toString();
}
}

View File

@@ -0,0 +1,30 @@
package com.kri.lge.penetration.common.cache;
import com.kri.common.cache.AbstractMapCache;
import com.kri.lge.penetration.dto.VariableDto;
import com.kri.lge.penetration.persistence.entity.TVariableEntity;
import com.kri.lge.penetration.persistence.repository.VariableRepository;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class VariableCache extends AbstractMapCache<String, VariableDto> {
private final StatisticsDbCache statisticsDbCache;
private final VariableRepository variableRepository;
@Override
protected Map<String, VariableDto> loadFromDb() {
statisticsDbCache.clearStatisticsCache();
return variableRepository.findAll().stream()
.collect(Collectors.toMap(TVariableEntity::getVariableId, VariableDto::from));
}
public Map<String, VariableDto> getVariableMap() {
return getMap();
}
}

View File

@@ -0,0 +1,27 @@
package com.kri.lge.penetration.common.cache;
import com.kri.common.cache.AbstractListCache;
import com.kri.lge.penetration.persistence.repository.YearRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class YearCache extends AbstractListCache<Integer> {
private final StatisticsDbCache statisticsDbCache;
private final YearRepository yearRepository;
@Override
protected List<Integer> loadFromDb() {
statisticsDbCache.clearStatisticsCache();
return yearRepository.findYearList();
}
// 기존 메서드명이 필요하면 래핑
public List<Integer> getYearList() {
return getList();
}
}

View File

@@ -0,0 +1,19 @@
package com.kri.lge.penetration.common.consts;
public final class LgePenetrationConst {
public static final int BATCH_SIZE = 1000;
public static final int ADMIN_ROLE = 9999;
public static final String EXCEL_ID = "ID";
public static final String YEAR_STR = "K연도";
public static final String YEAR_LABEL_STR = "year";
public static final String LABEL_STR = "rowLabel";
public static final String TOTAL_LABEL_STR = "사례수";
public static final String TOTAL_STR = "total";
public static final int DECIMAL_POINT = 0;
private LgePenetrationConst() {
}
}

View File

@@ -0,0 +1,34 @@
package com.kri.lge.penetration.common.enums;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.kri.common.enums.CodeEnum;
import java.util.Objects;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter(onMethod_ = @JsonValue)
@RequiredArgsConstructor
public enum VariableType implements CodeEnum<Integer> {
SINGLE(21),
MULTI(31),
RANK(32),
SCALE(22),
NUMERIC(41);
private final Integer code;
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public static VariableType fromCode(Integer code) {
if (code == null) {
return null;
}
for (VariableType s : values()) {
if (Objects.equals(s.code, code)) {
return s;
}
}
throw new IllegalArgumentException("Unknown VariableType code: " + code);
}
}

View File

@@ -0,0 +1,33 @@
package com.kri.lge.penetration.controller;
import com.kri.common.datetime.DateTimeUtil;
import com.kri.common.webmvc.RequestUserContext;
import com.kri.lge.penetration.dto.LoginRequestDto;
import com.kri.lge.penetration.dto.LoginResponseDto;
import com.kri.lge.penetration.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
@Tag(name = "Auth", description = "인증 API")
public class AuthController {
private final AuthService authService;
@Operation(summary = "로그인 요청")
@PostMapping("/login")
public LoginResponseDto tryLogin(
@Valid @RequestBody LoginRequestDto request
) {
return authService.tryLogin(request);
}
}

View File

@@ -0,0 +1,62 @@
package com.kri.lge.penetration.controller;
import com.kri.common.web.BusinessException;
import com.kri.lge.penetration.common.cache.StatisticsDbCache;
import com.kri.lge.penetration.common.cache.VariableCache;
import com.kri.lge.penetration.common.cache.YearCache;
import com.kri.lge.penetration.dto.VariableDto;
import com.kri.lge.penetration.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/admin-cache")
@RequiredArgsConstructor
@Tag(name = "Admin Cache", description = "캐시 관리 API")
public class CacheAdminController {
private final YearCache yearCache;
private final VariableCache variableCache;
private final StatisticsDbCache statisticsDbCache;
private final AuthService authService;
@Operation(summary = "가용 연도 리스트 캐시 조회")
@GetMapping("/year")
public List<Integer> getYearList() {
return yearCache.getYearList();
}
@Operation(summary = "변수 맵 캐시 조회")
@GetMapping("variable")
public Map<String, VariableDto> getVariableMap() { return variableCache.getVariableMap(); }
@Operation(summary = "가용 연도 리스트 캐시 수동 갱신")
@PostMapping("/year/reload/{adminPw}")
public List<Integer> reloadYearList(@PathVariable String adminPw) {
if (!authService.isAdmin(adminPw)) {
throw BusinessException.unauthorized("IS_NOT_ADMIN", "권한이 없습니다.");
}
statisticsDbCache.clearStatisticsCache();
return yearCache.reload();
}
@Operation(summary = "변수 맵 캐시 수동 갱신")
@PostMapping("/variable/reload/{adminPw}")
public Map<String, VariableDto> reloadVariableMap(@PathVariable String adminPw) {
if (!authService.isAdmin(adminPw)) {
throw BusinessException.unauthorized("IS_NOT_ADMIN", "권한이 없습니다.");
}
statisticsDbCache.clearStatisticsCache();
return variableCache.reload();
}
}

View File

@@ -0,0 +1,35 @@
package com.kri.lge.penetration.controller;
import com.kri.lge.penetration.dto.StatisticsRequestDto;
import com.kri.lge.penetration.dto.StatisticsRequestOptionDto;
import com.kri.lge.penetration.dto.StatisticsResponseDto;
import com.kri.lge.penetration.service.StatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/statistics")
@RequiredArgsConstructor
@Tag(name = "Statistics", description = "통계 API")
public class StatisticsController {
private final StatisticsService statisticsService;
@Operation(summary = "통계 처리")
@PostMapping
public StatisticsResponseDto getStatistics(
@Valid @RequestBody StatisticsRequestDto request
) {
if (request.getOption() == null) {
request.setOption(new StatisticsRequestOptionDto());
}
return statisticsService.getStatistics(request);
}
}

View File

@@ -0,0 +1,78 @@
package com.kri.lge.penetration.controller;
import com.kri.lge.penetration.common.cache.StatisticsDbCache;
import com.kri.lge.penetration.dto.ExcelUploadResultDto;
import com.kri.lge.penetration.dto.ExcelUploadSuccessDto;
import com.kri.lge.penetration.service.UploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.nio.charset.StandardCharsets;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/api/v1/upload")
@RequiredArgsConstructor
@Tag(name = "Upload", description = "업로드 API")
public class UploadController {
private static final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
private final StatisticsDbCache statisticsDbCache;
private final UploadService uploadService;
@PostMapping(
value = "/data",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = {MediaType.APPLICATION_JSON_VALUE, XLSX}
)
@Operation(summary = "파일 업로드", description = "성공이면 JSON, 실패가 있으면 실패 내역 엑셀(xlsx) 반환")
@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "성공(JSON) 또는 실패 엑셀(xlsx)",
content = {
@Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ExcelUploadSuccessDto.class)
),
@Content(
mediaType = XLSX,
schema = @Schema(type = "string", format = "binary")
)
}
)
})
public ResponseEntity<?> importData(
@Parameter(description = "업로드 파일", required = true)
@RequestPart("file") MultipartFile file
) {
statisticsDbCache.clearStatisticsCache();
ExcelUploadResultDto result = uploadService.uploadData(file);
if (result.isHasErrorFile()) {
String contentDisposition = "attachment; filename*=UTF-8''" +
java.net.URLEncoder.encode(result.getErrorFileName(), StandardCharsets.UTF_8);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
.contentType(MediaType.parseMediaType(XLSX))
.body(result.getErrorFile());
} else {
return ResponseEntity.ok(ExcelUploadSuccessDto.from(result));
}
}
}

View File

@@ -0,0 +1,22 @@
package com.kri.lge.penetration.dto;
import com.kri.lge.penetration.persistence.entity.TDataEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Builder
@AllArgsConstructor
@Schema(description = "응닶 데이터 Dto")
public class DataDto {
private String dataIdx;
private String sid;
private Map<String, List<DataItemDto>> rawData;
}

View File

@@ -0,0 +1,36 @@
package com.kri.lge.penetration.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "변수 데이터 Dto")
public class DataItemDto {
@Schema(description = "2차원 값")
private Integer m;
@Schema(description = "순위 값")
private Integer r;
@Schema(description = "1차원 값(응답값)")
private Double s;
public static DataItemDto from(Integer m, Integer r, Double s) {
return DataItemDto.builder()
.m(m)
.r(r)
.s(s)
.build();
}
}

View File

@@ -0,0 +1,37 @@
package com.kri.lge.penetration.dto;
import com.kri.common.datetime.DateTimeUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
@Getter
@Setter
@SuperBuilder
public class ExcelUploadResultDto extends ExcelUploadSuccessDto {
@Schema(description = "에러 엑셀 파일이 있는지 여부")
private boolean hasErrorFile;
@Schema(description = "프론트가 저장용으로 쓸 파일명")
private String errorFileName;
@Schema(description = "에러 엑셀 파일 (xlsx)")
private byte[] errorFile;
public static ExcelUploadResultDto of(int totalCount, int successCount, int failCount, byte[] errorFile) {
LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
String fileName = "upload data error_" + now.format(DateTimeUtil.DATETIME_FMT) + ".xlsx";
return ExcelUploadResultDto.builder()
.totalCount(totalCount)
.successCount(successCount)
.failCount(failCount)
.hasErrorFile(errorFile != null && errorFile.length > 0)
.errorFileName(fileName)
.errorFile(errorFile)
.build();
}
}

View File

@@ -0,0 +1,30 @@
package com.kri.lge.penetration.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
@Getter
@Setter
@SuperBuilder
public class ExcelUploadSuccessDto {
@Schema(description = "전체 읽은 행 수 (헤더 제외)")
private int totalCount;
@Schema(description = "DB에 실제로 insert/update 성공한 개수")
private int successCount;
@Schema(description = "실패(에러) 개수")
private int failCount;
public static ExcelUploadSuccessDto from(ExcelUploadResultDto r) {
if (r == null) return null;
return ExcelUploadSuccessDto.builder()
.totalCount(r.getTotalCount())
.successCount(r.getSuccessCount())
.failCount(r.getFailCount())
.build();
}
}

View File

@@ -0,0 +1,17 @@
package com.kri.lge.penetration.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Schema(description = "사용자 로그인 요청")
public class LoginRequestDto {
@Schema(description = "사용자 ID")
private String userId;
@Schema(description = "사용자 PW")
private String userPw;
}

View File

@@ -0,0 +1,26 @@
package com.kri.lge.penetration.dto;
import com.kri.common.auth.dto.JwtUserClaimsDto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
@Schema(description = "로그인 응답")
public class LoginResponseDto {
@Schema(description = "jwt access 토큰")
private String accessToken;
@Schema(description = "사용자 정보")
private JwtUserClaimsDto user;
public static LoginResponseDto from(String accessToken, JwtUserClaimsDto jwtUserClaims) {
return LoginResponseDto.builder()
.accessToken(accessToken)
.user(jwtUserClaims)
.build();
}
}

View File

@@ -0,0 +1,38 @@
package com.kri.lge.penetration.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "통계 요청 Dto")
public class StatisticsRequestDto {
@NotNull
@NotEmpty
@Schema(description = "변수 ID 맵(Key: 요청 Key, Value: 변수 ID)")
private Map<@NotBlank String, @NotBlank String> variableIdMap;
@Schema(description = "배너 ID 배열 (1단 배너, 옆으로 붙이기)")
private List<@NotBlank String> bannerIdList;
@Schema(description = "필터 정보")
private Map<@NotBlank String, @NotEmpty Collection<@NotNull Integer>> filterCondition;
@Schema(description = "요청 옵션")
@Builder.Default
private StatisticsRequestOptionDto option = new StatisticsRequestOptionDto();
}

View File

@@ -0,0 +1,25 @@
package com.kri.lge.penetration.dto;
import com.kri.lge.penetration.common.consts.LgePenetrationConst;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "통계 요청 옵션 Dto")
public class StatisticsRequestOptionDto {
@Schema(description = "마지막 연도만 조회")
private boolean useCache = true;
@Schema(description = "마지막 연도만 조회")
private boolean lastYear = false;
@Schema(description = "소수점")
private int decimalPoint = LgePenetrationConst.DECIMAL_POINT;
}

View File

@@ -0,0 +1,24 @@
package com.kri.lge.penetration.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Builder
@AllArgsConstructor
@Schema(description = "통계 데이터 응답 Dto")
public class StatisticsResponseDto {
@Schema(description = "통계 데이터(Key: 요청 Key, Value: 결과물)")
private Map<String, List<Map<String, String>>> statisticsDataMap;
static public StatisticsResponseDto of(Map<String, List<Map<String, String>>> statisticsDataMap) {
return StatisticsResponseDto.builder().statisticsDataMap(statisticsDataMap).build();
}
}

View File

@@ -0,0 +1,56 @@
package com.kri.lge.penetration.dto;
import com.kri.lge.penetration.common.enums.VariableType;
import com.kri.lge.penetration.persistence.entity.TVariableEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Builder
@AllArgsConstructor
@Schema(description = "변수 Dto")
public class VariableDto {
@Schema(description = "변수 ID")
private String variableId;
@Schema(description = "변수 순서")
private Integer variableOrder;
@Schema(description = "변수 이름")
private String variableName;
@Schema(description = "실제 분석에 사용될 QID")
private String targetQid;
@Schema(description = "변수 차원(여기서는 1,2차원만)")
private Integer variableDimension;
@Schema(description = "변수 타입(여기선 모두 single)")
private VariableType variableType;
@Schema(description = "Matrix 값 정보")
private VariableItemInfoDto[] matrixInfo;
@Schema(description = "Scalar 값 정보")
private VariableItemInfoDto[] scalarInfo;
public static VariableDto from(TVariableEntity entity) {
return VariableDto.builder()
.variableId(entity.getVariableId())
.variableOrder(entity.getVariableOrder())
.variableName(entity.getVariableName())
.targetQid(entity.getTargetQid())
.variableDimension(entity.getVariableDimension())
.variableType(VariableType.fromCode(entity.getVariableType()))
.matrixInfo(entity.getMatrixInfo())
.scalarInfo(entity.getScalarInfo())
.build();
}
}

View File

@@ -0,0 +1,25 @@
package com.kri.lge.penetration.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Builder
@AllArgsConstructor
@Schema(description = "변수 보기 정보 Dto")
public class VariableItemInfoDto {
private Integer order;
private List<Integer> value;
private String label;
@Builder.Default
private boolean useDeny = false;
}

View File

@@ -0,0 +1,83 @@
package com.kri.lge.penetration.persistence.entity;
import com.kri.lge.penetration.dto.DataItemDto;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
@Getter
@Setter
@Entity
@Table(name = "t_data")
public class TDataEntity {
@Id
@Column(name="data_idx", nullable=false, updatable=false, columnDefinition = "char(36)")
private String dataIdx;
@Size(max = 32)
@Column(name = "sid", nullable = false, length = 32)
private String sid;
@NotNull
@Column(name = "raw_data", nullable = false)
@JdbcTypeCode(SqlTypes.JSON)
private Map<String, List<DataItemDto>> rawData;
@Size(max = 32)
@NotNull
@Column(name = "reg_user", nullable = false, length = 32)
private String regUser;
@NotNull
@ColumnDefault("CURRENT_TIMESTAMP")
@Column(name = "reg_dt", nullable = false)
private LocalDateTime regDt;
@Column(name = "wave", insertable = false, updatable = false)
private Integer wave;
@Column(name = "product", insertable = false, updatable = false)
private Integer product;
@Column(name = "life_stage", insertable = false, updatable = false)
private Integer lifeStage;
@Column(name = "household", insertable = false, updatable = false)
private Integer household;
@Column(name = "age", insertable = false, updatable = false)
private Integer age;
@Column(name = "occupancy", insertable = false, updatable = false)
private Integer occupancy;
@Column(name = "housing", insertable = false, updatable = false)
private Integer housing;
@Column(name = "house_size", insertable = false, updatable = false)
private Integer houseSize;
@Column(name = "region", insertable = false, updatable = false)
private Integer region;
@Column(name = "income", insertable = false, updatable = false)
private Integer income;
@PrePersist
public void prePersist() {
if (dataIdx == null) dataIdx = java.util.UUID.randomUUID().toString();
}
}

View File

@@ -0,0 +1,44 @@
package com.kri.lge.penetration.persistence.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
@Getter
@Setter
@Builder
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "t_statistics_cache")
public class TStatisticsCacheEntity {
@Id
@Size(max = 128)
@Column(name = "cache_key", nullable = false, length = 128)
private String cacheKey;
@NotNull
@JdbcTypeCode(SqlTypes.JSON)
@Column(name = "result_cache", nullable = false)
private List<Map<String, String>> resultCache;
public static TStatisticsCacheEntity of(String cacheKey, List<Map<String, String>> resultCache) {
return TStatisticsCacheEntity.builder()
.cacheKey(cacheKey)
.resultCache(resultCache)
.build();
}
}

View File

@@ -0,0 +1,64 @@
package com.kri.lge.penetration.persistence.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;
@Getter
@Setter
@Entity
@Table(name = "t_user")
public class TUserEntity {
@Id
@Size(max = 32)
@Column(name = "user_id", nullable = false, length = 32)
private String userId;
@Size(max = 64)
@NotNull
@Column(name = "user_pw", nullable = false, length = 64)
private String userPw;
@Size(max = 32)
@NotNull
@Column(name = "user_name", nullable = false, length = 32)
private String userName;
@Size(max = 32)
@NotNull
@Column(name = "user_cmpn", nullable = false, length = 32)
private String userCmpn;
@Size(max = 32)
@NotNull
@Column(name = "user_team", nullable = false, length = 32)
private String userTeam;
@NotNull
@Column(name = "user_role", nullable = false)
private Integer userRole;
@Lob
@Column(name = "memo")
private String memo;
@Size(max = 32)
@NotNull
@Column(name = "reg_user", nullable = false, length = 32)
private String regUser;
@NotNull
@ColumnDefault("CURRENT_TIMESTAMP")
@Column(name = "reg_dt", nullable = false)
private LocalDateTime regDt;
}

View File

@@ -0,0 +1,69 @@
package com.kri.lge.penetration.persistence.entity;
import com.kri.lge.penetration.dto.VariableItemInfoDto;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
@Getter
@Setter
@Entity
@Table(name = "t_variable")
public class TVariableEntity {
@Id
@Size(max = 16)
@Column(name = "variable_id", nullable = false, length = 16)
private String variableId;
@NotNull
@Column(name = "variable_order", nullable = false)
private Integer variableOrder;
@Size(max = 32)
@NotNull
@Column(name = "variable_name", nullable = false, length = 32)
private String variableName;
@Size(max = 16)
@Column(name = "target_qid", nullable = false, length = 16)
private String targetQid;
@NotNull
@ColumnDefault("1")
@Column(name = "variable_dimension", nullable = false)
private Integer variableDimension;
@NotNull
@ColumnDefault("21")
@Column(name = "variable_type", nullable = false)
private Integer variableType;
@Column(name = "matrix_info")
@JdbcTypeCode(SqlTypes.JSON)
private VariableItemInfoDto[] matrixInfo;
@Column(name = "scalar_info")
@JdbcTypeCode(SqlTypes.JSON)
private VariableItemInfoDto[] scalarInfo;
@Size(max = 32)
@NotNull
@Column(name = "reg_user", nullable = false, length = 32)
private String regUser;
@NotNull
@ColumnDefault("CURRENT_TIMESTAMP")
@Column(name = "reg_dt", nullable = false)
private LocalDateTime regDt;
}

View File

@@ -0,0 +1,19 @@
package com.kri.lge.penetration.persistence.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity
@Table(name = "t_year")
public class TYearEntity {
@Id
@Column(name = "use_year", nullable = false)
private Integer useYear;
}

View File

@@ -0,0 +1,9 @@
package com.kri.lge.penetration.persistence.repository;
import com.kri.lge.penetration.persistence.entity.TDataEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DataRepository extends JpaRepository<TDataEntity, Integer>,
DataRepositoryCustom {
}

View File

@@ -0,0 +1,18 @@
package com.kri.lge.penetration.persistence.repository;
import com.kri.lge.penetration.dto.DataDto;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface DataRepositoryCustom {
List<DataDto> findByFilterConditionsNative(
List<Integer> waves,
Map<String, ? extends Collection<Integer>> filterCondition,
Set<String> indexKeySet,
Set<String> bannerKeySet,
boolean dropRowsWhenAnyKeyMissingOrNull, // true면 row 자체 제외(요청키 모두 유효해야 통과)
boolean omitNullKeysInJson // true면 JSON 결과에서 null/미존재 키는 아예 빼기
);
}

View File

@@ -0,0 +1,166 @@
package com.kri.lge.penetration.persistence.repository;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kri.lge.penetration.common.consts.LgePenetrationConst;
import com.kri.lge.penetration.dto.DataDto;
import com.kri.lge.penetration.dto.DataItemDto;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
@Repository
@RequiredArgsConstructor
public class DataRepositoryCustomImpl implements DataRepositoryCustom {
private final EntityManager em;
private final ObjectMapper objectMapper;
private static final Map<String, String> FILTER_COL_MAP = Map.ofEntries(
Map.entry("product", "product"),
Map.entry("lifeStage", "life_stage"),
Map.entry("household", "household"),
Map.entry("age", "age"),
Map.entry("occupancy", "occupancy"),
Map.entry("housing", "housing"),
Map.entry("houseSize", "house_size"),
Map.entry("region", "region"),
Map.entry("income", "income")
);
@Override
public List<DataDto> findByFilterConditionsNative(
List<Integer> waves,
Map<String, ? extends Collection<Integer>> filterCondition,
Set<String> indexKeySet,
Set<String> bannerKeySet,
boolean dropRowsWhenAnyKeyMissingOrNull,
boolean omitNullKeysInJson
) {
if (waves == null || waves.isEmpty()) return List.of();
if (indexKeySet == null || indexKeySet.isEmpty() || bannerKeySet == null || bannerKeySet.isEmpty()) {
throw new IllegalArgumentException("rawKeySet is required for native partial raw_data fetch");
}
List<String> waveStr = waves.stream().map(String::valueOf).toList();
bannerKeySet.add(LgePenetrationConst.YEAR_STR);
// key별 JSON_EXTRACT 식 만들기
List<String> bannerKeyList = bannerKeySet.stream().distinct().toList();
List<String> indexKeyList = indexKeySet.stream().distinct().toList();
List<String> selectKeyList = Stream.concat(bannerKeyList.stream(), indexKeyList.stream()).distinct().toList();
// MySQL JSON path: $."key" (키에 특수문자 대비)
// SQL literal escape: ' -> ''
Function<String, String> escSqlLiteral = k -> k.replace("'", "''");
Function<String, String> jsonPath = k -> "$.\"" + k.replace("\"", "\\\"") + "\""; // 큰따옴표 escape
// 유효 조건(미존재/JSON null 제외)
Function<String, String> validExpr = k -> {
String p = jsonPath.apply(k);
return "(JSON_EXTRACT(t.raw_data, '" + p + "') IS NOT NULL " +
"AND JSON_TYPE(JSON_EXTRACT(t.raw_data, '" + p + "')) <> 'NULL')";
};
// SELECT raw_data 부분
// 1) omitNullKeysInJson=false -> JSON_OBJECT('k1', JSON_EXTRACT(...), ...)
// 2) omitNullKeysInJson=true -> JSON_MERGE_PATCH(IF(valid,k->JSON_OBJECT(...),{}), ...)
String rawSelect;
if (!omitNullKeysInJson) {
String jsonObjectArgs = selectKeyList.stream()
.map(k -> "'" + escSqlLiteral.apply(k) + "', JSON_EXTRACT(t.raw_data, '" + jsonPath.apply(k) + "')")
.reduce((a, b) -> a + ", " + b)
.orElse("");
rawSelect = "JSON_OBJECT(" + jsonObjectArgs + ") AS raw_data";
} else {
// null/미존재 키는 JSON에서 아예 제외
String mergeArgs = selectKeyList.stream()
.map(k -> "JSON_OBJECT('" + escSqlLiteral.apply(k) + "', JSON_EXTRACT(t.raw_data, '" + jsonPath.apply(k) + "'))")
.reduce((a, b) -> a + ", " + b)
.orElse("JSON_OBJECT()");
rawSelect = "JSON_MERGE_PATCH(" + mergeArgs + ") AS raw_data";
}
StringBuilder sql = new StringBuilder();
sql.append("SELECT t.data_idx, t.sid, ").append(rawSelect).append("\n")
.append("FROM t_data t\n")
.append("WHERE t.wave IN (:waves)\n");
// filterCondition -> 컬럼 IN (...) (화이트리스트 기반)
Map<String, Collection<Integer>> safeFilters = new LinkedHashMap<>();
if (filterCondition != null) {
for (var e : filterCondition.entrySet()) {
String reqKey = e.getKey();
Collection<Integer> vals = e.getValue();
if (vals == null || vals.isEmpty()) continue;
String col = FILTER_COL_MAP.get(reqKey);
if (col == null) continue;
safeFilters.put(reqKey, vals);
sql.append(" AND t.").append(col).append(" IN (:f_").append(reqKey).append(")\n");
}
}
if (dropRowsWhenAnyKeyMissingOrNull) {
for (String k : bannerKeyList) {
sql.append(" AND ").append(validExpr.apply(k)).append("\n");
}
String joined = indexKeyList.stream()
.map(validExpr)
.collect(Collectors.joining("\n OR "));
sql.append(" AND (\n")
.append(" ")
.append(joined)
.append("\n )\n");
}
Query q = em.createNativeQuery(sql.toString());
q.setParameter("waves", waveStr);
for (var e : safeFilters.entrySet()) {
q.setParameter("f_" + e.getKey(), e.getValue());
}
@SuppressWarnings("unchecked")
List<Object[]> rows = q.getResultList();
// 결과 매핑: (data_idx, sid, raw_json) -> DataDto
List<DataDto> out = new ArrayList<>(rows.size());
for (Object[] r : rows) {
String dataIdx = (String) r[0];
String sid = (String) r[1];
Object rawObj = r[2];
String rawJson = (rawObj == null) ? "{}" : rawObj.toString();
Map<String, List<DataItemDto>> rawMap;
try {
rawMap = objectMapper.readValue(rawJson, new com.fasterxml.jackson.core.type.TypeReference<>() {});
} catch (Exception ex) {
// 파싱 실패 시 방어 (원인 로깅 권장)
rawMap = Map.of();
}
out.add(DataDto.builder()
.dataIdx(dataIdx) // dataIdx 타입에 맞춰 조정
.sid(sid)
.rawData(rawMap)
.build());
}
return out;
}
}

View File

@@ -0,0 +1,9 @@
package com.kri.lge.penetration.persistence.repository;
import com.kri.lge.penetration.persistence.entity.TStatisticsCacheEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface StatisticsCacheRepository extends JpaRepository<TStatisticsCacheEntity, String>,
JpaSpecificationExecutor<TStatisticsCacheEntity>{
}

View File

@@ -0,0 +1,14 @@
package com.kri.lge.penetration.persistence.repository;
import com.kri.lge.penetration.persistence.entity.TUserEntity;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface UserRepository extends JpaRepository<TUserEntity, Long>,
JpaSpecificationExecutor<TUserEntity> {
Optional<TUserEntity> findByUserIdAndUserPw(String userId, String userPw);
boolean existsByUserRoleAndUserPw(Integer userRole, String userPw);
}

View File

@@ -0,0 +1,10 @@
package com.kri.lge.penetration.persistence.repository;
import com.kri.lge.penetration.persistence.entity.TVariableEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface VariableRepository extends JpaRepository<TVariableEntity, Integer>,
JpaSpecificationExecutor<TVariableEntity> {
}

View File

@@ -0,0 +1,16 @@
package com.kri.lge.penetration.persistence.repository;
import com.kri.lge.penetration.persistence.entity.TYearEntity;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface YearRepository extends JpaRepository<TYearEntity, Long> {
@Query("""
select y.useYear
from TYearEntity y
order by y.useYear
""")
List<Integer> findYearList();
}

View File

@@ -0,0 +1,45 @@
package com.kri.lge.penetration.service;
import com.kri.common.auth.dto.JwtUserClaimsDto;
import com.kri.common.auth.jwt.JwtService;
import com.kri.common.web.BusinessException;
import com.kri.lge.penetration.common.consts.LgePenetrationConst;
import com.kri.lge.penetration.dto.LoginRequestDto;
import com.kri.lge.penetration.dto.LoginResponseDto;
import com.kri.lge.penetration.persistence.entity.TUserEntity;
import com.kri.lge.penetration.persistence.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class AuthService {
private final JwtService jwtService;
private final UserRepository userRepository;
public LoginResponseDto tryLogin(LoginRequestDto request) {
TUserEntity user = userRepository.findByUserIdAndUserPw(request.getUserId(),
request.getUserPw())
.orElseThrow(() ->
BusinessException.notFound("USER_NOT_FOUND", "사용자가 없습니다.")
.with("userId", request.getUserId())
);
JwtUserClaimsDto claims = JwtUserClaimsDto.builder()
.userId(user.getUserId())
.userCmpn(user.getUserCmpn())
.userName(user.getUserName())
.userTeam(user.getUserTeam())
.userRole(user.getUserRole())
.build();
String accessToken = jwtService.generateAccessToken(claims);
return LoginResponseDto.from(accessToken, claims);
}
public boolean isAdmin(String adminPw) {
return userRepository.existsByUserRoleAndUserPw(LgePenetrationConst.ADMIN_ROLE, adminPw);
}
}

View File

@@ -0,0 +1,782 @@
package com.kri.lge.penetration.service;
import com.kri.common.web.BusinessException;
import com.kri.lge.penetration.common.cache.StatisticsDbCache;
import com.kri.lge.penetration.common.cache.VariableCache;
import com.kri.lge.penetration.common.cache.YearCache;
import com.kri.lge.penetration.common.consts.LgePenetrationConst;
import com.kri.lge.penetration.common.enums.VariableType;
import com.kri.lge.penetration.dto.DataDto;
import com.kri.lge.penetration.dto.DataItemDto;
import com.kri.lge.penetration.dto.StatisticsRequestDto;
import com.kri.lge.penetration.dto.StatisticsRequestOptionDto;
import com.kri.lge.penetration.dto.StatisticsResponseDto;
import com.kri.lge.penetration.dto.VariableDto;
import com.kri.lge.penetration.dto.VariableItemInfoDto;
import com.kri.lge.penetration.persistence.entity.TStatisticsCacheEntity;
import com.kri.lge.penetration.persistence.repository.DataRepository;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.DoubleSummaryStatistics;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
public class StatisticsService {
private final YearCache yearCache;
private final VariableCache variableCache;
private final StatisticsDbCache statisticsDbCache;
private final DataRepository dataRepository;
private record BannerSpec(
String title, // 예: "QID#1|2" 또는 "TOTAL"
String qid, // 예: "QID" (TOTAL이면 null)
List<Integer> values // TOTAL이면 null
) {
boolean isTotal() {
return values == null;
}
}
private record BannerCondition(
String qid,
int[] values
) {}
private record BannerRuntime(
String title,
boolean total,
List<BannerCondition> conditions
) {}
private record YearSidIndex(
Map<String, Integer> sidToId,
int sidCount
) {}
@Transactional(readOnly = true)
public StatisticsResponseDto getStatistics(StatisticsRequestDto request) {
Map<String, VariableDto> variableMap = variableCache.getVariableMap();
List<Integer> yearList = getFilteredYearList(request.getOption());
validateCache(yearList, variableMap);
Map<String, List<Map<String, String>>> cacheDataMap = new HashMap<>();
Map<String, String> remainingVariableIdMap = new HashMap<>(request.getVariableIdMap());
if(request.getOption().isUseCache()) {
for (Map.Entry<String, String> e : request.getVariableIdMap().entrySet()) {
String requestKey = e.getKey();
String cacheKey = statisticsDbCache.makeResultCacheKey(requestKey,
request.getFilterCondition());
TStatisticsCacheEntity cache = statisticsDbCache.getStatisticsCache(cacheKey);
if (cache != null) {
cacheDataMap.put(requestKey, cache.getResultCache());
remainingVariableIdMap.remove(requestKey); // 집계 대상에서 제외
}
}
// 전부 캐시에 존재하면 DB 조회/집계 불필요
if (remainingVariableIdMap.isEmpty()) {
return StatisticsResponseDto.of(cacheDataMap);
}
}
StatisticsRequestDto remainingRequest = request.toBuilder()
.variableIdMap(remainingVariableIdMap)
.build();
List<List<BannerSpec>> bannerSpecsList = buildBannerSpecs(remainingRequest.getBannerIdList(), variableMap);
Set<String> indexKeySet = getTargetQids(variableMap, remainingRequest.getVariableIdMap());
Set<String> bannerKeySet = getTargetQids(bannerSpecsList);
List<DataDto> dataList = fetchDataListNative(yearList, remainingRequest.getFilterCondition(), indexKeySet, bannerKeySet);
Map<String, List<Map<String, String>>> statisticsDataMap = processStatistics(
remainingRequest, yearList, variableMap, bannerSpecsList, dataList
);
// 캐시 저장
if(request.getOption().isUseCache()){
List<TStatisticsCacheEntity> cacheDataList = new ArrayList<>();
for (Map.Entry<String, List<Map<String, String>>> e : statisticsDataMap.entrySet()) {
String cacheKey = statisticsDbCache.makeResultCacheKey(e.getKey(),
request.getFilterCondition());
List<Map<String, String>> result = e.getValue();
cacheDataList.add(TStatisticsCacheEntity.of(cacheKey, result));
}
if(!cacheDataList.isEmpty()){
statisticsDbCache.saveStatisticsCache(cacheDataList);
}
}
//캐시랑 결과 합치기
if(!cacheDataMap.isEmpty()){
statisticsDataMap.putAll(cacheDataMap);
}
return StatisticsResponseDto.of(statisticsDataMap);
}
private List<Integer> getFilteredYearList(StatisticsRequestOptionDto option) {
List<Integer> yearList = yearCache.getYearList();
if (option.isLastYear()) {
Integer maxYear =
(yearList == null) ? null : yearList.stream().max(Integer::compareTo).orElse(null);
return (maxYear != null) ? List.of(maxYear) : List.of();
}
return yearList;
}
private Set<String> getTargetQids(
Map<String, VariableDto> variableMap,
Map<String, String> indexMap
) {
return indexMap.values().stream()
.map(variableMap::get)
.filter(Objects::nonNull)
.map(VariableDto::getTargetQid)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}
private Set<String> getTargetQids(
List<List<BannerSpec>> bannerSpecsList
) {
return bannerSpecsList.stream()
.filter(Objects::nonNull)
.flatMap(List::stream) // List<BannerSpec> -> BannerSpec
.filter(Objects::nonNull)
.filter(spec -> !spec.isTotal())
.map(BannerSpec::qid)
.collect(Collectors.toSet());
}
private List<List<BannerSpec>> buildBannerSpecs(List<String> bannerIdList, Map<String, VariableDto> variableMap) {
List<List<BannerSpec>> specs = new ArrayList<>();
List<BannerSpec> totalSpecs = new ArrayList<>();
totalSpecs.add(new BannerSpec(LgePenetrationConst.TOTAL_STR, null, null));
specs.add(totalSpecs);
if (bannerIdList == null || bannerIdList.isEmpty()) {
return specs;
}
for (String bannerId : bannerIdList) {
String[] bannerIds = bannerId.split("\\|"); // split 수정
List<List<BannerSpec>> choicesPerBId = new ArrayList<>();
for (String bId : bannerIds) {
VariableDto v = variableMap.get(bId); // get 수정
if (v == null || v.getScalarInfo() == null) continue;
String qid = v.getTargetQid();
List<BannerSpec> choices = new ArrayList<>();
for (VariableItemInfoDto scalar : v.getScalarInfo()) {
if (scalar == null) continue;
List<Integer> vals = scalar.getValue();
if (vals == null || vals.isEmpty()) continue;
String vars = vals.stream().map(String::valueOf).collect(Collectors.joining("|"));
String title = "%s#%s".formatted(qid, vars);
choices.add(new BannerSpec(title, qid, vals));
}
if (!choices.isEmpty()) choicesPerBId.add(choices);
}
if (choicesPerBId.isEmpty()) continue;
List<BannerSpec> topChoices = choicesPerBId.get(0); // 상위(첫 번째 bId)
List<List<BannerSpec>> restCombos =
(choicesPerBId.size() == 1)
? List.of(List.of())
: crossBanner(choicesPerBId.subList(1, choicesPerBId.size()));
// 원하는 출력:
// [{Q1,1}]
// [{Q1,1},{Q2,1}] ...
for (BannerSpec top : topChoices) {
specs.add(List.of(top)); // 서머리(상위만)
for (List<BannerSpec> rest : restCombos) {
List<BannerSpec> combo = new ArrayList<>(1 + rest.size());
combo.add(top);
combo.addAll(rest);
specs.add(combo);
}
}
}
return specs;
}
private static List<List<BannerSpec>> crossBanner(List<List<BannerSpec>> lists) {
List<List<BannerSpec>> result = new ArrayList<>();
result.add(new ArrayList<>());
for (List<BannerSpec> choices : lists) {
List<List<BannerSpec>> next = new ArrayList<>();
for (List<BannerSpec> base : result) {
for (BannerSpec c : choices) {
List<BannerSpec> merged = new ArrayList<>(base);
merged.add(c);
next.add(merged);
}
}
result = next;
}
return result;
}
public List<DataDto> fetchDataListNative(
List<Integer> waves,
Map<String, ? extends Collection<Integer>> filterCondition,
Set<String> indexKeySet,
Set<String> bannerKeySet
) {
return dataRepository.findByFilterConditionsNative(
waves,
filterCondition,
indexKeySet,
bannerKeySet,
true, // dropRowsWhenAnyKeyMissingOrNull
true // omitNullKeysInJson
);
}
private Map<String, List<Map<String, String>>> processStatistics(
StatisticsRequestDto request,
List<Integer> yearList,
Map<String, VariableDto> variableMap,
List<List<BannerSpec>> bannerSpecsList,
List<DataDto> dataList
) {
Map<String, List<Map<String, String>>> statisticsDataMap = new HashMap<>();
int decimalPoint = request.getOption().getDecimalPoint();
// banner spec -> runtime (values를 int[]로 변환해 hasValue 의미(OR)를 빠르게 재현)
List<BannerRuntime> bannerRuntimes = new ArrayList<>();
for (List<BannerSpec> bannerSpecs : bannerSpecsList) {
boolean total = bannerSpecs.get(0).isTotal();
List<String> bannerTitle = new ArrayList<>();
List<BannerCondition> bannerConditions = new ArrayList<>();
if(total){
bannerRuntimes.add(new BannerRuntime(bannerSpecs.get(0).title(), true, bannerConditions));
}
else{
for(BannerSpec s : bannerSpecs){
if (s == null) continue;
bannerTitle.add(s.title());
bannerConditions.add(new BannerCondition(s.qid(), toIntArray(s.values())));
}
bannerRuntimes.add(new BannerRuntime(String.join("|", bannerTitle), false, bannerConditions));
}
}
request.getVariableIdMap().forEach((requestKey, variableId) -> {
VariableDto indexVariable = variableMap.get(variableId);
if (indexVariable == null) {
statisticsDataMap.computeIfAbsent(requestKey, k -> new ArrayList<>()).addAll(new ArrayList<>());
return;
}
String indexQid = indexVariable.getTargetQid();
Set<Integer> denyIndexValueSet = getDenyIndexValueSet(indexVariable);
VariableType indexVariableType = indexVariable.getVariableType();
List<DataDto> variableData = filterDataByVariable(dataList, indexQid, denyIndexValueSet);
VariableItemInfoDto[] indexScalar = filterVariableScalarInfo(
indexVariable.getScalarInfo(), variableData, indexQid
);
// year별 bucket을 1회 구성 (주의: YEAR_STR이 다중 값이면 한 row가 여러 year bucket에 들어갈 수 있게 OR 유지)
Map<Integer, List<DataDto>> byYear = bucketByYear(variableData, yearList);
// categorical이면 code -> scalarMask(bitset)를 1회 준비 (scalar 평균 10개라 long 마스크로 충분)
Map<Integer, Long> codeToScalarMask =
(indexVariableType == VariableType.NUMERIC) ? Collections.emptyMap() : buildCodeToScalarMask(indexScalar);
for (Integer year : yearList) {
List<DataDto> yearRows = byYear.getOrDefault(year, List.of());
List<Map<String, String>> statisticsData =
initializeStatisticsData(year, indexVariableType, indexScalar);
// 연도 단위로 sid -> id 매핑을 만들어서 per-banner 중복 제거(=distinct sid) 비용을 배열로 처리
YearSidIndex sidIndex = buildSidIndex(yearRows);
// per-banner 스캔에 재사용할 워크 배열(배너마다 reset)
boolean[] inBanner = new boolean[sidIndex.sidCount];
int[] touchedInBanner = new int[sidIndex.sidCount];
int touchedInBannerN;
long[] sidScalarMask = (indexVariableType == VariableType.NUMERIC) ? null : new long[sidIndex.sidCount];
int[] touchedMask = (indexVariableType == VariableType.NUMERIC) ? null : new int[sidIndex.sidCount];
int touchedMaskN;
boolean[] numericSeen = (indexVariableType == VariableType.NUMERIC) ? new boolean[sidIndex.sidCount] : null;
int[] touchedNumeric = (indexVariableType == VariableType.NUMERIC) ? new int[sidIndex.sidCount] : null;
int touchedNumericN;
for (BannerRuntime banner : bannerRuntimes) {
touchedInBannerN = 0;
touchedMaskN = 0;
touchedNumericN = 0;
// 배너 1개당: yearRows를 1회 스캔하며
// - 배너 분모(=distinct sid)
// - categorical: scalar별 distinct sid 카운트(전체 scalar를 동시에)
// - numeric: 기존 summarizeData의 "sid별 최초 값 1개" 규칙대로 stats
int bannerSidCount = 0;
long[] scalarCounts = (indexVariableType == VariableType.NUMERIC) ? null : new long[indexScalar.length];
DoubleSummaryStatistics numericStats = (indexVariableType == VariableType.NUMERIC) ? new DoubleSummaryStatistics() : null;
for (DataDto row : yearRows) {
if (row == null) continue;
// banner match (기존 filterBannerData + hasValue 의미와 동일)
if (!banner.total && !rowHasAnyRoundedValue(row, banner.conditions())) {
continue;
}
String sid = row.getSid();
if (sid == null) {
// 기존 distinct sid에서는 제외되므로 여기서도 제외
continue;
}
Integer sidId = sidIndex.sidToId.get(sid);
if (sidId == null) {
continue;
}
// 분모 distinct sid
if (!inBanner[sidId]) {
inBanner[sidId] = true;
touchedInBanner[touchedInBannerN++] = sidId;
bannerSidCount++;
}
if (indexVariableType == VariableType.NUMERIC) {
// 기존 summarizeData와 동일: sid별로 "최초로 발견되는 non-null s"만 반영
// (값이 null인 row는 sid를 '소비'하지 않음: 나중 row에서 값이 발견되면 반영)
if (!numericSeen[sidId]) {
Double v = firstNonNullDoubleInRow(row, indexQid);
if (v != null) {
numericSeen[sidId] = true;
touchedNumeric[touchedNumericN++] = sidId;
numericStats.accept(v);
}
}
} else {
// categorical: row 내 indexQid의 모든 item을 읽어 scalarMask 누적(=hasValue OR 의미 유지)
List<DataItemDto> items = safeItems(row, indexQid);
if (items == null || items.isEmpty()) continue;
for (DataItemDto it : items) {
if (it == null || it.getS() == null) continue;
int code = (int) Math.round(it.getS());
Long m = codeToScalarMask.get(code);
if (m == null || m == 0L) continue;
long before = sidScalarMask[sidId];
long add = m & ~before;
if (add == 0L) continue;
if (before == 0L) {
touchedMask[touchedMaskN++] = sidId;
}
sidScalarMask[sidId] = before | m;
// add에 포함된 bit(=scalar index)만 카운트 증가
while (add != 0L) {
int bit = Long.numberOfTrailingZeros(add);
if (bit < scalarCounts.length) {
scalarCounts[bit]++;
}
add &= (add - 1);
}
}
}
}
// row0: TOTAL 카운트(기존과 동일: countDistinctSid(bannerData))
statisticsData.get(0).put(banner.title, String.valueOf(bannerSidCount));
if (indexVariableType == VariableType.NUMERIC) {
// 기존 calculateNumericStatistics와 동일: bannerData empty면 "0", 아니면 summarizeData 결과 출력
if (bannerSidCount == 0) {
statisticsData.get(1).put(banner.title, "0");
statisticsData.get(2).put(banner.title, "0");
statisticsData.get(3).put(banner.title, "0");
} else {
statisticsData.get(1).put(banner.title, toScaledString(numericStats.getAverage(), decimalPoint));
statisticsData.get(2).put(banner.title, toScaledString(numericStats.getMax(), decimalPoint));
statisticsData.get(3).put(banner.title, toScaledString(numericStats.getMin(), decimalPoint));
}
} else {
// 기존 calculateCategoricalStatistics + calculateFrequency와 동일
for (int i = 0; i < indexScalar.length; i++) {
int rowIdx = i + 1;
BigDecimal freq;
long matchCount = scalarCounts[i];
if (bannerSidCount == 0 || matchCount == 0L) {
freq = BigDecimal.ZERO;
}
else{
freq = BigDecimal.valueOf(matchCount)
.multiply(BigDecimal.valueOf(100))
.divide(BigDecimal.valueOf(bannerSidCount), decimalPoint + 4, RoundingMode.HALF_UP);
}
statisticsData.get(rowIdx).put(banner.title, toScaledString(freq, decimalPoint));
}
}
// reset arrays for next banner (touched만 초기화)
for (int i = 0; i < touchedInBannerN; i++) {
inBanner[touchedInBanner[i]] = false;
}
if (indexVariableType == VariableType.NUMERIC) {
for (int i = 0; i < touchedNumericN; i++) {
numericSeen[touchedNumeric[i]] = false;
}
} else {
for (int i = 0; i < touchedMaskN; i++) {
sidScalarMask[touchedMask[i]] = 0L;
}
}
}
statisticsDataMap.computeIfAbsent(requestKey, k -> new ArrayList<>())
.addAll(statisticsData);
}
});
return statisticsDataMap;
}
private Map<Integer, List<DataDto>> bucketByYear(List<DataDto> variableData, List<Integer> yearList) {
Map<Integer, List<DataDto>> out = new HashMap<>();
for (Integer y : yearList) {
out.put(y, new ArrayList<>());
}
for (DataDto row : variableData) {
if (row == null) continue;
// YEAR_STR item들 중 하나라도 해당 year면 그 year bucket에 포함(기존 hasValue 의미)
List<DataItemDto> items = safeItems(row, LgePenetrationConst.YEAR_STR);
if (items == null || items.isEmpty()) continue;
for (Integer y : yearList) {
if (y == null) continue;
if (itemsContainRoundedInt(items, y)) {
out.get(y).add(row);
}
}
}
return out;
}
private boolean itemsContainRoundedInt(List<DataItemDto> items, int target) {
for (DataItemDto it : items) {
if (it == null || it.getS() == null) continue;
int v = (int) Math.round(it.getS());
if (v == target) return true;
}
return false;
}
private boolean rowHasAnyRoundedValue(DataDto data, List<BannerCondition> bannerConditions) {
if (data == null || bannerConditions.isEmpty()) return false;
for (BannerCondition bc : bannerConditions) {
List<DataItemDto> items = safeItems(data, bc.qid());
if (items == null || items.isEmpty()) return false;
boolean matchedThisCondition = false;
for (DataItemDto it : items) {
if (it == null || it.getS() == null) continue;
int v = (int) Math.round(it.getS());
for (int t : bc.values()) {
if (v == t) {
matchedThisCondition = true;
break;
}
}
if (matchedThisCondition) break;
}
// 이 조건(bc)에서 한 번도 매칭이 안 됐으면 AND 실패
if (!matchedThisCondition) return false;
}
// 모든 조건을 통과
return true;
}
private Double firstNonNullDoubleInRow(DataDto row, String qid) {
List<DataItemDto> items = safeItems(row, qid);
if (items == null || items.isEmpty()) return null;
for (DataItemDto it : items) {
if (it != null && it.getS() != null) {
return it.getS(); // 기존 summarizeData는 첫 non-null s를 사용
}
}
return null;
}
private List<DataItemDto> safeItems(DataDto row, String qid) {
if (row == null || qid == null) return Collections.emptyList();
Map<String, List<DataItemDto>> raw = row.getRawData();
if (raw == null) return Collections.emptyList();
return raw.get(qid);
}
private int[] toIntArray(List<Integer> list) {
if (list == null || list.isEmpty()) return new int[0];
int[] out = new int[list.size()];
int n = 0;
for (Integer v : list) {
if (v == null) continue;
out[n++] = v;
}
return (n == out.length) ? out : Arrays.copyOf(out, n);
}
private YearSidIndex buildSidIndex(List<DataDto> rows) {
Map<String, Integer> sidToId = new HashMap<>();
for (DataDto r : rows) {
if (r == null || r.getSid() == null) continue;
sidToId.computeIfAbsent(r.getSid(), k -> sidToId.size());
}
return new YearSidIndex(sidToId, sidToId.size());
}
private Map<Integer, Long> buildCodeToScalarMask(VariableItemInfoDto[] scalarInfo) {
if (scalarInfo == null || scalarInfo.length == 0) return Collections.emptyMap();
if (scalarInfo.length > 62) {
// 현재 데이터(평균 10개)에서는 여기 안 탐. 필요하면 BitSet 버전으로 확장하면 됨.
throw new IllegalStateException("scalar size too large for long mask: " + scalarInfo.length);
}
Map<Integer, Long> out = new HashMap<>();
for (int i = 0; i < scalarInfo.length; i++) {
VariableItemInfoDto info = scalarInfo[i];
if (info == null) continue;
List<Integer> codes = info.getValue();
if (codes == null || codes.isEmpty()) continue;
long bit = 1L << i;
for (Integer c : codes) {
if (c == null) continue;
out.merge(c, bit, (a, b) -> a | b);
}
}
return out;
}
// ===== 기존 코드(일부는 그대로 재사용) =====
private List<DataDto> filterDataByVariable(List<DataDto> dataList, String indexQid, Set<Integer> denyIndexValueSet) {
return dataList.stream()
.filter(data -> data.getRawData() != null && data.getRawData().containsKey(indexQid))
.filter(d -> {
List<DataItemDto> items = d.getRawData().get(indexQid);
if (items == null || items.isEmpty()) return false; // indexQid 값이 없으면 제외(원하면 true로 변경)
// s(Double) -> int로 보고 비교 (예: 7.0)
List<Integer> values = items.stream()
.map(DataItemDto::getS)
.filter(Objects::nonNull)
.map(Double::intValue)
.toList();
if (values.isEmpty()) return false; // 유효값 없으면 제외(원하면 true로 변경)
boolean allDenied = denyIndexValueSet.containsAll(values);
return !allDenied; // 전부 deny면 제외, 하나라도 allow면 통과
})
.toList();
}
private Set<Integer> getDenyIndexValueSet(VariableDto indexVariable){
return Arrays.stream(indexVariable.getScalarInfo())
.filter(VariableItemInfoDto::isUseDeny)
.map(VariableItemInfoDto::getValue)
.filter(Objects::nonNull)
.flatMap(List::stream)
.collect(Collectors.toSet());
}
private VariableItemInfoDto[] filterVariableScalarInfo(
VariableItemInfoDto[] scalarInfo,
List<DataDto> variableData,
String indexQid
) {
if (scalarInfo == null || scalarInfo.length == 0) {
return new VariableItemInfoDto[0];
}
Set<Integer> sValueSet = extractDistinctSValues(variableData, indexQid);
if (sValueSet.isEmpty()) {
return new VariableItemInfoDto[0];
}
return Arrays.stream(scalarInfo)
.filter(Objects::nonNull)
.filter(info -> containsAny(info.getValue(), sValueSet))
.toArray(VariableItemInfoDto[]::new);
}
private boolean containsAny(List<Integer> values, Set<Integer> sValueSet) {
if (values == null || values.isEmpty()) {
return false;
}
for (Integer v : values) {
if (v != null && sValueSet.contains(v)) {
return true;
}
}
return false;
}
private Set<Integer> extractDistinctSValues(List<DataDto> variableData, String indexQid) {
Set<Integer> out = new HashSet<>();
for (DataDto d : variableData) {
if (d != null && d.getRawData() != null && d.getRawData().containsKey(indexQid)) {
for (DataItemDto item : d.getRawData().get(indexQid)) {
if (item != null && item.getS() != null) {
out.add(item.getS().intValue());
}
}
}
}
return out;
}
private List<Map<String, String>> initializeStatisticsData(
Integer year,
VariableType variableType,
VariableItemInfoDto[] scalarInfo
) {
int listSize = resolveListSize(variableType, scalarInfo);
List<Map<String, String>> statisticsData = makeMapList(listSize + 1);
String yearStr = year.toString();
statisticsData.get(0).put(LgePenetrationConst.YEAR_LABEL_STR, yearStr);
statisticsData.get(0).put(LgePenetrationConst.LABEL_STR, LgePenetrationConst.TOTAL_LABEL_STR);
if (variableType == VariableType.NUMERIC) {
initializeNumericLabels(statisticsData, yearStr);
} else {
initializeCategoricalLabels(statisticsData, yearStr, Objects.requireNonNull(scalarInfo));
}
return statisticsData;
}
private int resolveListSize(VariableType type, VariableItemInfoDto[] scalarInfo) {
if (type == VariableType.NUMERIC) {
return 3;
}
return scalarInfo.length;
}
private void initializeNumericLabels(List<Map<String, String>> statisticsData, String yearStr) {
statisticsData.get(1).put(LgePenetrationConst.YEAR_LABEL_STR, yearStr);
statisticsData.get(1).put(LgePenetrationConst.LABEL_STR, "MEAN");
statisticsData.get(2).put(LgePenetrationConst.YEAR_LABEL_STR, yearStr);
statisticsData.get(2).put(LgePenetrationConst.LABEL_STR, "MAX");
statisticsData.get(3).put(LgePenetrationConst.YEAR_LABEL_STR, yearStr);
statisticsData.get(3).put(LgePenetrationConst.LABEL_STR, "MIN");
}
private void initializeCategoricalLabels(
List<Map<String, String>> statisticsData,
String yearStr,
VariableItemInfoDto[] scalarInfo
) {
for (int i = 0; i < scalarInfo.length; i++) {
int rowIdx = i + 1;
statisticsData.get(rowIdx).put(LgePenetrationConst.YEAR_LABEL_STR, yearStr);
statisticsData.get(rowIdx).put(LgePenetrationConst.LABEL_STR, scalarInfo[i].getLabel());
}
}
private <T> List<Map<String, T>> makeMapList(int size) {
if (size < 0) {
throw new IllegalArgumentException("size must be >= 0: " + size);
}
List<Map<String, T>> list = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
list.add(new LinkedHashMap<>());
}
return list;
}
private static String toScaledString(BigDecimal v, int dp) {
return v.setScale(dp, RoundingMode.HALF_UP).toPlainString();
}
private static String toScaledString(double v, int scale) {
return BigDecimal.valueOf(v).setScale(scale, RoundingMode.HALF_UP).toPlainString();
}
private void validateCache(List<Integer> yearList, Map<String, VariableDto> variableMap) {
if (yearList == null || yearList.isEmpty()) {
throw BusinessException.internalError("NOT_LOAD_YEAR_LIST", "가용 연도가 로드 되지 못 했습니다.");
}
if (variableMap == null || variableMap.isEmpty()) {
throw BusinessException.internalError("NOT_LOAD_VARIABLE_MAP", "변수 정보가 로드 되지 못 했습니다.");
}
}
}

View File

@@ -0,0 +1,256 @@
package com.kri.lge.penetration.service;
import static com.kri.lge.penetration.common.consts.LgePenetrationConst.BATCH_SIZE;
import static com.kri.lge.penetration.common.consts.LgePenetrationConst.EXCEL_ID;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kri.common.datetime.DateTimeUtil;
import com.kri.common.excel.ExcelMapper;
import com.kri.common.web.BusinessException;
import com.kri.lge.penetration.dto.DataItemDto;
import com.kri.lge.penetration.dto.ExcelUploadResultDto;
import com.kri.lge.penetration.persistence.entity.TDataEntity;
import com.kri.lge.penetration.persistence.repository.DataRepository;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@Log4j2
@Service
@RequiredArgsConstructor
public class UploadService {
@PersistenceContext
private EntityManager em;
private final ObjectMapper objectMapper;
private final DataRepository dataRepository;
public ExcelUploadResultDto uploadData(MultipartFile file){
try (InputStream in = file.getInputStream()) {
List<RowContext> buffer = new ArrayList<>(BATCH_SIZE);
List<Map<String, String>> failRows = new ArrayList<>();
AtomicInteger total = new AtomicInteger();
AtomicInteger success = new AtomicInteger();
AtomicInteger fail = new AtomicInteger();
final LocalDateTime now = DateTimeUtil.nowKstLocalDateTime();
final String regUser = "system";
ExcelMapper.readStreaming(in, (row, rowIndex) -> {
total.incrementAndGet();
ParsedRow parsed = parseRowToEntity(row, regUser, now);
if (parsed.reason != null) {
fail.incrementAndGet();
failRows.add(buildFailRow(row, rowIndex, parsed.reason));
return;
}
buffer.add(new RowContext(rowIndex, row, parsed.entity));
if (buffer.size() >= BATCH_SIZE) {
flushBuffer(buffer, success, fail, failRows);
}
});
if (!buffer.isEmpty()) {
flushBuffer(buffer, success, fail, failRows);
}
byte[] errorFile = null;
boolean hasErrorFile = !failRows.isEmpty();
if (hasErrorFile) {
errorFile = toXlsx(failRows);
}
return ExcelUploadResultDto.of(total.get(), success.get(), fail.get(), errorFile);
} catch (IOException ex){
throw BusinessException.badRequest("CAN_NOT_OPEN", "파일 스트림을 열 수 없습니다.")
.with("fileName",file.getOriginalFilename())
.with("errorMessage",ex.getMessage());
}
}
private void flushBuffer(
List<RowContext> buffer,
AtomicInteger success,
AtomicInteger fail,
List<Map<String, String>> failRows
) {
try {
List<TDataEntity> entities = buffer.stream().map(rc -> rc.entity).toList();
dataRepository.saveAll(entities);
success.addAndGet(buffer.size());
} catch (DataAccessException e) {
// 배치가 한 번에 터지면, 개별 저장으로 폴백해서 "어느 행이 문제인지" 잡는다
for (RowContext rc : buffer) {
try {
dataRepository.saveAndFlush(rc.entity);
success.incrementAndGet();
} catch (DataAccessException ex) {
fail.incrementAndGet();
failRows.add(buildFailRow(rc.rawRow, rc.rowIndex, "DB 저장 실패: " + rootMessage(ex)));
}
}
} finally {
em.clear();
buffer.clear();
}
}
private ParsedRow parseRowToEntity(
Map<String, String> row,
String regUser,
LocalDateTime regDt
) {
if (row == null || row.isEmpty()) {
return ParsedRow.fail("빈 행");
}
// sid
String sid = firstNonBlank(row.get(EXCEL_ID), row.get("sid"), row.get("SID"));
if (sid == null) {
return ParsedRow.fail("ID(sid) 누락");
}
if (sid.length() > 32) {
return ParsedRow.fail("ID(sid) 길이 초과(>32): " + sid);
}
// rawData 구성
Map<String, List<DataItemDto>> rawData = new LinkedHashMap<>();
for (Map.Entry<String, String> e : row.entrySet()) {
String key = e.getKey();
if (key == null) continue;
// 제외
if (EXCEL_ID.equals(key)) continue;
//if (EXCEL_YEAR.equals(key)) continue;
//if (EXCEL_PRODUCT.equals(key)) continue;
String v = e.getValue();
if (v == null || v.isBlank()) {
continue; // 빈 값은 rawData에 안 넣음
}
Double s = parseDoubleLoose(v);
if (s == null) {
return ParsedRow.fail("숫자 파싱 실패: key=" + key + ", value=" + safe(v));
}
if(key.contains("#")){
key = key.substring(0,key.indexOf("#"));
}
rawData.computeIfAbsent(key, k -> new ArrayList<>()).add(DataItemDto.from(null, null, s));
}
TDataEntity entity = new TDataEntity();
entity.setSid(sid);
entity.setRawData(rawData);
entity.setRegUser(regUser);
entity.setRegDt(regDt);
return ParsedRow.ok(entity);
}
private Map<String, String> buildFailRow(Map<String, String> row, int rowIndex, String reason) {
// 에러 엑셀에 행번호/사유를 앞에 고정 컬럼으로 넣고, 원본 컬럼들도 같이 싣는다
Map<String, String> m = new LinkedHashMap<>();
m.put("rowIndex", String.valueOf(rowIndex));
m.put("reason", reason);
if (row != null) {
m.putAll(row);
}
return m;
}
private byte[] toXlsx(List<Map<String, String>> failRows) {
Workbook wb = ExcelMapper.writeFromMapList(failRows);
try (wb; ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
wb.write(bos);
return bos.toByteArray();
} catch (Exception e) {
throw new RuntimeException("에러 엑셀(byte[]) 생성 실패", e);
}
}
private static String rootMessage(Throwable t) {
Throwable cur = t;
while (cur.getCause() != null) cur = cur.getCause();
return cur.getMessage();
}
private void doDebugLog(TDataEntity e){
try {
String json = objectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(e.getRawData());
log.info("rawDataJson={}", json);
} catch (JsonProcessingException ex) {
log.warn("rawData json serialize failed", ex);
}
}
private static Double parseDoubleLoose(String s) {
if (s == null) return null;
String t = s.trim();
if (t.isEmpty()) return null;
// 천단위 콤마 제거
t = t.replace(",", "");
try {
return Double.parseDouble(t);
} catch (NumberFormatException e) {
return null;
}
}
private static String firstNonBlank(String... values) {
if (values == null) return null;
for (String v : values) {
if (v != null && !v.isBlank()) return v.trim();
}
return null;
}
private static String safe(String s) {
return s == null ? "null" : s;
}
private record RowContext(
int rowIndex,
Map<String, String> rawRow,
TDataEntity entity
) {}
private record ParsedRow(
TDataEntity entity,
String reason
) {
static ParsedRow ok(TDataEntity entity) {
return new ParsedRow(entity, null);
}
static ParsedRow fail(String reason) {
return new ParsedRow(null, reason);
}
}
}

View File

@@ -0,0 +1,7 @@
spring:
datasource:
#private 도메인
url: jdbc:mysql://db-md87m-kr.vpc-pub-cdb.ntruss.com:3306/test_kri_contact?characterEncoding=UTF-8&rewriteBatchedStatements=true
username: kri_contact
password: contact#51304
driver-class-name: com.mysql.cj.jdbc.Driver

View File

@@ -0,0 +1,25 @@
spring:
datasource:
#public 도메인
url: jdbc:mysql://db-md87m-kr.vpc-pub-cdb.ntruss.com:3306/test_lge_cx_dash?characterEncoding=UTF-8&rewriteBatchedStatements=true
username: kriit
password: it#51304
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: none # 필요에 따라 update / validate 등으로 변경
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL8Dialect
format_sql: true
show_sql: false
generate_statistics: true
open-in-view: false
logging:
level:
org.hibernate.SQL: DEBUG
org.hibernate.orm.jdbc.bind: TRACE
#org.hibernate.orm.jdbc.extract: TRACE
org.hibernate.engine.jdbc.batch.internal: DEBUG

View File

@@ -0,0 +1,7 @@
spring:
datasource:
#private 도메인
url: jdbc:mysql://db-md87m-kr.vpc-pub-cdb.ntruss.com:3306/test_kri_contact?characterEncoding=UTF-8&rewriteBatchedStatements=true
username: kri_contact
password: contact#51304
driver-class-name: com.mysql.cj.jdbc.Driver

View File

@@ -0,0 +1,58 @@
server:
port: 6101
forward-headers-strategy: framework
tomcat:
max-http-form-post-size: 50MB
max-swallow-size: -1
spring:
application:
name: lge-penetration-back
jpa:
properties:
hibernate:
jdbc:
batch_size: 1000
order_inserts: true
order_updates: true
profiles:
# 아무 설정 없을 때 기본으로 쓸 프로파일
default: local
servlet:
multipart:
max-file-size: 50MB
max-request-size: 50MB
logging:
file:
path: ./logs
kri:
security:
enabled: true
public-paths: # 인증 없이 열어둘 URL
- /v3/api-docs/**
- /swagger-ui.html
- /swagger-ui/**
- /ping
- /api/v1/auth/login
- /api/v1/admin-cache/**
swagger:
enabled: true
title: "LGE CX Dash"
description: "LGE 가전 보급률 Dashboard"
version: "v1.0.0"
base-package: "com.kri.lge.penetration"
common:
web:
enabled: true # 공통 Web 구성 사용 여부 (기본 true)
webmvc:
enabled: true # 공통 Webmvc 구성 사용 여부 (기본 true)
redis:
enabled: false # RedisQueueHelper 사용 시 true
scheduling:
enabled: true # @Scheduled 사용 시 true

View File

@@ -0,0 +1,13 @@
package com.kri.lge.penetration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class LgePenetrationBackApplicationTests {
@Test
void contextLoads() {
}
}