package sgc.portal.cd.cd0101.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.cd.cd0101.dto.CD01010001USearch;
import sgc.portal.cd.cd0101.model.CD01010001U;
import sgc.portal.cd.cd0101.model.CD01010002U;
import sgc.portal.cd.cd0101.model.CD01010003U;
import sgc.portal.cd.cd0101.persistence.CD01010001UMapper;

@Service
public class CD01010001UService {

	private final CD01010001UMapper mapper;

	public CD01010001UService(CD01010001UMapper mapper) {
		this.mapper = mapper;
	}

	//공종 조회
	public List<EasyHashMap<String, Object>> selectConDayMstJobtypList(CD01010001USearch search) {
		return mapper.selectConDayMstJobtypList(search);
	}

	//공종 등록 변경
	@Transactional
	public Map<String, String> saveConDayMstJobtyp(List<CD01010001U> CD01010001U) {

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

		for (CD01010001U cD01010001U : CD01010001U) {
			switch (cD01010001U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertConDayMstJobtyp(cD01010001U);
				break;
				
			case GridRowStatus.UPDATED:
				mapper.updateConDayMstJobtyp(cD01010001U);
				break;
				
			case GridRowStatus.DELETED:
				int count = mapper.selectConDayMstJobtypCount(cD01010001U);
				if (count > 0) {
					result.put("result", "N");
				} else if (count == 0) {
					result.put("result", "Y");
					mapper.deleteConDayMstJobtyp(cD01010001U);
				}
				break;
				
			default:
				break;
			}
		}
		return result;
	}
	
	//장비 조회
	public List<EasyHashMap<String, Object>> selectConDayMstEqpmList(CD01010001USearch search) {
		return mapper.selectConDayMstEqpmList(search);
	}

	//장비 등록 변경
	@Transactional
	public Map<String, String> saveConDayMstEqpm(List<CD01010002U> CD01010002U) {

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

		for (CD01010002U cD01010002U : CD01010002U) {

			switch (cD01010002U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertConDayMstEqpm(cD01010002U);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateConDayMstEqpm(cD01010002U);
				break;

			case GridRowStatus.DELETED:
				int count = mapper.selectConDayMstEqpmCount(cD01010002U);
				if (count > 0) {
					result.put("result", "N");
				} else if (count == 0) {
					result.put("result", "Y");
					mapper.deleteConDayMstEqpm(cD01010002U);
				}
				break;
			default:
				break;
			}
		}
		return result;
	}

	//자재 조회
	public List<EasyHashMap<String, Object>> selectConDayMstBdmtList(CD01010001USearch search) {
		return mapper.selectConDayMstBdmtList(search);
	}

	//자재 등록 변경
	@Transactional
	public Map<String, String> saveConDayMstBdmt(List<CD01010003U> CD01010003U) {

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

		for (CD01010003U cD01010003U : CD01010003U) {

			switch (cD01010003U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertConDayMstBdmt(cD01010003U);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateConDayMstBdmt(cD01010003U);
				break;

			case GridRowStatus.DELETED:
				int count = mapper.selectConDayMstBdmtCount(cD01010003U);
				if (count > 0) {
					result.put("result", "N");
				} else if (count == 0) {
					result.put("result", "Y");
					mapper.deleteConDayMstBdmt(cD01010003U);
				}
				break;
			default:
				break;
			}
		}
		return result;
	}
}