diff --git a/src/main/java/cokr/xit/fims/stat/StatQuery.java b/src/main/java/cokr/xit/fims/stat/StatQuery.java index 2cf1c327..1d9e6cbe 100644 --- a/src/main/java/cokr/xit/fims/stat/StatQuery.java +++ b/src/main/java/cokr/xit/fims/stat/StatQuery.java @@ -26,6 +26,11 @@ public class StatQuery extends CmmnQuery { */ private String[] categorization; + /** + * 항목 추가 분류 방법 + */ + private String[] categorizationAddOption; + /** * 항목명 변환(코드그룹ID) */ @@ -93,6 +98,15 @@ public class StatQuery extends CmmnQuery { return self(); } + public String[] getCategorizationAddOption() { + return ifEmpty(categorizationAddOption, () -> null); + } + + public T setCategorizationAddOption(String... categorizationAddOption) { + this.categorizationAddOption = categorizationAddOption; + return self(); + } + public String[] getCategoryNameConverter() { return ifEmpty(categoryNameConverter, () -> null); } diff --git a/src/main/java/cokr/xit/fims/stat/service/bean/StatBean.java b/src/main/java/cokr/xit/fims/stat/service/bean/StatBean.java index d1ad74b2..560041e7 100644 --- a/src/main/java/cokr/xit/fims/stat/service/bean/StatBean.java +++ b/src/main/java/cokr/xit/fims/stat/service/bean/StatBean.java @@ -1,10 +1,14 @@ package cokr.xit.fims.stat.service.bean; import java.lang.reflect.Method; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.format.TextStyle; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -24,6 +28,35 @@ import cokr.xit.foundation.data.DataObject; @Component("statBean") public class StatBean extends AbstractComponent { + /**쿼리에서 날짜와 관련된 파라미터를 설정한다. + * @param sql처리용 query, dayCol 날짜조회용 컬럼, from 조회시작일, to 조회종료일 + * @return + */ + public void daySetting(CmmnQuery someQuery, String dayCol, String from, String to) { + try { + if(dayCol.equals("REG_DT")) { + + Method m0 = CmmnQuery.class.getDeclaredMethod("setSchDateOpt", String.class); + m0.invoke(someQuery, "regDt"); + Method m1 = CmmnQuery.class.getDeclaredMethod("setSchDateFrom", String.class); + m1.invoke(someQuery, from); + Method m2 = CmmnQuery.class.getDeclaredMethod("setSchDateTo", String.class); + m2.invoke(someQuery, to); + } else { + + if(dayCol.equals("CRDN_YMD")) { + Method m1 = someQuery.getClass().getDeclaredMethod("setSchCrdnYmdFrom", String.class); + m1.invoke(someQuery, from); + Method m2 = someQuery.getClass().getDeclaredMethod("setSchCrdnYmdTo", String.class); + m2.invoke(someQuery, to); + } + + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + /**통계요청에 따라 데이터목록에서 통계를 생성한다. * @param queryResult SQL쿼리 처리 결과, statQuery 통계 요청 * @return 통계 @@ -109,6 +142,9 @@ public class StatBean extends AbstractComponent { public Map> grouping(List queryResult, StatQuery statQuery){ String[] categorizations = statQuery.getCategorization(); + String[] addOptions = statQuery.getCategorizationAddOption(); + + Map> group = queryResult.stream() .collect( Collectors.groupingBy( @@ -118,7 +154,33 @@ public class StatBean extends AbstractComponent { if(i != 0) { result += ","; } - result += item.string(categorizations[i]); + String colValue = item.string(categorizations[i]); + + if(addOptions != null) { + if(addOptions[i].equals("YYYY")) { //연도별 + result += colValue.substring(0,4); + } else if(addOptions[i].equals("YYYYMM")) { //연월별 + result += colValue.substring(0,6); + } else if(addOptions[i].equals("DAY_OF_WEEK")) { //요일별 + int y = Integer.parseInt(colValue.substring(0,4)); + int m = Integer.parseInt(colValue.substring(4,6)); + int d = Integer.parseInt(colValue.substring(6,8)); + LocalDate date = LocalDate.of(y, m, d); + DayOfWeek dayOfWeek = date.getDayOfWeek(); + String dow = dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.KOREAN); + result += dow; + } else if(addOptions[i].equals("HH")) { //시간대별 + if(colValue.length() == 14) { + result += colValue.substring(8,10); + } else { + result += colValue.substring(0,2); + } + } else { + result += colValue; + } + } else { + result += colValue; + } } return result; } @@ -325,33 +387,35 @@ public class StatBean extends AbstractComponent { } } - /**쿼리에서 날짜와 관련된 파라미터를 설정한다. - * @param sql처리용 query, dayCol 날짜조회용 컬럼, from 조회시작일, to 조회종료일 + /** 통계의 수치값 중 비율(%)을 계산한다. + * @param statItems 통계 항목 목록, statQuery 통계 요청 * @return */ - public void daySetting(CmmnQuery someQuery, String dayCol, String from, String to) { - try { - if(dayCol.equals("REG_DT")) { + public void attachRatioValue(List statItems, StatQuery statQuery) { - Method m0 = CmmnQuery.class.getDeclaredMethod("setSchDateOpt", String.class); - m0.invoke(someQuery, "regDt"); - Method m1 = CmmnQuery.class.getDeclaredMethod("setSchDateFrom", String.class); - m1.invoke(someQuery, from); - Method m2 = CmmnQuery.class.getDeclaredMethod("setSchDateTo", String.class); - m2.invoke(someQuery, to); - } else { + String[] aggregateTypes = statQuery.getAggregateType(); + + for(StatItem statItem : statItems) { + + for(int i = 0; i < aggregateTypes.length ; i++) { + + if(aggregateTypes[i].startsWith("ratioRef=")) { + + int refIndex = Integer.parseInt(aggregateTypes[i].split("=")[1]); + + int totValue = statItem.getNumberValue()[refIndex]; + if(totValue == 0) { + statItem.getNumberValue()[i] = 0; + } else { + statItem.getNumberValue()[i] = statItem.getNumberValue()[i] * 100 / totValue; + } - if(dayCol.equals("CRDN_YMD")) { - Method m1 = someQuery.getClass().getDeclaredMethod("setSchCrdnYmdFrom", String.class); - m1.invoke(someQuery, from); - Method m2 = someQuery.getClass().getDeclaredMethod("setSchCrdnYmdTo", String.class); - m2.invoke(someQuery, to); } } - } catch (Exception e) { - throw new RuntimeException(e); + } + } } diff --git a/src/main/java/cokr/xit/fims/stat/service/bean/StatServiceBean.java b/src/main/java/cokr/xit/fims/stat/service/bean/StatServiceBean.java index 217d082b..3214152b 100644 --- a/src/main/java/cokr/xit/fims/stat/service/bean/StatServiceBean.java +++ b/src/main/java/cokr/xit/fims/stat/service/bean/StatServiceBean.java @@ -2,6 +2,7 @@ package cokr.xit.fims.stat.service.bean; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; @@ -119,9 +120,6 @@ public class StatServiceBean extends AbstractServiceBean implements StatService } - - - String domain = ""; String[] categorizations = statQuery.getCategorization(); @@ -191,14 +189,28 @@ public class StatServiceBean extends AbstractServiceBean implements StatService } + //통계 생성 stat = statBean.groupByCartegory(queryResult, statQuery); + //통계 항목명 변환 statBean.attachItemName(stat.getStatItems(), statQuery, commonCodes); + //통계 항목 재그룹화 if(statQuery.getRegroupInfos() != null){ statBean.regroupItem(stat.getStatItems(), statQuery.getRegroupInfos()); } + //비율 계산 + boolean exsistRatioValue = false; + for(String aggregateType : Arrays.asList(statQuery.getAggregateType())) { + if(aggregateType.startsWith("ratioRef=")) { + exsistRatioValue = true; + } + } + if(exsistRatioValue){ + statBean.attachRatioValue(stat.getStatItems(), statQuery); + } + if(numberValueLabel != null) { stat.setNumberValueLabel(numberValueLabel); }