CmmnCdService.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.its.op.service.database;
  2. import com.its.op.model.dto.database.CmmnCdDto;
  3. import com.its.op.model.entity.database.CmmnCd;
  4. import com.its.op.model.entity.database.CmmnCdKey;
  5. import com.its.op.repository.database.CmmnCdRepository;
  6. import lombok.RequiredArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.stereotype.Service;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Optional;
  12. @Slf4j
  13. @RequiredArgsConstructor
  14. @Service
  15. public class CmmnCdService {
  16. private final CmmnCdRepository repo;
  17. public List<CmmnCd> findAll() {
  18. try {
  19. return this.repo.findAll();
  20. }
  21. catch (Exception e) {
  22. log.error("{}.findAll: Exception: {}", getClass().getSimpleName(), e.getMessage());
  23. }
  24. return new ArrayList<>();
  25. }
  26. public List<CmmnCd> findAllByCmmnClsfCd(String CMMN_CLSF_CD) {
  27. try {
  28. return this.repo.findAllByCmmnClsfCd(CMMN_CLSF_CD);
  29. }
  30. catch (Exception e) {
  31. log.error("{}.findAllByCmmnClsfCd: Exception: {}", getClass().getSimpleName(), e.getMessage());
  32. }
  33. return new ArrayList<>();
  34. }
  35. public CmmnCd findAllByCmmnClsfCdCmmnCd(String CMMN_CLSF_CD, String CMMN_CD) {
  36. try {
  37. CmmnCd data = this.repo.findAllByCmmnClsfCdCmmnCd(CMMN_CLSF_CD, CMMN_CD);
  38. return data;
  39. }
  40. catch (Exception e) {
  41. log.error("{}.findById: Exception: {}", getClass().getSimpleName(), e.getMessage());
  42. }
  43. return null;
  44. }
  45. public CmmnCd mergeInfo(CmmnCdDto.CmmnCdUpdateReq req) {
  46. try {
  47. CmmnCdKey key = new CmmnCdKey(req.getCMMN_CLSF_CD(), req.getCMMN_CD());
  48. Optional<CmmnCd> data = this.repo.findById(key);
  49. CmmnCd newObj = data.orElseGet(() -> new CmmnCd(req.getCMMN_CLSF_CD(), req.getCMMN_CD()));
  50. newObj.updateInfo(req);
  51. this.repo.save(newObj);
  52. return newObj;
  53. }
  54. catch (Exception e) {
  55. log.error("{}.mergeInfo: Object: {}, Exception: {}", getClass().getSimpleName(), req, e.getMessage());
  56. }
  57. return null;
  58. }
  59. public CmmnCd deleteById(String id, String sub) {
  60. try {
  61. CmmnCdKey key = new CmmnCdKey(id, sub);
  62. Optional<CmmnCd> data = this.repo.findById(key);
  63. if (data.isPresent()) {
  64. CmmnCd obj = data.get();
  65. this.repo.deleteById(key);
  66. return obj;
  67. }
  68. }
  69. catch (Exception e) {
  70. log.error("{}.deleteById: Object: {}, Exception: {}", getClass().getSimpleName(), id, e.getMessage());
  71. }
  72. return null;
  73. }
  74. }