package com.bocloud.ims.entity.topology;

import com.bocloud.common.utils.DateSerializer;
import com.bocloud.entity.annotations.Column;
import com.bocloud.entity.annotations.IgnoreAll;
import com.bocloud.entity.annotations.PK;
import com.bocloud.entity.annotations.Table;
import com.bocloud.entity.bean.Generic;
import com.bocloud.entity.meta.PKStrategy;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;

/**
 * @author tangcq
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Table("ci_module_relation")
public class ModuleRelation extends Generic {
    @PK(value = PKStrategy.AUTO)
    private Long id;
    @Column("source_id")
    private Long sourceId;
    @Column("source_type")
    private String sourceType;
    @Column("target_type")
    private String targetType;
    @Column("relation_id")
    private Long relationId;
    @Column("target_id")
    private Long targetId;
    /**
     * 关系类型ID
     */
    @Column("category_id")
    private Long categoryId;
    // 创建时间
    @Column("gmt_create")
    @JsonSerialize(using = DateSerializer.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtCreate;
    // 创建人
    @Column("creator_id")
    private Long creatorId;
    @Column("is_deleted")
    private Boolean deleted;
    // 源模型
    @IgnoreAll
    private String sourceName;
    // 目标模型
    @IgnoreAll
    private String targetName;
    @IgnoreAll
    private String relationName;
    @IgnoreAll
    private List<Instance> instances;

    public Boolean isSource(Long moduleId) {
        if (moduleId == null) {
            return false;
        }
        return this.sourceId.equals(moduleId);
    }

    public Boolean isTarget(Long moduleId) {
        if (moduleId == null) {
            return false;
        }
        return this.targetId.equals(moduleId);
    }
}
