package cn.ibizlab.core.rule.domain;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.util.ObjectUtils;
import org.springframework.util.DigestUtils;
import cn.ibizlab.util.domain.EntityBase;
import cn.ibizlab.util.annotation.DEField;
import cn.ibizlab.util.enums.DEPredefinedFieldType;
import cn.ibizlab.util.enums.DEFieldDefaultValueType;
import cn.ibizlab.util.helper.DataObject;
import cn.ibizlab.util.enums.DupCheck;
import java.io.Serializable;
import lombok.*;
import org.springframework.data.annotation.Transient;
import cn.ibizlab.util.annotation.Audit;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.baomidou.mybatisplus.annotation.*;
import cn.ibizlab.util.domain.EntityMP;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

/**
 * 实体[执行结果]
 */
@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(value = "handler")
@TableName(value = "IBZRULERESULT", resultMap = "ExecResultResultMap")
@ApiModel("执行结果")
public class ExecResult extends EntityMP implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 规则执行结果标识
     */
    @DEField(name = "ru_execresultid", isKeyField = true)
    @TableId(value = "ru_execresultid", type = IdType.ASSIGN_UUID)
    @JSONField(name = "id")
    @JsonProperty("id")
    @ApiModelProperty("规则执行结果标识")
    private String id;
    /**
     * 规则执行结果描述
     */
    @DEField(name = "ru_execresultname")
    @TableField(value = "ru_execresultname")
    @JSONField(name = "name")
    @JsonProperty("name")
    @ApiModelProperty("规则执行结果描述")
    private String name;
    /**
     * 规则标识
     */
    @TableField(value = "ruleid")
    @JSONField(name = "rule_id")
    @JsonProperty("rule_id")
    @ApiModelProperty("规则标识")
    private String ruleId;
    /**
     * 规则
     */
    @TableField(value = "rulename")
    @JSONField(name = "rule_name")
    @JsonProperty("rule_name")
    @ApiModelProperty("规则")
    private String ruleName;
    /**
     * 是否匹配
     */
    @TableField(value = "retvalue")
    @JSONField(name = "ret_value")
    @JsonProperty("ret_value")
    @ApiModelProperty("是否匹配")
    private Integer retValue;
    /**
     * 业务类别
     */
    @TableField(value = "businesscat")
    @JSONField(name = "business_cat")
    @JsonProperty("business_cat")
    @ApiModelProperty("业务类别")
    private String businessCat;
    /**
     * 标识属性
     */
    @TableField(value = "keyvaluefield")
    @JSONField(name = "key_value_field")
    @JsonProperty("key_value_field")
    @ApiModelProperty("标识属性")
    private String keyValueField;
    /**
     * 分区字段
     */
    @TableField(value = "domainsfield")
    @JSONField(name = "domains_field")
    @JsonProperty("domains_field")
    @ApiModelProperty("分区字段")
    private String domainsField;
    /**
     * 时间属性
     */
    @TableField(value = "timefield")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JSONField(name = "time_field", format = "yyyy-MM-dd HH:mm:ss")
    @JsonProperty("time_field")
    @ApiModelProperty("时间属性")
    private Timestamp timeField;
    /**
     * 维度属性
     */
    @TableField(value = "dimfield")
    @JSONField(name = "dim_field")
    @JsonProperty("dim_field")
    @ApiModelProperty("维度属性")
    private String dimField;
    /**
     * 建立时间
     */
    @DEField(preType = DEPredefinedFieldType.CREATEDATE)
    @TableField(value = "createdate", fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JSONField(name = "createdate", format = "yyyy-MM-dd HH:mm:ss")
    @JsonProperty("createdate")
    @ApiModelProperty("建立时间")
    private Timestamp createdate;
    /**
     * 指标属性
     */
    @TableField(value = "metricfield")
    @JSONField(name = "metric_field")
    @JsonProperty("metric_field")
    @ApiModelProperty("指标属性")
    private BigDecimal metricField;
    /**
     * 更新时间
     */
    @DEField(preType = DEPredefinedFieldType.UPDATEDATE)
    @TableField(value = "updatedate")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    @JSONField(name = "updatedate", format = "yyyy-MM-dd HH:mm:ss")
    @JsonProperty("updatedate")
    @ApiModelProperty("更新时间")
    private Timestamp updatedate;
    /**
     * 扩展属性1
     */
    @TableField(value = "ext1field")
    @JSONField(name = "ext1_field")
    @JsonProperty("ext1_field")
    @ApiModelProperty("扩展属性1")
    private String ext1Field;
    /**
     * 扩展属性2
     */
    @TableField(value = "ext2field")
    @JSONField(name = "ext2_field")
    @JsonProperty("ext2_field")
    @ApiModelProperty("扩展属性2")
    private String ext2Field;
    /**
     * 系统标识
     */
    @TableField(value = "systemid")
    @JSONField(name = "system_id")
    @JsonProperty("system_id")
    @ApiModelProperty("系统标识")
    private String systemId;

    /**
     * 规则
     */
    @JsonIgnore
    @JSONField(serialize = false)
    @TableField(exist = false)
    private cn.ibizlab.core.rule.domain.RuleItem rule;



    /**
     * 设置 [规则执行结果描述]
     */
    public void setName(String name) {
        this.name = name;
        this.modify("ru_execresultname", name);
    }

    /**
     * 设置 [规则标识]
     */
    public void setRuleId(String ruleId) {
        this.ruleId = ruleId;
        this.modify("ruleid", ruleId);
    }

    /**
     * 设置 [规则]
     */
    public void setRuleName(String ruleName) {
        this.ruleName = ruleName;
        this.modify("rulename", ruleName);
    }

    /**
     * 设置 [是否匹配]
     */
    public void setRetValue(Integer retValue) {
        this.retValue = retValue;
        this.modify("retvalue", retValue);
    }

    /**
     * 设置 [业务类别]
     */
    public void setBusinessCat(String businessCat) {
        this.businessCat = businessCat;
        this.modify("businesscat", businessCat);
    }

    /**
     * 设置 [标识属性]
     */
    public void setKeyValueField(String keyValueField) {
        this.keyValueField = keyValueField;
        this.modify("keyvaluefield", keyValueField);
    }

    /**
     * 设置 [分区字段]
     */
    public void setDomainsField(String domainsField) {
        this.domainsField = domainsField;
        this.modify("domainsfield", domainsField);
    }

    /**
     * 设置 [时间属性]
     */
    public void setTimeField(Timestamp timeField) {
        this.timeField = timeField;
        this.modify("timefield", timeField);
    }

    /**
     * 格式化日期 [时间属性]
     */
    public String formatTimeField() {
        if (this.timeField == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(timeField);
    }
    /**
     * 设置 [维度属性]
     */
    public void setDimField(String dimField) {
        this.dimField = dimField;
        this.modify("dimfield", dimField);
    }

    /**
     * 设置 [指标属性]
     */
    public void setMetricField(BigDecimal metricField) {
        this.metricField = metricField;
        this.modify("metricfield", metricField);
    }

    /**
     * 设置 [扩展属性1]
     */
    public void setExt1Field(String ext1Field) {
        this.ext1Field = ext1Field;
        this.modify("ext1field", ext1Field);
    }

    /**
     * 设置 [扩展属性2]
     */
    public void setExt2Field(String ext2Field) {
        this.ext2Field = ext2Field;
        this.modify("ext2field", ext2Field);
    }

    /**
     * 设置 [系统标识]
     */
    public void setSystemId(String systemId) {
        this.systemId = systemId;
        this.modify("systemid", systemId);
    }


    /**
     * 获取 [规则执行结果标识]
     */
    public String getId() {
        if(ObjectUtils.isEmpty(id)) {
            id=(String)getDefaultKey(true);
        }
        return id;
    }

    @Override
    public Serializable getDefaultKey(boolean gen) {
        if((!ObjectUtils.isEmpty(this.getRuleId()))&&(!ObjectUtils.isEmpty(this.getDimField()))&&(!ObjectUtils.isEmpty(this.getKeyValueField())))
            return DigestUtils.md5DigestAsHex(String.format("%s||%s||%s" ,this.getRuleId(),this.getDimField(),this.getKeyValueField()).getBytes());
        return null;
    }
    /**
     * 复制当前对象数据到目标对象(粘贴重置)
     * @param targetEntity 目标数据对象
     * @param bIncEmpty  是否包括空值
     * @param <T>
     * @return
     */
    @Override
    public <T> T copyTo(T targetEntity, boolean bIncEmpty) {
        this.reset("ru_execresultid");
        return super.copyTo(targetEntity, bIncEmpty);
    }
}


