package sgc.portal.cw.cw0901.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.auth.model.SmComHdlgAuth;
import sgc.portal.com.auth.persistence.ComBusiHandleAuthMngMapper;
import sgc.portal.com.sym.mail.persistence.MailMapper;
import sgc.portal.com.sym.mail.service.MailService;
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 sgc.portal.cw.cw0901.persistence.CW09010001UMapper;
import sgc.portal.cw.cw0901.dto.CW09010001USearch;
import sgc.portal.cw.cw0901.model.CW09010001U;

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

@Service
public class CW09010001UService {

	private final CW09010001UMapper mapper;

    public CW09010001UService(CW09010001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //주간보고작성  조회
    public List<EasyHashMap<String, Object>> selectZehrWeekWorkList(CW09010001USearch search) {
        return mapper.selectZehrWeekWorkList(search);
    }
    
    //달력  조회
    public List<EasyHashMap<String, Object>> selectZehrWeekWorkMonthList(CW09010001USearch search) {
        return mapper.selectZehrWeekWorkMonthList(search);
    }
    
    public List<EasyHashMap<String, Object>> selectZehrWeekWorkAtchfileIdList(CW09010001USearch search) {
        return mapper.selectZehrWeekWorkAtchfileIdList(search);
    }

    public EasyHashMap<String, Object> selectZehrWeekWorkAtchfileIdMap(CW09010001USearch search) {
        return mapper.selectZehrWeekWorkAtchfileIdMap(search);
    }
    
    @Transactional
	public Map<String, String> saveZehrWeekWork(CW09010001U cW09010001U) {
		Map<String, String> result = new HashMap<>();
		
		int count1 = mapper.selectZehrWeekWorkRptCount(cW09010001U);
		
		if(count1 == 0) {
			
			String workBasedateTo = mapper.selectworkBasedateTo(cW09010001U);
			cW09010001U.setWorkBasedateTo(workBasedateTo);
			mapper.insertZehrWeekWork(cW09010001U);
			
			for (CW09010001U vo1 : cW09010001U.getDsList1()) {
				switch (vo1.getRowStatus()) {

				case GridRowStatus.CREATED:
					mapper.insertZehrWeekWorkRpt(vo1);
					break;

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

				case GridRowStatus.DELETED:
					mapper.deleteZehrWeekWorkRpt(vo1);
					break;
				default:
					break;
				}
			}
			
		}else if(count1 > 0) {
			
			for (CW09010001U vo1 : cW09010001U.getDsList1()) {
				switch (vo1.getRowStatus()) {

				case GridRowStatus.CREATED:
					mapper.insertZehrWeekWorkRpt(vo1);
					break;

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

				case GridRowStatus.DELETED:
					mapper.deleteZehrWeekWorkRpt(vo1);
					
					int count2 = mapper.selectZehrWeekWorkRptCount(cW09010001U);
					if(count2 == 0) {
						mapper.deleteZehrWeekWork(vo1);	
					}
					break;
				default:
					break;
				}
			}
		}
		return result;
	}
    
    
    @Transactional
   	public Map<String, String> updateZehrWeekWorkAtchfileId1(CW09010001U cW09010001U) {
   		Map<String, String> result = new HashMap<>();
   		
   		mapper.updateZehrWeekWorkAtchfileId(cW09010001U);;
   		return result;
    };
    

    @Transactional
   	public Map<String, String> updateZehrWeekWorkAtchfileId2(CW09010001U cW09010001U) {
   		Map<String, String> result = new HashMap<>();
   		
   		int count1 = mapper.selectZehrWeekWorkAtchfileIdCount(cW09010001U);
		if(count1 == 0) {
			cW09010001U.setAtchfileId("");
			mapper.updateZehrWeekWorkAtchfileId(cW09010001U);	
		}
		return result;
    };
    
    public List<EasyHashMap<String, Object>> selectZehrWeekWorkDeptCdList(CW09010001USearch search) {
    	String chk = mapper.selectDeptChk(search);
    	search.setChk(chk);
        return mapper.selectZehrWeekWorkDeptCdList(search);
    }
}