package sgc.portal.cs.cs0001.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.cs0001.persistence.CS00010001UMapper;
import sgc.portal.cs.cs0103.model.CS01030000U;
import sgc.portal.cs.cs0001.dto.CS00010001USearch;
import sgc.portal.cs.cs0001.model.CS00010001U;
import sgc.portal.cs.cs0001.model.CS00010002U;
import sgc.portal.security.PasswordEncryptionService;

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

@Service
public class CS00010001UService {
    
	private final CS00010001UMapper mapper;
	private final PasswordEncryptionService passwordEncryptionService;

    public CS00010001UService(CS00010001UMapper mapper, PasswordEncryptionService passwordEncryptionService) {
        this.mapper = mapper;
        this.passwordEncryptionService = passwordEncryptionService;
    }
    
    //협력업체관리 조회
    public List<EasyHashMap<String, Object>> selectSmComCovendList(CS00010001USearch search) {
        return mapper.selectSmComCovendList(search);
    }
    
    //협력업체관리팝업 조회
    public EasyHashMap<String, Object> selectSmComCovendPopList(CS00010001USearch search) {
        return mapper.selectSmComCovendPopList(search);
    }
    
    //협력업체 업체유형 조회
    public List<EasyHashMap<String, Object>> selectSmComCovendTypePopList(CS00010001USearch search) {
        return mapper.selectSmComCovendTypePopList(search);
    }
    
    //협력업체 담당자정보 조회
    public List<EasyHashMap<String, Object>> selectSmComCovendChrgrPopList(CS00010001USearch search) {
        return mapper.selectSmComCovendChrgrPopList(search);
    }
    
    public List<EasyHashMap<String, Object>> selectSmComCovendChrgrUserIdList(CS00010001USearch search) {
        return mapper.selectSmComCovendChrgrUserIdList(search);
    }
    
    public List<EasyHashMap<String, Object>> selectSmComCovendCorpRegNoList(CS00010001USearch search) {
        return mapper.selectSmComCovendCorpRegNoList(search);
    }
    
    //협력업체관리팝업 등록 변경
    @Transactional
    public Map<String, String> saveSmComCovend (CS00010001U cS00010001U) {

		Map<String, String> result = new HashMap<>();
		
		int count1 = mapper.selectSmComCovendCount(cS00010001U);
		
		if(count1 == 0) {
			
			String id = mapper.selectSmComCovendId(cS00010001U);
			cS00010001U.setCovendId(id);
			
			mapper.insertSmComCovend(cS00010001U);
			
			for (CS00010001U vo2 : cS00010001U.getDsList2()) {
				vo2.setCovendId(id);
				mapper.insertSmComCovendType(vo2);
			}
			
			result.put("count", "0");
			result.put("covendId", id);
			
		}else if(count1 > 0){
			mapper.updateSmComCovend(cS00010001U);
			
			for (CS00010001U 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> deleteSmComCovend (CS00010001U cS00010001U) {
    	Map<String, String> result = new HashMap<>();

    	mapper.deleteSmComCovendChrgr2(cS00010001U);
    	mapper.deleteSmComCovendType(cS00010001U);
    	
    	int count = mapper.selectSmComCovendAflIdCount(cS00010001U);
    	if(count > 0) {
    		mapper.deleteSmComCovendAflId(cS00010001U);
    	}
    	
    	mapper.deleteSmComCovend(cS00010001U);
    	return result;
    }
    
    //협력업체 담당자정보팝업 등록 변경 
    @Transactional
    public Map<String, String> saveSmComCovendChrgr (List<CS00010002U> CS00010002U) {
    	
    	String tmpPwd 		= "";
    	String encryptedPwd = "";
    	
		Map<String, String> result = new HashMap<>();

		for (CS00010002U cS00010002U : CS00010002U) {

			switch (cS00010002U.getRowStatus()) {

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