package com.its.op.service.database; import com.its.op.model.dto.RoadDto; import com.its.op.model.dto.RoadIfscRltnDto; import com.its.op.model.entity.Road; import com.its.op.model.entity.RoadIfscRltn; import com.its.op.model.entity.RoadIfscRltnKey; import com.its.op.repository.RoadIfscRltnRepository; import com.its.op.repository.RoadRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.util.ArrayList; import java.util.List; import java.util.Optional; @Slf4j @RequiredArgsConstructor @Service public class TbRoadService { private final RoadRepository repo; private final RoadIfscRltnRepository repoRltn; public List findAll() { try { return this.repo.findAll(); } catch (Exception e) { log.error("{}.findAll: Exception: {}", getClass().getSimpleName(), e.getMessage()); } return new ArrayList(); } public Road findById(Long id) { try { Optional data = this.repo.findById(id); if (data.isPresent()) { return data.get(); } } catch (Exception e) { log.error("{}.findById: Object: {}, Exception: {}", getClass().getSimpleName(), id, e.getMessage()); } return null; } // 도로명 변경 public Road updateNameById(Long id, RoadDto.RoadNameUpdateReq req) { try { Optional data = this.repo.findById(id); if (data.isPresent()) { Road obj = data.get(); obj.updateName(req); this.repo.save(obj); return obj; } } catch (Exception e) { log.error("{}.updateNameById: Object: {}, Exception: {}", getClass().getSimpleName(), req, e.getMessage()); } return null; } public List findLinkRltnById(Long id) { try { List data = this.repoRltn.findRltnById(id); return data; } catch (Exception e) { log.error("{}.findById: Object: {}, Exception: {}", getClass().getSimpleName(), id, e.getMessage()); } return null; } @Transactional public List updateLinkRltn(Long id, List req) { try { // 기존 데이터를 모두 삭제 List lists = this.repoRltn.findRltnById(id); for (RoadIfscRltn delObj : lists) { this.repoRltn.deleteById(new RoadIfscRltnKey(delObj.getROAD_ID(), delObj.getIFSC_ID(), delObj.getORD())); } // 데이터 다시 입력 for (RoadIfscRltnDto.RoadIfscRltnUpdateReq obj : req) { RoadIfscRltn data = new RoadIfscRltn(obj.getROAD_ID(), obj.getIFSC_ID(), obj.getORD(), null); this.repoRltn.save(data); } return req; } catch (Exception e) { log.error("{}.updateLinkRltn: Object: {}, {}, Exception: {}", getClass().getSimpleName(), id, req, e.getMessage()); } return null; } }