package com.cftech.core.util;

import com.cftech.accounts.model.MpAccountsEntity;
import com.cftech.accounts.service.MpAccountsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;
//@Component
public class InstantiationTracingBeanPostProcessor
//		implements ApplicationListener<ContextRefreshedEvent>
{
	  private static Logger logger = Logger.getLogger(InstantiationTracingBeanPostProcessor.class);
//	  @Autowired
	  private MpAccountsService mpAccountService;
	  
//	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if(event.getApplicationContext().getParent() == null)
		{
			//root application context 
			//需要执行的逻辑代码，当spring容器初始化完成后就会执行该方法。
			  logger.info("Init token start ");
			  List<MpAccountsEntity> mpAccounts = mpAccountService.getAllMpAccounts(); // MpAccountsEntity accountsEntity
			  for(MpAccountsEntity accountsEntity:mpAccounts)
			  {
//				  String accessToken = accountsEntity.getAccesstoken();
//			      try  
//			     {
//					accessToken = JwTokenAPI.getAccessToken(accountsEntity.getAppid(), accountsEntity.getSecret());
//					accountsEntity.setJsapi_ticket(JwThirdAPI.args(accessToken));
//					accountsEntity.setAccesstoken(accessToken);
//					mpAccountService.updateByPrimaryKey(accountsEntity);
//				 } catch (WexinReqException e) 
//			     {
//					logger.error("启动更新TOKEN失败"+accountsEntity.getId(), e);
//				 }
			  }
		    logger.info("Init token do end ");
		}
	}
}
