package com.interplug.qcast.biz.commCode;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.interplug.qcast.biz.commCode.dto.CodeReq;
import com.interplug.qcast.biz.commCode.dto.CodeRes;
import com.interplug.qcast.biz.commCode.dto.CommCodeRes;
import com.interplug.qcast.biz.commCode.dto.DetailCodeRequest;
import com.interplug.qcast.biz.commCode.dto.HeadCodeRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class CommCodeService {

  private final CommCodeMapper commCodeMapper;

  public int setCommHUpdate(CodeReq codeReq) {
    int resultCnt = 0;
    if (!codeReq.getApiHeadCdList1().isEmpty()) {
      for (CodeRes codeRes : codeReq.getApiHeadCdList1()) {
        if ("Y".equals(codeRes.getQcCommYn())) {
          codeRes.setDelFlg(0);
        } else {
          codeRes.setDelFlg(1);
        }
        resultCnt += commCodeMapper.setCommHUpdate(codeRes);
      }
    }

    if (!codeReq.getApiCommCdList1().isEmpty()) {

      for (CodeRes codeRes : codeReq.getApiCommCdList1()) {
        if ("A".equals(codeRes.getClStatCd())) {
          codeRes.setClDelFlg(0);
        } else {
          codeRes.setClDelFlg(1);
        }
        resultCnt += commCodeMapper.setCommLUpdate(codeRes);
      }
    }
    return resultCnt;
  }

  /**
   * QCast에서 사용하는 공통코드 조회
   *
   * @return
   */
  public List<CommCodeRes> selectQcastCommCode() {
    List<CodeRes> result = commCodeMapper.selectQcastCommCode();
    List<CommCodeRes> commCodeList = new ArrayList<>();
    result.forEach(cr -> {
      commCodeList.add(CommCodeRes.builder().clHeadCd(cr.getClHeadCd()).clCode(cr.getClCode())
          .clCodeNm(cr.getClCodeNm()).clCodeJp(cr.getClCodeJp()).clPriority(cr.getClPriority())
          .build());
    });
    return commCodeList;
  }


  /**
   * 헤더코드 동기화
   * 
   * @param headCodeList
   * @throws Exception
   */
  public void setHeaderCodeSyncSave(List<HeadCodeRequest> headCodeList) throws Exception {
    // 헤더코드 동기화
    for (HeadCodeRequest headCodeReq : headCodeList) {
      try {
        if ("Y".equals(headCodeReq.getQcCommYn()) && "N".equals(headCodeReq.getDelYn())) {
          headCodeReq.setDelFlg(0);
        } else {
          headCodeReq.setDelFlg(1);
        }
        commCodeMapper.setHeadCodeSyncSave(headCodeReq);
      } catch (Exception e) {
        log.error(e.getMessage());
      }
    }
  }

  /**
   * 상세코드 동기화
   * 
   * @param detailCodeList
   * @throws Exception
   */
  public void setCommonCodeSyncSave(List<DetailCodeRequest> detailCodeList) throws Exception {
    // 상세코드 동기화
    for (DetailCodeRequest detailCodeReq : detailCodeList) {
      try {
        if ("A".equals(detailCodeReq.getStatCd()) && "N".equals(detailCodeReq.getDelYn())) {
          detailCodeReq.setDelFlg(0);
        } else {
          detailCodeReq.setDelFlg(1);
        }
        commCodeMapper.setCommonCodeSyncSave(detailCodeReq);
      } catch (Exception e) {
        log.error(e.getMessage());
      }
    }
  }
}
