feat : 디비폴링 2차완료. 폴링후 백업 로직 수정,

master
Kurt92 6 months ago
parent dd6f119332
commit c2a95e7c7f

@ -3,5 +3,9 @@ package com.worker.domain.repo.ep;
import com.worker.domain.entity.CpBdong;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface EpBdongRepository extends JpaRepository<CpBdong, Integer> {
List<CpBdong> findAllByBdCodeStartingWith(String string);
}

@ -7,4 +7,5 @@ import org.springframework.stereotype.Repository;
@Repository
public interface EpMainRepository extends JpaRepository<CpMain,Integer> {
CpMain findTopByMmCodeStartingWithOrderByMmCodeDesc(String prefix);
}

@ -23,11 +23,13 @@ public class SinmungoDto {
private CpSetinfo cpSetinfo;
private CpSetinfo epSetinfo;
private List<CpBdong> cpBdongList;
private List<CpBdong> epBdongList;
}
@Data
@Builder
public static class SinmungoXml {
private String fileName;
private String interface_seq_n;
private String sys_gubun_c;
private String anc_code_v;

@ -2,7 +2,7 @@ package com.worker.scheduler.smg.service;
import com.worker.domain.entity.*;
import com.worker.domain.repo.cp.*;
import com.worker.domain.repo.ep.EpSetinfoRepository;
import com.worker.domain.repo.ep.*;
import com.worker.dto.SinmungoDto;
import com.worker.util.common.CommonUtils;
import com.worker.util.common.commEnum.DateTimePatternEnum;
@ -24,7 +24,6 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.aspectj.asm.internal.ProgramElement.trim;
@Service
@RequiredArgsConstructor
@ -37,10 +36,14 @@ public class DbPolling {
private final CpSetinfoRepository cpSetinfoRepository;
private final EpSetinfoRepository epSetinfoRepository;
private final CpBdongRepository cpBdongRepository;
private final EpBdongRepository epBdongRepository;
private final CpMainRepository cpMainRepository;
private final EpMainRepository epMainRepository;
private final CpAnswerRepository cpAnswerRepository;
private final EpAnswerRepository epAnswerRepository;
private final CpMainEtc1Repository cpMainEtc1Repository;
private final EpMainEtc1Repository epMainEtc1Repository;
public SinmungoDto.SetInfo findSetInfo() {
@ -77,89 +80,164 @@ public class DbPolling {
@Transactional
public void saveCP(List<SinmungoDto.SinmungoXml> cpList, SinmungoDto.SetInfo setInfo) {
// job group 1 = 장애인 / 2 = 전기차
List<CpMain> cpMainList = new ArrayList<>();
List<CpAnswer> cpAnswerList = new ArrayList<>();
List<CpMainEtc1> cpMainEtc1List = new ArrayList<>();
//mm_code 맥스값 가져오기
String mmCode = getMaxMmCode(setInfo);
List<CpMain> cpMainList = cpList.stream()
.map(xml -> {
String sgpos = getSgPosByReason(setInfo, xml.getPeti_reason_l());
return CpMain.builder()
.mmCode(mmCode)
.mmSggcode(setInfo.getCpSetinfo().getIntValue1().toString())
.mmIngb("1") //접수구분
.mmDate(DateTimePatternEnum.DATE_ONLY.extractFirst(xml.getPeti_reason_l())) //신고날짜
.mmTime(DateTimePatternEnum.TIME_ONLY.extractFirst(xml.getPeti_reason_l())) //신고시간
.mmSgnm(xml.getPeter_name_v()) //신고자 이름
.mmSgtel(xml.getDuty_id_v()) //담당자 아이디
.mmSgcont(trim(xml.getPeti_reason_l())) // 신고내용
.mmSgpos(sgpos) //사고발생지역 주소추출
.mmBdcode(getBdcodeBySgpos(setInfo, sgpos)) //사고발생지역 코드
.mmImagecnt(Integer.parseInt(xml.getApndfilcount())) // 이미지 갯수
// .mmImagegb() //동영상여부
.mmCarno(null) // 차량번호
.mmLawgb(getLawGBByReason(xml.getPeti_reason_l()).getCode()) //위반항목
.mmKeum1(getLawGBByReason(xml.getPeti_reason_l()).getImKeum1()) // 감경금액 (자진납부 20퍼 감경)
.mmKeum2(getLawGBByReason(xml.getPeti_reason_l()).getImKeum2()) // 감경 후 금액 (본금액)
.mmInuser(0) //등록자
// .mmIndt() //등록일시
.mmState("01") //처리상태
.mmStateDt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))) //마지막 처리일시
.mmDlgb("2") //등록구분
.mmOmcode("0") //소유주테이블 코드 (논리fk)
.mmGpsX(getGpsByReason(xml.getPeti_reason_l(), "위도"))
.mmGpsY(getGpsByReason(xml.getPeti_reason_l(), "경도"))
.mmVideofilenm("" + "Z.mp4")
.build();
})
.collect(Collectors.toUnmodifiableList());
List<CpAnswer> cpAnswerList = cpList.stream()
.map(xml -> CpAnswer.builder()
.asMmcode(mmCode)
.asJsno(xml.getPeti_no_c())
.asJsnoM(xml.getCivil_no_c())
.asLimitDt(xml.getPeti_end_d())
.asUser(xml.getPeter_name_v())
.asSysGubunC(xml.getSys_gubun_c())
.asCell(xml.getCel_no_v())
.asTel(xml.getTel_no_v())
.build())
.collect(Collectors.toList());
long maxMmCode = getMaxMmCode(setInfo, "CP");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
for (int i = 0; i < cpList.size(); i++) {
SinmungoDto.SinmungoXml xml = cpList.get(i);
String sgpos = getSgPosByReason(setInfo, xml.getPeti_reason_l());
// CpMain
cpMainList.add(CpMain.builder()
.mmCode(String.valueOf(maxMmCode + i))
.mmSggcode(setInfo.getCpSetinfo().getIntValue1().toString())
.mmIngb("1")
.mmDate(DateTimePatternEnum.DATE_YYYYMMDD.extractFirst(xml.getPeti_reason_l()))
.mmTime(DateTimePatternEnum.TIME_HHMM.extractFirst(xml.getPeti_reason_l()))
.mmSgnm(xml.getPeter_name_v())
.mmSgtel(xml.getDuty_id_v())
.mmSgcont(CommonUtils.truncateUtf8(xml.getPeti_reason_l(), 1000))
.mmSgpos(sgpos)
.mmBdcode(getBdcodeBySgpos(setInfo, sgpos))
.mmImagecnt(Integer.parseInt(xml.getApndfilcount()))
.mmLawgb(getLawGBByReason(xml.getPeti_reason_l()).getCode())
.mmKeum1(getLawGBByReason(xml.getPeti_reason_l()).getImKeum1())
.mmKeum2(getLawGBByReason(xml.getPeti_reason_l()).getImKeum2())
.mmInuser(0)
.mmState("01")
.mmStateDt(LocalDateTime.now().format(dtf))
.mmDlgb("2")
.mmOmcode("0")
.mmGpsX(getGpsByReason(xml.getPeti_reason_l(), "위도"))
.mmGpsY(getGpsByReason(xml.getPeti_reason_l(), "경도"))
.mmVideofilenm("Z.mp4")
.build());
// CpAnswer
cpAnswerList.add(CpAnswer.builder()
.asMmcode(String.valueOf(maxMmCode + i))
.asJsno(xml.getPeti_no_c())
.asJsnoM(xml.getCivil_no_c())
.asLimitDt(DateTimePatternEnum.DATE_YYYYMMDD.extractFirst(xml.getPeti_end_d()))
.asUser(xml.getPeter_name_v())
.asSysGubunC(xml.getSys_gubun_c())
.asCell(xml.getCel_no_v())
.asTel(xml.getTel_no_v())
.build());
// CpMainEtc1
cpMainEtc1List.add(CpMainEtc1.builder()
.mmCode(String.valueOf(maxMmCode + i))
.mmJsdate(xml.getAnc_reg_d().substring(0, 8))
.mmKey(xml.getInterface_seq_n())
.mmText(CommonUtils.truncate(xml.getPeti_reason_l(), 4000))
.build());
}
List<CpMainEtc1> cpMainEtc1List = cpList.stream()
.map(xml -> CpMainEtc1.builder()
.mmCode(mmCode)
.mmJsdate(xml.getAnc_reg_d().substring(0, 8))
.mmKey(xml.getInterface_seq_n())
.mmText(CommonUtils.truncate(xml.getPeti_reason_l(), 4000))
.build())
.collect(Collectors.toList());
log.info(cpAnswerList.toString());
//
// cpMainRepository.saveAll(cpMainList);
// cpAnswerRepository.saveAll(cpAnswerList);
// cpMainEtc1Repository.saveAll(cpMainEtc1List);
// fileBackup(setInfo, cpList);
cpMainRepository.saveAll(cpMainList);
cpAnswerRepository.saveAll(cpAnswerList);
cpMainEtc1Repository.saveAll(cpMainEtc1List);
fileBackup(setInfo.getCpSetinfo(), cpList);
}
@Transactional
public void saveEP(List<SinmungoDto.SinmungoXml> epList, SinmungoDto.SetInfo setInfo) {
List<CpMain> cpMainList = new ArrayList<>();
List<CpAnswer> cpAnswerList = new ArrayList<>();
List<CpMainEtc1> cpMainEtc1List = new ArrayList<>();
List<String> insertCompleteFileNm = new ArrayList<>();
//mm_code 맥스값 가져오기
long maxMmCode = getMaxMmCode(setInfo, "EP");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
for (int i = 0; i < epList.size(); i++) {
SinmungoDto.SinmungoXml xml = epList.get(i);
String sgpos = getSgPosByReason(setInfo, xml.getPeti_reason_l());
cpMainList.add(CpMain.builder()
.mmCode(String.valueOf(maxMmCode + i))
.mmSggcode(setInfo.getCpSetinfo().getIntValue1().toString())
.mmIngb("1")
.mmDate(DateTimePatternEnum.DATE_YYYYMMDD.extractFirst(xml.getPeti_reason_l()))
.mmTime(DateTimePatternEnum.TIME_HHMM.extractFirst(xml.getPeti_reason_l()))
.mmSgnm(xml.getPeter_name_v())
.mmSgtel(xml.getDuty_id_v())
.mmSgcont(CommonUtils.truncateUtf8(xml.getPeti_reason_l(), 1000))
.mmSgpos(sgpos)
.mmBdcode(getBdcodeBySgpos(setInfo, sgpos))
.mmImagecnt(Integer.parseInt(xml.getApndfilcount()))
.mmLawgb(getLawGBByReason(xml.getPeti_reason_l()).getCode())
.mmKeum1(getLawGBByReason(xml.getPeti_reason_l()).getImKeum1())
.mmKeum2(getLawGBByReason(xml.getPeti_reason_l()).getImKeum2())
.mmInuser(0)
.mmState("01")
.mmStateDt(LocalDateTime.now().format(dtf))
.mmDlgb("2")
.mmOmcode("0")
.mmGpsX(getGpsByReason(xml.getPeti_reason_l(), "위도"))
.mmGpsY(getGpsByReason(xml.getPeti_reason_l(), "경도"))
.mmVideofilenm("Z.mp4")
.build());
// CpAnswer
cpAnswerList.add(CpAnswer.builder()
.asMmcode(String.valueOf(maxMmCode + i))
.asJsno(xml.getPeti_no_c())
.asJsnoM(xml.getCivil_no_c())
.asLimitDt(DateTimePatternEnum.DATE_YYYYMMDD.extractFirst(xml.getPeti_end_d()))
.asUser(xml.getPeter_name_v())
.asSysGubunC(xml.getSys_gubun_c())
.asCell(xml.getCel_no_v())
.asTel(xml.getTel_no_v())
.build());
// CpMainEtc1
cpMainEtc1List.add(CpMainEtc1.builder()
.mmCode(String.valueOf(maxMmCode + i))
.mmJsdate(xml.getAnc_reg_d().substring(0, 8))
.mmKey(xml.getInterface_seq_n())
.mmText(CommonUtils.truncate(xml.getPeti_reason_l(), 4000))
.build());
insertCompleteFileNm.add(epList.get(i).getFileName());
}
// fileBackup(setInfo, epList);
log.info(cpAnswerList.toString());
epMainRepository.saveAll(cpMainList);
epAnswerRepository.saveAll(cpAnswerList);
epMainEtc1Repository.saveAll(cpMainEtc1List);
fileBackup(setInfo.getEpSetinfo(), epList);
}
// xml파일백업 및 바이너리 이미지 저장
private void fileBackup(SinmungoDto.SetInfo setInfo, List<SinmungoDto.SinmungoXml> cpList) {
private void fileBackup(CpSetinfo setInfo, List<SinmungoDto.SinmungoXml> eachList) {
xmlReader.xmlFileBackup(Paths.get(setInfo.getCpSetinfo().getStrValue2()), Paths.get(setInfo.getCpSetinfo().getStrValue4()));
List<SinmungoDto.ImgParser> imgParsers = cpList.stream()
//xml 백업
xmlReader.xmlFileBackup(Paths.get(setInfo.getStrValue2()), Paths.get(setInfo.getStrValue4()), eachList);
List<SinmungoDto.ImgParser> imgParsers = eachList.stream()
.flatMap(result -> result.getImgParsers().stream())
.collect(Collectors.toList());
xmlReader.imgLocalSave(Paths.get(setInfo.getCpSetinfo().getStrValue5()), imgParsers);
//이미지 백업
xmlReader.imgLocalSave(Paths.get(setInfo.getStrValue5()), imgParsers);
}
/**
@ -167,16 +245,15 @@ public class DbPolling {
* MMCode MAX + 1
* 0000000
* */
private String getMaxMmCode(SinmungoDto.SetInfo setInfo) {
String prefix = setInfo.getCpSetinfo() + Integer.toString(Year.now().getValue());
CpMain cpMainMax = cpMainRepository.findTopByMmCodeStartingWithOrderByMmCodeDesc(prefix);
String maxCode;
if (cpMainMax != null) {
maxCode = String.valueOf(Integer.parseInt(cpMainMax.getMmCode()) + 1);
} else {
maxCode = prefix + "0000000";
}
return maxCode;
private long getMaxMmCode(SinmungoDto.SetInfo setInfo, String db) {
String prefix = setInfo.getCpSetinfo().getIntValue1() + Integer.toString(Year.now().getValue());
CpMain cpMainMax;
if(db.equals("CP")) cpMainMax = cpMainRepository.findTopByMmCodeStartingWithOrderByMmCodeDesc(prefix);
else cpMainMax = epMainRepository.findTopByMmCodeStartingWithOrderByMmCodeDesc(prefix);
if (cpMainMax != null) return Long.parseLong(cpMainMax.getMmCode());
else return Long.parseLong(prefix + "0000000");
}

@ -2,6 +2,7 @@ package com.worker.util.common;
import com.worker.util.common.commEnum.DateTimePatternEnum;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
@ -17,12 +18,32 @@ public class CommonUtils {
}
/**
*
* utf8 db varchar
* */
public static String truncateUtf8(String input, int maxBytes) {
if (input == null) return null;
int byteCount = 0;
int endIndex = 0;
for (int i = 0; i < input.length(); i++) {
String c = String.valueOf(input.charAt(i));
int len = c.getBytes(StandardCharsets.UTF_8).length;
if (byteCount + len > maxBytes) break;
byteCount += len;
endIndex = i + 1;
}
return input.substring(0, endIndex);
}
/**
*
* */

@ -12,8 +12,8 @@ import java.util.regex.Pattern;
* */
public enum DateTimePatternEnum {
DATETIME_FULL("\\d{4}[-/.]\\d{2}[-/.]\\d{2} \\d{2}:\\d{2}:\\d{2}", true),
DATE_ONLY("\\d{4}[-/.]\\d{2}[-/.]\\d{2}", true),
TIME_ONLY("\\d{2}:\\d{2}:\\d{2}", true);
DATE_YYYYMMDD("\\d{4}[-/.]\\d{2}[-/.]\\d{2}", true),
TIME_HHMM("\\d{2}:\\d{2}", true);
private final Pattern pattern;

@ -11,6 +11,8 @@ import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
@ -37,16 +39,22 @@ public class XmlReader {
return result;
}
public void xmlFileBackup(Path esbRcvPath, Path localXmlPath) {
public void xmlFileBackup(Path esbRcvPath, Path localXmlPath, List<SinmungoDto.SinmungoXml> eachList) {
try {
if (!Files.exists(localXmlPath)) {
log.info("디렉토리 생성 :" + localXmlPath);
Files.createDirectories(localXmlPath); // 백업 디렉토리가 없으면 생성
}
// eachList에 있는 fileName만 필터링
Set<String> fileNameSet = eachList.stream()
.map(SinmungoDto.SinmungoXml::getFileName)
.collect(Collectors.toSet());
try (Stream<Path> files = Files.list(esbRcvPath)) {
for (Path file : files.toList()) {
if (Files.isRegularFile(file)) {
if (Files.isRegularFile(file) && fileNameSet.contains(file.getFileName().toString())) {
Path target = localXmlPath.resolve(file.getFileName());
Files.move(file, target, StandardCopyOption.REPLACE_EXISTING);
log.info("파일 이동: {} → {}", file, target);

@ -22,50 +22,51 @@ public class SinmungoXmlParser implements XmlParserInterface<SinmungoDto.Sinmung
Element root = doc.getDocumentElement();
return SinmungoDto.SinmungoXml.builder()
.interface_seq_n(getTagValue("interface_seq_n", root))
.sys_gubun_c(getTagValue("sys_gubun_c", root))
.anc_code_v(getTagValue("anc_code_v", root))
.peti_gubun_c(getTagValue("peti_gubun_c", root))
.peti_anc_code_v(getTagValue("peti_anc_code_v", root))
.peti_no_c(getTagValue("peti_no_c", root))
.civil_no_c(getTagValue("civil_no_c", root))
.peter_name_v(getTagValue("peter_name_v", root))
.zip_code_c(getTagValue("zip_code_c", root))
.address_v(getTagValue("address_v", root))
.email_v(getTagValue("email_v", root))
.cel_no_v(getTagValue("cel_no_v", root))
.tel_no_v(getTagValue("", root))
.peti_title_v(getTagValue("tel_no_v", root))
.peti_reason_l(getTagValue("peti_reason_l", root))
.open_yn_c(getTagValue("open_yn_c", root))
.peti_reg_d(getTagValue("peti_reg_d", root))
.peti_reason_attach_yn_c(getTagValue("peti_reason_attach_yn_c", root))
.peti_file_size_n(getTagValue("peti_file_size_n", root))
.peti_file_path1_v(getTagValue("peti_file_path1_v", root))
.peti_file_path2_v(getTagValue("peti_file_path2_v", root))
.peti_file_path3_v(getTagValue("peti_file_path3_v", root))
.peti_file_path4_v(getTagValue("peti_file_path4_v", root))
.peti_file_path5_v(getTagValue("peti_file_path5_v", root))
.peti_file_path6_v(getTagValue("peti_file_path6_v", root))
.pcd_anc_code_v(getTagValue("pcd_anc_code_v", root))
.peti_proc_dur_n(getTagValue("peti_proc_dur_n", root))
.peti_end_d(getTagValue("peti_end_d", root))
.anc_reg_d(getTagValue("anc_reg_d", root))
.reg_d(getTagValue("reg_d", root))
.send_yn_c(getTagValue("send_yn_c", root))
.send_d(getTagValue("send_d", root))
.apply_d(getTagValue("apply_d", root))
.apply_gubun_c(getTagValue("apply_gubun_c", root))
.pcd_dept_v(getTagValue("pcd_dept_v", root))
.pcd_dept_nm_v(getTagValue("pcd_dept_nm_v", root))
.duty_id_v(getTagValue("duty_id_v", root))
.pcd_email_v(getTagValue("pcd_email_v", root))
.pcd_tel_v(getTagValue("pcd_tel_v", root))
.peti_path_gubun_c(getTagValue("peti_path_gubun_c", root))
.apndfilcount(getTagValue("apndfilcount", root))
// getImg 매서드로 바이너리 객체화
.imgParsers(getImg(root))
.build();
.fileName(file.getName())
.interface_seq_n(getTagValue("interface_seq_n", root))
.sys_gubun_c(getTagValue("sys_gubun_c", root))
.anc_code_v(getTagValue("anc_code_v", root))
.peti_gubun_c(getTagValue("peti_gubun_c", root))
.peti_anc_code_v(getTagValue("peti_anc_code_v", root))
.peti_no_c(getTagValue("peti_no_c", root))
.civil_no_c(getTagValue("civil_no_c", root))
.peter_name_v(getTagValue("peter_name_v", root))
.zip_code_c(getTagValue("zip_code_c", root))
.address_v(getTagValue("address_v", root))
.email_v(getTagValue("email_v", root))
.cel_no_v(getTagValue("cel_no_v", root))
.tel_no_v(getTagValue("", root))
.peti_title_v(getTagValue("tel_no_v", root))
.peti_reason_l(getTagValue("peti_reason_l", root))
.open_yn_c(getTagValue("open_yn_c", root))
.peti_reg_d(getTagValue("peti_reg_d", root))
.peti_reason_attach_yn_c(getTagValue("peti_reason_attach_yn_c", root))
.peti_file_size_n(getTagValue("peti_file_size_n", root))
.peti_file_path1_v(getTagValue("peti_file_path1_v", root))
.peti_file_path2_v(getTagValue("peti_file_path2_v", root))
.peti_file_path3_v(getTagValue("peti_file_path3_v", root))
.peti_file_path4_v(getTagValue("peti_file_path4_v", root))
.peti_file_path5_v(getTagValue("peti_file_path5_v", root))
.peti_file_path6_v(getTagValue("peti_file_path6_v", root))
.pcd_anc_code_v(getTagValue("pcd_anc_code_v", root))
.peti_proc_dur_n(getTagValue("peti_proc_dur_n", root))
.peti_end_d(getTagValue("peti_end_d", root))
.anc_reg_d(getTagValue("anc_reg_d", root))
.reg_d(getTagValue("reg_d", root))
.send_yn_c(getTagValue("send_yn_c", root))
.send_d(getTagValue("send_d", root))
.apply_d(getTagValue("apply_d", root))
.apply_gubun_c(getTagValue("apply_gubun_c", root))
.pcd_dept_v(getTagValue("pcd_dept_v", root))
.pcd_dept_nm_v(getTagValue("pcd_dept_nm_v", root))
.duty_id_v(getTagValue("duty_id_v", root))
.pcd_email_v(getTagValue("pcd_email_v", root))
.pcd_tel_v(getTagValue("pcd_tel_v", root))
.peti_path_gubun_c(getTagValue("peti_path_gubun_c", root))
.apndfilcount(getTagValue("apndfilcount", root))
// getImg 매서드로 바이너리 객체화
.imgParsers(getImg(root))
.build();
}

Loading…
Cancel
Save