package cn.ibizlab.core.extensions.cql;

import com.alibaba.fastjson.annotation.JSONField;
import com.datastax.driver.core.LocalDate;
import com.datastax.driver.mapping.annotations.ClusteringColumn;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;
import com.datastax.driver.mapping.annotations.Transient;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

/**
 * 实体[RUExecResult] 数据对象
 */
@Table(name = "ru_execresult")
@Data
@Slf4j
public class ExecResult implements Serializable {
    @PartitionKey
    private String ruleid;
    @PartitionKey(1)
    private Integer retvalue;
    @ClusteringColumn
    private String dimfield;
    @ClusteringColumn(1)
    @JsonIgnore
    @JSONField(serialize = false)
    private LocalDate timefield;
    @ClusteringColumn(2)
    private String domainsfield;
    @ClusteringColumn(3)
    private String keyvaluefield;

    private String ruexecresultname;
    @JsonIgnore
    @JSONField(serialize = false)
    private LocalDate updatedate;

    private String rulename;


    private Double metricfield;


    private String ext1field;
    private String ext2field;
    private String businesscat;

    @Transient
    public long getTimefieldLV() {
        return timefield.getMillisSinceEpoch();
    }

    public void setTimefieldLV(long timefield) {
        this.timefield = LocalDate.fromMillisSinceEpoch(timefield);
    }

    @Transient
    public long getUpdatedateLV() {
        return updatedate.getMillisSinceEpoch();
    }

    public void setUpdatedateLV(long updatedate) {
        this.updatedate = LocalDate.fromMillisSinceEpoch(updatedate);
    }

    public static LocalDate nowLocalDate() {
        Date time = new Date();
        return LocalDate.fromYearMonthDay(time.getYear() + 1900, time.getMonth() + 1, time.getDate());
    }

    public void stringTimeField(String timeDate) {
        if (StringUtils.isEmpty(timeDate) && timeDate.length() != 8)
            return;
        try {
            this.timefield = LocalDate.fromYearMonthDay(Integer.valueOf(timeDate.substring(0, 4)), Integer.valueOf(timeDate.substring(5, 7)), Integer.valueOf(timeDate.substring(8, 10)));
        } catch (Exception ex) {
            log.error(String.format("setTimeField Error[%1$s]:%2$s", timeDate, ex.getMessage()));
        }
    }


    public static LocalDate time2LocalDate(Timestamp time) {
        return LocalDate.fromYearMonthDay(time.getYear() + 1900, time.getMonth() + 1, time.getDate());
    }

    /**
     * 复制当前对象数据到目标对象(粘贴重置)
     *
     * @throws Exception
     */
    public ExecResult copyTo(ExecResult targetEntity) {
        BeanCopier copier = BeanCopier.create(ExecResult.class, ExecResult.class, false);
        copier.copy(this, targetEntity, null);
        return targetEntity;
    }

    public String debugInfo() {
        return String.format("%1$s,%2$s,%3$s,%4$s,%5$s,%6$s", keyvaluefield, ruleid, retvalue, dimfield, timefield, domainsfield);
    }
}
