package com.icomsys.main_vm.biz.rcp.conversation.vo.synonym;

import com.icomsys.main_vm.db.jpa.repo.system.TbSynonymMasterRepo;
import com.icomsys.main_vm.db.jpa.entity.conversation.TbSynonymWord;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class SynonymProcess {

    private final ModelMapper modelMapper;
    @Autowired
    private TbSynonymMasterRepo tbSynonymMasterRepo;

    //JPA 연관관계 방식
    public List<SelectSynonymList2Vo> selectSynonymList2(String opr, String repWord) {
        Long startSec = System.currentTimeMillis();
        List<SelectSynonymList2Vo> resulttest = tbSynonymMasterRepo.findByUseYnAndOprMngCode("Y", opr)
                .stream()
                .map(e -> modelMapper.map(e, SelectSynonymList2Vo.class))
                .collect(Collectors.toList());
        Long endtSec = System.currentTimeMillis();
//        log.info("resultd test = {}", new Gson().toJson(resulttest));

        log.info("time jpa = {}", endtSec-startSec);
        return resulttest;
    }

    //굳이 조이닝을 쓰겠다면.
    /*public List<SelectSynonymListVo> selectSynonymList3(String opr, String repWord) {
        Long startSec = System.currentTimeMillis();
        List<SelectSynonymListVo> result = new ArrayList<>();
        selectSynonymList2(opr, repWord).stream().filter(e -> e.getTbSynonymWords() != null).forEach(e -> {
            for (TbSynonymWord f : e.getTbSynonymWords()) {
                if (f.getUseYn().equals("Y")) {
                    SelectSynonymListVo add = new SelectSynonymListVo();
                    add.setSeq(e.getSeq());
                    add.setRepWord(e.getRepWord());
                    add.setIdivWord(e.getIdivWord());
                    add.setSynonymSeqs(e.getTbSynonymWords().stream().filter(g -> g.getUseYn().equals("Y")).map(h -> h.getSeq().toString()).collect(Collectors.joining(",")));
                    add.setSynonymWords(e.getTbSynonymWords().stream().filter(g -> g.getUseYn().equals("Y")).map(h -> h.getSynonymWord()).collect(Collectors.joining(",")));
                    result.add(add);
                }
            }
        });
        Long endtSec = System.currentTimeMillis();

        log.info("time = {}", endtSec-startSec);
        return result.stream().distinct().collect(Collectors.toList());
    }*/

}
