22-09-06 진행상황

(。◠ ◠。)·2022년 9월 6일
0

메인페이지가 심심했어서 그래프를 넣는다고 했는데
넣는거 까지는 문제가 없었는데 값을 가져오는데
가져오는거 까지도 문제가 없었는데ㅋㅋㅋㅋ...

가입회원 데이터 쿼리문

일단 가입/탈퇴회원의 날짜정보는 년/월/일로 되어있어서 groupby를 사용해서
년월이 같은 회원 수를 카운트 했다.

select TO_CHAR(regdate, 'YYYYMM') joinmonth, count(*) cnt 
	from prj_member 
	group by  TO_CHAR(regdate, 'YYYYMM') 
    order by joinmonth desc;

모든 날짜 데이터가 필요한건 아니라서 row_number()로 최근 5개월 까지만 추렸다.

select * from 
	(select TO_CHAR(regdate, 'YYYYMM') joinmonth, count(*) cnt, ROW_NUMBER() 
    	over(order by TO_CHAR(regdate, 'YYYYMM') desc) as num
	from prj_member group by  TO_CHAR(regdate, 'YYYYMM')
	order by joinmonth desc) where num < 6;

문제점

5개월의 데이터가 모두 있는경우에는 9월 8월 7월 6월 5월 이렇게 정확하게 나오지만
만약에 해당 월에 가입/탈퇴한 인원이 없을경우에는
DB에서 9월 8월 7월 5월 4월 로 6월이 없이 그냥 출력이 되고
그상태로 값을 받아버리면 중간에서 꼬여버린다.

이렇게 해볼까....

그래서 자바에서 현재의 월과 데이터의 월을 비교해서
같지 않은 값에는 0을 넣어주려고했다.
..그러려고 했다...

현재 날짜 추출

자동으로 날짜가 바뀌면 그래프의 값도 달라져야 하기에
자바에서 현재의 날짜데이터를 불러왔다.

모두 인트타입으로 반환되기에 YYYYMM형식으로 만든 후 형식을 String으로 만들었다.

		LocalDate now = LocalDate.now();
		now.getMonthValue(); //해당 월
		now.getYear(); //해당 년도
		
        String thisMonth0 = Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue());
		//9
		String thisMonth1 = Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-1);
		//8
		String thisMonth2 = Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-2);
		//7
		String thisMonth3 = Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-3);
		//6
		String thisMonth4 = Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-4);
		//5

0명이 들어갈 HashMap생성

//DB에서 받아온 데이터
		List<MemberCntVO> joinList = dao.joinMemberCount();
		List<MemberCntVO> quitList = dao.quitMemberCount();
//키+벨류로 받을 hash map
        Map<String, String> joinMap = new HashMap<>();
		Map<String, String> quitMap = new HashMap<>();

현재 월과 DB의 월을 비교해서...

같은 값일 경우 key로는 년월YYYYMM데이터가 value로는 DB에서 불러낸 인원수카운트가 들어간다
만약에 해당 과정을 다 거친 후에 날짜값을 key값으로 조회했을때 null값이 있으면
DB에 없는 데이터라는 뜻임으로 key값에는 자바의 현재 월 데이터를 value값에는 0을 넣어줬다.

구구단처럼 해당 단이 다 돌아야 다음단으로 갈 수 있음으로 반복문을 2중으로 돌려줘야했다.

//join
		//이번달 -5와 같은 값이 있으면 넣어주고
		for(int i = 0; i < 5; i++) {
			for(int j = 0; j < 5; j++) {
				if(joinList.get(i).getJoinMonth().equals(Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-j))) {
					joinMap.put(joinList.get(i).getJoinMonth(), joinList.get(i).getJoinMonthCnt());
					continue;
				}
			}
		}
		//해당 달에 사람이 없으면 0값을 넣어준다
		for(int i = 0; i < 5; i++) {
			if(joinMap.get(Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-i))==null) {
				joinMap.put(Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-i),"0");
			}
		}

그냥 뷰에 보내면 되는줄 알았더니....

HashMap의 값 {202208=1, 202209=2, 202206=0, 202207=2, 202205=1}
원하던 값(없는 날짜과 0데이터)이 나왔기때문에 다 된 줄 알았더니
자바스크립트 뷰단에서 HashMap의 key값을 변수명으로 받지 못하는 문제가 있었다.
${joinMap.get('202209')} ${joinMap['202209']}
일반 문자열로는 내가 원하는 value값이 나왔지만

let today = new Date(); 
let month = today.getMonth() + 1;
let year = today.getFullYear();
let day = year+'0'+month;

자바스크립트로 날자값을 만들어서
${joinMap.get(day)} 이렇게 변수로 설정해줘도 알아먹지를 못했다.

다시 한번더 ArrayList에 넣어줬다.

어쩌겠는가.. 방법을 모르겠으니 아는방법으로 가는 수밖에..
그래서 새로운 list를 만들어서 리스트에 최신 월부터 순서대로
HashMap의 value값을 넣어줬다.
key값을 변수로 설정해놔서 그냥 반복문만 쫙 돌렸다.

		List <String> join = new ArrayList<>();
		for(int i = 0; i < 5; i++) {
			join.add(
				joinMap.get(
					now.getYear()+"0"+Integer.toString(now.getMonthValue()-i)));
		}

드디어 끝!

해당 값을 jsp로 보내주었고 회원이 0명이든 날짜가 변하든 상관없이 자동으로 DB값을 그래프에서 처리할 수 있게 되었다~~

  series: [{
    name: '가입회원',
    data: [
    	${joinList[4] },
    	${joinList[3] },
    	${joinList[2] },
    	${joinList[1] },
    	${joinList[0] }
    ]

전체코드

		LocalDate now = LocalDate.now();
		System.out.println(now.getMonthValue()); 
		System.out.println(now.getYear()); 
		
		String thisMonth0 = Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue());
		//9
		String thisMonth1 = Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-1);
		//8
		String thisMonth2 = Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-2);
		//7
		String thisMonth3 = Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-3);
		//6
		String thisMonth4 = Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-4);
		//5
		
		Map<String, String> joinMap = new HashMap<>();
		Map<String, String> quitMap = new HashMap<>();
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		AdminDAO dao = new AdminDAO();
		List<MemberCntVO> joinList = dao.joinMemberCount();
		List<MemberCntVO> quitList = dao.quitMemberCount();
		
		//join
		//이번달 -5와 같은 값이 있으면 넣어주고
		for(int i = 0; i < 5; i++) {
			for(int j = 0; j < 5; j++) {
				if(joinList.get(i).getJoinMonth().equals(Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-j))) {
					joinMap.put(joinList.get(i).getJoinMonth(), joinList.get(i).getJoinMonthCnt());
					continue;
				}
			}
		}
		//해당 달에 사람이 없으면 0값을 넣어준다
		for(int i = 0; i < 5; i++) {
			if(joinMap.get(Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-i))==null) {
				joinMap.put(Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-i),"0");
			}
		}
		
		//quit
		//이번달 -5와 같은 값이 있으면 넣어주고
		for(int i = 0; i < 5; i++) {
			for(int j = 0; j < 5; j++) {
				if(quitList.get(i).getQuitMonth().equals(Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-j))) {
					quitMap.put(quitList.get(i).getQuitMonth(), quitList.get(i).getQuitMonthCnt());
					continue;
				}
			}
		}
		//해당 달에 사람이 없으면 0값을 넣어준다
		for(int i = 0; i < 5; i++) {
			if(quitMap.get(Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-i))==null) {
				quitMap.put(Integer.toString(now.getYear())+"0"+Integer.toString(now.getMonthValue()-i),"0");
			}
		}
		List <String> join = new ArrayList<>();
		for(int i = 0; i < 5; i++) {
			join.add(
				joinMap.get(
					now.getYear()+"0"+Integer.toString(now.getMonthValue()-i)));
		}
		
		List <String> quit = new ArrayList<>();
		for(int i = 0; i < 5; i++) {
			quit.add(
				quitMap.get(
					now.getYear()+"0"+Integer.toString(now.getMonthValue()-i)));
		}
		
		request.setAttribute("joinList", join);
		request.setAttribute("quitList", quit);
		request.getRequestDispatcher("adminMain.jsp").forward(request, response);
	}

하자하자..

  • 찜트리거 활용
  • 따봉알림
  • 탈퇴회원페이지
  • 의견페이지
    왜 일이 줄지 않는가
profile
화이탱!

0개의 댓글