package com.dbnt.kcscbackend.commonCode.repository;

import com.dbnt.kcscbackend.admin.committee.entity.TnCmtDraft;
import com.dbnt.kcscbackend.commonCode.entity.TnPopupMng;
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 TnCmtDraftRepository extends JpaRepository<TnCmtDraft, Long> {

    @Query(value = "CALL sp_add_tn_cmt_draft (" +
            ":_drft_title, " +
            ":_drft_type_cd, " +
            ":_drft_dt, " +
            ":_cmt_seq, " +
            ":_measure_pre_data_file_grp_id, " +
            ":_measure_pre_form_file_grp_id, " +
            ":_measure_partner_file_grp_id, " +
            ":_drft_confe_charger, " +
            ":_drft_confe_pw, " +
            ":_drft_confe_room, " +
            ":_drft_stat_cd, " +
            ":_drft_summery, " +
            ":_measure_plan_file_grp_id, " +
            ":_measure_result_file_grp_id, " +
            ":_start_dt, " +
            ":_end_dt, " +
            ":_modi_id, " +
            ":_drft_seq, " +
            ":_result_count, " +
            ":_result_code, " +
            ":_error_message)",
            nativeQuery = true)
    Map<String, Object> spAddTnCmtDraft(
            @Param("_drft_title")                       String          drftTitle,
            @Param("_drft_type_cd")                     Integer         drftTypeCd,
            @Param("_drft_dt")                          String          drftDt,
            @Param("_cmt_seq")                          Integer         cmtSeq,
            @Param("_measure_pre_data_file_grp_id")     String          measurePreDataFileGrpId,
            @Param("_measure_pre_form_file_grp_id")     String          measurePreFormFileGrpId,
            @Param("_measure_partner_file_grp_id")      String          measurePartnerFileGrpId,
            @Param("_drft_confe_charger")               String          drftConfeCharger,
            @Param("_drft_confe_pw")                    String          drftConfePw,
            @Param("_drft_confe_room")                  String          drftConfeRoom,
            @Param("_drft_stat_cd")                     String          drftStatCd,
            @Param("_drft_summery")                     String          drftSummery,
            @Param("_measure_plan_file_grp_id")         String          measurePlanFileGrpId,
            @Param("_measure_result_file_grp_id")       String          measureResultFileGrpId,
            @Param("_start_dt")                         String          startDt,
            @Param("_end_dt")                           String          endDt,
            @Param("_modi_id")                          String          modiId,
            @Param("_drft_seq")                         Integer         drftSeq,
            @Param("_result_count")                     Integer         resultCount,
            @Param("_result_code")                      String          resultCode,
            @Param("_error_message")                    String          errorMessage
    );


    @Query(value = "CALL sp_update_tn_cmt_draft (" +
            ":_drft_seq, " +
            ":_drft_title, " +
            ":_doc_info_seq, " +
            ":_drft_type_cd, " +
            ":_drft_dt, " +
            ":_cmt_seq, " +
            ":_measure_pre_data_file_grp_id, " +
            ":_measure_pre_form_file_grp_id, " +
            ":_measure_partner_file_grp_id, " +
            ":_drft_confe_charger, " +
            ":_drft_confe_pw, " +
            ":_drft_confe_room, " +
            ":_drft_stat_cd, " +
            ":_drft_summery, " +
            ":_measure_plan_file_grp_id, " +
            ":_measure_result_file_grp_id, " +
            ":_start_dt, " +
            ":_end_dt, " +
            ":_modi_id, " +
            ":_result_count, " +
            ":_result_code, " +
            ":_error_message)",
            nativeQuery = true)
    Map<String, Object> spUpdateTnCmtDraft(
            @Param("_drft_seq")			                        Integer     drftSeq,
            @Param("_drft_title")			                    String      drftTitle,
            @Param("_doc_info_seq")		                        Integer     docInfoSeq,
            @Param("_drft_type_cd")		                        String      drftTypeCd,
            @Param("_drft_dt")			                        String      drftDt,
            @Param("_cmt_seq")			                        Integer     cmtSeq,
            @Param("_measure_pre_data_file_grp_id")	            String      measurePreDataFileGrpId,
            @Param("_measure_pre_form_file_grp_id")	            String      measurePreFormFileGrpId,
            @Param("_measure_partner_file_grp_id")	            String      measurePartnerFileGrpId,
            @Param("_drft_confe_charger")				        String      drftConfeCharger,
            @Param("_drft_confe_pw")					        String      drftConfePw,
            @Param("_drft_confe_room")				            String      drftConfeRoom,
            @Param("_drft_stat_cd")					            String      drftStatCd,
            @Param("_drft_summery")					            String      drftSummery,
            @Param("_measure_plan_file_grp_id")		            String      measurePlanFileGrpId,
            @Param("_measure_result_file_grp_id")		        String      measureResultFileGrpId,
            @Param("_start_dt")						            String      startDt,
            @Param("_end_dt")							        String      endDt,
            @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_draft (" +
            ":_drft_seq, " +
            ":_modi_id, " +
            ":_result_count, " +
            ":_result_code, " +
            ":_error_message)",
            nativeQuery = true)
    Map<String, Object> spDeleteTnCmtDraft(
            @Param("_drft_seq")         Integer       drftSeq,
            @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_draft_id")
    int spIsValidTnCmtDraftId( Integer        drftSeq );

    TnCmtDraft findByDrftSeq(Long drftSeq);
    List<TnCmtDraft> findByUseYnOrderByDrftSeqDesc(String useYn);


    @Query(value =
            "select " +
                "	tcd.drft_seq as \"seq\", " +
                "	tcd.doc_info_seq as \"categoryId\", " +
                "	tcd.drft_dt as \"drftDatetime\", " +
                "	tcd.drft_title as \"title\", " +
                "	null as \"drftCharger\", " +
                "	null as \"drftChargerEmail\", " +
                "	null as \"drftChargerPhone\", " +
                "	null as \"drftStatCode\", " +
                "	tcd.drft_type_cd as \"drftTypeCode\", " +
                "	tcd.cmt_seq as \"orgId\", " +
                "	null as \"startDatetime\", " +
                "	null as \"endDatetime\", " +
                "	null as \"closeDatetime\", " +
                "	tcd.frst_crt_dt as \"createDate\", " +
                "	tcd.last_chg_dt as \"updateDate\", " +
                "	null as \"createUserId\", " +
                "	null as \"updateUserId\", " +
                "	case when UPPER(tcd.use_yn) = 'Y' then 'N' else 'Y'	end as \"delYn\", " +
                "	tcd.drft_summery as \"drftSummery\", " +
                "	tcd.drft_confe_charger as \"drftConfeCharger\", " +
                "	tcd.drft_confe_pw as \"drftConfePw\", " +
                "	tcd.drft_confe_room as \"drftConfeRoom\", " +
                "	null as \"drftMeasurePlanFileSeq\", " +
                "	null as \"drftMeasureResultFileSeq\", " +
                "	null as \"drftMeasurePlanFileName\", " +
                "	null as \"drftMeasureResultFileName\", " +
                "	null as \"drftMeasurePreDataFileName\", " +
                "	null as \"drftMeasurePreDataFileSeq\", " +
                "	null as \"drftMeasurePreFormFileName\", " +
                "	null as \"drftMeasurePreFormFileSeq\", " +
                "	null as \"drftMeasurePartnerFileName\", " +
                "	null as \"drftMeasurePartnerFileSeq\", " +
                "	null as \"drftProcess\", " +
                "	( " +
                "		select tci.item_nm " +
                "		from tc_code_item tci " +
                "		where tci.item_cd = tcd.drft_type_cd  " +
                "		limit 1 offset 0 " +
                "	) as \"drftTypeNm\", " +
                "	concat(tdi.kcsc_cd, ' ', tdi.doc_nm) as \"categoryNm\", " +
                "	tco.cmt_nm  as \"orgNm\", " +
                "	( " +
                "		select tci.item_nm " +
                "		from tc_code_item tci " +
                "		where tci.item_cd = tcd.drft_stat_cd " +
                "		limit 1 offset 0 " +
                "	) as \"drftStatNm\", " +
                "	null as \"categoryInfo\", " +
                "	null as \"orgType\", " +
                "	null as \"upOrgId\", " +
                "	null as \"orgList\", " +
                "	null as \"orgList1\", " +
                "	null as \"orgList2\", " +
                "	null as \"orgList3\", " +
                "	null as \"depthOrgId\", " +
                "	null as \"depthOrgId1\", " +
                "	null as \"depthOrgId2\", " +
                "	null as \"depthOrgId3\", " +
                "	to_char(tcd.frst_crt_dt, 'yyyy-mm-dd') as \"regDate\" " +
            "from tn_cmt_draft tcd   " +
                "left join tn_document_info tdi on tcd.doc_info_seq = tdi.doc_info_seq  " +
                "left join tn_document_group tdg on tdi.group_seq = tdg.group_seq " +
                "left join tn_cmt_org tco on tcd.cmt_seq = tco.cmt_seq  " +
            "where tcd.use_yn = 'Y' " +
            "order by tcd.drft_seq desc " +
            "limit :limit offset :limit * :offset",
            nativeQuery = true)
    List<Map<String, Object>> getCommitteeProgressStatus(
            @Param("limit")             Integer         limit,
            @Param("offset")            Integer         offset
    );

    @Query(value =
            "select " +
                    "	tcd.drft_seq as \"seq\", " +
                    "	tcd.doc_info_seq as \"categoryId\", " +
                    "	tcd.drft_dt as \"drftDatetime\", " +
                    "	tcd.drft_title as \"title\", " +
                    "	null as \"drftCharger\", " +
                    "	null as \"drftChargerEmail\", " +
                    "	null as \"drftChargerPhone\", " +
                    "	null as \"drftStatCode\", " +
                    "	tcd.drft_type_cd as \"drftTypeCode\", " +
                    "	tcd.cmt_seq as \"orgId\", " +
                    "	null as \"startDatetime\", " +
                    "	null as \"endDatetime\", " +
                    "	null as \"closeDatetime\", " +
                    "	tcd.frst_crt_dt as \"createDate\", " +
                    "	tcd.last_chg_dt as \"updateDate\", " +
                    "	null as \"createUserId\", " +
                    "	null as \"updateUserId\", " +
                    "	case when UPPER(tcd.use_yn) = 'Y' then 'N' else 'Y'	end as \"delYn\", " +
                    "	tcd.drft_summery as \"drftSummery\", " +
                    "	tcd.drft_confe_charger as \"drftConfeCharger\", " +
                    "	tcd.drft_confe_pw as \"drftConfePw\", " +
                    "	tcd.drft_confe_room as \"drftConfeRoom\", " +
                    "	null as \"drftMeasurePlanFileSeq\", " +
                    "	null as \"drftMeasureResultFileSeq\", " +
                    "	null as \"drftMeasurePlanFileName\", " +
                    "	null as \"drftMeasureResultFileName\", " +
                    "	null as \"drftMeasurePreDataFileName\", " +
                    "	null as \"drftMeasurePreDataFileSeq\", " +
                    "	null as \"drftMeasurePreFormFileName\", " +
                    "	null as \"drftMeasurePreFormFileSeq\", " +
                    "	null as \"drftMeasurePartnerFileName\", " +
                    "	null as \"drftMeasurePartnerFileSeq\", " +
                    "	null as \"drftProcess\", " +
                    "	( " +
                    "		select tci.item_nm " +
                    "		from tc_code_item tci " +
                    "		where tci.item_cd = tcd.drft_type_cd  " +
                    "		limit 1 offset 0 " +
                    "	) as \"drftTypeNm\", " +
                    "	concat(tdi.kcsc_cd, ' ', tdi.doc_nm) as \"categoryNm\", " +
                    "	tco.cmt_nm  as \"orgNm\", " +
                    "	( " +
                    "		select tci.item_nm " +
                    "		from tc_code_item tci " +
                    "		where tci.item_cd = tcd.drft_stat_cd " +
                    "		limit 1 offset 0 " +
                    "	) as \"drftStatNm\", " +
                    "	null as \"categoryInfo\", " +
                    "	null as \"orgType\", " +
                    "	null as \"upOrgId\", " +
                    "	null as \"orgList\", " +
                    "	null as \"orgList1\", " +
                    "	null as \"orgList2\", " +
                    "	null as \"orgList3\", " +
                    "	null as \"depthOrgId\", " +
                    "	null as \"depthOrgId1\", " +
                    "	null as \"depthOrgId2\", " +
                    "	null as \"depthOrgId3\", " +
                    "	tdi.kcsc_cd as \"standardCode\", " +
                    "	to_char(tcd.frst_crt_dt, 'yyyy-mm-dd') as \"regDate\" " +
                    "from tn_cmt_draft tcd   " +
                    "left join tn_document_info tdi on tcd.doc_info_seq = tdi.doc_info_seq  " +
                    "left join tn_document_group tdg on tdi.group_seq = tdg.group_seq " +
                    "left join tn_cmt_org tco on tcd.cmt_seq = tco.cmt_seq  " +
                    "where tcd.drft_seq = :drftSeq " +
                    "order by tcd.drft_seq desc ",
            nativeQuery = true)
    Map<String, Object> getCommitteeProgressStatusDetail(
            @Param("drftSeq")             Long drftSeq
    );


}
