package com.eactive.eai.rms.common.meta;

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

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.rms.common.base.BaseService;

/**
 * 대시보드에서 사용하기 위한 DB Query 서비스
 * 거래로그건수, 타임아웃건수, 에러로그 건수를 조회하여 리턴한다.
 */

@Service("metaSyncService")
public class MetaSyncService extends BaseService {

    @Autowired
    @Qualifier("metaSyncDao")       
    private MetaSyncDao metaSyncDao;

    @Autowired
    @Qualifier("metaSelectDao")       
    private MetaSelectDao metaSelectDao;
    
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(MetaSyncService.class);

	public int syncItem() {

		try{
			HashMap<String,Object> param = new HashMap<String,Object>();
			
			String time = metaSyncDao.getLastUpdateTimeItem(param);
			param.put("time", time);
			List<HashMap<String,String>> data =metaSelectDao.selectSyncItem( param);
			int cnt =0;
			if(data != null){ //취약점소스 수정 2020.08.24
				for(HashMap <String,String> m : data){
					cnt +=metaSyncDao.updateSyncItem(m);
				}
			}
			
			return cnt;
		}catch(Exception e){
			//e.printStackTrace();
			logger.error("syncItem error", e); 
		}
		return 0;
	}

	public int syncTelegram() {
		try{
			HashMap<String,Object> param = new HashMap<String,Object>();
			String time = metaSyncDao.getLastUpdateTimeTelegram(param);
			param.put("time", time);
			List<HashMap<String,String>> data =metaSelectDao.selectSyncTelegram( param);
			int cnt =0;
			if(data != null)	{ //취약점소스 수정 2020.08.24
				for(HashMap <String,String> m : data){
					cnt +=metaSyncDao.updateSyncTelegram(m);
				}
			}
			return cnt;
		}catch(Exception e){
			//e.printStackTrace();
			logger.error("syncTelegram error", e); 
		}
		return 0;
	}

	public int syncStdErrMsg() {
		try{
			HashMap<String,Object> param = new HashMap<String,Object>();
			String time = metaSyncDao.getLastUpdateTimeStdErrMsg(param);
			param.put("time", time);
			List<HashMap<String,String>> data =metaSelectDao.selectSyncStdErrMsg( param);
			int cnt =0;
			if(data != null){ //취약점소스 수정 2020.08.24
				for(HashMap <String,String> m : data){
					cnt += metaSyncDao.updateSyncStdErrMsg(m);
				}
			}
			
			return cnt;
			
		}catch(Exception e){
			logger.error("Sync StdErrMsg Error", e);
			//e.printStackTrace();
		}
		return 0;
	}

	public int syncCode() {
		try{
			HashMap<String,Object> param = new HashMap<String,Object>();
			String time = metaSyncDao.getLastUpdateTimeCode(param);
			param.put("time", time);
			List<HashMap<String,String>> data =metaSelectDao.selectSyncCode( param);
			int cnt =0;
			
			if(data != null)	{ //취약점소스 수정 2020.08.24
				for(HashMap <String,String> m : data){
					cnt += metaSyncDao.updateSyncCode(m);
				}
			}
			
			return cnt;
			
		}catch(Exception e){
			//e.printStackTrace();
			logger.error("syncCode error", e);
		}
		return 0;
	}
	public int syncActnMsg() {
		try{
			HashMap<String,Object> param = new HashMap<String,Object>();
			String time = metaSyncDao.getLastUpdateTimeActnMsg(param);
			param.put("time", time);
			List<HashMap<String,String>> data =metaSelectDao.selectSyncActnMsg( param);
			int cnt =0;
			if(data != null)	{ //취약점소스 수정 2020.08.24
				for(HashMap <String,String> m : data){
					cnt += metaSyncDao.updateSyncActnMsg(m);
				}
			}
			
			return cnt;
			
		}catch(Exception e){
			//e.printStackTrace();
			logger.error("syncActnMsg error", e);
		}
		return 0;
	}
	

}
