package cn.ibizlab.core.extensions.dto;

import cn.ibizlab.core.analysis.domain.DAMetric;
import cn.ibizlab.util.dict.Catalog;
import cn.ibizlab.util.dict.Option;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
public class LookupResult {



    private List<String> keys;

    private String metricId;
    private String dimValue;

    private DAMetric metric;

    public String getField()
    {
        if(metric!=null&&(!StringUtils.isEmpty(metric.getFieldSet())))
        {
            JSONObject jo= JSON.parseObject(metric.getFieldSet());
            String keyvaluefield="";
            if(jo.containsKey("keyvaluefield"))
                keyvaluefield = jo.getString("keyvaluefield");
            if(StringUtils.isEmpty(keyvaluefield)&&(jo.containsKey("metricfield")))
                keyvaluefield = jo.getString("metricfield");
            return keyvaluefield;
        }
        return null;
    }

    @JsonFormat(pattern = "yyyy-MM-dd", locale = "zh", timezone = "GMT+8")
    @JSONField(name = "startTime", format = "yyyy-MM-dd")
    private Timestamp startTime;
    @JsonFormat(pattern = "yyyy-MM-dd", locale = "zh", timezone = "GMT+8")
    @JSONField(name = "endTime", format = "yyyy-MM-dd")
    private Timestamp endTime;

    private Integer page = 0;
    private Integer size = 1000;
    private Integer total = 0;

    public Integer getTotalPages() {
        return this.getSize() == 0 ? 1 : (int)Math.ceil((double)this.total / (double)this.getSize());
    }


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JSONField(name = "snapTime", format = "yyyy-MM-dd HH:mm:ss")
    private Timestamp snapTime;


}
