package sgc.portal.cs.cs0403.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.cs0403.persistence.CS04030001UMapper;
import sgc.portal.cs.cs0403.model.CS04030001U;
import sgc.portal.cs.cs0403.dto.CS04030001USearch;

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

@Service
public class CS04030001UService {
    
	private final CS04030001UMapper mapper;

    public CS04030001UService(CS04030001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //주간 현장별 주요현안 조회
    public List<EasyHashMap<String, Object>> selectTcsWeekWorkMainisuList(CS04030001USearch search) {
        return mapper.selectTcsWeekWorkMainisuList(search);
    }
    
    //주간 현장별 주요현안 상세 조회
    public List<EasyHashMap<String, Object>> selectTcsWeekWorkMainisuDtlList(CS04030001USearch search) {
        return mapper.selectTcsWeekWorkMainisuDtlList(search);
    }
    
    public List<EasyHashMap<String, Object>> selectTcsWeekWorkMainisuCommList1(CS04030001USearch search) {
        return mapper.selectTcsWeekWorkMainisuCommList1(search);
    }
    
    public List<EasyHashMap<String, Object>> selectTcsWeekWorkMainisuCommList2(CS04030001USearch search) {
        return mapper.selectTcsWeekWorkMainisuCommList2(search);
    }
    
    //주간 현장별 주요현안 등록 변경 
    @Transactional
	public Map<String, String> saveTcsWeekWorkMainisu1(CS04030001U cS04030001U) {

		Map<String, String> result = new HashMap<>();
		cS04030001U.setWorkBasedate(cS04030001U.getWorkBasedate2());
		int count1 = mapper.selectTcsWeekWorkMainisuCount(cS04030001U);
		
		if(count1 > 0) {
			result.put("count", "0");
			result.put("message", "주간 현장별 주요 현안이 이미 존재합니다.");
		}else if(count1 == 0){
			
			String workBasedateTo = mapper.selectworkBasedateTo(cS04030001U);
			cS04030001U.setWorkBasedateTo(workBasedateTo);
			mapper.insertTcsWeekWorkMainisu2(cS04030001U);	
		}
		return result;
	}
    
    //주간 현장별 주요현안 등록 변경 
    @Transactional
	public Map<String, String> saveTcsWeekWorkMainisu2(CS04030001U cS04030001U) {

		Map<String, String> result = new HashMap<>();
		int count1 = mapper.selectTcsWeekWorkMainisuCount(cS04030001U);
		
		if(count1 > 0) {
			result.put("count", "0");
			result.put("message", "주간 현장별 주요 현안이 이미 존재합니다.");
		}else if(count1 == 0){
			
			String workBasedateTo = mapper.selectworkBasedateTo(cS04030001U);
			cS04030001U.setWorkBasedateTo(workBasedateTo);
			mapper.insertTcsWeekWorkMainisu(cS04030001U);	
		}
		return result;
	}
    
    //주간 현장별 주요현안 등록 변경 
    @Transactional
	public Map<String, String> saveTcsWeekWorkMainisu3 (List<CS04030001U> CS04030001U) {

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

		for (CS04030001U cS04030001U : CS04030001U) {

			switch (cS04030001U.getRowStatus()) {

			case GridRowStatus.CREATED:
				String workBasedateTo = mapper.selectworkBasedateTo(cS04030001U);
				cS04030001U.setWorkBasedateTo(workBasedateTo);
				mapper.insertTcsWeekWorkMainisu(cS04030001U);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsWeekWorkMainisu(cS04030001U);
				break;

			case GridRowStatus.DELETED:
				mapper.deleteTcsWeekWorkMainisu(cS04030001U);
				break;
			default:
				break;
			}
		}
		return result;
	}
}