package org.xyzh.common.utils.excel;

import org.xyzh.common.utils.validation.ValidationParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @description Excel列映射配置
 * @filename ExcelColumnMapping.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-16
 */
public class ExcelColumnMapping {
    
    /**
     * @description Excel列名（表头名称）
     */
    private String columnName;
    
    /**
     * @description Excel列索引（从0开始，优先级高于列名）
     */
    private Integer columnIndex;
    
    /**
     * @description 对象字段名
     */
    private String fieldName;
    
    /**
     * @description 字段类型
     */
    private Class<?> fieldType;
    
    /**
     * @description 是否必填
     */
    private boolean required = false;
    
    /**
     * @description 默认值
     */
    private String defaultValue;
    
    /**
     * @description 日期格式（当字段类型为Date时使用）
     */
    private String dateFormat = "yyyy-MM-dd";
    
    /**
     * @description 校验参数列表
     */
    private List<ValidationParam> validationParams;
    
    private ExcelColumnMapping() {
        this.validationParams = new ArrayList<>();
    }
    
    public String getColumnName() {
        return columnName;
    }
    
    public Integer getColumnIndex() {
        return columnIndex;
    }
    
    public String getFieldName() {
        return fieldName;
    }
    
    public Class<?> getFieldType() {
        return fieldType;
    }
    
    public boolean isRequired() {
        return required;
    }
    
    public String getDefaultValue() {
        return defaultValue;
    }
    
    public String getDateFormat() {
        return dateFormat;
    }
    
    public List<ValidationParam> getValidationParams() {
        return validationParams;
    }
    
    /**
     * @description Builder类
     */
    public static class Builder {
        private ExcelColumnMapping mapping = new ExcelColumnMapping();
        
        /**
         * 设置Excel列名
         */
        public Builder columnName(String columnName) {
            mapping.columnName = columnName;
            return this;
        }
        
        /**
         * 设置Excel列索引（从0开始）
         */
        public Builder columnIndex(int columnIndex) {
            mapping.columnIndex = columnIndex;
            return this;
        }
        
        /**
         * 设置对象字段名
         */
        public Builder fieldName(String fieldName) {
            mapping.fieldName = fieldName;
            return this;
        }
        
        /**
         * 设置字段类型
         */
        public Builder fieldType(Class<?> fieldType) {
            mapping.fieldType = fieldType;
            return this;
        }
        
        /**
         * 设置是否必填
         */
        public Builder required(boolean required) {
            mapping.required = required;
            return this;
        }
        
        /**
         * 设置为必填
         */
        public Builder required() {
            mapping.required = true;
            return this;
        }
        
        /**
         * 设置默认值
         */
        public Builder defaultValue(String defaultValue) {
            mapping.defaultValue = defaultValue;
            return this;
        }
        
        /**
         * 设置日期格式
         */
        public Builder dateFormat(String dateFormat) {
            mapping.dateFormat = dateFormat;
            return this;
        }
        
        /**
         * 添加校验参数
         */
        public Builder addValidation(ValidationParam param) {
            mapping.validationParams.add(param);
            return this;
        }
        
        /**
         * 设置校验参数列表
         */
        public Builder validations(List<ValidationParam> params) {
            mapping.validationParams = params;
            return this;
        }
        
        public ExcelColumnMapping build() {
            if (mapping.fieldName == null || mapping.fieldName.isEmpty()) {
                throw new IllegalArgumentException("字段名不能为空");
            }
            if (mapping.columnName == null && mapping.columnIndex == null) {
                throw new IllegalArgumentException("必须指定列名或列索引");
            }
            if (mapping.fieldType == null) {
                mapping.fieldType = String.class;
            }
            return mapping;
        }
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    @Override
    public String toString() {
        return "ExcelColumnMapping{" +
                "columnName='" + columnName + '\'' +
                ", columnIndex=" + columnIndex +
                ", fieldName='" + fieldName + '\'' +
                ", fieldType=" + (fieldType != null ? fieldType.getSimpleName() : "null") +
                ", required=" + required +
                '}';
    }
}

