package sgc.portal.cs.cs0104.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.cs0104.persistence.CS01040001UMapper;
import sgc.portal.cs.cs0104.dto.CS01040001USearch;
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 CS01040001UService {

	private final CS01040001UMapper mapper;

	public CS01040001UService(CS01040001UMapper mapper) {
		this.mapper = mapper;
	}

	// 사용자별현장권한 조회
	public List<EasyHashMap<String, Object>> selectSmComUserList(CS01040001USearch search) {
		return mapper.selectSmComUserList(search);
	}
	
	public List<EasyHashMap<String, Object>> selectTcsAuthUsersiteList(CS01040001USearch search) {
		return mapper.selectTcsAuthUsersiteList(search);
	}

	// 사용자별현장권한 변경
	@Transactional
	public Map<String, String> saveTcsAuthUsersite(CS01040001U cS01040001U) {

		Map<String, String> result = new HashMap<>();
		String userId = "";
		
		for (CS01040001U vo1 : cS01040001U.getDsList1()) {
			userId = vo1.getUserId();
			
			for (CS01040001U vo2 : cS01040001U.getDsList2()) {
				
				vo2.setUserId(userId);
				int count1 = mapper.selectTcsAuthUsersiteCount(vo2);
				
				if(count1 == 0) {
					mapper.insertTcsAuthUsersite(vo2);
				}else if(count1 > 0){
					mapper.updateTcsAuthUsersite(vo2);
				}	
			}
		}
		return result;
	}
}