package com.dbnt.kcscbackend.admin.config.service;

import com.dbnt.kcscbackend.admin.config.entity.TcCodeGrp;
import com.dbnt.kcscbackend.admin.config.entity.TcCodeItem;
import com.dbnt.kcscbackend.admin.config.repository.TcCodeGrpRepository;
import com.dbnt.kcscbackend.admin.config.repository.TcCodeItemRepository;
import lombok.RequiredArgsConstructor;
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminConfigService extends EgovAbstractServiceImpl {

  private final TcCodeGrpRepository codeGrpRepository;
  private final TcCodeItemRepository codeItemRepository;

  public List<TcCodeGrp> selectCodeGrpList(){
    return codeGrpRepository.findByUseYn("Y");
  }

  @Transactional
  public String addCodeGrp(TcCodeGrp codeGrp) {
    TcCodeGrp savedGrp = codeGrpRepository.findById(codeGrp.getGrpCd()).orElse(null);
    if(savedGrp!=null){
      if(savedGrp.getUseYn().equals("Y")){
        return "isSaved";
      }else{
        savedGrp.setGrpCdNm(codeGrp.getGrpCdNm());
        savedGrp.setUseYn("Y");
        codeGrpRepository.save(savedGrp);
        return savedGrp.getGrpCd();
      }
    }else{
      codeGrpRepository.save(codeGrp);
      return codeGrp.getGrpCd();
    }
  }

  @Transactional
  public String modifyCodeGrp(TcCodeGrp codeGrp) {
    TcCodeGrp savedGrp = codeGrpRepository.findById(codeGrp.getGrpCd()).orElse(null);
    if (savedGrp != null) {
      savedGrp.setGrpCdNm(codeGrp.getGrpCdNm());
      savedGrp.setLastChgId(codeGrp.getLastChgId());
      savedGrp.setLastChgDt(codeGrp.getLastChgDt());
      savedGrp.setUseYn(codeGrp.getUseYn());
      codeGrpRepository.save(savedGrp);
      return "modified";
    }else{
      return "validGrpCd";
    }
  }

  public List<TcCodeItem> selectCodeItemList(String grpCd){
    return codeItemRepository.findByGrpCdAndUseYnOrderByGrpOrder(grpCd, "Y");
  }
  @Transactional
  public String addCodeItem(TcCodeItem codeItem) {
    TcCodeItem savedItem = codeItemRepository.findById(
        new TcCodeItem.TcCodeItemId(codeItem.getItemCd(), codeItem.getGrpCd())
    ).orElse(null);
    if(savedItem!=null){
      if(savedItem.getUseYn().equals("Y")){
        return "isSaved";
      }else{
        savedItem.setItemNm(savedItem.getItemNm());
        savedItem.setUseYn("Y");
        codeItemRepository.save(savedItem);
        return savedItem.getGrpCd();
      }
    }else{
      codeItemRepository.save(codeItem);
      return codeItem.getItemCd();
    }
  }

  @Transactional
  public String modifyCodeItem(TcCodeItem codeItem) {
    TcCodeItem savedItem = codeItemRepository.findById(
        new TcCodeItem.TcCodeItemId(codeItem.getItemCd(), codeItem.getGrpCd())
    ).orElse(null);
    if (savedItem != null) {
      savedItem.setItemNm(codeItem.getItemNm());
      savedItem.setLastChgId(codeItem.getLastChgId());
      savedItem.setLastChgDt(codeItem.getLastChgDt());
      savedItem.setGrpOrder(codeItem.getGrpOrder());
      savedItem.setUseYn(codeItem.getUseYn());
      codeItemRepository.save(savedItem);
      return "modified";
    }else{
      return "validGrpCd";
    }
  }
}
