package com.its.op.service.its.link;

import com.its.op.dto.its.link.LinkParaClctSystDto;
import com.its.op.entity.its.link.TbLinkParaClctSyst;
import com.its.op.entity.its.link.TbLink;
import com.its.op.dao.mapper.its.link.LinkPrcsParaDao;
import com.its.op.dao.repository.its.link.TbLinkParaClctSystRepository;
import com.its.op.dao.repository.its.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() {
        return this.repo.findAll();
    }

    public List<TbLinkParaClctSyst> findListById(Long id) {
        return null;//this.repo.findListById(id);
    }

    @Transactional
    public List<LinkParaClctSystDto.LinkParaClctSystUpdateReq> mergeById(Long id, List<LinkParaClctSystDto.LinkParaClctSystUpdateReq> req) {
        this.repo.deleteListById(id);
        for (LinkParaClctSystDto.LinkParaClctSystUpdateReq obj : req) {
            TbLinkParaClctSyst newObj = null;//new TbLinkParaClctSyst(id);
            //newObj.updateInfo(obj);
            this.repo.save(newObj);
        }
        return req;
    }

    @Transactional
    public List<LinkParaClctSystDto.LinkParaClctSystUpdateReq> mergeAll(List<LinkParaClctSystDto.LinkParaClctSystUpdateReq> req) {

        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);
        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;*/
    }
}
