/************************************************************ * * @packageName : io.company.localhost.service * @fileName : worddictyService.java * @author : 공현지 * @date : 25.01.07 * @description : * * =========================================================== * DATE AUTHOR NOTE * ----------------------------------------------------------- * 24.12.06 공현지 최초 생성 * *************************************************************/ package io.company.localhost.service; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.company.localhost.common.dto.MapDto; import io.company.localhost.mapper.commoncodMapper; import io.company.localhost.mapper.worddictyMapper; import io.company.localhost.utils.BlobUtil; import lombok.RequiredArgsConstructor; @Service @RequiredArgsConstructor public class worddictyService { private final worddictyMapper worddictymapper; private final commoncodMapper commoncodmapper; private final FileService fileService; public List selectWordList(MapDto map) { if( map.getString("pageNum") != null) { String pageNumStr = map.getString("pageNum"); int pageNum = Integer.parseInt(pageNumStr); map.put("pageNum", pageNum); } List wordList = worddictymapper.selectWordList(map); String keyword = map.getString("searchKeyword"); List processedList = new ArrayList<>(); // 데이터 가공 for (MapDto dto : wordList) { MapDto author = new MapDto(); author.put("profileImage", dto.remove("REGPRF")); author.put("name", dto.remove("REGNAME")); author.put("color", dto.remove("REGCOLOR")); author.put("createdAt", dto.remove("REGRDT")); MapDto lastEditor = new MapDto(); lastEditor.put("profileImage", dto.remove("UPDPRF")); lastEditor.put("name", dto.remove("UPDNAME")); lastEditor.put("color", dto.remove("UPDCOLOR")); lastEditor.put("updatedAt", dto.remove("UPDUDT")); dto.put("author", author); dto.put("lastEditor", lastEditor); MapDto processedDto = new MapDto(); processedDto.putAll(dto); processedList.add(processedDto); } return filterPostsByKeyword(processedList,keyword); } /** * 검색어로 순수 텍스트 기준 게시글 필터링 */ private List filterPostsByKeyword(List posts, String keyword) { keyword = keyword.trim(); List filtered = new ArrayList<>(); for (MapDto post : posts) { String title = post.getString("WRDDICTTL"); Object content = post.get("WRDDICCON"); // BlobUtil.procBlobToString으로 content 변환 String contentString = BlobUtil.procBlobToString(content); if(keyword != null || !keyword.trim().isEmpty()) { String plainText = extractPlainText(contentString); if ((title != null && title.contains(keyword)) ||plainText.contains(keyword)) { post.put("WRDDICCON", contentString); // content는 원래대로 복원 filtered.add(post); } } } return filtered; } public String extractPlainText(String deltaJson) { StringBuilder sb = new StringBuilder(); ObjectMapper mapper = new ObjectMapper(); try { JsonNode root = mapper.readTree(deltaJson); if (root != null && root.isArray()) { for (JsonNode op : root) { JsonNode insertNode = op.get("insert"); // insert가 문자열일 경우만 추출 if (insertNode != null && insertNode.isTextual()) { String text = insertNode.asText(); if (!"\n".equals(text.trim())) { // 줄바꿈 제외 sb.append(text).append(" "); } } // insert가 객체면 image일 가능성 → 제외 } } } catch (Exception e) { e.printStackTrace(); // 실제 운영에서는 로깅 처리 } return sb.toString().trim(); } public Long insertWord(MapDto map) { Long result = 1L; if (map.containsKey("CMNCODNAM") && map.get("CMNCODNAM") != null && map.get("CMNCODNAM") != "") { commoncodmapper.insertCategory(map); result = 2L; } worddictymapper.insertWord(map); //에디터 첨부 이미지 게시글 번호 업데이트 if(map.get("editorUploadedImgList") != null) { ArrayList editorUploadedImgList = (ArrayList) map.getList("editorUploadedImgList", String.class); map.put("editorImgList", editorUploadedImgList); this.updateBoardIndexToFile(map); } // 에디터 수정 시 업로드 된 에디터 이미지 삭제 if(map.get("editorDeleteImgList") != null) { ArrayList editorDeleteImgList = (ArrayList) map.getList("editorDeleteImgList", String.class); String[] array = editorDeleteImgList.stream().toArray(String[]::new); this.deleteFileAndData(array); } return result; } /** * 실제 파일 삭제 및 db 데이터 제거 * * @param array */ private void deleteFileAndData(String[] array) { List delListInfo = this.selectDelFileInfo(array); // 삭제할 파일 정보 조회 for(String item : delListInfo) { fileService.removeFile(item); // 파일 삭제 } this.deleteFileInfo(array); } /** * 삭제 첨부파일 정보 조회 * * @param array * @return */ private List selectDelFileInfo(String[] array) { return worddictymapper.selectDelFileInfo(array); } /** * 첨부파일 데이터 삭제 * * @param array */ private void deleteFileInfo(String[] array) { worddictymapper.deleteFileInfo(array); } private int updateBoardIndexToFile(MapDto map) { return worddictymapper.updateBoardIndexToFile(map); } public Long updateWord(MapDto map) { Long result = worddictymapper.updateWord(map); if(result == 1) { map.put("id", map.get("WRDDICSEQ")); //에디터 첨부 이미지 게시글 번호 업데이트 if(map.get("editorUploadedImgList") != null) { ArrayList editorUploadedImgList = (ArrayList) map.getList("editorUploadedImgList", String.class); map.put("editorImgList", editorUploadedImgList); this.updateBoardIndexToFile(map); } // 에디터 수정 시 업로드 된 에디터 이미지 삭제 if(map.get("editorDeleteImgList") != null) { ArrayList editorDeleteImgList = (ArrayList) map.getList("editorDeleteImgList", String.class); String[] array = editorDeleteImgList.stream().toArray(String[]::new); this.deleteFileAndData(array); } } return result; } public MapDto selectWordDetail(MapDto map) { return worddictymapper.selectWordDetail(map); } public int getTotal(MapDto map) { return worddictymapper.getTotal(map); } public Long updateword(MapDto map) { return worddictymapper.updateword(map); } public List selectIndexCategory() { return worddictymapper.selectIndexCategory(); } }