package quant.rich.emoney.entity.postgre;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import quant.rich.emoney.enums.StockSpan;

/**
 * 益盟指标
 * @author Barry
 *
 */
@Data
@Accessors(chain=true)
public class EmoneyIndex {

    @Getter(AccessLevel.PRIVATE)
    @Setter(AccessLevel.PRIVATE)
    private ObjectMapper mapper = new ObjectMapper();
    
    /**
     * 股票代码
     */
    private String tsCode;
    /**
     * 时间
     */
    @TableField("trade_date")
    private LocalDateTime date;
    /**
     * 指标值
     */
    private Long value;
    /**
     * 指标名称
     */
    private String indexName;
    /**
     * 指标线名称
     */
    private String lineName;
    /**
     * 指标参数
     */
    private JsonNode indexParam;
    /**
     * 指标形状
     */
    private Integer lineShape;
    /**
     * 数据粒度
     */
    private Integer dataPeriod;
    
    public StockSpan getStockSpan() {
        return StockSpan.fromEmoneyCode(dataPeriod);
    }
    
    public EmoneyIndex setStockSpan(StockSpan stockSpan) {
        
        if (dataPeriod == null) {
            dataPeriod = stockSpan.getEmoneyCode();
        }
        
        return this;
    }
    
    public EmoneyIndex setIndexParam(@SuppressWarnings("rawtypes") Map map) {
    	if (map == null) {
    		indexParam = mapper.createObjectNode();
    	}
    	else {
    		indexParam = mapper.valueToTree(map);
    	}
    	return this;
    }
    
    public JsonNode getIndexParam() {
    	if (indexParam == null) {
    		indexParam = mapper.createObjectNode();
    	}
    	return indexParam;
    }
    
    public String toString() {
    	return mapper.valueToTree(this).toString();
    }
    
    public boolean equals(Object other) {
    	  if (this == other) return true;
          if (other == null || getClass() != other.getClass()) return false;
          EmoneyIndex index = (EmoneyIndex) other;
          return this.toString().equals(index.toString());
    }
    
    public int hashCode() {
    	return Objects.hash(tsCode, date, value, indexName, lineName, indexParam, lineShape, dataPeriod);
    }
    
}
