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 cn.ibizlab.util.domain.EntityMongo;
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 org.springframework.data.mongodb.core.mapping.Document;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.data.mongodb.core.mapping.MongoId;
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 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}}.core.{{module}}.domain.{{codeName}};
{{/entity.relEntities}}

@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(value = "handler")
@Document(collection = "{{entity.codeName}}")
@ApiModel(value = "{{entity.name}}", description = "{{entity.logicName}}")
public class {{entity.codeName}} extends EntityMongo implements Serializable
{

    {{#each entity.fields}}
        {{#unless deepStructure}}
    /**
     * {{logicName}}
     */
            {{#if keyDEField}}
    @Id
            {{else}}
            {{/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(value = "{{columnName}}", notes = "{{logicName}}")
    private {{type.java}} {{camelCase codeName}};

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

    {{#each entity.references}}
    /**
     * {{entityLogicName}}
     */
    @JsonIgnore
    @JSONField(serialize = 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}}
    @ApiModelProperty(value = "{{columnName}}", notes = "{{logicName}}")
        {{else}}
    @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("{{listCode}}", ({{camelCase codeName}}!=null)?{{camelCase codeName}}:(new ArrayList()));
    }
        {{/columnName}}
    
    {{/entity.nesteds}}

    
    {{#if entity.keyField.type.number}}
    @Override
    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}}

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

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