package com.leanwo.activiti.delegate.base;

import javax.inject.Inject;
import javax.inject.Named;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.leanwo.activiti.service.base.TokenService;
import com.leanwo.activiti.service.base.impl.DbSyncTimeStampServiceImpl;
import com.leanwo.activiti.service.base.impl.ProdogServer;
import com.leanwo.activiti.util.LoginContext;
import com.leanwo.activiti.util.LoginContextUtil;
import com.leanwo.activiti.util.SpringUtil;
import com.leanwo.activiti.util.TokenUtil;

/**
 * 用户组同步 Delegate
 * @author ZhiJie
 *
 */
@Named
public class UserSyncDelegate implements JavaDelegate {
	private static Logger logger = LoggerFactory.getLogger(UserSyncDelegate.class);
	
	@Inject
	private ProdogServer prodogServer;
	
	@Inject
	private TokenService tokenService;
	
	@Override
	public void execute(DelegateExecution execution)  {
		
		Long accountId = 0L;
		LoginContext loginContext = new LoginContext();
        loginContext.setAccountId(accountId);
		String tempToken = tokenService.generateTempToken();
		TokenUtil.setToken(tempToken);
		LoginContextUtil.setLoginContext(loginContext);
		
		DbSyncTimeStampServiceImpl dbSyncTimeStampService = (DbSyncTimeStampServiceImpl) SpringUtil.getSingleBean(DbSyncTimeStampServiceImpl.class);
		
		// Prodog数据库版本
		byte[] dbts = prodogServer.getProdogDbVersion();
		
		// Activiti中最新版本
		byte[] ufts = dbSyncTimeStampService.getLatestDbVersion(accountId);
		
		dbSyncTimeStampService.saveDbVersion(accountId, dbts);
		
	}
	
}
