package sgc.portal.cs.cs0203.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.cs0203.persistence.CS02030001UMapper;
import sgc.portal.cs.cs0102.model.CS01020001U;
import sgc.portal.cs.cs0203.dto.CS02030001USearch;
import sgc.portal.cs.cs0203.model.CS02030001U;

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

@Service
public class CS02030001UService {
    
	private final CS02030001UMapper mapper;

    public CS02030001UService(CS02030001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //현장동호수등록 조회
    public List<EasyHashMap<String, Object>> selectTcsSiteDhsList(CS02030001USearch search) {
        return mapper.selectTcsSiteDhsList(search);
    }
    
    //현장동호수엑셀양식 조회
    public List<EasyHashMap<String, Object>> selectTcsSiteDhsPopList(CS02030001USearch search) {
        return mapper.selectTcsSiteDhsPopList(search);
    }
    

    //현장동호수등록 등록 변경 
    @Transactional
	public Map<String, String> saveTcsSiteDhs(List<CS02030001U> CS02030001U) {

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

		for (CS02030001U cS02030001U : CS02030001U) {

			switch (cS02030001U.getRowStatus()) {

			case GridRowStatus.CREATED:
				mapper.insertTcsSiteDhs(cS02030001U);
				break;

			case GridRowStatus.UPDATED:
				mapper.updateTcsSiteDhs(cS02030001U);
				break;

			case GridRowStatus.DELETED:
				
				int count1 = mapper.selectTcsDeftCount(cS02030001U); 
				
				if(count1 > 0) {
					result.put("count", "0");
					result.put("message", "하자 접수가 처리되어 삭제가 불가능합니다.");
				}else if(count1 == 0) {
					mapper.deleteTcsSiteDhs(cS02030001U);
				}
				break;
			default:
				break;
			}
		}
		return result;
	}
    
}