package com.dbnt.kcscbackend.standardCode.service;

import com.dbnt.kcscbackend.standardCode.entity.TnDocumentCodeList;
import com.dbnt.kcscbackend.standardCode.entity.TnDocumentContent;
import com.dbnt.kcscbackend.standardCode.entity.TnDocumentInfo;
import com.dbnt.kcscbackend.standardCode.mapper.StandardCodeMapper;
import com.dbnt.kcscbackend.standardCode.repository.TnDocumentContentRepository;
import com.dbnt.kcscbackend.standardCode.repository.TnDocumentGroupRepository;
import com.dbnt.kcscbackend.standardCode.repository.TnDocumentInfoRepository;
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 StandardCodeService extends EgovAbstractServiceImpl {

  private final TnDocumentGroupRepository tnDocumentGroupRepository;
  private final TnDocumentContentRepository tnDocumentContentRepository;
  private final TnDocumentInfoRepository tnDocumentInfoRepository;
  private final StandardCodeMapper standardCodeMapper;

  @Transactional
  public void saveErrorCd(TnDocumentContent content) {
    TnDocumentContent saveContent = tnDocumentContentRepository.findByDocContSeq(content.getDocContSeq()).orElse(null);
    if(saveContent!=null){
      saveContent.setErrorCd(content.getErrorCd());
    }
  }

  public List<StandardCodeTreeInterface> selectStandardCodeTree(){
    return tnDocumentGroupRepository.spGetTnDocumentCodeByTree();
  }

  public List<StandardCodeContentInterface> selectStandardCodeDocument(StandardCodeVO param) {
    return tnDocumentContentRepository.spGetRecentFullContextByContent(param.getDocCode(), param.getDocPart(), param.getYmd());
  }

  public List<TnDocumentInfoInterface> selectStandardCodeDocInfo(StandardCodeVO param) {
    return tnDocumentInfoRepository.spGetTnDocumentInfoByGroupCd(param.getDocCode());
  }

  public List<TnDocumentCodeList> selectStandardCodeList(TnDocumentInfo tnDocumentInfo){ return standardCodeMapper.selectStandardCodeList(tnDocumentInfo); }

  public Integer selectStandardCodeListCnt(TnDocumentInfo tnDocumentInfo) {
    return standardCodeMapper.selectStandardCodeListCnt(tnDocumentInfo);
  }

  public List<TnDocumentInfo> selectStandardCodeRevisionhistoryList(Integer groupseq) {
    return standardCodeMapper.selectStandardCodeRevisionhistoryList(groupseq);
  }

  public Integer selectStandardCodeGroupSeq(String groupFullCd) { return standardCodeMapper.selectStandardCodeGroupSeq(groupFullCd); }

  public List<TnDocumentCodeList> selectStandardCodeCategoryList(Integer parentGroupSeq){ return standardCodeMapper.selectStandardCodeCategoryList(parentGroupSeq); }

}
