package com.leanwo.activiti.db;

import org.activiti.engine.impl.cfg.multitenant.TenantAwareDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidDataSource;
import com.leanwo.activiti.config.MyTenantInfoHolder;
import com.leanwo.activiti.dto.base.AccountManagementDto;

@Service
public class DataSourceServiceImpl implements DataSourceService {

	private static Logger logger = LoggerFactory.getLogger(DataSourceServiceImpl.class);

	@Autowired
	private DataSourceTemplate dataSourceTemplate;
	
	@Autowired
	private TenantAwareDataSource tenantAwareDataSource;
	
	@Autowired
	private MyTenantInfoHolder myTenantInfoHolder;

	
	@Override
	public void createDynamicDataSource(AccountManagementDto accountManagementDto) {
		String key = "dataSource" + accountManagementDto.getAccountId();
		if(myTenantInfoHolder.getAllTenants().contains(key)) {
			return;
		}
		try {
			DruidDataSource druidDataSource = new DruidDataSource();
			// 新的地址
			druidDataSource.setUrl(accountManagementDto.getUrl());
			// 用户
			druidDataSource.setUsername(accountManagementDto.getUserName());
			// 密码
			druidDataSource.setPassword(accountManagementDto.getPassword());
			druidDataSource.addConnectionProperty("accountId", accountManagementDto.getAccountId().toString());
			druidDataSource.addConnectionProperty("accountName", accountManagementDto.getAccountName());
			druidDataSource.setInitialSize(dataSourceTemplate.getInitialSize());
			druidDataSource.setMinIdle(dataSourceTemplate.getMinIdle());
			druidDataSource.setMaxActive(dataSourceTemplate.getMaxActive());
			druidDataSource.setMaxWait(dataSourceTemplate.getMaxWait());
			druidDataSource.setTimeBetweenConnectErrorMillis(dataSourceTemplate.getTimeBetweenEvictionRunsMillis());
			druidDataSource.setMinEvictableIdleTimeMillis(dataSourceTemplate.getMinEvictableIdleTimeMillis());
			druidDataSource.setValidationQuery(dataSourceTemplate.getValidationQuery());
			druidDataSource.setTestWhileIdle(dataSourceTemplate.getTestWhileIdle());
			druidDataSource.setTestOnBorrow(dataSourceTemplate.getTestOnBorrow());
			druidDataSource.setTestOnReturn(dataSourceTemplate.getTestOnReturn());
			druidDataSource.setPoolPreparedStatements(dataSourceTemplate.getPoolPreparedStatements());
			druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(
					dataSourceTemplate.getMaxPoolPreparedStatementPerConnectionSize());
			druidDataSource.setRemoveAbandoned(dataSourceTemplate.getRemoveAbandoned());
			druidDataSource.setRemoveAbandonedTimeout(dataSourceTemplate.getRemoveAbandonedTimeout());
			druidDataSource.setLogAbandoned(dataSourceTemplate.getLogAbandoned());

			druidDataSource.setFilters(dataSourceTemplate.getFilters());
			
			tenantAwareDataSource.addDataSource(key, druidDataSource);
			myTenantInfoHolder.addTenantId(key);			
			
		} catch (Exception ex) {
			logger.error("数据源accountId=" + accountManagementDto.getAccountId() + "创建失败", ex);
		}
	}
}
