package sgc.portal.cs.cs0401.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.cs0401.persistence.CS04010001UMapper;
import sgc.portal.cs.cs0401.model.CS04010001U;
import sgc.portal.cs.cs0401.dto.CS04010001USearch;
import sgc.portal.cs.cs0401.model.CS04010001U;
import sgc.portal.cs.cs0401.model.CS04010002U;

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

@Service
public class CS04010001UService {
    
	private final CS04010001UMapper mapper;

    public CS04010001UService(CS04010001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //담당자별주간보고  조회
    public EasyHashMap<String, Object> selectTcsWeekWorkList(CS04010001USearch search) {
        return mapper.selectTcsWeekWorkList(search);
    }
    
    //담당자별주간보고상세 조회
    public List<EasyHashMap<String, Object>> selectTcsWeekWorkRptList1(CS04010001USearch search) {
        return mapper.selectTcsWeekWorkRptList1(search);
    }
    
    public List<EasyHashMap<String, Object>> selectTcsWeekWorkRptList2(CS04010001USearch search) {
        return mapper.selectTcsWeekWorkRptList2(search);
    }
    
    public List<EasyHashMap<String, Object>> selectTcsWeekWorkCommList(CS04010001USearch search) {
        return mapper.selectTcsWeekWorkCommList(search);
    }
    
    public List<EasyHashMap<String, Object>> selectTcsWeekWorkCommList2(CS04010001USearch search) {
        return mapper.selectTcsWeekWorkCommList2(search);
    }
    
    //담당자별주간보고 등록 변경 
    @Transactional
	public Map<String, String> saveTcsWeekWork1(CS04010001U cS04010001U) {

		Map<String, String> result = new HashMap<>();
		CS04010002U cS04010002U = new CS04010002U();
		cS04010001U.setWorkBasedateFrom(cS04010001U.getWorkBasedateFrom2());
		int count1 = mapper.selectTcsWeekWorkCount(cS04010001U);
		
		if(count1 > 0) {
			result.put("count", "0");
			result.put("message", "해당 주의 주간 보고는 이미 존재합니다.");
		}else if(count1 == 0){
			
			String workBasedateTo = mapper.selectworkBasedateTo(cS04010001U);
			cS04010001U.setWorkBasedateTo(workBasedateTo);
			mapper.insertTcsWeekWork2(cS04010001U);
			
			//금주등록
			List<EasyHashMap<String, Object>> weekList1 = mapper.selectCopyWeekCountList(cS04010001U);
			List<EasyHashMap<String, Object>> weekList3 = mapper.selectCopyWeekCountList2(cS04010001U);
			
			int index = 0;
			for(EasyHashMap<String, Object> vo1 : weekList1 ) {
				cS04010002U.setCmpnCd(cS04010001U.getCmpnCd()); 
				cS04010002U.setApicEmpno(cS04010001U.getApicEmpno());
				cS04010002U.setWorkBasedateFrom1(cS04010001U.getWorkBasedateFrom1());
				cS04010002U.setWorkBasedateFrom2(cS04010001U.getWorkBasedateFrom2());
				cS04010002U.setWorkDate1((String)vo1.get("rptDay"));
				cS04010002U.setWorkDate2((String)weekList3.get(index).get("rptDay"));
				mapper.insertTcsWeekWorkRpt2(cS04010002U);
				index++;
			}
			
			//차주등록
			List<EasyHashMap<String, Object>> weekList2 = mapper.selectCopyNextWeekCountList(cS04010001U);
			for(EasyHashMap<String, Object> vo2 : weekList2 ) {
				cS04010002U.setCmpnCd(cS04010001U.getCmpnCd()); 
				cS04010002U.setApicEmpno(cS04010001U.getApicEmpno());
				cS04010002U.setWorkBasedateFrom(cS04010001U.getWorkBasedateFrom2());
				cS04010002U.setWorkDate((String)vo2.get("rptDay"));
				mapper.insertTcsWeekWorkRpt(cS04010002U);
			}
			
		}
		return result;
	}
    
    @Transactional
	public Map<String, String> saveTcsWeekWork2(CS04010001U cS04010001U) {

		Map<String, String> result = new HashMap<>();
		CS04010002U cS04010002U = new CS04010002U();
		
		int count1 = mapper.selectTcsWeekWorkCount(cS04010001U);
		if(count1 > 0) {
			result.put("count", "0");
			result.put("message", "해당 주의 주간 보고는 이미 존재합니다.");
		}else if(count1 == 0){
			
			String workBasedateTo = mapper.selectworkBasedateTo(cS04010001U);
			cS04010001U.setWorkBasedateTo(workBasedateTo);
			mapper.insertTcsWeekWork(cS04010001U);
			
			List<EasyHashMap<String, Object>> weekList = mapper.selectWeekAndNextWeekCountList(cS04010001U);
			
			for(EasyHashMap<String, Object> vo1 : weekList ) {
				cS04010002U.setCmpnCd(cS04010001U.getCmpnCd()); 
				cS04010002U.setApicEmpno(cS04010001U.getApicEmpno());
				cS04010002U.setWorkBasedateFrom(cS04010001U.getWorkBasedateFrom());
				cS04010002U.setWorkDate((String)vo1.get("rptDay"));
				mapper.insertTcsWeekWorkRpt(cS04010002U);
			}
			
		}
		return result;
	}
    
    @Transactional
	public Map<String, String> saveTcsWeekWork3(CS04010001U cS04010001U) {

		Map<String, String> result = new HashMap<>();
		mapper.updateTcsWeekWork(cS04010001U);
		
		for(CS04010002U vo1 : cS04010001U.getDsList1()) {
			mapper.updateTcsWeekWorkRpt(vo1);
		}
		
		for(CS04010002U vo2 : cS04010001U.getDsList2()) {
			mapper.updateTcsWeekWorkRpt(vo2);	
		}

		return result;
	}
    
    //삭제 
    @Transactional
	public Map<String, String> deleteTcsWeekWork(CS04010001U cS04010001U) {

		Map<String, String> result = new HashMap<>();
		mapper.deleteTcsWeekWorkRpt(cS04010001U);
		mapper.deleteTcsWeekWork(cS04010001U);
		return result;
	}
}