package sgc.portal.cs.cs0201.service;

import org.springframework.beans.factory.annotation.Autowired;
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.com.com.persistence.ComAuthUserMngMapper;
import sgc.portal.cs.cs0201.persistence.CS02010001UMapper;
import sgc.portal.cs.cs0201.dto.CS02010001USearch;
import sgc.portal.cs.cs0201.model.CS02010001U;
import sgc.portal.cs.cs0201.model.CS02010003U;
import sgc.portal.cs.cs0201.model.CS02010004U;
import sgc.portal.cs.cs0201.model.CS02010005U;
import sgc.portal.cs.cs0201.model.CS02010006U;
import sgc.portal.cs.cs0201.model.CS02010007U;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CS02010001UService {

	private final CS02010001UMapper mapper;

	public CS02010001UService(CS02010001UMapper mapper) {
		this.mapper = mapper;
	}

	// 현장협력업체정보 팝업 조회
	public List<EasyHashMap<String, Object>> selectTcsSitePopList(CS02010001USearch search) {
		return mapper.selectTcsSitePopList(search);
	}
	
	//현장협력업체정보  조회
	public List<EasyHashMap<String, Object>> selectSmComCovendSiteTradeList(CS02010001USearch search) {
		return mapper.selectSmComCovendSiteTradeList(search);
	}
	
	//협력 업체 담당자  조회
	public EasyHashMap<String, Object> selectSmComCovendMapList(CS02010001USearch search) {
		return mapper.selectSmComCovendMapList(search);
	}
	
	public List<EasyHashMap<String, Object>> selectTcsSiteTradeChrgrList(CS02010001USearch search) {
		return mapper.selectTcsSiteTradeChrgrList(search);
	}
	
	public List<EasyHashMap<String, Object>> selectConvendTcsSiteTradeList(CS02010001USearch search) {
		return mapper.selectConvendTcsSiteTradeList(search);
	}
	
	public List<EasyHashMap<String, Object>> selectConvendTcsSiteTradeItemList(CS02010001USearch search) {
		return mapper.selectConvendTcsSiteTradeItemList(search);
	}
	
	public List<EasyHashMap<String, Object>> selectConvendTcsSiteTradeGuarList(CS02010001USearch search) {
		return mapper.selectConvendTcsSiteTradeGuarList(search);
	}
	
	@Transactional
	public Map<String, String> saveTcsSiteTrade(CS02010004U cS02010004U) {
		Map<String, String> result = new HashMap<>();
		
		mapper.updateSmComCovend(cS02010004U);
		
		int count1 = 0;
		if(cS02010004U.getContSeq().equals("")) {
			count1 = mapper.selectContSeqCount(cS02010004U);	
		}
		
		for (CS02010004U vo1 : cS02010004U.getDsList1()) {
			switch (vo1.getRowStatus()) {

			case GridRowStatus.CREATED:
				
				if(cS02010004U.getContSeq().equals("")) {
					vo1.setContSeq(Integer.toString(count1));
				}
				
				mapper.insertTcsSiteTrade(vo1);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsSiteTrade(vo1);
				break;

			case GridRowStatus.DELETED:
				mapper.deleteTcsSiteTradeGuar2(vo1);
				mapper.deleteTcsSiteTradeItem2(vo1);
				mapper.deleteTcsSiteTrade(vo1);
				break;
			default:
				break;
			}
		}
				
		for (CS02010005U vo2 : cS02010004U.getDsList2()) {
			
			switch (vo2.getRowStatus()) {
					
			case GridRowStatus.CREATED:
				
				if(cS02010004U.getContSeq().equals("")) {
					vo2.setContSeq(Integer.toString(count1));
				}else {
					String count2 = mapper.selectTcsSiteSeqCount(cS02010004U);
					vo2.setContSeq(count2);
				}
				
				mapper.insertTcsSiteTradeItem(vo2);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsSiteTradeItem(vo2);
				break;

			case GridRowStatus.DELETED:
				mapper.deleteTcsSiteTradeItem(vo2);
				break;
			default:
				break;
			}
		}
		
		
		for (CS02010006U vo3 : cS02010004U.getDsList3()) {
			
			switch (vo3.getRowStatus()) {

			case GridRowStatus.CREATED:
				
				if(cS02010004U.getContSeq().equals("")) {
					vo3.setContSeq(Integer.toString(count1));
				}else {
					String count3 = mapper.selectTcsSiteSeqCount(cS02010004U);
					vo3.setContSeq(count3);
				}

				mapper.insertTcsSiteTradeGuar(vo3);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsSiteTradeGuar(vo3);
				break;

			case GridRowStatus.DELETED:
				mapper.deleteTcsSiteTradeGuar(vo3);
				break;
			default:
				break;
			}
		}
		
		for (CS02010003U vo4 : cS02010004U.getDsList4()) {
			switch (vo4.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertSmComCovendChrgr(vo4);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateSmComCovendChrgr(vo4);
				break;

			case GridRowStatus.DELETED:
				break;
			default:
				break;
			}
		}
		return result;
	}
	
	@Transactional
	public Map<String, String> deleteTcsSiteTrade(CS02010004U cS02010004U) {
		Map<String, String> result = new HashMap<>();
		
		int count1 = mapper.selectTcsSiteTradeCheckCount(cS02010004U);
		
		if(count1 > 0) {
			result.put("count", "0");
			result.put("message", "본공사는 삭제가 불가능합니다.");
		}else if(count1 == 0) {
			//mapper.deleteTcsSiteTradeChrgr2(cS02010004U);
			mapper.deleteTcsSiteTradeGuar2(cS02010004U);
			mapper.deleteTcsSiteTradeItem2(cS02010004U);
			mapper.deleteTcsSiteTrade2(cS02010004U);
		}		
		return result;
	}
	
	@Transactional
	public Map<String, String> updateIfTcsSiteTrade(CS02010004U cS02010004U) {
		Map<String, String> result = new HashMap<>();
		mapper.updateIfTcsSiteTrade(cS02010004U);	
		return result;
	}
	
	//협력 업체  조회
	public List<EasyHashMap<String, Object>> selectSmComCovendPopList(CS02010001USearch search) {
		return mapper.selectSmComCovendPopList(search);
	}
	
	public int selectTcsSiteTradeNewCheckCount(CS02010001USearch search) {
		return mapper.selectTcsSiteTradeNewCheckCount(search);
	}
	
	public EasyHashMap<String, Object> selectSmComCovendMapNewList(CS02010001USearch search) {
		return mapper.selectSmComCovendMapNewList(search);
	}
	
	public List<EasyHashMap<String, Object>> selectSmComCovendChrgrNewList(CS02010001USearch search) {
		return mapper.selectSmComCovendChrgrNewList(search);
	}
	
	@Transactional
	public Map<String, String> convendTcsSiteTradefileCheck(CS02010006U cS02010006U) {
		Map<String, String> result = new HashMap<>();
		
		int count1 = mapper.convendTcsSiteTradefileCheckCount(cS02010006U);
		if(count1 == 0) {
			mapper.updateTcsSiteTradefileCheckCount(cS02010006U);	
		}

		return result;
	}
	
	public int selectSmComCovendAS1(CS02010001USearch search) {
		return mapper.selectSmComCovendAS1(search);
	}
	
	public int selectSmComCovendAS2(CS02010001USearch search) {
		return mapper.selectSmComCovendAS2(search);
	}
	
	public int selectSmComCovendAS3(CS02010001USearch search) {
		return mapper.selectSmComCovendAS3(search);
	}
}