package sgc.portal.ei.ei0201.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.base.websquare.GridRowStatus;
import sgc.portal.ei.ei0201.dto.EI02010001USearch;
import sgc.portal.ei.ei0201.model.EI02010001U;
import sgc.portal.ei.ei0201.persistence.EI02010001UMapper;

@Service
public class EI02010001UService {
	private final EI02010001UMapper mapper;

	public EI02010001UService(EI02010001UMapper mapper) {
		this.mapper = mapper;
	}

	//자금관리 조회
	public List<EasyHashMap<String, Object>> getList1(EI02010001USearch search) {
		return mapper.getList1(search);
	}
	
	//이월금액 조회
	public List<EasyHashMap<String, Object>> getList2(EI02010001USearch search) {
		return mapper.getList2(search);
	}

	//저장
	@Transactional
	public Map<String, String> save(Map<String, List<EI02010001U>> param) {
		Map<String, String> result = new HashMap<>();
		
		List<EI02010001U> param1 = param.get("list1");
		EI02010001U param2 = param.get("list2").get(0);
		
		if(!param1.isEmpty() && param1.get(0).getUseExcelYn().equals("Y")) {
			mapper.delete2(param1.get(0));
		}
		
		for (EI02010001U eI02010001U : param1) {
			switch (eI02010001U.getRowStatus()) {
			case GridRowStatus.CREATED:
				mapper.insert(eI02010001U);
				break;
			case GridRowStatus.UPDATED:
				mapper.update(eI02010001U);
				break;
			case GridRowStatus.DELETED:
				mapper.delete1(eI02010001U);
				break;
			default:
				break;
			}
		}
		
		mapper.insertOrUpdate(param2);
		
		return result;
	}
}
