/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.modbus.channel.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiModelProperty;
import cn.hutool.core.bean.copier.CopyOptions;
import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.validation.constraints.*;
import javax.persistence.Entity;
import javax.persistence.Table;

import me.zhengjie.base.BaseEntity;
import me.zhengjie.modules.dm.modbus.device.domain.DmModbusDevice;
import me.zhengjie.modules.dm.modbus.drive.domain.DmModbusDrive;
import org.hibernate.annotations.*;
import java.sql.Timestamp;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
* @website https://el-admin.vip
* @description /
* @author wanglongda
* @date 2022-05-20
**/
@Entity
@Data
@Table(name="dm_modbus_channel")
public class DmModbusChannel extends BaseEntity implements Serializable {

    @Id
    @Column(name = "channel_id")
    @ApiModelProperty(value = "信道id")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long channelId;

    @Column(name = "channel_name",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "通道名称")
    private String channelName;

    @Column(name = "serial_port",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "串口参数")
    private String serialPort;

    @Column(name = "baud_rate",nullable = false)
    @NotNull
    @ApiModelProperty(value = "波特率")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long baudRate;

    @Column(name = "data_bits",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "数据位")
    private String dataBits;

    @Column(name = "stop_bits",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "停止位")
    private String stopBits;

    @Column(name = "verify_bits",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "校验位")
    private String verifyBits;

    @Column(name = "stream_control",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "流控制")
    private String streamControl;

    @Column(name = "timeout",nullable = false)
    @NotNull
    @ApiModelProperty(value = "超时时间")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long timeout;

    @Column(name = "drive_id",nullable = false)
    @NotNull
    @ApiModelProperty(value = "驱动id")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long driveId;

    @ApiModelProperty(value = "关联的驱动channel_id")
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "channel_id")
    private Set<DmModbusDevice> dmModbusDevice;

    public void copy(DmModbusChannel source){
        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
    }
}