package com.gyee.wisdom.dao.sql.topic.entity;

import com.gyee.wisdom.common.data.ToData;
import com.gyee.wisdom.common.data.topic.TopicPoint;
import com.gyee.wisdom.common.data.topic.Topic;
import com.gyee.wisdom.common.utils.StringUtil;
import com.gyee.wisdom.dao.sql.ModelConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @descrition:
 * @author:Wanghs
 * @date:2018-04-25
 */
@Data
@NoArgsConstructor
@ToString
@EqualsAndHashCode()
@Entity
@Table(name = ModelConstants.TOPIC_)
public class TopicEntity implements ToData<Topic> {

    @Id
    @Column(name = ModelConstants.ID_)
    private String id;

    //主题
    @Column(name = "topic")
    private String topic;

    //设备
    @Column(name = ModelConstants.THING_ID)
    private String thingId;

    //统一编码
    @Column(name = ModelConstants.UNIFORM_CODES)
    private String uniformCodes;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = ModelConstants.TOPIC_POINT,
            joinColumns = @JoinColumn(name = ModelConstants.TOPIC_ID, referencedColumnName = ModelConstants.ID_),
            inverseJoinColumns = @JoinColumn(name = ModelConstants.POINT_ID, referencedColumnName = ModelConstants.ID_))
    private List<PointEntity> pointEntityList;

    //描述
    @Column(name = ModelConstants.DESCRIPTION_)
    private String description;

    //是否启用
    @Column(name = ModelConstants.ENABLED_)
    private boolean enabled;

    //数据类型
    @Column(name = ModelConstants.DATA_TYPE)
    private int dataType;

    //序号
    @Column(name = ModelConstants.ORDER_NO)
    private int orderNo;

    //创建时间
    @Column(name = ModelConstants.CREATE_TIME)
    private Date createTime;

    //场站
    @Column(name = ModelConstants.STATION_ID)
    private String stationId;

    public TopicEntity(Topic data) {
        this.id = data.getId();
        this.topic = data.getTopic();
        if (StringUtil.isNotBlank(data.getUniformCodes()))
            this.uniformCodes = String.join(",", data.getUniformCodes());
        List<PointEntity> list = new ArrayList<>();

        for (int i = 0; i < data.getPointList().size(); i++) {
            list.add(new PointEntity(data.getPointList().get(i)));
        }
        this.pointEntityList = list;
        this.thingId = data.getThingId();
        this.description = data.getDescription();
        this.enabled = data.isEnabled();
        this.orderNo = data.getOrderNo();
        this.createTime = data.getCreateTime();
        this.stationId = data.getStationId();

    }

    @Override
    public Topic toData() {
        Topic data = new Topic();
        data.setId(this.id);
        data.setTopic(this.topic);
        data.setThingId(this.thingId);
        if (StringUtil.isNotBlank(this.uniformCodes))
            data.setUniformCodes(Arrays.asList(this.uniformCodes.split(",")));
        List<TopicPoint> list = new ArrayList<>();
        for (int i = 0; i < this.getPointEntityList().size(); i++) {
            list.add(this.getPointEntityList().get(i).toData());
        }

        data.setPointList(list);
        data.setDescription(this.description);
        data.setEnabled(this.enabled);
        data.setOrderNo(this.orderNo);
        data.setDataType(this.dataType);
        data.setCreateTime(this.createTime);
        data.setStationId(this.stationId);
        return data;
    }
}
