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

import com.alibaba.fastjson.JSONObject;
import com.purui.prims.headless.api.pojo.MetricDefineByFieldParams;
import com.purui.prims.headless.api.pojo.MetricDefineByMeasureParams;
import com.purui.prims.headless.api.pojo.MetricDefineByMetricParams;
import com.purui.prims.headless.api.pojo.enums.MetricDefineType;
import com.purui.prims.headless.api.pojo.enums.MetricType;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class MetricReq extends MetricBaseReq {

    @ApiModelProperty(value = "指标定义类型")
    private MetricDefineType metricDefineType = MetricDefineType.MEASURE;

    @ApiModelProperty(value = "指标定义度量参数")
    private MetricDefineByMeasureParams metricDefineByMeasureParams;

    @ApiModelProperty(value = "指标定义字段参数")
    private MetricDefineByFieldParams metricDefineByFieldParams;

    @ApiModelProperty(value = "指标定义指标参数")
    private MetricDefineByMetricParams metricDefineByMetricParams;

    public String getTypeParamsJson() {
        if (MetricDefineType.CUSTOM.equals(metricDefineType) && metricDefineByFieldParams != null) {
            return JSONObject.toJSONString(metricDefineByFieldParams);
        } else if (MetricDefineType.MEASURE.equals(metricDefineType) && metricDefineByMeasureParams != null) {
            return JSONObject.toJSONString(metricDefineByMeasureParams);
        } else if (MetricDefineType.METRIC.equals(metricDefineType) && metricDefineByMetricParams != null) {
            return JSONObject.toJSONString(metricDefineByMetricParams);
        }
        return null;
    }

    // 获取指标类型
    public MetricType getMetricType() {
        if (MetricType.isComposite(metricDefineType)) {
            return MetricType.COMPOSITE;
        }
        // 有业务限定
        if (getConditionId() != null && getConditionId() > 0) {
            return MetricType.DERIVED;
        }
        if (MetricType.isDerived(metricDefineType, metricDefineByMeasureParams)) {
            return MetricType.DERIVED;
        }
        return MetricType.ATOMIC;
    }

}
