package nlib.sch.service;

import java.util.ArrayList;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import nlib.cmm.crypto.AriaCrypto;
import nlib.cmm.service.NlibProperty;
import nlib.cmm.service.PagingVO;
import nlib.col.service.CollectionVO;
import nlib.util.StringUtil;


/**  
 * 검색엔진 . NlibSearchVO 
 * @Class Name : NlibSearchVO.java
 * @author JSYOO
 * @since 2021-10-18
 * @version 1.0
 */

public class NlibSearchVO extends PagingVO {
	private String query ="";			 	//검색어
	private String collection = "lib_total";			//검색 대상 컬렉션 지정
	private String listCount  = NlibProperty.getProperty("list.paging.page.size");			//컬렉션별 출력 결과수
	private String sort		  = "DATE";			//결과 정렬 DATE 날짜 내림차, RANK 정확도 내림차
	private String startCount = "";			//검색 결과 시작 페이지	
	private String sfield	  = "";			//검색 영역	
	private String creatYyyyStart  = "";	//생산년도 검색(시작일)
	private String creatYyyyEnd	   = "";	//생산년도 검색(종료일)
	private String creatYyyyYn   = "";		//미상포함 Y N
	
	private String collectionList = ""; 	//상세검색시 콜렉션 셀렉트 검색
	private String bookIndex 	   = ""; 	//목차검색
	private String createStartDate = "";	//연대 검색(시작일)
	private String createEndDate   = "";	//연대 검색(종료일)
	private String mngtNo		   = "";	//관리번호	
	private String ageCode 		   = "";	//시대	
	private String useStatus	   = "";	//이용구분	
	private String typeDivCd	   = "";	//자료유형	
	private String subjectCode	   = "";	//주제분야	
	private String orgNm		   = "";	//생산기관	
	private String mngOrgCd		   = "";	//문화원코드
	
	private String reQuery = "";			//결과내 재검색
	private String reQueryChk ="query";	//결과내재검색 체크 유지
	private String realQuery ="";	//사용자페이지에 보여줄 검색내용
	private String lib_total_totalCount 	= "0"; //전체의 총 COUNT
	private String lib_loan_totalCount 		= "0"; //대출의 총 COUNT
	private String lib_online_totalCount 	= "0"; //온라인열람의 총 COUNT
	private String lib_offline_totalCount 	= "0"; //방문열람의 총 COUNT
	private String lib_etc_totalCount 		= "0"; //기타의 총 COUNT

	private String totalCount 				= "0"; //현재 선택된 콜렉션의 총 COUNT
	
	private String filter_class				= ""; 		//필터 더보기버튼 유지+depth1 open
	private String filter_category ;		//필터 더보기버튼 유지+depth1 open
	private String filter_agency ;		// 생산기관은 depth1 이므로 filter_agency로 checked,검색필터유지
	private String filter_mylist;			//검색결과 필터 순서 저장
	private String filter_creatYyyy ="";	//생산년도 필터

	private String first_agency ;			//생산기관 선택
	private String first_category ;			//주제분야(대) 선택
	private String second_category ;	//주제분야(소) 선택
	private String first_class ;				//자료유형(대) 선택
	private String second_class;			//자료유형(소) 선택
	
	private List<CollectionVO> resultList = new ArrayList<>(); // 자료 리스트
	private List<HashMap<String,String>> typeCountList = new ArrayList<HashMap<String,String>>(); // 자료유형 count 리스트
	private List<HashMap<String,String>> subjectCountList = new ArrayList<HashMap<String,String>>(); // 주제분야 count 리스트
	private List<HashMap<String,String>> orgCountList = new ArrayList<HashMap<String,String>>(); // 생산기관 count 리스트
	
	public String getFilter_class() {
		return filter_class;
	}
	public void setFilter_class(String filter_class) {
		this.filter_class = filter_class;
	}
	public String getFilter_category() {
		return filter_category;
	}
	public void setFilter_category(String filter_category) {
		this.filter_category = filter_category;
	}
	public String getFilter_mylist() {
		return filter_mylist;
	}
	public void setFilter_mylist(String filter_mylist) {
		this.filter_mylist = filter_mylist;
	}
	public String getReQuery() {
		return reQuery;
	}
	public void setReQuery(String reQuery) {
		this.reQuery = reQuery;
	}
	public String getReQueryChk() {
		return reQueryChk;
	}
	public void setReQueryChk(String reQueryChk) {
		this.reQueryChk = reQueryChk;
	}
	public String getListCount() {
		return listCount;
	}
	public void setListCount(String listCount) {
		this.listCount = listCount;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getStartCount() {
		return startCount;
	}
	public void setStartCount(String startCount) {
		this.startCount = startCount;
	}
	public String getSfield() {
		return sfield;
	}
	public void setSfield(String sfield) {
		this.sfield = sfield;
	}
	public String getCreatYyyyStart() {
		return creatYyyyStart;
	}
	public void setCreatYyyyStart(String creatYyyyStart) {
		this.creatYyyyStart = creatYyyyStart;
	}
	public String getCreatYyyyEnd() {
		return creatYyyyEnd;
	}
	public void setCreatYyyyEnd(String creatYyyyEnd) {
		this.creatYyyyEnd = creatYyyyEnd;
	}
	public String getCreateStartDate() {
		return createStartDate;
	}
	public void setCreateStartDate(String createStartDate) {
		this.createStartDate = createStartDate;
	}
	public String getCreateEndDate() {
		return createEndDate;
	}
	public void setCreateEndDate(String createEndDate) {
		this.createEndDate = createEndDate;
	}
	public String getMngtNo() {
		return mngtNo;
	}
	public void setMngtNo(String mngtNo) {
		this.mngtNo = mngtNo;
	}
	public String getAgeCode() {
		return ageCode;
	}
	public void setAgeCode(String ageCode) {
		this.ageCode = ageCode;
	}
	public String getUseStatus() {
		return useStatus;
	}
	public void setUseStatus(String useStatus) {
		this.useStatus = useStatus;
	}
	public String getTypeDivCd() {
		return typeDivCd;
	}
	public void setTypeDivCd(String typeDivCd) {
		this.typeDivCd = typeDivCd;
	}
	public String getSubjectCode() {
		return subjectCode;
	}
	public void setSubjectCode(String subjectCode) {
		this.subjectCode = subjectCode;
	}
	public String getOrgNm() {
		return orgNm;
	}
	public void setOrgNm(String orgNm) {
		this.orgNm = orgNm;
	}
	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	public String getCollection() {
		return collection;
	}
	public void setCollection(String collection) {
		this.collection = collection;
	}
	public String getLib_total_totalCount() {
		return lib_total_totalCount;
	}
	public void setLib_total_totalCount(String lib_total_totalCount) {
		this.lib_total_totalCount = lib_total_totalCount;
	}
	public String getLib_loan_totalCount() {
		return lib_loan_totalCount;
	}
	public void setLib_loan_totalCount(String lib_loan_totalCount) {
		this.lib_loan_totalCount = lib_loan_totalCount;
	}
	public String getLib_online_totalCount() {
		return lib_online_totalCount;
	}
	public void setLib_online_totalCount(String lib_online_totalCount) {
		this.lib_online_totalCount = lib_online_totalCount;
	}
	public String getLib_offline_totalCount() {
		return lib_offline_totalCount;
	}
	public void setLib_offline_totalCount(String lib_offline_totalCount) {
		this.lib_offline_totalCount = lib_offline_totalCount;
	}
	public String getLib_etc_totalCount() {
		return lib_etc_totalCount;
	}
	public void setLib_etc_totalCount(String lib_etc_totalCount) {
		this.lib_etc_totalCount = lib_etc_totalCount;
	}
	public List<CollectionVO> getResultList() {
		return resultList;
	}
	public void setResultList(List<CollectionVO> resultList) {
		this.resultList = resultList;
	}
	public String getFirst_category() {
		return first_category;
	}
	public void setFirst_category(String first_category) {
		this.first_category = first_category;
	}
	public String getSecond_category() {
		return second_category;
	}
	public void setSecond_category(String second_category) {
		this.second_category = second_category;
	}
	public String getFirst_class() {
		return first_class;
	}
	public void setFirst_class(String first_class) {
		this.first_class = first_class;
	}
	public String getSecond_class() {
		return second_class;
	}
	public void setSecond_class(String second_class) {
		this.second_class = second_class;
	}
	public String getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(String totalCount) {
		this.totalCount = totalCount;
	}
	public void add(MultiValueMap<String, String> map,String name, String value) {
		if(!"".equals(StringUtil.getString(value,"")))
		{
			map.add(name,value);
		}
	}
	public String getFilter_creatYyyy() {
		return filter_creatYyyy;
	}
	public void setFilter_creatYyyy(String filter_creatYyyy) {
		this.filter_creatYyyy = filter_creatYyyy;
	}
	public String getCreatYyyyYn() {
		return creatYyyyYn;
	}
	public void setCreatYyyyYn(String creatYyyyYn) {
		this.creatYyyyYn = creatYyyyYn;
	}

	public List<HashMap<String, String>> getTypeCountList() {
		return typeCountList;
	}
	public void setTypeCountList(List<HashMap<String, String>> typeCountList) {
		this.typeCountList = typeCountList;
	}
	public List<HashMap<String, String>> getSubjectCountList() {
		return subjectCountList;
	}
	public void setSubjectCountList(List<HashMap<String, String>> subjectCountList) {
		this.subjectCountList = subjectCountList;
	}
	public List<HashMap<String, String>> getOrgCountList() {
		return orgCountList;
	}
	public void setOrgCountList(List<HashMap<String, String>> orgCountList) {
		this.orgCountList = orgCountList;
	}
	public String getRealQuery() {
		String realQuery="";
		if(StringUtil.getString(this.query,"") != "")
			realQuery = this.query;
		if(StringUtil.getString(this.reQuery,"") != "")
		{
			if(realQuery.length() > 0)
			{
				realQuery += "(" + this.reQuery + ")"; 
			}else
			{
				realQuery = this.reQuery;
			}
		}
		return realQuery;
	}
	
	//검색요청시 URL 생성
	public MultiValueMap<String, String> getSearchMap() {
		
		MultiValueMap<String, String> map =  new LinkedMultiValueMap<String,String>();
		
		try {
			if(!StringUtil.isEmpty(this.query + " " + this.reQuery))
			add(map,"query",URLEncoder.encode(this.query + " " + this.reQuery , "UTF-8"));
			
			add(map,"collection",URLEncoder.encode(this.collection , "UTF-8"));
			add(map,"sort",URLEncoder.encode(this.sort , "UTF-8"));
			add(map,"startCount",URLEncoder.encode(this.startCount , "UTF-8"));
			add(map,"sfield",URLEncoder.encode(this.sfield , "UTF-8"));
			add(map,"CREAT_YYYY_START",URLEncoder.encode(this.creatYyyyStart , "UTF-8"));
			add(map,"CREAT_YYYY_END",URLEncoder.encode(this.creatYyyyEnd , "UTF-8"));
			add(map,"CREAT_YYYY_YN",URLEncoder.encode(this.creatYyyyYn , "UTF-8"));
			add(map,"createStartDate",URLEncoder.encode(this.createStartDate , "UTF-8"));
			add(map,"createEndDate",URLEncoder.encode(this.createEndDate , "UTF-8"));
			add(map,"MNGT_NO",URLEncoder.encode(this.mngtNo , "UTF-8"));
			add(map,"BOOK_INDEX",URLEncoder.encode(this.bookIndex , "UTF-8"));
			add(map,"AGE_CODE",URLEncoder.encode(this.ageCode.replaceAll("[,]", "|") , "UTF-8"));
			add(map,"USE_STATUS",URLEncoder.encode(this.useStatus , "UTF-8"));
			add(map,"TYPE_DIV_CD",URLEncoder.encode(this.typeDivCd , "UTF-8"));
			add(map,"SUBJECT_CODE",URLEncoder.encode(this.subjectCode , "UTF-8"));
			add(map,"listCount",URLEncoder.encode(this.listCount , "UTF-8"));
			add(map,"SUBJECT_CATE_DEPTH",URLEncoder.encode("2" , "UTF-8")); 	//주제분야 2뎁스까지 나오게 고정
			add(map,"DIV_CATE_DEPTH",URLEncoder.encode("2" , "UTF-8")); 		//자료유형 2뎁스까지 나오게 고정
			
			add(map,"DTLS_TYPE_DIV_CD",URLEncoder.encode(StringUtil.getString(this.second_class,"").replace(",","|") , "UTF-8"));
			add(map,"SUBJECT_CODE",URLEncoder.encode(StringUtil.getString(this.second_category,"").replace(",","|") , "UTF-8"));
			add(map,"ORG_NM",URLEncoder.encode(StringUtil.getString(this.filter_agency,"").replace(",","|") , "UTF-8"));
			add(map,"MNG_ORG_CD",URLEncoder.encode(this.mngOrgCd , "UTF-8")); 		//자료유형 2뎁스까지 나오게 고정
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return map;
	}

	// 탭별 total count set
	public void setCollectionTotalCount (String collection, String totalCount) {
		if(collection.equals("lib_loan"))
		{
			this.lib_loan_totalCount = totalCount;
		}else if(collection.equals("lib_online"))
		{
			this.lib_online_totalCount = totalCount;
		}else if(collection.equals("lib_offline"))
		{
			this.lib_offline_totalCount = totalCount;
		}else if(collection.equals("lib_etc"))
		{
			this.lib_etc_totalCount = totalCount;
		}else if(collection.equals("lib_total"))
		{
			this.lib_total_totalCount = totalCount;
		}
	}
	public String getFirst_agency() {
		return first_agency;
	}
	public void setFirst_agency(String first_agency) {
		this.first_agency = first_agency;
	}
	public String getFilter_agency() {
		return filter_agency;
	}
	public void setFilter_agency(String filter_agency) {
		this.filter_agency = filter_agency;
	}
	public String getBookIndex() {
		return bookIndex;
	}
	public void setBookIndex(String bookIndex) {
		this.bookIndex = bookIndex;
	}
	public String getCollectionList() {
		return collectionList;
	}
	public void setCollectionList(String collectionList) {
		this.collectionList = collectionList;
	}
	public String getMngOrgCd() {
		return mngOrgCd;
	}
	public void setMngOrgCd(String mngOrgCd) {
		this.mngOrgCd = mngOrgCd;
	}
}
