package com.its.api.its.service.link;

import com.its.api.its.model.dto.link.LinkParaClctSystDto;
import com.its.api.its.model.entity.link.TbLinkParaClctSyst;
import com.its.api.its.model.entity.link.TbLink;
import com.its.api.its.repository.dao.LinkPrcsParaDao;
import com.its.api.its.repository.link.TbLinkParaClctSystRepository;
import com.its.api.its.repository.link.TbLinkRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class LinkParaClctSystService {

    private final TbLinkParaClctSystRepository repo;
    private final TbLinkRepository linkRepo;
    private final LinkPrcsParaDao dao;

    public List<TbLinkParaClctSyst> findAll() {
        try {
            return this.repo.findAll();
        }
        catch (Exception e) {
            log.error("{}.findAll: Exception: {}", getClass().getSimpleName(), e.getMessage());
        }
        return new ArrayList<>();
    }

    public List<TbLinkParaClctSyst> findListById(Long id) {
        try {
            return this.repo.findListById(id);
        }
        catch (Exception e) {
            log.error("{}.findById: Object: {}, Exception: {}", getClass().getSimpleName(), id, e.getMessage());
        }
        return new ArrayList<>();
    }

    @Transactional
    public List<LinkParaClctSystDto.LinkParaClctSystUpdateReq> mergeById(Long id, List<LinkParaClctSystDto.LinkParaClctSystUpdateReq> req) {
        try {
            this.repo.deleteListById(id);
            for (LinkParaClctSystDto.LinkParaClctSystUpdateReq obj : req) {
                TbLinkParaClctSyst newObj = new TbLinkParaClctSyst(id);
                newObj.updateInfo(obj);
                this.repo.save(newObj);
            }
            return req;
        }
        catch (Exception e) {
            log.error("{}.mergeInfo: Object: {}, Exception: {}", getClass().getSimpleName(), req, e.getMessage());
        }
        return null;
    }

    @Transactional
    public List<LinkParaClctSystDto.LinkParaClctSystUpdateReq> mergeAll(List<LinkParaClctSystDto.LinkParaClctSystUpdateReq> req) {
        try {
            this.dao.execute("deleteLinkParaClctSyst");

            List<HashMap<String, Object>> lists = new ArrayList<>();

            List<TbLink> links = this.linkRepo.findAll();
            for (TbLink link : links) {
                if (link.getDelYn().equals("Y")) {
                    continue;
                }
                for (LinkParaClctSystDto.LinkParaClctSystUpdateReq obj : req) {
                    HashMap<String, Object> param = new HashMap<>();
                    param.put("LINK_ID",      link.getLinkId());
                    param.put("CLCT_SYST_CD", obj.getCLCT_SYST_CD());
                    param.put("FUSN_PRRT",    obj.getFUSN_PRRT());
                    param.put("FUSN_WGHT",    obj.getFUSN_WGHT());
                    lists.add(param);
                }
            }
            this.dao.insertBatch("insertLinkParaClctSyst", lists);
        }
        catch (Exception e) {
            log.error("{}.mergeAll: Object: {}, Exception: {}", getClass().getSimpleName(), req, e.getMessage());
        }
        return req;

        /*try {
            this.repo.deleteAllInBatch();

            List<LinkParaClctSyst> jobs = new ArrayList<>();

            //this.repo.deleteDataAll();//deleteAll();
            List<Link> links = this.linkRepo.findAll();
            for (Link link : links) {
                if (link.getDEL_YN().equals("Y")) {
                    continue;
                }

                for (LinkParaClctSystDto.LinkParaClctSystUpdateReq obj : req) {
                    LinkParaClctSyst newObj = new LinkParaClctSyst(link.getLINK_ID());
                    newObj.updateInfo(obj);
                    //this.repo.save(newObj);
                    jobs.add(newObj);
                }
            }
            this.repo.saveAll(jobs);
            log.error("{} EA", jobs.size());
        }
        catch (Exception e) {
            log.error("{}.mergeAll: Object: {}, Exception: {}", getClass().getSimpleName(), req, e.getMessage());
        }
        return req;*/
    }
}
