package sgc.portal.ei.ei0203.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.ei0203.dto.EI02030001USearch;
import sgc.portal.ei.ei0203.model.EI02030001U;
import sgc.portal.ei.ei0203.persistence.EI02030001UMapper;

@Service
public class EI02030001UService {
	private final EI02030001UMapper mapper;

	public EI02030001UService(EI02030001UMapper mapper) {
		this.mapper = mapper;
	}

	//대여금 내역 조회
	public List<EasyHashMap<String, Object>> getList1(EI02030001USearch search) {
		return mapper.getList1(search);
	}
	
	//상환 내역 조회
	public List<EasyHashMap<String, Object>> getList2(EI02030001USearch search) {
		return mapper.getList2(search);
	}

	//대여금 내역 저장
	@Transactional
	public Map<String, String> save1(List<EI02030001U> param) {

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

		for (EI02030001U eI02030001U : param) {
			switch (eI02030001U.getRowStatus()) {
			case GridRowStatus.CREATED:
				mapper.insert1(eI02030001U);
				break;
			case GridRowStatus.UPDATED:
				mapper.update1(eI02030001U);
				break;
			case GridRowStatus.DELETED:
				mapper.delete3(eI02030001U);
				mapper.delete1(eI02030001U);
				break;
			default:
				break;
			}
		}
		return result;
	}
	
	//상환 내역 저장
	@Transactional
	public Map<String, String> save2(List<EI02030001U> param) {
		
		Map<String, String> result = new HashMap<>();
		
		for (EI02030001U eI02030001U : param) {
			switch (eI02030001U.getRowStatus()) {
			case GridRowStatus.CREATED:
				mapper.insert2(eI02030001U);
				break;
			case GridRowStatus.UPDATED:
				mapper.update2(eI02030001U);
				break;
			case GridRowStatus.DELETED:
				mapper.delete2(eI02030001U);
				break;
			default:
				break;
			}
		}
		return result;
	}
	
	//대여금,상환 내역 복사
	@Transactional
	public Map<String, String> copy(EI02030001USearch search) {
		Map<String, String> result = new HashMap<>();
		if(mapper.getList3(search) == 0) {
			result.put("result", "전월 대여금 내역이 없습니다.");
			return result;
		}
		mapper.deleteRepyCopy(search);
		mapper.deleteCopy(search);
		
		mapper.insertCopy(search);
		mapper.insertRepyCopy(search);
		 
		return result;
	}
}
