Skip to content

Commit

Permalink
Merge pull request #29 from TEAM-HUNDRED/develop
Browse files Browse the repository at this point in the history
Refactor : sql 쿼리문 xml파일로 관리
  • Loading branch information
DAY0522 authored Sep 9, 2023
2 parents 8fea8b0 + 6e00591 commit d91ee2f
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 140 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
.DS_Store

### STS ###
.apt_generated
Expand Down Expand Up @@ -36,4 +37,4 @@ out/
### VS Code ###
.vscode/

*.yml
*.yml
162 changes: 25 additions & 137 deletions src/main/java/com/management/web/repository/MemberJdbcWebRepository.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.management.web.repository;

import com.management.web.service.dto.*;
import org.springframework.beans.factory.annotation.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.PropertySource;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
Expand All @@ -10,176 +12,62 @@
import javax.sql.DataSource;
import java.util.List;


@Slf4j
@Repository
@PropertySource("classpath:sql.xml")
public class MemberJdbcWebRepository implements MemberWebRepository {

private final JdbcTemplate template;

public MemberJdbcWebRepository(DataSource dataSource) {
this.template = new JdbcTemplate(dataSource);
}

@Value("${privateRankingSQL}")
String privateRanking;
@Value("${privateRankingNullSQL}")
String privateRankingNull;
@Override
public MyPrivateRankingInfoDto findPrivateRnkingByKakaoId(String kakaoId) {
String sql = "with rankingCert as(\n" +
"select\n" +
" m.kakao_id,\n" +
" m.username,\n" +
" cert_data->>'date' AS date,\n" +
" cert_data->>'check' as check,\n" +
" data_row ->> 'challenge_id' as c_id\n" +
"FROM \"member\" m ,\n" +
" jsonb_array_elements(m.certification) AS data_row,\n" +
" jsonb_array_elements(data_row->'cert') AS cert_data\n" +
"where CAST(cert_data ->> 'date' as date) >= date_trunc('week',current_date)::date\n" +
"and cert_data->>'check' != 'FAIL'),\n" +
"ranked_members as (select r.username as username,\n" +
"sum(c.saved_money) as smoney,\n" +
"count(*) as cnt,\n" +
"r.kakao_id as kakao_id,\n" +
"rank() over (order by sum(c.saved_money) desc,count(*) desc)\n" +
"from rankingCert r join challenge c on r.c_id::int = c.id\n" +
"group by r.username,r.kakao_id),\n" +
"oneMember as(\n" +
"select * from ranked_members rr\n" +
"where rr.kakao_id = ?\n" +
")\n" +
"select\n" +
" case when o.rank::integer = 1::integer then 0 else rrr.smoney-o.smoney end as gap,\n" +
" o.username as username,\n" +
" o.rank as myrank,\n" +
" o.cnt as mycnt,\n" +
" o.smoney as mysmoney\n" +
" from ranked_members rrr, oneMember o\n" +
"where rrr.rank< o.rank or o.rank = 1\n" +
"order by rrr.smoney,rrr.rank desc\n" +
"limit 1;\n";
String sql_null = "with rankingCert as(\n" +
"select\n" +
" m.kakao_id,\n" +
" m.username,\n" +
" cert_data->>'date' AS date,\n" +
" cert_data->>'check' as check,\n" +
" data_row ->> 'challenge_id' as c_id\n" +
"FROM \"member\" m ,\n" +
" jsonb_array_elements(m.certification) AS data_row,\n" +
" jsonb_array_elements(data_row->'cert') AS cert_data\n" +
"where CAST(cert_data ->> 'date' as date) >= date_trunc('week',current_date)::date\n" +
"and cert_data->>'check' != 'FAIL'),\n" +
"rankingResult as (\n" +
"select \n" +
"\tr.username as username,\n" +
"\tsum(c.saved_money) as smoney,\n" +
"\tcount(*) as cnt,\n" +
"\tr.kakao_id,\n" +
"\trank() over (order by sum(c.saved_money) desc, count(*) desc)\n" +
"from rankingCert r join challenge c on r.c_id::int = c.id\n" +
"group by r.username,r.kakao_id)\n" +
"select\n" +
"\tmin(rr.smoney) as gap,\n" +
"\tmax(rr.rank)+1 as myRank,\n" +
"\tm.username as username,\n" +
"\t0 as mysmoney,\n" +
"\t0 as mycnt\n" +
"from rankingResult rr, member m\n" +
"where m.kakao_id=?\n" +
"group by m.username;";

public MyPrivateRankingInfoDto findPrivateRankingByKakaoId(String kakaoId) {
MyPrivateRankingInfoDto privateRankingInfoDto;
try {
privateRankingInfoDto = template.queryForObject(sql, rankingPrivateRowMapper(), kakaoId);
privateRankingInfoDto = template.queryForObject(privateRanking, rankingPrivateRowMapper(), kakaoId);
}catch(IncorrectResultSizeDataAccessException error) {
privateRankingInfoDto = template.queryForObject(sql_null, rankingPrivateRowMapper(), kakaoId);
privateRankingInfoDto = template.queryForObject(privateRankingNull, rankingPrivateRowMapper(), kakaoId);
}
return privateRankingInfoDto;

}

@Value("${rankingListSQL}")
String rankingList;
@Override
public List<MyRankingInfoDto> findRankingInfoList(){
String sql = """
with rankingCert as(
select
m.kakao_id,
m.username,
cert_data->>'date' AS date,
cert_data->>'check' as check,
data_row ->> 'challenge_id' as c_id
FROM "member" m ,
jsonb_array_elements(m.certification) AS data_row,
jsonb_array_elements(data_row->'cert') AS cert_data
where CAST(cert_data ->> 'date' as date) >= date_trunc('week',current_date)::date
and cert_data->>'check' != 'FAIL')
select r.username as username, r.kakao_id as kakaoId, sum(c.saved_money) as smoney, count(*) as cnt,
rank() over (order by sum(c.saved_money) desc, count(*) desc)
from rankingCert r join challenge c on r.c_id::int = c.id
group by r.username, r.kakao_id;
""";
List<MyRankingInfoDto> certRankingList = template.query(sql, rankingRowMapper());
List<MyRankingInfoDto> certRankingList = template.query(rankingList, rankingRowMapper());
return certRankingList;
}

@Value("${participateChallengeListSQL}")
String participateChallengeList;
@Override
public List<MyChallengeInfoDto> findParticipateChallengeList(String kakaoId){
String sql= """
SELECT
c.title AS title,
m.username AS username,
data_row ->> 'certificationCnt' AS cnt,
c.reward*(data_row ->> 'certificationCnt')::int AS reward,
c.saved_money*(data_row ->> 'certificationCnt')::int AS saved_money,
data_row ->> 'challengeId' AS challengeId
FROM "member" m,
jsonb_array_elements(m.participation) AS data_row JOIN challenge c ON (data_row ->> 'challengeId')::integer=c.id
WHERE m. kakao_id =?
AND current_date >= (data_row ->> 'startDate')::date
AND current_date <= (data_row ->> 'endDate')::date;
""";
List<MyChallengeInfoDto> myChallengeList = template.query(sql, challengeRowMapper(),kakaoId);
List<MyChallengeInfoDto> myChallengeList = template.query(participateChallengeList, challengeRowMapper(),kakaoId);
return myChallengeList;
}

@Value("${challengeCertListSQL}")
String challengeCertListQuery;
@Override
public List<MyChallengeCertDto> findChallengeCertList(Integer challengeId, String kakaoId){
String sql="WITH partiChall AS(SELECT\n" +
" m.username,\n" +
" m.kakao_id AS kakao_id,\n" +
" c.title AS title,\n" +
" data_row ->> 'startDate' AS startDate,\n" +
" data_row ->> 'endDate' AS endDate,\n" +
" data_row ->> 'goalCnt' AS goalCnt,\n" +
" data_row ->> 'challengeId' AS challengeId,\n" +
" data_row ->> 'certificationCnt' AS certificationCnt,\n" +
" data_row ->> 'isSuccess' AS isSuccess\n" +
"FROM \"member\" m ,\n" +
" jsonb_array_elements(m.participation) AS data_row JOIN challenge c ON (data_row ->> 'challengeId')::integer=c.id\n" +
"WHERE m.kakao_id =?\n" +
"AND current_date >= (data_row ->> 'startDate')::date\n" +
"\tAND current_date <= (data_row ->> 'endDate')::date\n" +
"AND (data_row ->> 'challengeId')::integer = ?)\n" +
"SELECT\n" +
" DATE_TRUNC('day', (cert_data->>'date')::date) AS DAY,\n" +
" count(1) AS count\n" +
"FROM \"member\" m ,\n" +
"\tpartiChall AS pc JOIN jsonb_array_elements(m.certification) AS data_row \n" +
"\tON (pc.challengeId = data_row ->> 'challenge_id'),\n" +
" jsonb_array_elements(data_row->'cert') AS cert_data\n" +
"WHERE m.kakao_id = pc.kakao_id\n" +
"AND pc.startDate::date <= (cert_data->>'date')::date\n" +
"AND pc.endDate::date >=(cert_data->>'date')::date\n" +
"AND cert_data->>'check' != 'FAIL'\n" +
"GROUP BY day;";
List<MyChallengeCertDto> myChallengeCertList = template.query(sql, challengeCertRowMapper(),kakaoId,challengeId);

List<MyChallengeCertDto> myChallengeCertList = template.query(challengeCertListQuery, challengeCertRowMapper(),kakaoId,challengeId);
return myChallengeCertList;
}

@Value("${mainInfoSQL}")
String mainInfoQuery;
@Override
public MyMainInfoDto findMainInfoByKakaoId(String kakaoId){
String sql = "select username, saved_money, reward\n" +
"from \"member\" m \n" +
"where kakao_id =?;";
MyMainInfoDto mainInfoDto = template.queryForObject(sql, mainInfoDtoRowMapper(),kakaoId);

MyMainInfoDto mainInfoDto = template.queryForObject(mainInfoQuery, mainInfoDtoRowMapper(),kakaoId);
return mainInfoDto;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public interface MemberWebRepository {


MyPrivateRankingInfoDto findPrivateRnkingByKakaoId(String kakaoId);
MyPrivateRankingInfoDto findPrivateRankingByKakaoId(String kakaoId);

List<MyRankingInfoDto> findRankingInfoList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public List<MyRankingInfoDto> getMyRankingInfo(){
return memberWebRepository.findRankingInfoList();
}
public MyPrivateRankingInfoDto getMyPrivateRankingInfo(String kakaoId) {
return memberWebRepository.findPrivateRnkingByKakaoId(kakaoId);
return memberWebRepository.findPrivateRankingByKakaoId(kakaoId);
}

@Transactional
Expand Down
137 changes: 137 additions & 0 deletions src/main/resources/sql.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd" >
<properties>
<entry key="privateRankingSQL">
with rankingCert as(
select
m.kakao_id,
m.username,
cert_data->>'date' AS date,
cert_data->>'check' as check,
data_row ->> 'challenge_id' as c_id
FROM "member" m ,
jsonb_array_elements(m.certification) AS data_row,
jsonb_array_elements(data_row->'cert') AS cert_data
where CAST(cert_data ->> 'date' as date) >= date_trunc('week',current_date)::date
and cert_data->>'check' != 'FAIL'),
ranked_members as (select r.username as username,
sum(c.saved_money) as smoney,
count(*) as cnt,
r.kakao_id as kakao_id,
rank() over (order by sum(c.saved_money) desc,count(*) desc)
from rankingCert r join challenge c on r.c_id::int = c.id
group by r.username,r.kakao_id),
oneMember as(
select * from ranked_members rr
where rr.kakao_id = ? )
select
case when o.rank::integer = 1::integer then 0 else rrr.smoney-o.smoney end as gap,
o.username as username,
o.rank as myrank,
o.cnt as mycnt,
o.smoney as mysmoney
from ranked_members rrr, oneMember o
where rrr.rank&lt; o.rank or o.rank = 1
order by rrr.smoney,rrr.rank desc
limit 1;
</entry>
<entry key="privateRankingNullSQL">
with rankingCert as(
select
m.kakao_id,
m.username,
cert_data->>'date' AS date,
cert_data->>'check' as check,
data_row ->> 'challenge_id' as c_id
FROM "member" m ,
jsonb_array_elements(m.certification) AS data_row,
jsonb_array_elements(data_row->'cert') AS cert_data
where CAST(cert_data ->> 'date' as date) >= date_trunc('week',current_date)::date
and cert_data->>'check' != 'FAIL'),
rankingResult as (
select
r.username as username,
sum(c.saved_money) as smoney,
count(*) as cnt,
r.kakao_id,
rank() over (order by sum(c.saved_money) desc, count(*) desc)
from rankingCert r join challenge c on r.c_id::int = c.id
group by r.username,r.kakao_id)
select
min(rr.smoney) as gap,
max(rr.rank)+1 as myRank,
m.username as username,
0 as mysmoney,
0 as mycnt
from rankingResult rr, member m
where m.kakao_id=?
group by m.username;
</entry>
<entry key="participateChallengeListSQL">
SELECT
c.title AS title,
m.username AS username,
data_row ->> 'certificationCnt' AS cnt,
c.reward*(data_row ->> 'certificationCnt')::int AS reward,
c.saved_money*(data_row ->> 'certificationCnt')::int AS saved_money,
data_row ->> 'challengeId' AS challengeId
FROM "member" m,
jsonb_array_elements(m.participation) AS data_row JOIN challenge c ON (data_row ->> 'challengeId')::integer=c.id
WHERE m. kakao_id =?
AND current_date >= (data_row ->> 'startDate')::date
AND current_date &lt;= (data_row ->> 'endDate')::date;
</entry>
<entry key="rankingListSQL">
with rankingCert as(
select
m.kakao_id,
m.username,
cert_data->>'date' AS date,
cert_data->>'check' as check,
data_row ->> 'challenge_id' as c_id
FROM "member" m ,
jsonb_array_elements(m.certification) AS data_row,
jsonb_array_elements(data_row->'cert') AS cert_data
where CAST(cert_data ->> 'date' as date) >= date_trunc('week',current_date)::date
and cert_data->>'check' != 'FAIL')
select r.username as username, r.kakao_id as kakaoId, sum(c.saved_money) as smoney, count(*) as cnt,
rank() over (order by sum(c.saved_money) desc, count(*) desc)
from rankingCert r join challenge c on r.c_id::int = c.id
group by r.username, r.kakao_id;
</entry>
<entry key="challengeCertListSQL">
WITH partiChall AS(SELECT
m.username,
m.kakao_id AS kakao_id,
c.title AS title,
data_row ->> 'startDate' AS startDate,
data_row ->> 'endDate' AS endDate,
data_row ->> 'goalCnt' AS goalCnt,
data_row ->> 'challengeId' AS challengeId,
data_row ->> 'certificationCnt' AS certificationCnt,
data_row ->> 'isSuccess' AS isSuccess
FROM "member" m ,
jsonb_array_elements(m.participation) AS data_row JOIN challenge c ON (data_row ->> 'challengeId')::integer=c.id
WHERE m.kakao_id =?
AND current_date >= (data_row ->> 'startDate')::date
AND current_date &lt;= (data_row ->> 'endDate')::date
AND (data_row ->> 'challengeId')::integer = ?)
SELECT
(cert_data->>'date')::date AS DAY,
count(1) AS count
FROM "member" m ,
partiChall AS pc JOIN jsonb_array_elements(m.certification) AS data_row
ON (pc.challengeId = data_row ->> 'challenge_id'),
jsonb_array_elements(data_row->'cert') AS cert_data
WHERE m.kakao_id = pc.kakao_id
AND pc.startDate::date &lt;= (cert_data->>'date')::date
AND pc.endDate::date >=(cert_data->>'date')::date
AND cert_data->>'check' != 'FAIL'
GROUP BY day;
</entry>
<entry key="mainInfoSQL">
select username, saved_money, reward
from "member" m
where kakao_id =?;
</entry>
</properties>

0 comments on commit d91ee2f

Please sign in to comment.