package {{packageName}}.{{entity.module}}.domain;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
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 com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.util.ObjectUtils;
import org.springframework.util.DigestUtils;
import org.springframework.util.Assert;
import net.ibizsys.central.plugin.boot.core.domain.BaseData;
import java.io.Serializable;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Transient;
import org.springframework.data.annotation.Id;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
{{#entity.relEntities}}
import {{packageName}}.{{module}}.domain.{{codeName}};
{{/entity.relEntities}}

@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(value = "handler")
@TableName(value = "{{entity.tableName}}", resultMap = "{{entity.codeName}}ResultMap")
@ApiModel(value = "{{entity.name}}", description = "{{entity.logicName}}")
public class {{entity.codeName}} extends BaseData implements Serializable
{

    {{#each entity.fields}}
        {{#unless deepStructure}}
    /**
     * {{logicName}}
     */
            {{#if keyDEField}}
    @Id
                {{#if phisicalDEField}}
    @TableId(value = "{{columnName}}"{{#type.number}} , type = IdType.ASSIGN_ID{{/type.number}}{{#type.string}} , type = IdType.ASSIGN_UUID{{/type.string}})
                {{else}}
    @TableField(exist = false)
                {{/if}}
            {{else}}
    @TableField(value = "{{columnName}}"{{#insertOnly}} , fill = FieldFill.INSERT{{/insertOnly}}{{^phisicalDEField}} , exist = false{{/phisicalDEField}})
                {{#logicValidField}}
    @TableLogic{{#entity.validLogicValue}}(value = "{{entity.validLogicValue}}"{{#entity.invalidLogicValue}} , delval = "{{entity.invalidLogicValue}}"{{/entity.invalidLogicValue}}){{/entity.validLogicValue}}
                {{/logicValidField}}
            {{/if}}
    @JsonProperty("{{jsonName}}")
            {{#timeType}}
    @JsonFormat(pattern = "{{format}}", locale = "zh", timezone = "GMT+8")
            {{/timeType}}
    @JSONField(name = "{{jsonName}}"{{#timeType}} , format = "{{format}}"{{/timeType}})
            {{#eq type.java "Long"}}
    @JsonSerialize(using = ToStringSerializer.class)            
            {{/eq}}
    @ApiModelProperty(value = "{{columnName}}", notes = "{{logicName}}")
    private {{type.java}} {{camelCase codeName}};

        {{/unless}}
    {{/each}}

    {{#each entity.references}}
    /**
     * {{entityLogicName}}
     */
    @JsonIgnore
    @JSONField(serialize = false)
    @TableField(exist = false)
    @ApiModelProperty(value = "{{lowerCase codeName}}", notes = "{{logicName}}")
    private {{entityCodeName}} {{camelCase codeName}};
    {{/each}}

    {{#each entity.nesteds}}
    /**
     * {{entityLogicName}}
     */
        {{#if listCode}}
    @JSONField(name = "{{listCode}}")
    @JsonProperty("{{listCode}}")
        {{else}}
    @JsonIgnore
    @JSONField(serialize = false)
        {{/if}}
        {{#if columnName}}
    @TableField(value = "{{columnName}}" , typeHandler = {{packageName}}.{{module}}.domain.handlers.{{entityCodeName}}TypeHandler.class)
    @ApiModelProperty(value = "{{columnName}}", notes = "{{logicName}}")
        {{else}}
    @TableField(exist = false)
    @ApiModelProperty(value = "{{lowerCase codeName}}", notes = "{{logicName}}")
        {{/if}}
    protected List<{{entityCodeName}}> {{camelCase codeName}};
    {{/each}}

    {{#each entity.fields}}
        {{#unless deepStructure}}
            {{#unless keyDEField}}
                {{#unless predefinedType}}
    /**
     * 设置 [{{logicName}}]
     */
    public void set{{pascalCase codeName}}({{type.java}} {{camelCase codeName}}) {
        this.{{camelCase codeName}} = {{camelCase codeName}};
        this.modify("{{jsonName}}", {{camelCase codeName}});
    }
                {{/unless}}
            {{/unless}}

        {{/unless}}
    {{/each}}

    {{#entity.nesteds}}
        {{#columnName}}
    /**
     * 设置 [{{entityLogicName}}]
     */
    public void set{{pascalCase codeName}}(List<{{entityCodeName}}> {{camelCase codeName}}) {
        this.{{camelCase codeName}} = {{camelCase codeName}};
        this.modify("{{columnName}}", ({{camelCase codeName}}!=null)?{{camelCase codeName}}:(new ArrayList()));
    }
        {{/columnName}}
    
    {{/entity.nesteds}}

    
    {{#if entity.keyField.type.number}}
    public Serializable getDefaultKey(boolean gen) { 
        return IdWorker.getId();
    }
    {{else}}
    {{#entity.unionKeyMode}}

    {{#unless entity.keyField.phisicalDEField}}
    /**
     * 设置 [{{logicName}}]
     */
    public void set{{pascalCase entity.keyField.codeName}}({{entity.keyField.type.java}} {{camelCase entity.keyField.codeName}}) {
        this.{{camelCase entity.keyField.codeName}} = {{camelCase entity.keyField.codeName}};
        if(!ObjectUtils.isEmpty({{camelCase entity.keyField.codeName}})) {
            String [] args={{camelCase entity.keyField.codeName}}.split("\\|\\|");
        {{#each entity.unionKeyFields}}
            this.set("{{camelCase codeName}}", args[{{@index}}]);
        {{/each}}
        }
    }

    public {{entity.keyField.type.java}} get{{pascalCase entity.keyField.codeName}}() {
        if(ObjectUtils.isEmpty(this.{{camelCase entity.keyField.codeName}})) 
            this.{{camelCase entity.keyField.codeName}} = ({{entity.keyField.type.java}})getDefaultKey(true);
        return this.{{camelCase entity.keyField.codeName}};
    }
    {{/unless}}

    public Serializable getDefaultKey(boolean gen) {
        {{#each entity.unionKeyFields}}
        Assert.notNull(get{{pascalCase codeName}}(),"未设置{{logicName}}");    
        {{/each}}
        String key = String.format("{{#each entity.unionKeyFields}}{{#unless @first}}||{{/unless}}%s{{/each}}"
            {{#each entity.unionKeyFields}},{{#if timeType}}DataObject.datetimeFormat.format(get{{pascalCase codeName}}()){{else}}get{{pascalCase codeName}}(){{/if}}{{/each}});
        {{#if entity.keyField.phisicalDEField}}
        key = DigestUtils.md5DigestAsHex(key.getBytes());    
        {{/if}}
        return key;
    }

    {{/entity.unionKeyMode}}   
    {{/if}}

}