package com.madeu.batch;

import com.madeu.dao.crm.crmmember.CrmMemberSqlMapDAO;
import com.madeu.dao.web.webuser.WebUserSqlMapDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Component
public class InsertUserBatch {

	@Autowired
	private WebUserSqlMapDAO webUserSqlMapDAO;

	@Autowired
	private CrmMemberSqlMapDAO crmMemberSqlMapDAO;

//    @Scheduled(fixedDelay = 33000000) // 5분30초마다 실행
	public void go() {
		log.debug("****************** InsertUserBatch START ******************");

		try{
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			String tId = String.valueOf(System.currentTimeMillis());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c1 = Calendar.getInstance();
			String tDate = sdf.format(c1.getTime());

			paramMap.put("tId", tId);
			paramMap.put("tDate", tDate);


			// 대상자 조회 (기존 DB 식별자)
			// 최근 등록된 사람 10000명
			HashMap<String, Object> selectMap = new HashMap<String, Object>();
			List<Map<String, Object>> listMap = crmMemberSqlMapDAO.selectListMember(selectMap);
			int listMapSize = listMap.size();

			if(0<listMapSize){
				for(int i=0; i<listMapSize; i++){
					log.debug("pId : "+listMap.get(i).get("pId"));
					paramMap.put("pId", listMap.get(i).get("pId"));

					List<Map<String, Object>> listMapCheck = webUserSqlMapDAO.selectUserCheck(paramMap);
					if(0==listMapCheck.size()){
						String muUserId = ("U").concat(String.valueOf(System.currentTimeMillis()));

						paramMap.put("muUserId", muUserId);
						webUserSqlMapDAO.insertUserBatch(paramMap);
					}
				}
			}
		}catch(Exception e){
			log.debug("Exception InsertUserBatch - run" + e.getMessage());
		}

		log.debug("****************** InsertUserBatch  END  ******************");
	}
}
