package sgc.portal.as.as0102.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sgc.portal.as.as0102.dto.AS01020001USearch;
import sgc.portal.as.as0102.model.AS01020001U;
import sgc.portal.as.as0102.model.AS01020002U;
import sgc.portal.as.as0102.persistence.AS01020001UMapper;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.security.PasswordEncryptionService;

@Service
public class AS01020001UService {
    
	private final AS01020001UMapper mapper;
	private final PasswordEncryptionService passwordEncryptionService;

    public AS01020001UService(AS01020001UMapper mapper, PasswordEncryptionService passwordEncryptionService) {
        this.mapper = mapper;
        this.passwordEncryptionService = passwordEncryptionService;
    }
    
    //협력업체관리 조회
    public List<EasyHashMap<String, Object>> selectSmComCovendList(AS01020001USearch search) {
        return mapper.selectSmComCovendList(search);
    }
    
    //협력업체관리팝업 조회
    public EasyHashMap<String, Object> selectSmComCovendPopList(AS01020001USearch search) {
        return mapper.selectSmComCovendPopList(search);
    }
    
    //협력업체 업체유형 조회
    public List<EasyHashMap<String, Object>> selectSmComCovendTypePopList(AS01020001USearch search) {
        return mapper.selectSmComCovendTypePopList(search);
    }
    
    //협력업체 담당자정보 조회
    public List<EasyHashMap<String, Object>> selectSmComCovendChrgrPopList(AS01020001USearch search) {
    	List<EasyHashMap<String, Object>> modList = mapper.selectSmComCovendChrgrPopList(search);
    	for(EasyHashMap<String, Object> item : modList) {
    		String chrgrTypeCd = (String) item.get("chrgrTypeCd");
    		if ("".equals(chrgrTypeCd) || chrgrTypeCd == null) {
    			chrgrTypeCd = "AS1";
    			item.put("chrgr_Type_Cd", chrgrTypeCd);
    		}
    	}
    	return modList;
        
    }
    
    public List<EasyHashMap<String, Object>> selectSmComCovendChrgrUserIdList(AS01020001USearch search) {
        return mapper.selectSmComCovendChrgrUserIdList(search);
    }
    
    //협력업체관리팝업 등록 변경
    @Transactional
    public Map<String, String> saveSmComCovend (AS01020001U cS00010001U) {
		Map<String, String> result = new HashMap<>();
		int count1 = mapper.selectSmComCovendCount(cS00010001U);
		if(count1 > 0){
			mapper.updateSmComCovend(cS00010001U);
			
			for (AS01020001U vo2 : cS00010001U.getDsList2()) {
				vo2.setCovendId(cS00010001U.getCovendId());	
				
				int count2 = mapper.selectSmComCovendTypeCount(vo2);
				
				if(count2 > 0) {
					mapper.updateSmComCovendType(vo2);
		    	}else {
		    		mapper.insertSmComCovendType(vo2);	
		    	}
			}
		}
		return result;
	}
    
    //협력업체 담당자정보팝업 등록 변경 
    @Transactional
    public Map<String, String> saveSmComCovendChrgr (List<AS01020002U> AS01020002U) {
    	
    	String tmpPwd 		= "";
    	String encryptedPwd = "";
    	
		Map<String, String> result = new HashMap<>();

		for (AS01020002U cS00010002U : AS01020002U) {

			switch (cS00010002U.getRowStatus()) {
			case GridRowStatus.UPDATED:
				if(cS00010002U.getGuBun().equals("Y")) {
				    tmpPwd = "sgc1234!@"; // generateRandomPassword(5);
		            encryptedPwd = passwordEncryptionService.encryptPasswordWithSalt(tmpPwd);
					cS00010002U.setPasswd(encryptedPwd);
				}
				mapper.updateSmComCovendChrgr(cS00010002U);
				break;
			default:
				break;
			}
		}
		return result;
	}
    
    //협력업체 첨부파일 등록
    @Transactional
    public Map<String, String> updateSmComCovendAflId (AS01020001U cS00010001U) {
		Map<String, String> result = new HashMap<>();
		mapper.updateSmComCovendAflId(cS00010001U);
		return result;
	}
}