/**
 * 
 */
package org.springblade.etl.connector.source;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springblade.core.tool.exception.ServiceException;
import org.springblade.datasource.context.DialectContext;
import org.springblade.etl.connector.base.SeatunnelConnector;
import org.springblade.etl.connector.source.config.SeatunnelJdbcSourceConfig;
import org.springblade.etl.entity.bi.ReportDatasource;
import org.springblade.etl.enums.JdbcType;
import org.springblade.etl.service.bi.ReportDatasourceService;
import org.springblade.etl.util.EtlJsonUtil;
import org.springblade.etl.util.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author wookvn
 *
 */
@Component
public class PlatomixJdbcSource implements SeatunnelConnector<SeatunnelJdbcSourceConfig> {

	@Autowired
	private ReportDatasourceService reportDatasourceService;
	
	@Autowired
	private DialectContext dialectContext;
	 
	@Data
	public static class Config {
		@NotNull(message = "Source数据源不能为空")
		@ApiModelProperty("数据源ID")
		private Integer datasourceId;
		@NotEmpty(message = "Source数据表不能为空")
		@ApiModelProperty("数据表")
		private String tableName;
		
		@ApiModelProperty("数据字段列表【query和columnFields有且只有一个】")
		private List<String> columnFields;
		@ApiModelProperty("SQL查询语句")
		private String query;
	}

	@Override
	public SeatunnelJdbcSourceConfig buildConfig(String configJson) {
		Config ptConfig = (Config)validate(configJson);
		ReportDatasource sourceModel = reportDatasourceService.getById(ptConfig.getDatasourceId());
		if(sourceModel == null) {
			throw new ServiceException("未查询到数据源|datasourceId="+ptConfig.getDatasourceId());
		}
		SeatunnelJdbcSourceConfig seatunnelConfig = new SeatunnelJdbcSourceConfig();
		seatunnelConfig.setJdbcType(JdbcType.ofDsType(sourceModel.getDsType()));
		seatunnelConfig.setUrl(sourceModel.getUrl());
		seatunnelConfig.setUsername(sourceModel.getDsUser());
		seatunnelConfig.setPassword(sourceModel.getDsPassword());
		seatunnelConfig.setDriver(sourceModel.getDriverClassName());
		if(StrUtil.isBlank(ptConfig.getQuery())) {
			String tableName = dialectContext.getDialect(sourceModel.getDsTypeSyl()).dbTableName(sourceModel, ptConfig.getTableName());
			ptConfig.setQuery(String.format("select %s from %s", String.join(",", ptConfig.getColumnFields()), tableName));
		}
		seatunnelConfig.setQuery(ptConfig.getQuery());
		return seatunnelConfig;
	}
	
	@Override
	public Object validate(String configJson) {
		Config ptConfig = EtlJsonUtil.parseJsonString(configJson, PlatomixJdbcSource.Config.class);
		ValidateUtil.validate(ptConfig);
		if(CollectionUtil.isEmpty(ptConfig.getColumnFields()) && StrUtil.isBlank(ptConfig.getQuery())) {
			throw new ServiceException("Source数据表字段不能为空");
		}
		return ptConfig;
	}
}
