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

import java.util.List;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springblade.etl.connector.base.SeatunnelConnector;
import org.springblade.etl.connector.sink.config.SeatunnelFtpFileSinkConfig;
import org.springblade.etl.util.EtlJsonUtil;
import org.springblade.etl.util.ValidateUtil;
import org.springframework.stereotype.Component;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author wookvn
 *
 */
@Component
public class PlatomixFtpSink implements SeatunnelConnector<SeatunnelFtpFileSinkConfig> {
	
	@Data
	public static class Config {
		@NotEmpty(message = "输出:FtpHost不能为空")
		@ApiModelProperty("ftp的地址")
		private String host;
		@NotNull(message = "输出:Ftp端口不能为空")
		@ApiModelProperty("ftp的端口")
		private Integer port;
		@NotEmpty(message = "输出:Ftp用户名不能为空")
		@ApiModelProperty("ftp的用户名")
		private String user;
		@NotEmpty(message = "输出:Ftp密码不能为空")
		@ApiModelProperty("ftp的密码")
		private String password;
		@NotEmpty(message = "输出:path不能为空")
		@ApiModelProperty("ftp的path路径")
		private String path;
		@ApiModelProperty("sheet名称，可不填写")
		private String sheetName;
		@NotNull(message = "输出:写入字段不能为空")
		@ApiModelProperty("输出字段名列表【注意顺序】")
		private List<String> sinkColumns;
		@NotEmpty(message = "输出:文件名称不能为空")
		@ApiModelProperty("文件名称[注意：系统自动添加yyyyMMddHHmmss防止重名]")
		private String filename;
	}

	@Override
	public SeatunnelFtpFileSinkConfig buildConfig(String configJson) {
		return (SeatunnelFtpFileSinkConfig)validate(configJson);
	}

	@Override
	public Object validate(String configJson) {
		Config config = EtlJsonUtil.parseJsonString(configJson, Config.class);
		ValidateUtil.validate(config);
		SeatunnelFtpFileSinkConfig ftpFileSinkConfig = BeanUtil.copyProperties(config, SeatunnelFtpFileSinkConfig.class);
		ftpFileSinkConfig.setFileNameExpression(config.getFilename() + "_${now}");
		return ftpFileSinkConfig;
	}

}
