package com.purui.prims.headless.api.pojo;

import com.google.common.collect.Lists;
import com.purui.prims.headless.api.pojo.enums.DimensionType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "模型详情")
public class ModelDetail {

    @ApiModelProperty(value = "查询类型")
    private String queryType;

    // 自定义SQL模式
    @ApiModelProperty(value = "自定义SQL")
    private String sqlQuery;

    // 快速创建模式(单表)
    @ApiModelProperty(value = "快速创建模式(单表)")
    private String tableQuery;

    // 多表之间的关联关系(仅快速创建模式使用)
    @ApiModelProperty(value = "多表之间的关联关系(仅快速创建模式使用)")
    private List<TableJoinRelation> joinRelations = Lists.newArrayList();

    @ApiModelProperty(value = "主键字段")
    private List<Identify> identifiers = Lists.newArrayList();

    @ApiModelProperty(value = "维度")
    private List<Dim> dimensions = Lists.newArrayList();

    @ApiModelProperty(value = "度量")
    private List<Measure> measures = Lists.newArrayList();

    @ApiModelProperty(value = "其它字段")
    private List<Field> fields = Lists.newArrayList();

    @ApiModelProperty(value = "SQL变量")
    private List<SqlVariable> sqlVariables = Lists.newArrayList();

    /**
     * 获取自定义SQL模式的sql
     * @return sql语句
     */
    public String getSqlQuery() {
        if (StringUtils.isNotBlank(sqlQuery) && sqlQuery.endsWith(";")) {
            sqlQuery = sqlQuery.substring(0, sqlQuery.length() - 1);
        }
        return sqlQuery;
    }

    public List<Dim> filterTimeDims() {
        if (CollectionUtils.isEmpty(dimensions)) {
            return Lists.newArrayList();
        }
        return dimensions.stream()
                .filter(dim -> DimensionType.time.name().equalsIgnoreCase(dim.getType()))
                .collect(Collectors.toList());
    }

    public List<Field> getFields() {
        if (!CollectionUtils.isEmpty(fields)) {
            return fields;
        }
        List<Field> fieldList = Lists.newArrayList();
        //Compatible with older versions
        if (!CollectionUtils.isEmpty(identifiers)) {
            fieldList.addAll(identifiers.stream()
                    .map(identify -> Field.builder().fieldName(identify.getFieldName()).build())
                    .collect(Collectors.toSet()));
        }
        if (!CollectionUtils.isEmpty(dimensions)) {
            fieldList.addAll(dimensions.stream()
                    .map(dim -> Field.builder().fieldName(dim.getFieldName()).build())
                    .collect(Collectors.toSet()));
        }
        if (!CollectionUtils.isEmpty(measures)) {
            fieldList.addAll(measures.stream()
                    .map(measure -> Field.builder().fieldName(measure.getFieldName()).build())
                    .collect(Collectors.toSet()));
        }
        return fieldList;
    }

}
