package sgc.portal.cs.cs0501.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.cs.cs0501.persistence.CS05010001UMapper;
import sgc.portal.cs.cs0501.dto.CS05010001USearch;
import sgc.portal.cs.cs0501.model.CS05010001U;

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

@Service
public class CS05010001UService {
    
	private final CS05010001UMapper mapper;

    public CS05010001UService(CS05010001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //카테고리 트리 조회
    public List<EasyHashMap<String, Object>> selectTcsDocTree(CS05010001USearch search) {
        return mapper.selectTcsDocTree(search);
    }
    
    //카테고리 내역 조회
    public List<EasyHashMap<String, Object>> selectTcsDocGrd(CS05010001USearch search) {
        return mapper.selectTcsDocGrd(search);
    }
    
    //카테고리 내역 수정
    @Transactional
    public Map<String, String> saveTcsDoc(List<CS05010001U> CS05010001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (CS05010001U cS05010001U : CS05010001U) {
        	
            switch (cS05010001U.getRowStatus()) {
            
                case GridRowStatus.CREATED:
                	insertTcsDoc(cS05010001U);
                    break;
                    
                case GridRowStatus.UPDATED:
                	updateTcsDoc(cS05010001U);
                    break;
                    
                case GridRowStatus.DELETED:
                    deleteTcsDoc(cS05010001U);
                    break;
                default:
                    break;
            }
        }
        return result;
    }

    private void insertTcsDoc(CS05010001U cS05010001U) {
        mapper.insertTcsDoc(cS05010001U);
    }

    private void updateTcsDoc(CS05010001U cS05010001U) {
        mapper.updateTcsDoc(cS05010001U);
    }

    private void deleteTcsDoc(CS05010001U cS05010001U) {
    	
    	mapper.deleteTcsDoc(cS05010001U);
    	List<EasyHashMap<String, Object>> resultList1 = mapper.deleteDocIdList(cS05010001U);
    	
    	for(EasyHashMap<String, Object> item : resultList1) {
    		String docId = (String) item.get("docId");
    		if (!"".equals(docId) || docId != null) {
    			cS05010001U.setDocId(docId);
    			mapper.deleteTcsDoc(cS05010001U);
    			List<EasyHashMap<String, Object>> resultList2 = mapper.deleteDocIdList(cS05010001U);
    			for(EasyHashMap<String, Object> item2 : resultList2) {
    	    		String docId2 = (String) item2.get("docId");
    	    		if (!"".equals(docId2) || docId2 != null) {
    	    			cS05010001U.setDocId(docId2);
    	    			mapper.deleteTcsDoc(cS05010001U);
    	    			List<EasyHashMap<String, Object>> resultList3 = mapper.deleteDocIdList(cS05010001U);
    	    			for(EasyHashMap<String, Object> item3 : resultList3) {
    	    	    		String docId3 = (String) item3.get("docId");
    	    	    		if (!"".equals(docId3) || docId3 != null) {
    	    	    			cS05010001U.setDocId(docId3);
    	    	    			mapper.deleteTcsDoc(cS05010001U);
    	    	    			List<EasyHashMap<String, Object>> resultList4 = mapper.deleteDocIdList(cS05010001U);
    	    	    			for(EasyHashMap<String, Object> item4 : resultList4) {
    	    	    	    		String docId4 = (String) item4.get("docId");
    	    	    	    		if (!"".equals(docId4) || docId4 != null) {
    	    	    	    			cS05010001U.setDocId(docId4);
    	    	    	    			mapper.deleteTcsDoc(cS05010001U);
    	    	    	    			List<EasyHashMap<String, Object>> resultList5 = mapper.deleteDocIdList(cS05010001U);
    	    	    	    			for(EasyHashMap<String, Object> item5 : resultList5) {
    	    	    	    	    		String docId5 = (String) item5.get("docId");
    	    	    	    	    		if (!"".equals(docId5) || docId5 != null) {
    	    	    	    	    			cS05010001U.setDocId(docId5);
    	    	    	    	    			mapper.deleteTcsDoc(cS05010001U);
    	    	    	    	    			List<EasyHashMap<String, Object>> resultList6 = mapper.deleteDocIdList(cS05010001U);
    	    	    	    	    			for(EasyHashMap<String, Object> item6 : resultList6) {
    	    	    	    	    	    		String docId6 = (String) item6.get("docId");
    	    	    	    	    	    		if (!"".equals(docId6) || docId6 != null) {
    	    	    	    	    	    			cS05010001U.setDocId(docId6);
    	    	    	    	    	    			mapper.deleteTcsDoc(cS05010001U);
    	    	    	    	    	    			List<EasyHashMap<String, Object>> resultList7 = mapper.deleteDocIdList(cS05010001U);
    	    	    	    	    	    			for(EasyHashMap<String, Object> item7 : resultList7) {
    	    	    	    	    	    	    		String docId7 = (String) item7.get("docId");
    	    	    	    	    	    	    		if (!"".equals(docId7) || docId7 != null) {
    	    	    	    	    	    	    			cS05010001U.setDocId(docId7);
    	    	    	    	    	    	    			mapper.deleteTcsDoc(cS05010001U);
    	    	    	    	    	    	    			List<EasyHashMap<String, Object>> resultList8 = mapper.deleteDocIdList(cS05010001U);
    	    	    	    	    	    	    			for(EasyHashMap<String, Object> item8 : resultList8) {
    	    	    	    	    	    	    	    		String docId8 = (String) item8.get("docId");
    	    	    	    	    	    	    	    		if (!"".equals(docId8) || docId8 != null) {
    	    	    	    	    	    	    	    			cS05010001U.setDocId(docId8);
    	    	    	    	    	    	    	    			mapper.deleteTcsDoc(cS05010001U);
    	    	    	    	    	    	    	    			List<EasyHashMap<String, Object>> resultList9 = mapper.deleteDocIdList(cS05010001U);
    	    	    	    	    	    	    	    			for(EasyHashMap<String, Object> item9 : resultList9) {
    	    	    	    	    	    	    	    	    		String docId9 = (String) item9.get("docId");
    	    	    	    	    	    	    	    	    		if (!"".equals(docId9) || docId9 != null) {
    	    	    	    	    	    	    	    	    			cS05010001U.setDocId(docId9);
    	    	    	    	    	    	    	    	    			mapper.deleteTcsDoc(cS05010001U);
    	    	    	    	    	    	    	    	    		}
    	    	    	    	    	    	    	    	    	}
    	    	    	    	    	    	    	    		}
    	    	    	    	    	    	    	    	}
    	    	    	    	    	    	    		}
    	    	    	    	    	    	    	}
    	    	    	    	    	    		}
    	    	    	    	    	    	}
    	    	    	    	    		}
    	    	    	    	    	}
    	    	    	    		}
    	    	    	    	}
    	    	    		}
    	    	    	}
    	    		}
    	    	}
    		}
    	}
    }
}