package sgc.portal.cs.cspop.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
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.cs.cspop.model.CSPOPVo;
import sgc.portal.cs.cspop.model.CSTcsSiteTradeChrgrVo;
import sgc.portal.cs.cspop.dto.CSPOPSearch;
import sgc.portal.cs.cspop.persistence.CSPOPMapper;

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

@Service
public class CSPOPService {
    private final CSPOPMapper mapper;
    
    @Autowired
    private Environment environment;

    public CSPOPService(CSPOPMapper mapper) {
        this.mapper = mapper;
    }
    
    public List<EasyHashMap<String, Object>> getSiteList(CSPOPSearch search) {
        return mapper.getSiteList(search);
    }
    
    public List<EasyHashMap<String, Object>> getSiteAllList(CSPOPSearch search) {
        return mapper.getSiteAllList(search);
    }
    
    public List<EasyHashMap<String, Object>> getProdList(CSPOPSearch search) {
        return mapper.getProdList(search);
    }
    
    public List<EasyHashMap<String, Object>> getVendList(CSPOPSearch search) {
        return mapper.getVendList(search);
    }
    
    public List<EasyHashMap<String, Object>> getSmsList(CSPOPSearch search) {
        return mapper.getSmsList(search);
    }
    
    public List<EasyHashMap<String, Object>> getEmpPopLst(CSPOPSearch search) {
        return mapper.getEmpPopLst(search);
    }
    
    @Transactional
    public Map<String, String> sendSms(CSPOPVo cSPOPVo) {
    	
    	Map<String,String> result = new HashMap<>();
    	String[] activeProfiles = environment.getActiveProfiles();
    	
    	//운영에서만 SMS전송 테이블에 데이터 INSERT
		if(activeProfiles[0].equals("local")) {			//local
			result.put("status", "OK");
	    	result.put("gubun", cSPOPVo.getGubun());
	    	result.put("sconts", cSPOPVo.getSconts());
		} else if(activeProfiles[0].equals("dev")) {	//개발
			result.put("status", "OK");
	    	result.put("gubun", cSPOPVo.getGubun());
	    	result.put("sconts", cSPOPVo.getSconts());
		} else {										//운영
			insertSms(cSPOPVo);
			result.put("status", "OK");
	    	result.put("gubun", cSPOPVo.getGubun());
	    	result.put("sconts", cSPOPVo.getSconts());
		}
		
        return result;
    }
    
    @Transactional
    public Map<String, String> sendSmsM(List<CSPOPVo> CSPOPVo) {
    	
    	Map<String,String> result = new HashMap<>();
    	String[] activeProfiles = environment.getActiveProfiles();
    	
    	for (CSPOPVo cSPOPVo : CSPOPVo) {
    		//운영에서만 SMS전송 테이블에 데이터 INSERT
    		if(activeProfiles[0].equals("local")) {			//local
    			result.put("status", "OK");
    	    	result.put("gubun", cSPOPVo.getGubun());
    	    	result.put("sconts", cSPOPVo.getSconts());
    		} else if(activeProfiles[0].equals("dev")) {	//개발
    			result.put("status", "OK");
    	    	result.put("gubun", cSPOPVo.getGubun());
    	    	result.put("sconts", cSPOPVo.getSconts());
    		} else if(activeProfiles[0].equals("prod")) {	//운영
    			insertSms(cSPOPVo);
    			result.put("status", "OK");
    	    	result.put("gubun", cSPOPVo.getGubun());
    	    	result.put("sconts", cSPOPVo.getSconts());
    		} else {
    			result.put("status", "NO");
    	    	result.put("gubun", "");
    	    	result.put("sconts", "");
    		}
    	}
        return result;
    }
    
    private void insertSms(CSPOPVo cSPOPVo) {
        mapper.insertSms(cSPOPVo);
    }
    
    public List<EasyHashMap<String, Object>> selectGuarIntuNmList(CSPOPSearch search) {
		return mapper.selectGuarIntuNmList(search);
	}
    
    public List<EasyHashMap<String, Object>> selectSiteCovendList(CSPOPSearch search) {
		return mapper.selectSiteCovendList(search);
	}
    
    public List<EasyHashMap<String, Object>> selectTcsSiteTradeItemPopList(CSPOPSearch search) {
		return mapper.selectTcsSiteTradeItemPopList(search);
	}
    
    public List<EasyHashMap<String, Object>> selectCovendPopList(CSPOPSearch search) {
		return mapper.selectCovendPopList(search);
	}
    
    public EasyHashMap<String, Object> selectCommonPopSmComCovendList(CSPOPSearch search) {
		return mapper.selectCommonPopSmComCovendList(search);
	}
    
    public List<EasyHashMap<String, Object>> selectCommonPopTcsSiteTradeList(CSPOPSearch search) {
		return mapper.selectCommonPopTcsSiteTradeList(search);
	}
    
    public List<EasyHashMap<String, Object>> selectCommonPopTcsSiteTradeGuarList(CSPOPSearch search) {
		return mapper.selectCommonPopTcsSiteTradeGuarList(search);
	}
    
    public List<EasyHashMap<String, Object>> selectCommonPopSmComCovendChrgrList(CSPOPSearch search) {
		return mapper.selectCommonPopSmComCovendChrgrList(search);
	}
    
    @Transactional
	public Map<String, String> updateSmComCovendChrgrPop(CSTcsSiteTradeChrgrVo cSTcsSiteTradeChrgrVo) {
		Map<String, String> result = new HashMap<>();
		
		for (CSTcsSiteTradeChrgrVo vo1 : cSTcsSiteTradeChrgrVo.getDsList1()) {
			
			switch (vo1.getRowStatus()) {

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

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

			case GridRowStatus.DELETED:
				break;
			default:
				break;
			}
		}
		return result;
	}
    
    public int selectSmComCovendAS1(CSPOPSearch search) {
    	return mapper.selectSmComCovendAS1(search);
    }
    
    public int selectSmComCovendAS2(CSPOPSearch search) {
    	return mapper.selectSmComCovendAS2(search);
    }
    
    public int selectSmComCovendAS3(CSPOPSearch search) {
    	return mapper.selectSmComCovendAS3(search);
    }
}
