package com.dbnt.sims.translator.service;


import com.dbnt.sims.translator.mapper.TranslatorMapper;
import com.dbnt.sims.translator.model.Translator;
import com.dbnt.sims.translator.model.Translator.TranslatorId;
import com.dbnt.sims.translator.model.TranslatorCrr;
import com.dbnt.sims.translator.repository.TranslatorCareerRepository;
import com.dbnt.sims.translator.repository.TranslatorRepository;
import com.dbnt.sims.util.ParamMap;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.*;

@Service
@RequiredArgsConstructor
public class TranslatorService {

	private final TranslatorRepository translatorRepository;
	private final TranslatorCareerRepository translatorCareerRepository;
	private final TranslatorMapper translatorMapper;

	public String insertTranslatorInfo(Translator translator) {
		Translator dbTranslator = translatorRepository.findFirstByOrderByTranslatorKeyDesc();
		translator.setWrtDt(LocalDateTime.now());
		if (dbTranslator == null) {
			translator.setTranslatorKey(1);
			translator.setVersionNo(1);
			return translatorRepository.save(translator).getTrName();
		} else {
			translator.setTranslatorKey(dbTranslator.getTranslatorKey() + 1);
			translator.setVersionNo(1);
			return translatorRepository.save(translator).getTrName();
		}

	}

	public List<Translator> selectTranslatorList(Translator translator) {
		return translatorMapper.selectTranslatorList(translator);
	}

	public Integer selectTranslatorListCnt(Translator translator) {
		return translatorMapper.selectTranslatorListCnt(translator);
	}

	public Translator selectTranslatorView(Translator translator) {
		return translatorRepository.findById(new TranslatorId(translator.getTranslatorKey(), translator.getVersionNo()))
				.orElse(null);
	}

	@Transactional
	public Translator updatetranslatorInfo(Translator translator) {
		Translator dbTranslator = translatorRepository
				.findById(new TranslatorId(translator.getTranslatorKey(), translator.getVersionNo())).orElse(null);
		Translator translatorTmp = new Translator();
		translatorTmp.setTranslatorKey(dbTranslator.getTranslatorKey());
		translatorTmp.setVersionNo(dbTranslator.getVersionNo() + 1);
		translatorTmp.setOgdp1(translator.getOgdp1());
		translatorTmp.setTrLang(translator.getTrLang());
		translatorTmp.setTrCareer(translator.getTrCareer());
		translatorTmp.setTrName(translator.getTrName());
		translatorTmp.setTrSex(translator.getTrSex());
		translatorTmp.setTrPhone(translator.getTrPhone());
		translatorTmp.setTrNny(translator.getTrNny());
		translatorTmp.setTrAge(translator.getTrAge());
		translatorTmp.setTrEdu(translator.getTrEdu());
		translatorTmp.setTrCft(translator.getTrCft());
		translatorTmp.setTrVisa(translator.getTrVisa());
		translatorTmp.setAptDt(translator.getAptDt());
		translatorTmp.setDmlYn(translator.getDmlYn());
		translatorTmp.setRemark(translator.getRemark());
		translatorTmp.setWrtNm(translator.getWrtNm());
		translatorTmp.setWrtDt(LocalDateTime.now());
		translatorTmp.setWrtOrgan(translator.getWrtOrgan());
		translatorRepository.save(translatorTmp);
		return translatorTmp;
	}

	public List<Translator> selectHistoryList(Translator translator) {
		return translatorMapper.selectHistoryList(translator);
	}

	public Translator HistoryView(Translator translator) {		
		return translatorMapper.HistoryView(translator);
	}

	public int insertTranslatorCareer(List<TranslatorCrr> translatorCrr) {
		int trKey = 0;
		for(TranslatorCrr tCrr : translatorCrr ) {
			TranslatorCrr dbCareer = translatorCareerRepository.findTopByTranslatorKeyOrderByCareerSeqDesc(tCrr.getTranslatorKey());
			if (dbCareer == null) {
				tCrr.setCareerSeq(1);
			    translatorCareerRepository.save(tCrr);
			} else {
				tCrr.setCareerSeq(dbCareer.getCareerSeq()+ 1);
				translatorCareerRepository.save(tCrr);
			}
			trKey = tCrr.getTranslatorKey();
		}
		return trKey;
	}

	public List<TranslatorCrr> selectMaritimePoliceCareer(TranslatorCrr translatorCrr) {
		translatorCrr.setHisGubun("Y");
		return translatorMapper.selectCareerList(translatorCrr);
	}

	public List<TranslatorCrr> selectAnotherOrganizationCareer(TranslatorCrr translatorCrr) {
		translatorCrr.setHisGubun("N");
		return translatorMapper.selectCareerList(translatorCrr);
	}
	
	@Transactional
	public int deleteCareer(TranslatorCrr translatorCrr) {
		TranslatorCrr dbCareer = translatorCareerRepository.findByCareerSeqAndTranslatorKey(translatorCrr.getCareerSeq(),translatorCrr.getTranslatorKey()).orElse(null);
		translatorCareerRepository.delete(dbCareer);
		return translatorCrr.getTranslatorKey();
	}
    
	@Transactional
	public void deleteTranslatorInfo(int trKey) {
		translatorCareerRepository.deleteByTranslatorKey(trKey);
		translatorRepository.deleteByTranslatorKey(trKey);
	}

	public List<ParamMap> selectStatisticsLangCnt() {
		return translatorMapper.selectStatisticsLangCnt();
	}

	public ParamMap selectStatisticsLangTotal() {
		return translatorMapper.selectStatisticsLangTotal();
	}

	public List<ParamMap> selectTranslatorListEx(Translator translator) {
		return translatorMapper.selectTranslatorListEx(translator);
	}

	public String selectTrFristId(int translatorKey) {
		return translatorMapper.selectTrFristId(translatorKey);
	}

}
