package sgc.portal.ei.ei0102.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import sgc.portal.base.util.EasyHashMap;
import sgc.portal.ei.ei0102.dto.EI01020001USearch;
import sgc.portal.ei.ei0102.model.EI01020001U;

import sgc.portal.ei.ei0102.persistence.EI01020001UMapper;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
public class EI01020001UService {
    private final EI01020001UMapper mapper;

    public EI01020001UService(EI01020001UMapper mapper) {
        this.mapper = mapper;
    }
    
    /*
     * SEARCH LIST
     */
    public List<EasyHashMap<String, Object>> getList(EI01020001USearch search) {
        return mapper.getList(search);
    }
    
    public List<EasyHashMap<String, Object>> getList2(EI01020001USearch search) {
        return mapper.getList2(search);
    }
    
    public List<EasyHashMap<String, Object>> getList3(EI01020001USearch search) {
        return mapper.getList3(search);
    }
    
    public List<EasyHashMap<String, Object>> getSpecCd(EI01020001USearch search) {
        return mapper.getSpecCd(search);
    }
    
    public int getBacklogCnt(EI01020001USearch search) {
        return mapper.getBacklogCnt(search);
    }
    
    
    /*
     * SAVE LIST
     */
    @Transactional
    public Map<String, String> update1(List<EI01020001U> eI01020001U) {
    	Map<String,String> result = new HashMap<>();
    	for (EI01020001U vo : eI01020001U) {
    		update1(vo);
        }
    	
    	return result;
    }
    
    @Transactional
    public Map<String, String> update2(List<EI01020001U> eI01020001U) {
    	Map<String,String> result = new HashMap<>();
    	for (EI01020001U vo : eI01020001U) {
            update2(vo);
        }
    	
    	return result;
    }
    
    @Transactional
    public Map<String, String> update3(List<EI01020001U> eI01020001U) {
    	Map<String,String> result = new HashMap<>();
    	for (EI01020001U vo : eI01020001U) {
    		update3(vo);
        }
    	
    	return result;
    }
    
    @Transactional
    public Map<String, String> update4(List<EI01020001U> eI01020001U) {
    	log.info("EI01020001UService - update4()");
    	
    	Map<String,String> result = new HashMap<>();
    	for (EI01020001U vo : eI01020001U) {
    		update4(vo);
        }
    	
    	return result;
    }
    
    
    /*
     * UPDATE
     */
    private void update1(EI01020001U eI01020001U) {
    	mapper.insertOrUpdate1(eI01020001U);
    	
    	List<EasyHashMap<String, Object>> listCnt = mapper.getListCnt(eI01020001U);
    	int cnt = Integer.parseInt( listCnt.get(0).get("cnt").toString());
    	if(cnt < 1) {
    		mapper.insert1(eI01020001U);
    	}
    }
    
    private void update2(EI01020001U eI01020001U) {
    	mapper.insertOrUpdate2(eI01020001U);
    }
    
    private void update3(EI01020001U eI01020001U) {
    	mapper.insertOrUpdate3(eI01020001U);
    }
    
    private void update4(EI01020001U eI01020001U) {
    	mapper.insertBacklogFcst(eI01020001U);
    }
    
}
