package sgc.portal.ei.ei0202.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.ei0202.dto.EI02020001USearch;
import sgc.portal.ei.ei0202.model.EI02020001U;
import sgc.portal.ei.ei0202.persistence.EI02020001UMapper;

@Service
public class EI02020001UService {
	private final EI02020001UMapper mapper;

	public EI02020001UService(EI02020001UMapper mapper) {
		this.mapper = mapper;
	}

	//조회
	public List<EasyHashMap<String, Object>> getList(EI02020001USearch search) {
		return mapper.getList(search);
	}

	//저장
	@Transactional
	public Map<String, String> save(List<EI02020001U> param) {

		Map<String, String> result = new HashMap<>();

		for (EI02020001U eI02020001U : param) {
			switch (eI02020001U.getRowStatus()) {
			case GridRowStatus.CREATED:
				mapper.insert(eI02020001U);
				break;
			case GridRowStatus.UPDATED:
				mapper.update(eI02020001U);
				break;
			case GridRowStatus.DELETED:
				mapper.delete(eI02020001U);
				break;
			default:
				break;
			}
		}
		return result;
	}
	
	//복사
	@Transactional
	public Map<String, String> copy(EI02020001USearch search) {
		Map<String, String> result = new HashMap<>();
		if(mapper.getList1(search) == 0) {
			result.put("result", "전월 차입금 내역이 없습니다.");
			return result;
		}
		mapper.deleteCopy(search);
		mapper.insertCopy(search);
		return result;
	}
}
