package sgc.portal.cs.cs0103.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.cs0103.dto.CS01030001USearch;
import sgc.portal.cs.cs0103.model.CS01030000U;
import sgc.portal.cs.cs0103.model.CS01030001U;
import sgc.portal.cs.cs0103.model.CS01030002U;
import sgc.portal.cs.cs0103.model.CS01030003U;
import sgc.portal.cs.cs0103.model.CS01030004U;
import sgc.portal.cs.cs0103.model.CS01030005U;
import sgc.portal.cs.cs0103.persistence.CS01030001UMapper;
import sgc.portal.cs.cs0104.model.CS01040001U;

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

@Service
public class CS01030001UService {

	private final CS01030001UMapper mapper;

	public CS01030001UService(CS01030001UMapper mapper) {
		this.mapper = mapper;
	}

	// 하자관리현장 조회
	public List<EasyHashMap<String, Object>> selectTcsSiteList(CS01030001USearch search) {
		return mapper.selectTcsSiteList(search);
	}
	
	// 하자관리현장팝업조회 
	public List<EasyHashMap<String, Object>> selectZehrComDeptToTcsSiteList(CS01030001USearch search) {
		return mapper.selectZehrComDeptToTcsSiteList(search);
	}
	
	// 상품분류공통조회 
	public List<EasyHashMap<String, Object>> selectProbNoList(CS01030001USearch search) {
		return mapper.selectProbNoList(search);
	}

	// 하자관리현장 등록 변경
	@Transactional
	public Map<String, String> saveTcsSite (List<CS01030000U> CS01030000U) {

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

		for (CS01030000U cS01030000U : CS01030000U) {

			switch (cS01030000U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertTcsSite(cS01030000U);
				break;
			case GridRowStatus.UPDATED:
				mapper.updateTcsSite(cS01030000U);
				break;
			case GridRowStatus.DELETED:
				
				mapper.deleteTcsSiteStdLjobtyp2(cS01030000U);
				mapper.deleteTcsSiteStdLoc2(cS01030000U);
				mapper.deleteTcsSiteStdJobtyp2(cS01030000U);
				mapper.deleteTcsSiteStdType2(cS01030000U);
				mapper.deleteTcsSiteStdCaus2(cS01030000U);
				
				mapper.deleteTcsSite(cS01030000U);
				break;
			default:
				break;
			}
		}
		return result;
	}
	
	
	
	//하자대공종
	//하자대공종  조회
	public List<EasyHashMap<String, Object>> selectTcsStdLjobtypList(CS01030001USearch search) {
		return mapper.selectTcsStdLjobtypList(search);
	}
	
	//하자대공종현장  조회
	public List<EasyHashMap<String, Object>> selectTcsSiteStdLjobtypList(CS01030001USearch search) {
		return mapper.selectTcsSiteStdLjobtypList(search);
	}
	
	
	// 하자대공종현장 입력
	@Transactional
	public Map<String, String> saveTcsSiteStdLjobtyp(CS01030001U cS01030001U) {

		Map<String, String> result = new HashMap<>();
		
		for (CS01030001U vo1 : cS01030001U.getDsList1()) {
			
			vo1.setSiteCd(cS01030001U.getSiteCd());
			int count1 = mapper.selectTcsSiteStdLjobtypCount(vo1);
			
			if(count1 == 0) {
				mapper.insertTcsSiteStdLjobtyp(vo1);
			}else if(count1 > 0){
				result.put("count", "0");
				result.put("message", "하자 현장 대공종이 이미 존재합니다.");
			}
		}
		return result;
	}
	
	// 하자대공종현장 삭제
	public Map<String, String> deleteTcsSiteStdLjobtyp(CS01030001U cS01030001U) {
		
		Map<String, String> result = new HashMap<>();
		
		for (CS01030001U vo1 : cS01030001U.getDsList1()) {
			mapper.deleteTcsSiteStdLjobtyp(vo1);
		}
		
		return result;	
	}
	
	//위치
	//위치  조회
	public List<EasyHashMap<String, Object>> selectTcsStdLocList(CS01030001USearch search) {
		return mapper.selectTcsStdLocList(search);
	}
	
	//하자위치현장  조회
	public List<EasyHashMap<String, Object>> selectTcsSiteStdLocList(CS01030001USearch search) {
		return mapper.selectTcsSiteStdLocList(search);
	}
	
	//하자위치현장 입력
	@Transactional
	public Map<String, String> saveTcsSiteStdLoc(CS01030002U cS01030002U) {

		Map<String, String> result = new HashMap<>();
		
		for (CS01030002U vo1 : cS01030002U.getDsList1()) {
			
			vo1.setSiteCd(cS01030002U.getSiteCd());
			int count1 = mapper.selectTcsSiteStdLocCount(vo1);
			
			if(count1 == 0) {
				mapper.insertTcsSiteStdLoc(vo1);
			}else if(count1 > 0){
				result.put("count", "0");
				result.put("message", "하자 현장 위치가 이미 존재합니다.");
			}
		}
		return result;
	}
	
	// 하자위치현장 삭제
	public Map<String, String> deleteTcsSiteStdLoc(CS01030002U cS01030002U) {
		
		Map<String, String> result = new HashMap<>();
		
		for (CS01030002U vo1 : cS01030002U.getDsList1()) {
			mapper.deleteTcsSiteStdLoc(vo1);
		}
		
		return result;	
	}
	
	

	//하자공종
	//하자공종  조회
	public List<EasyHashMap<String, Object>> selectTcsStdJobtypList(CS01030001USearch search) {
		return mapper.selectTcsStdJobtypList(search);
	}
	
	//하자공종현장  조회
	public List<EasyHashMap<String, Object>> selectTcsSiteStdJobtypList(CS01030001USearch search) {
		return mapper.selectTcsSiteStdJobtypList(search);
	}
	
	//하자공종현장 입력
	@Transactional
	public Map<String, String> saveTcsSiteStdJobtyp(CS01030003U cS01030003U) {
        Map<String, String> result = new HashMap<>();
    	if (cS01030003U.getDsList1() != null && cS01030003U.getDsList1().size() > 0) {
    	    cS01030003U.setCmpnCd(cS01030003U.getDsList1().get(0).getCmpnCd());
            int count = mapper.selectTcsSiteStdJobtypCount(cS01030003U);
            if(count == 0) {
                mapper.insertTcsSiteStdJobtyp(cS01030003U);
            } else {
                result.put("count", "" + count);
                result.put("message", "하자 공종이 이미 존재합니다.");
            }
    	} else {
    	    result.put("count", "0");
            result.put("message", "처리할 하자 공종이 없습니다.");
        }
		return result;
	}
	
	// 하자공종현장 삭제
	public Map<String, String> deleteTcsSiteStdJobtyp(CS01030003U cS01030003U) {
		
		Map<String, String> result = new HashMap<>();
		
		for (CS01030003U vo1 : cS01030003U.getDsList1()) {
			mapper.deleteTcsSiteStdJobtyp(vo1);
		}
		
		return result;	
	}
	
	

	//하자유형
	//하자유형  조회
	public List<EasyHashMap<String, Object>> selectTcsStdTypeList(CS01030001USearch search) {
		return mapper.selectTcsStdTypeList(search);
	}
	
	//하자유형현장  조회
	public List<EasyHashMap<String, Object>> selectTcsSiteStdTypeList(CS01030001USearch search) {
		return mapper.selectTcsSiteStdTypeList(search);
	}
	
	//하자유형현장 입력
	@Transactional
	public Map<String, String> saveTcsSiteStdType(CS01030004U cS01030004U) {

		Map<String, String> result = new HashMap<>();
		
		for (CS01030004U vo1 : cS01030004U.getDsList1()) {
			
			vo1.setSiteCd(cS01030004U.getSiteCd());
			int count1 = mapper.selectTcsSiteStdTypeCount(vo1);
			
			if(count1 == 0) {
				mapper.insertTcsSiteStdType(vo1);
			}else if(count1 > 0){
				result.put("count", "0");
				result.put("message", "하자 유형이 이미 존재합니다.");
			}
		}
		return result;
	}
	
	// 하자유형현장 삭제
	public Map<String, String> deleteTcsSiteStdType(CS01030004U cS01030004U) {
		
		Map<String, String> result = new HashMap<>();
		
		for (CS01030004U vo1 : cS01030004U.getDsList1()) {
			mapper.deleteTcsSiteStdType(vo1);
		}
		
		return result;	
	}
	
	
	

	//하자원인
	//하자원인  조회
	public List<EasyHashMap<String, Object>> selectTcsStdCausList(CS01030001USearch search) {
		return mapper.selectTcsStdCausList(search);
	}
	
	//하자원인현장  조회
	public List<EasyHashMap<String, Object>> selectTcsSiteStdCausList(CS01030001USearch search) {
		return mapper.selectTcsSiteStdCausList(search);
	}
	
	//하자원인현장 입력
	@Transactional
	public Map<String, String> saveTcsSiteStdCaus(CS01030005U cS01030005U) {

		Map<String, String> result = new HashMap<>();
		
		for (CS01030005U vo1 : cS01030005U.getDsList1()) {
			
			vo1.setSiteCd(cS01030005U.getSiteCd());
			int count1 = mapper.selectTcsSiteStdCausCount(vo1);
			
			if(count1 == 0) {
				mapper.insertTcsSiteStdCaus(vo1);
			}else if(count1 > 0){
				result.put("count", "0");
				result.put("message", "하자 원인이 이미 존재합니다.");
			}
		}
		return result;
	}
	
	// 하자원인현장 삭제
	public Map<String, String> deleteTcsSiteStdCaus(CS01030005U cS01030005U) {
		
		Map<String, String> result = new HashMap<>();
		
		for (CS01030005U vo1 : cS01030005U.getDsList1()) {
			mapper.deleteTcsSiteStdCaus(vo1);
		}
		
		return result;	
	}
	
}