package kr.gmtc.gw.eyegw.controller;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import kr.gmtc.gw.eyegw.db1.EaiIfCheckService;
import kr.gmtc.gw.eyegw.db1.EaiProcService;

@Component
public class SchedulController {
    
    private EaiIfCheckService checkService;
    private EaiProcService eaiProcService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${settings.delete.save-days}")
	private int dataSaveDays = 120;


    private Map<String, String> mapLogCode;
    

    public SchedulController(EaiIfCheckService checkService, EaiProcService eaiProcService){
        this.checkService = checkService;
        this.eaiProcService = eaiProcService;
    }

    @Scheduled(cron = "${settings.cmmnCd.cron}")
	public void selectCmmnCd(){

        mapLogCode = new ConcurrentHashMap<String, String>();

        List<HashMap<String, Object>> logList = checkService.selectLogCode();

        logger.info("로그유형코드(CT033) " +logList.size()+ "건 조회 완료.");

        logList.forEach(codes -> mapLogCode.put( (String) codes.get("CD"), (String) codes.get("CD_NM") ) );

	}

    @Scheduled(cron = "${settings.delete.cron}")
    private void callThread_dataDelete() throws InterruptedException {
			
		Map<String, Object> paramMap = new HashMap<>();

		paramMap.put("ar_days", dataSaveDays);
		paramMap.put("ret_proc_msg", "");

		eaiProcService.callProcOldDataDelete(paramMap);
		
		String sProcMsg = (String) paramMap.get("ret_proc_msg");

		logger.info("callProcOldDataDelete Call : " + sProcMsg);


	}

    public Map<String, String> getMapLogCode() {
        return mapLogCode;
    }

    public void setMapLogCode(Map<String, String> mapLogCode) {
        this.mapLogCode = mapLogCode;
    }

    

}
