package com.dbnt.kcscbackend.commonCode.repository;

import com.dbnt.kcscbackend.commonCode.entity.TnCmtOrg;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.query.Procedure;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

public interface TnCmtOrgRepository extends JpaRepository<TnCmtOrg, TnCmtOrg.TnCmtOrgId> {
  List<TnCmtOrg> findByUseYnAndUpCmtSeqOrderByCmtOrder(String useYn, Long upCmtSeq);
  TnCmtOrg findByUseYnAndCmtSeq(String useYn, Long cmtSeq);
  List<TnCmtOrg> findByUseYnAndUpCmtSeqAndCmtTypeOrderByCmtOrder(String useYn, Long upCmtSeq, String cmtType);


  @Query(value = "CALL sp_add_tn_cmt_org (" +
          ":_cmt_nm, " +
          ":_cmt_type, " +
          ":_cmt_desc, " +
          ":_up_cmt_seq, " +
          ":_cmt_order, " +
          ":_modi_id, " +
          ":_cmt_seq, " +
          ":_result_count, " +
          ":_result_code, " +
          ":_error_message)",
          nativeQuery = true)
  Map<String, Object> spAddTnCmtOrg(
          @Param("_cmt_nm")         String          cmtNm,
          @Param("_cmt_type")       String          cmtType,
          @Param("_cmt_desc")       String          cmtDesc,
          @Param("_up_cmt_seq")     Integer         upCmtSeq,
          @Param("_cmt_order")      Integer         cmtOrder,
          @Param("_modi_id")        String          modiId,
          @Param("_cmt_seq")        Integer         evtSeq,
          @Param("_result_count")   Integer         resultCount,
          @Param("_result_code")    String          resultCode,
          @Param("_error_message")  String          errorMessage
  );


  @Query(value = "CALL sp_update_tn_cmt_org (" +
          ":_cmt_seq, " +
          ":_cmt_nm, " +
          ":_cmt_desc, " +
          ":_cmt_order, " +
          ":_modi_id, " +
          ":_result_count, " +
          ":_result_code, " +
          ":_error_message)",
          nativeQuery = true)
  Map<String, Object> spUpdateTnCmtOrg(
          @Param("_cmt_seq")          Integer       cmtSeq,
          @Param("_cmt_nm")           String       cmtNm,
          @Param("_cmt_desc")         String       cmtDesc,
          @Param("_cmt_order")        Integer       cmtOrder,
          @Param("_modi_id")          String        modiId,
          @Param("_result_count")     Integer       resultCount,
          @Param("_result_code")      String        resultCode,
          @Param("_error_message")    String        errorMessage
  );


  @Query(value = "CALL sp_delete_tn_cmt_org (" +
          ":_cmt_seq, " +
          ":_modi_id, " +
          ":_result_count, " +
          ":_result_code, " +
          ":_error_message)",
          nativeQuery = true)
  Map<String, Object> spDeleteTnCmtOrg(
          @Param("_cmt_seq")          Integer       cmtSeq,
          @Param("_modi_id")          String        modiId,
          @Param("_result_count")     Integer       resultCount,
          @Param("_result_code")      String        resultCode,
          @Param("_error_message")    String        errorMessage
  );

  @Procedure("sp_is_valid_tn_cmt_org_id")
  int spIsValidTnCmtOrgId( Integer       cmtSeq );

  TnCmtOrg findByCmtSeq(Long cmtSeq);
  List<TnCmtOrg> findByUpCmtSeq(Long upCmtSeq);


}
