package cn.estsh.i3plus.pojo.aps.bean;

import cn.estsh.i3plus.pojo.aps.annotation.FieldAnnotation;
import cn.estsh.i3plus.pojo.aps.annotation.MainKey;
import cn.estsh.i3plus.pojo.aps.common.BaseAPS;
import cn.estsh.i3plus.pojo.aps.common.BeanRelation;
import cn.estsh.i3plus.pojo.aps.holders.EImportDetail;
import cn.estsh.i3plus.pojo.aps.model.ImportDataModel;
import cn.estsh.i3plus.pojo.aps.validator.InsertGroup;
import cn.estsh.i3plus.pojo.aps.validator.UpdateGroup;
import cn.estsh.i3plus.pojo.base.enumutil.ApsEnumUtil;
import com.fasterxml.jackson.annotation.JsonBackReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;
import java.util.List;

/**
 * @Description :数据导入明细
 * @Reference :
 * @Author : jason.niu
 * @CreateDate : 2019-09-23
 * @Modify:
 **/
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "APS_IMPORT_DETAIL")
@Api("数据导入明细")
@MainKey(groups = {InsertGroup.class, UpdateGroup.class})
public class ImportDetail extends BaseAPS {

    private static final long serialVersionUID = 1264030397500660450L;

    @Column(name="NAME")
    @ApiParam(value ="名称")
    @FieldAnnotation(notEmpty = true, mainkey = true)
    private String name;

    @Column(name="GROUP_NAME")
    @ApiParam(value ="分组名称")
    @FieldAnnotation(notEmpty = true)
    private String groupName;

    @Column(name="SUB_GROUP_NAME")
    @ApiParam(value ="子分组名称")
    @FieldAnnotation(notEmpty = true)
    private String subGroupName;

    @Column(name="LINK_TYPE")
    @ApiParam(value ="链接类型")
    @FieldAnnotation(defaultValue = "EXCEL", notEmpty = true)
    private ApsEnumUtil.DATA_LINK_TYPE linkType;

    @Column(name="PATH")
    @ApiParam(value ="IP/文件路径")
    private String path;

    @Column(name="OUT_NAME")
    @ApiParam(value ="外部表名")
    private String outName;

    @Column(name="IN_NAME")
    @ApiParam(value ="内部表名")
    @FieldAnnotation(editType = ApsEnumUtil.EDIT_TYPE.QUERY_LIST, notEmpty = true)
    private String inName;

    @Column(name="PORT")
    @ApiParam(value ="端口")
    private Integer port;

    @Column(name="DB_NAME")
    @ApiParam(value ="数据库名")
    private String dbName;

    @Column(name="USER_NAME")
    @ApiParam(value ="用户名")
    private String userName;

    @Column(name="PASSWORD")
    @ApiParam(value ="密码")
    private String password;

    @Column(name="IMPORT_TYPE")
    @ApiParam(value ="导入类型")
    @FieldAnnotation(defaultValue = "DIFF")
    private ApsEnumUtil.IMPORT_DETAIL_TYPE importType;

    @Column(name="SKIP_ROWS")
    @ApiParam(value ="跳过行数")
    @FieldAnnotation(defaultValue = "0")
    private Integer skipRows;

    @Column(name="ORDER_NUMBER")
    @ApiParam(value ="序号")
    @FieldAnnotation(property = false)
    private Integer orderNumber;

    @Column(name="PROJECT_ID")
    @ApiParam(value ="导入项目")
    @FieldAnnotation(relation = "Project", property = false)
    private Long projectId;

    @Lob
    @Column(name="INNER_FILTER")
    @ApiParam(value ="内部对象筛选")
    private String innerFilter;

    @Lob
    @Column(name="OUTTER_FILTER")
    @ApiParam(value ="外部对象筛选")
    private String outterFilter;

    @FieldAnnotation(property = false)
    private transient ImportDataModel model;

    public ImportProject getProject() {
        return BeanRelation.get(this, EImportDetail.Project);
    }

    public void setProject(ImportProject project) {
        this.projectId = project != null ? project.getId() : 0l;
        BeanRelation.set(this, EImportDetail.Project, project);
    }

    @JsonBackReference
    public List<ImportField> getFields() {
        return BeanRelation.list(this, EImportDetail.Fields);
    }
}
