package itn.let.kakao.kakaoComm.kakaoApi; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.text.SimpleDateFormat; import java.util.Date; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import itn.let.kakao.kakaoComm.KakaoButtonVO; import itn.let.kakao.kakaoComm.KakaoReturnVO; import itn.let.kakao.kakaoComm.KakaoVO; @Component public class KakaoFTJsonSave { @Autowired KakaoApiTemplate kakaoApiTemplate; /** 비즈 JSON 파일저장 경로*/ @Value("#{globalSettings['Globals.mjon.kakao.dir']}") private String mjonBizJsonDir; static String json; @SuppressWarnings("unchecked") public String kakaoApiJsonSave(KakaoVO kakaoVO, String[] varValInfo) { // json파일 저장 Date nowDate = new Date(); SimpleDateFormat todayFrom = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat timeFrom = new SimpleDateFormat("HHmmss"); String jsonFileName = mjonBizJsonDir+"/"+kakaoVO.getUserId()+"/"+todayFrom.format(nowDate)+"/"+kakaoVO.getSendType(); // 아이디/날짜/타입 String fileName = timeFrom.format(nowDate)+"_"+kakaoVO.getDestPhone()+".json"; try { File userIdFile = new File(jsonFileName); if(!userIdFile.exists()) { userIdFile.mkdirs(); // 없으면 하위 디렉토리 까지 생성 jsonFileName = jsonFileName +"/"+fileName; }else { jsonFileName = jsonFileName +"/"+fileName; System.out.println("jsonFileName : "+jsonFileName); File file1 = new File(jsonFileName); if (file1.isFile()) { return jsonFileName; } } KakaoReturnVO templateDetail = kakaoApiTemplate.selectKakaoApiTemplateDetail(kakaoVO); // 버튼리스트 JSON 생성 JSONArray buttonList = new JSONArray(); // 버튼 수량 체크 후 진행 for(KakaoButtonVO buttonInfoVO : templateDetail.getButtonList()) { JSONObject buttonInfo = new JSONObject(); buttonInfo.put("name", buttonInfoVO.getName()); buttonInfo.put("type", buttonInfoVO.getLinkType()); if(buttonInfoVO.getLinkType().equals("WL")) { buttonInfo.put("url_mobile", buttonInfoVO.getLinkMo()); buttonInfo.put("url_pc", buttonInfoVO.getLinkPc()); }else if(buttonInfoVO.getLinkType().equals("AL")) { buttonInfo.put("scheme_ios", buttonInfoVO.getLinkIos()); buttonInfo.put("scheme_android", buttonInfoVO.getLinkAnd()); }else if(buttonInfoVO.getLinkType().equals("BC")) { // 상담톡 진행시 등록해야함 }else if(buttonInfoVO.getLinkType().equals("BT")) { // 봇 전환 시 전달 } buttonList.add(buttonInfo); } // Image JSON 생성 JSONObject imageJson = new JSONObject(); // img형과 wide 형인경우만 등록 (if문으로 제어) imageJson.put("img_url", "등록된 이미지 URL"); imageJson.put("img_link", "이동 페이지 URL"); // Wide JSON 생성 JSONObject wideJson = new JSONObject(); // wide 형인경우만 등록(if문으로 제어) wideJson.put("wide", "Y"); JSONObject jo = new JSONObject(); if(imageJson.size() != 0) { jo.put("image", imageJson); } if(wideJson.size() != 0) { jo.put("extra", wideJson); } if(buttonList.size() != 0) { jo.put("button", buttonList); } // 입력 json 데이터를 파일로 변경 String jsonStr = jo.toString(); System.out.println("jsonFileName : "+jsonFileName); File outPut = new File(jsonFileName); outPut.createNewFile(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPut), "euc-kr")); bw.write(jsonStr); bw.close(); } catch (IOException e) { System.out.println("json 생성 실패"); e.printStackTrace(); } return jsonFileName; } @SuppressWarnings("unchecked") public String kakaoApiJsonSave(KakaoVO kakaoVO) { // json파일 저장 Date nowDate = new Date(); SimpleDateFormat todayFrom = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat timeFrom = new SimpleDateFormat("HHmmss"); String jsonFileName = mjonBizJsonDir+"/"+kakaoVO.getUserId()+"/"+todayFrom.format(nowDate)+"/"+kakaoVO.getSendType(); // 아이디/날짜/타입 String fileName = timeFrom.format(nowDate)+"_"+kakaoVO.getUserId()+".json"; try { File userIdFile = new File(jsonFileName); if(!userIdFile.exists()) { userIdFile.mkdirs(); // 없으면 하위 디렉토리 까지 생성 jsonFileName = jsonFileName +"/"+fileName; }else { jsonFileName = jsonFileName +"/"+fileName; System.out.println("jsonFileName : "+jsonFileName); File file1 = new File(jsonFileName); if (file1.isFile()) { return jsonFileName; } } // 버튼리스트 JSON 생성 JSONArray buttonList = new JSONArray(); // 버튼 수량 체크 후 진행 for(KakaoButtonVO buttonInfoVO : kakaoVO.getButtonVOList()) { JSONObject buttonInfo = new JSONObject(); buttonInfo.put("name", buttonInfoVO.getName()); buttonInfo.put("type", buttonInfoVO.getLinkType()); if(buttonInfoVO.getLinkType().equals("WL")) { buttonInfo.put("url_mobile", buttonInfoVO.getLinkMo()); buttonInfo.put("url_pc", buttonInfoVO.getLinkPc()); }else if(buttonInfoVO.getLinkType().equals("AL")) { buttonInfo.put("scheme_ios", buttonInfoVO.getLinkIos()); buttonInfo.put("scheme_android", buttonInfoVO.getLinkAnd()); }else if(buttonInfoVO.getLinkType().equals("BC")) { // 상담톡 진행시 등록해야함 }else if(buttonInfoVO.getLinkType().equals("BT")) { // 봇 전환 시 전달 } buttonList.add(buttonInfo); } // Image JSON 생성 JSONObject imageJson = new JSONObject(); // img형과 wide 형인경우만 등록 (if문으로 제어) String imgUrl = kakaoVO.getTemplateImageUrl(); String imgLink = kakaoVO.getImgLink(); if(!imgUrl.equals("") && imgUrl != null) { imageJson.put("img_url", imgUrl); imageJson.put("img_link", imgLink); } // Wide JSON 생성 JSONObject wideJson = new JSONObject(); // wide 형인경우만 등록(if문으로 제어) String imgType = kakaoVO.getImageType(); if(imgType != null && !imgType.equals("")) { wideJson.put("wide", imgType); } JSONObject jo = new JSONObject(); if(imageJson.size() != 0) { jo.put("image", imageJson); } if(wideJson.size() != 0) { jo.put("extra", wideJson); } if(buttonList.size() != 0) { jo.put("button", buttonList); } // 입력 json 데이터를 파일로 변경 String jsonStr = jo.toString(); System.out.println("jsonFileName : "+jsonFileName); File outPut = new File(jsonFileName); outPut.createNewFile(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPut), "euc-kr")); bw.write(jsonStr); bw.close(); } catch (IOException e) { System.out.println("json 생성 실패"); e.printStackTrace(); } return jsonFileName; } }