package {{packageName}}.core.{{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 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.annotation.Audit;
import cn.ibizlab.util.enums.DupCheck;
import cn.ibizlab.util.domain.EntityMP;
import java.io.Serializable;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Transient;
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}}.core.{{module}}.domain.{{codeName}};
{{/entity.relEntities}}

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

    {{#each entity.fields}}
        {{#unless deepStructure}}
    /**
     * {{logicName}}
     */
            {{#if keyDEField}}
                {{#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{{#validLogicValue}}(value = "{{validLogicValue}}"{{#invalidLogicValue}} , delval = "{{invalidLogicValue}}"{{/invalidLogicValue}}){{/validLogicValue}}
                {{/logicValidField}}
            {{/if}}
            {{#enableAudit}}
    @Audit
            {{/enableAudit}}
    @DEField({{annotation}})
    @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("{{logicName}}")
    private {{type.java}} {{camelCase codeName}};

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

    {{#each entity.references}}
    /**
     * {{entityLogicName}}
     */
    @JsonIgnore
    @JSONField(serialize = false)
    @TableField(exist = false)
    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}}.core.{{module}}.domain.handlers.{{entityCodeName}}TypeHandler.class)
        {{else}}
    @TableField(exist = false)
        {{/if}}
    protected List<{{entityCodeName}}> {{camelCase codeName}};
    {{/each}}

    {{#each entity.fields}}
        {{#unless deepStructure}}
            {{#unless keyDEField}}
                {{#unless predefinedType}}
    /**
     * 设置 [{{logicName}}]
     */
    public {{entity.codeName}} set{{pascalCase codeName}}({{type.java}} {{camelCase codeName}}) {
        this.{{camelCase codeName}} = {{camelCase codeName}};
        this.modify("{{lowerCase name}}", {{camelCase codeName}});
        return this;
    }
                {{/unless}}
            {{/unless}}
            {{#timeType}}
    /**
     * 格式化日期 [{{logicName}}]
     */
    public String format{{pascalCase codeName}}() {
        if (this.{{camelCase codeName}} == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("{{format}}");
        return sdf.format({{camelCase codeName}});
    }
            {{/timeType}}

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

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

    {{#entity.unionKeyMode}}

    {{#unless entity.keyField.phisicalDEField}}
    /**
     * 设置 [{{logicName}}]
     */
    public {{entity.codeName}} 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}}
        }
        return this;
    }

    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}}

    @Override
    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 entity.keyField.type.number}}
    @Override
    public Serializable getDefaultKey(boolean gen) { 
        return IdWorker.getId();
    }   
    {{/if}}

    {{#if entity.hasResetField}}
    /**
     * 复制当前对象数据到目标对象(粘贴重置)
     * @param targetEntity 目标数据对象
     * @param bIncEmpty  是否包括空值
     * @param <T>
     * @return
     */
    @Override
    public <T> T copyTo(T targetEntity, boolean bIncEmpty) {
        {{#each entity.fields}}
            {{#if pasteReset}}
        this.reset("{{lowerCase name}}");        
            {{/if}}    
        {{/each}}
        return super.copyTo(targetEntity, bIncEmpty);
    }    
    {{/if}}
}