package itn.let.lett.service.impl;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import egovframework.rte.fdl.cmmn.EgovAbstractServiceImpl;
import egovframework.rte.fdl.idgnr.EgovIdGnrService;
import itn.com.cmm.service.FileVO;
import itn.com.cmm.service.impl.FileManageDAO;
import itn.let.lett.service.CateConfVO;
import itn.let.lett.service.HashConfVO;
import itn.let.lett.service.LetterService;
import itn.let.lett.service.LetterVO;
import itn.let.sym.ccm.cde.service.CateCode;
import itn.let.uss.umt.service.UserDefaultVO;

@Service("LetterService")
public class LetterServiceImpl extends EgovAbstractServiceImpl implements LetterService {

    @Resource(name="LetterDAO")
    private LetterDAO letterDAO;
    
	@Resource(name = "FileManageDAO")
	private FileManageDAO fileMngDAO;

    @Resource(name = "egovLetterInfoIdGnrService")
	private EgovIdGnrService idgenService;
    
    public List<CateCode> selectLetterMessageCategoryList() throws Exception{
    	
    	return letterDAO.selectLetterMessageCategoryList();
    }
    
    public String insertLetterMessages(LetterVO letterVO) throws Exception{
    	
    	// 문자등록 고유 아이디 생성하기
    	String letterId = idgenService.getNextStringId();
    	letterVO.setLetterId(letterId);
    	letterDAO.insertLetterMessages(letterVO);
    	
    	return letterId;
    }
    
    public List<?> selectLetterMessageList(UserDefaultVO userSearchVO) throws Exception{
    	
    	return letterDAO.selectLetterMessageList(userSearchVO);
    }
    
    public void deleteLetterMessage(String checkedIdForDel) throws Exception{
    	
    	String [] delId = checkedIdForDel.split(",");
		for (int i=0; i<delId.length ; i++){
		    
			letterDAO.deleteLetterMessage(delId[i]);

		}
    	
    }
    
    public LetterVO letterMessagesDetail(LetterVO letterVO) throws Exception{
    	
    	return letterDAO.letterMessagesDetail(letterVO);
    }
    
    public int updateLetterMessages(LetterVO letterVO) throws Exception{
    	
    	return letterDAO.updateLetterMessages(letterVO);
    }
    
    public List<CateCode> selectCateCodeTwoDptWithList(CateCode cateCode) throws Exception{
    	
    	return letterDAO.selectCateCodeTwoDptWithList(cateCode);
    }
    
    public List<CateCode> selectCateCodeThrDptWithList(CateCode cateCode) throws Exception{
    	
    	return letterDAO.selectCateCodeThrDptWithList(cateCode);
    }
    
    public List<CateCode> selectPhotoCateCodeWithList(CateCode cateCode) throws Exception{
    	
    	return letterDAO.selectPhotoCateCodeWithList(cateCode);
    }
    
    public List<CateCode> selectCateConfWithList(String categoryType) throws Exception{
    	
    	return letterDAO.selectCateConfWithList(categoryType);
    }
    
    public List<CateCode> selectCateConfWithList4Main(String categoryType) throws Exception{
    	
    	return letterDAO.selectCateConfWithList4Main(categoryType);
    }

    public List<CateCode> selectCateConfTwoDepthWithList(String categoryType) throws Exception{
    	
    	return letterDAO.selectCateConfTwoDepthWithList(categoryType);
    }
    
    public List<CateCode> selectCateConfThreeDepthWithList(CateCode cateCode) throws Exception{
    	
    	return letterDAO.selectCateConfThreeDepthWithList(cateCode);
    }
    
    public List<HashConfVO> selectHashTagWithList(String msgType) throws Exception{
    	
    	return letterDAO.selectHashTagWithList(msgType);
    }
    
    public String insertletterPhotosInfo(LetterVO letterVO) throws Exception{
    	
    	// 문자등록 고유 아이디 생성하기
    	String letterId = idgenService.getNextStringId();
    	letterVO.setLetterId(letterId);
    	letterDAO.insertletterPhotosInfo(letterVO);
    	
    	return letterId;
    }
    
    public List<?> selectLetterPhotosList(UserDefaultVO userSearchVO) throws Exception{
    	
    	return letterDAO.selectLetterPhotosList(userSearchVO);
    }
    
    public void deleteLetterPhotos(String checkedIdForDel) throws Exception{
    	
    	// 장/단문 문자 삭제와 동일한 프로세스 처리
    	String [] delId = checkedIdForDel.split(",");
    	LetterVO letterVO = new LetterVO();
    	
		for (int i=0; i<delId.length ; i++){
		    
			//첨부파일 삭제
			letterVO.setLetterId(delId[i]);
			
			// 그림문자 첨부파일 아이디 정보 받아오기
			LetterVO result = letterDAO.selectPhotosDetail(letterVO); 
			String attachFileId = result.getAttachFileId();
			
			// 첨부파일 시퀀스번호 때문에 불러옴.
			LetterVO resultFile = letterDAO.selectPhotoFileList(attachFileId);
			
			if(resultFile != null) { // 등록된 첨부파일이 있는 경우만 삭제처리
				
				//첨부파일 데이터 삭제
				FileVO returnFileVO = new FileVO();
				FileVO fileVO = new FileVO();
				fileVO.setAtchFileId(attachFileId);
				fileVO.setFileSn(resultFile.getFileSn());	//첨부파일을 하나만 등록하기 때문에 0으로 셋팅해줌
				returnFileVO = fileMngDAO.selectFileInf(fileVO);
				if(returnFileVO !=null) {
					File delFile = new File(returnFileVO.getFileStreCours()+returnFileVO.getStreFileNm());
					delFile.delete(); //실제파일  deletePerFile 삭제
				}

				// 첨부파일 테이블에서 첨부파일 내용 삭제
				fileMngDAO.deleteFileInf(fileVO); //DB삭제
				
			}
			
			// 그림 문자 내용 삭제처리 - Flag Update
			letterDAO.deleteLetterMessage(delId[i]);

		}
    }
    
    public List<CateCode> selectPhotoCateCodeListWithId(String cateCode) throws Exception{

    	return letterDAO.selectPhotoCateCodeListWithId(cateCode);
    }
    
    public LetterVO selectPhotosDetail(LetterVO letterVO) throws Exception{
    	
    	return letterDAO.selectPhotosDetail(letterVO); 
    }
    
    public LetterVO selectPhotoFileList(String attchFileId) throws Exception{
    	
    	return letterDAO.selectPhotoFileList(attchFileId);
    }
    
    public int updateLetterPhotosInfo(LetterVO letterVO) throws Exception{
    	
    	return letterDAO.updateLetterPhotosInfo(letterVO);
    }
    
    
    // 문자 발송 카테고리 관리
    
    // 문자 발송 카테고리 정보 저장하기
    public int insertCategoryConf(CateConfVO cateConfVO) throws Exception{
    	
    	return letterDAO.insertCategoryConf(cateConfVO);
    }
    
    public List<?> selectCategoryConfList(UserDefaultVO userSearchVO) throws Exception{
    	
    	return letterDAO.selectCategoryConfList(userSearchVO);
    }
    
    public CateConfVO categoryConfDetail(CateConfVO cateConfVO) throws Exception{
    	
    	return letterDAO.categoryConfDetail(cateConfVO);
    }
    
    public void deleteCategoryConf(String checkedIdForDel) throws Exception{
    	
    	String [] delId = checkedIdForDel.split(",");
		for (int i=0; i<delId.length ; i++){
		    
			letterDAO.deleteCategoryConf(delId[i]);

		}
    	
    }
    
    public int updateCategoryConf(CateConfVO cateConfVO) throws Exception{
    	
    	return letterDAO.updateCategoryConf(cateConfVO);
    }
    
    
    public int insertHashTagConf(HashConfVO hashConfVO) throws Exception{
    	
    	return letterDAO.insertHashTagConf(hashConfVO);
    }
    
    public List<?> selectHashTagConfList(UserDefaultVO userSearchVO) throws Exception{
    	
    	return letterDAO.selectHashTagConfList(userSearchVO);
    }
    
    public HashConfVO hashTagConfDetail(HashConfVO hashConfVO) throws Exception{
    	
    	return letterDAO.hashTagConfDetail(hashConfVO); 
    }
    
    public int updateHashTagConf(HashConfVO hashConfVO) throws Exception{
    	
    	return letterDAO.updateHashTagConf(hashConfVO);
    }
    
    public void deleteHashTagConf(String checkedIdForDel) throws Exception{
    	
    	String [] delId = checkedIdForDel.split(",");
		for (int i=0; i<delId.length ; i++){
		    
			letterDAO.deleteHashTagConf(delId[i]);

		}
		
    	
    }
    
	 // 맞춤제작 그림문자 리스트 불러오기
	 public List<?> selectLetterPhotosCustomList(UserDefaultVO userSearchVO) throws Exception{
		 
		 return letterDAO.selectLetterPhotosCustomList(userSearchVO);
	 }
    
	 
	// 문자 템플릿 사용 계
    public int updateTemplateUseCount(String attachFileId) throws Exception{
    	
    	return letterDAO.updateTemplateUseCount(attachFileId);
    }

	@Override
	public void updateLetterIdgen(int count) throws Exception {
		for(int i = 0 ; i < count ; i++) {
			idgenService.getNextStringId();
		}
	}

	@Override
	public int updateChkAfterPriorityPlus(LetterVO letterVO) throws Exception {
		return letterDAO.updateChkAfterPriorityPlus(letterVO);
	}

	@Override
	public int updateChkListPriority(LetterVO letterVO) throws Exception {
		return letterDAO.updateChkListPriority(letterVO);
	}

	@Override
	public int updateOrderByPriority(LetterVO letterVO) throws Exception {
		return letterDAO.updateOrderByPriority(letterVO);
	}
	
	@Override
	public int updateNullPriority(LetterVO letterVO) throws Exception {
		return letterDAO.updateNullPriority(letterVO);
	}
	
	
	@Override
	public int updateChkCateAfterSortPlus(LetterVO letterVO) throws Exception {
		return letterDAO.updateChkCateAfterSortPlus(letterVO);
	}
	
	@Override
	public int updateChkCateListSort(LetterVO letterVO) throws Exception {
		return letterDAO.updateChkCateListSort(letterVO);
	}
	
	@Override
	public int updateCateOrderBySort(LetterVO letterVO) throws Exception {
		return letterDAO.updateCateOrderBySort(letterVO);
	}
	
	@Override
	public int updateCateNullSort(LetterVO letterVO) throws Exception {
		return letterDAO.updateCateNullSort(letterVO);
	}
	
	@Override
	public int updateChkTagAfterSortPlus(LetterVO letterVO) throws Exception {
		return letterDAO.updateChkTagAfterSortPlus(letterVO);
	}
	
	@Override
	public int updateChkTagListSort(LetterVO letterVO) throws Exception {
		return letterDAO.updateChkTagListSort(letterVO);
	}
	
	@Override
	public int updateTagOrderBySort(LetterVO letterVO) throws Exception {
		return letterDAO.updateTagOrderBySort(letterVO);
	}
	
	@Override
	public int updateTagNullSort(LetterVO letterVO) throws Exception {
		return letterDAO.updateTagNullSort(letterVO);
	}
	
	//검색어 욕설 및 금지 단어 검색
	@Override
	public int selectSwearWordSearchCnt(String searchKeyWord) throws Exception{
		return letterDAO.selectSwearWordSearchCnt(searchKeyWord);
	}

	@Override
	public int updateSmsTemplateViewCount(String letterId) throws Exception {
		return letterDAO.updateSmsTemplateViewCount(letterId);
	}
	
	// 제목, 해쉬태그 일괄변경
	@Override
	public int updateHashTagList(LetterVO letterVO) throws Exception {
		int updateCnt = 0;
	    for(int i = 0; i < letterVO.getLetterIds().size(); i++){
	    	System.out.println(letterVO.getLetterIds().get(i));

	    	letterVO.setLetterId(letterVO.getLetterIds().get(i));
	    	letterVO.setLetterSj(letterVO.getLetterSjs().get(i));
	    	letterVO.setHashTag(letterVO.getHashTags().get(i));
	        letterDAO.updateHashTagList(letterVO);
	        
	        updateCnt++;
	    }

		return updateCnt; 
	}	
	
	// 메인태그 관리
	@Override
	public List<LetterVO> selectMainTagList(LetterVO letterVO) throws Exception{
		return letterDAO.selectMainTagList(letterVO);
	}
	

	// 메인태그 상세
	@Override
	public LetterVO selectMainTagDetail(LetterVO letterVO) throws Exception {
		return letterDAO.selectMainTagDetail(letterVO);
	}	
	
	// 메인태그 등록
	@Override
	public void insertMainTag(LetterVO letterVO) throws Exception {
		letterDAO.insertMainTag(letterVO);
	}
	
	// 메인태그 수정
	@Override
	public void updateMainTag(LetterVO letterVO) throws Exception {
		letterDAO.updateMainTag(letterVO);
	}	
	
	@Override
	public List<LetterVO> selectMainTagWebList(LetterVO letterVO) throws Exception{
		return letterDAO.selectMainTagWebList(letterVO);
	}
	
	@Override
	public LetterVO selectFrsKeyword(LetterVO letterVO) throws Exception {
		return letterDAO.selectFrsKeyword(letterVO);
	}	
}
