package cn.ibizlab.core.data.domain;

import java.sql.Timestamp;
import java.util.*;
import java.math.BigInteger;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;

import cn.ibizlab.core.data.model.POSchema;
import cn.ibizlab.core.data.model.PojoSchema;
import cn.ibizlab.core.data.model.TransUtils;
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 org.springframework.util.StringUtils;


/**
 * [业务实体] 对象
 */
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("业务实体")
public class DOModel extends EntityBase implements Serializable {

    /**
     * 标识
     */
    @DEField(isKeyField = true)
    @JSONField(name = "id")
    @JsonProperty("id")
    @ApiModelProperty("标识")
    private String id;

    /**
     * 名称
     */
    @JSONField(name = "name")
    @JsonProperty("name")
    @ApiModelProperty("名称")
    private String name;

    /**
     * 名称
     */
    @JSONField(name = "title")
    @JsonProperty("title")
    @ApiModelProperty("名称")
    private String title;

    /**
     * 系统
     */
    @JSONField(name = "systemId")
    @JsonProperty("systemId")
    @ApiModelProperty("系统")
    private String systemId;

    /**
     * 包名
     */
    @JSONField(name = "packageName")
    @JsonProperty("packageName")
    @ApiModelProperty("包名")
    private String packageName;

    /**
     * 描述
     */
    @JSONField(name = "description")
    @JsonProperty("description")
    @ApiModelProperty("描述")
    private String description;

    /**
     * 定义
     */
    @JSONField(name = "schema")
    @JsonProperty("schema")
    @ApiModelProperty("定义")
    private PojoSchema schema;

    /**
     * 模型
     */
    @JSONField(name = "model")
    @JsonProperty("model")
    @ApiModelProperty("模型")
    private String model;

    public void setSchema(PojoSchema schema) {

        if(schema!=null)
        {
            if(StringUtils.isEmpty(schema.getId()))
                schema.setId(schema.getSystem()+".domain."+schema.getName());
            this.setId(schema.getId());
            if(!StringUtils.isEmpty(schema.getName()))
                this.setName(schema.getName());
            if(!StringUtils.isEmpty(schema.getSystem()))
                this.setSystemId(schema.getSystem());
            if(!StringUtils.isEmpty(schema.getTitle()))
                this.setTitle(schema.getTitle());
            if(!StringUtils.isEmpty(schema.getDescription()))
                this.setDescription(schema.getDescription());
            if(!StringUtils.isEmpty(schema.getPackage()))
                this.setPackageName(schema.getPackage());
        }
        this.schema = schema;
    }

    /**
     * 存储
     */
    @JSONField(name = "poModels")
    @JsonProperty("poModels")
    @ApiModelProperty("存储")
    private String poModels;

    @JSONField(serialize = false)
    @JsonIgnore
    public String getKeyPropertyName()
    {
        if(this.getSchema()!=null&&(!ObjectUtils.isEmpty(this.schema.getKeyMap().values())))
            return this.schema.getKeyMap().values().iterator().next();
        return "id";
    }


    @JSONField(serialize = false)
    @JsonIgnore
    public String getKeyFieldName()
    {
        if(this.getSchema()!=null&&(!ObjectUtils.isEmpty(this.schema.getKeyMap().keySet())))
            return this.schema.getKeyMap().keySet().iterator().next();
        return "id";
    }



	@JSONField(serialize = false)
	@JsonIgnore
	public String getDefaultDataSource()
	{
		return getSchema()!=null?getSchema().getDefaultDataSoruce():"";
	}


    @JSONField(serialize = false)
    @JsonIgnore
	private Map<String,POSchema> poSchemas;

    @JSONField(serialize = false)
    @JsonIgnore
    public POSchema getDefaultPOSchema()
    {
        return getPOSchema("default");
    }
    public DOModel addPOSchema(String name,POSchema poSchema)
    {
        if(poSchemas==null)
            poSchemas=new LinkedHashMap<>();
        poSchemas.put(name,poSchema);
        return this;
    }
	public POSchema getPOSchema(String name)
    {
        if(poSchemas==null)
            poSchemas=new LinkedHashMap<>();
        if(poSchemas.containsKey(name))
        {
            return poSchemas.get(name);
        }
        else if(this.getSchema()!=null&&("default".equals(name)||this.getSchema().getDefaultTableName().equals(name)))
        {
            POSchema defaultPOSchema= TransUtils.PojoSchema2PO(this.getSchema());
            if(defaultPOSchema!=null) {
                poSchemas.put("default", defaultPOSchema);
                poSchemas.put(this.getSchema().getDefaultTableName(),defaultPOSchema);
                return defaultPOSchema;
            }
        }
        return null;
    }

}


