package com.its.op.service.database; import com.its.op.model.dto.database.CmmnCdDto; import com.its.op.model.entity.database.CmmnCd; import com.its.op.model.entity.database.CmmnCdKey; import com.its.op.repository.database.CmmnCdRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Optional; @Slf4j @RequiredArgsConstructor @Service public class CmmnCdService { private final CmmnCdRepository repo; public List findAll() { try { return this.repo.findAll(); } catch (Exception e) { log.error("{}.findAll: Exception: {}", getClass().getSimpleName(), e.getMessage()); } return new ArrayList<>(); } public List findAllByCmmnClsfCd(String CMMN_CLSF_CD) { try { return this.repo.findAllByCmmnClsfCd(CMMN_CLSF_CD); } catch (Exception e) { log.error("{}.findAllByCmmnClsfCd: Exception: {}", getClass().getSimpleName(), e.getMessage()); } return new ArrayList<>(); } public CmmnCd findAllByCmmnClsfCdCmmnCd(String CMMN_CLSF_CD, String CMMN_CD) { try { CmmnCd data = this.repo.findAllByCmmnClsfCdCmmnCd(CMMN_CLSF_CD, CMMN_CD); return data; } catch (Exception e) { log.error("{}.findById: Exception: {}", getClass().getSimpleName(), e.getMessage()); } return null; } public CmmnCd mergeInfo(CmmnCdDto.CmmnCdUpdateReq req) { try { CmmnCdKey key = new CmmnCdKey(req.getCMMN_CLSF_CD(), req.getCMMN_CD()); Optional data = this.repo.findById(key); CmmnCd newObj = data.orElseGet(() -> new CmmnCd(req.getCMMN_CLSF_CD(), req.getCMMN_CD())); newObj.updateInfo(req); this.repo.save(newObj); return newObj; } catch (Exception e) { log.error("{}.mergeInfo: Object: {}, Exception: {}", getClass().getSimpleName(), req, e.getMessage()); } return null; } public CmmnCd deleteById(String id, String sub) { try { CmmnCdKey key = new CmmnCdKey(id, sub); Optional data = this.repo.findById(key); if (data.isPresent()) { CmmnCd obj = data.get(); this.repo.deleteById(key); return obj; } } catch (Exception e) { log.error("{}.deleteById: Object: {}, Exception: {}", getClass().getSimpleName(), id, e.getMessage()); } return null; } }