package com.xydl.cac.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.LinkedHashMap;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "icd_config_type")
@ApiModel("ICD配置类型表")
public class IcdConfigType {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @ApiModelProperty(name = "Iedid")
    @Column(name = "icd_ied_id")
    private Integer icdIedId;

    @ApiModelProperty(name = "IED名称")
    @Column(name = "ied_name")
    private String iedName;

    @ApiModelProperty(name = "设备类型")
    @Column(name = "ldevice_inst")
    private String ldeviceInst;

    @ApiModelProperty(name = "LN类型")
    @Column(name = "ln_class")
    private String lnClass;

    @ApiModelProperty(name = "LN类型备注")
    @Column(name = "ln_desc")
    private String lnDesc;

    @ApiModelProperty(name = "表名")
    @Column(name = "table_name")
    private String tableName;

    @Transient
    private LinkedHashMap<String, IcdConfigTypeAtt> attMap;
    @Transient
    private List<IcdConfigTypeAtt> attList;

    @Transient
    private LinkedHashMap<String, IcdConfigTypeInst> instMap;
    @Transient
    private List<IcdConfigTypeInst> instList;

    public void addAtt(String doName, String fc, String lastName, String desc, String inst) {
        if (attMap == null) {
            attMap = new LinkedHashMap<>();
        }
        IcdConfigTypeAtt item = attMap.get(doName);
        if (item == null) {
            item = IcdConfigTypeAtt.builder()
                    .doName(doName)
                    .fc(fc)
                    .lastName(lastName)
                    .description(desc)
                    .build();
            attMap.put(doName, item);
        }
        item.addInst(inst);
    }

    public void addInst(String inst) {
        if (instMap == null) {
            instMap = new LinkedHashMap<>();
        }
        if (!instMap.containsKey(inst)) {
            IcdConfigTypeInst item = IcdConfigTypeInst.builder()
                    .inst(inst)
                    .build();
            instMap.put(inst, item);
        }

    }
}