/*
*  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.device.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.channel.domain.DmModbusChannel;
import me.zhengjie.modules.dm.modbus.group.domain.DmModbusGroup;
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 wld
* @date 2022-05-20
**/
@Entity
@Data
@Table(name="dm_modbus_device")
public class DmModbusDevice extends BaseEntity implements Serializable {

    @Id
    @Column(name = "device_id")
    @ApiModelProperty(value = "设备id")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long deviceId;

    @Column(name = "device_name",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "设备名称")
    private String deviceName;

    @Column(name = "salve_station_number",nullable = false)
    @NotNull
    @ApiModelProperty(value = "从站站号")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long salveStationNumber;

    @Column(name = "station_number",nullable = false)
    @NotNull
    @ApiModelProperty(value = "采集通讯站号")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long stationNumber;

    @Column(name = "Request_frame_interval",nullable = false)
    @NotNull
    @ApiModelProperty(value = "采集通讯请求帧间隔(毫秒)")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long requestFrameInterval;

    @Column(name = "write_value_refresh_interval",nullable = false)
    @NotNull
    @ApiModelProperty(value = "采集通讯写值刷新间隔(毫秒)")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long writeValueRefreshInterval;

    @Column(name = "two_byte_int_order",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "采集通讯2字节整数顺序")
    private String twoByteIntOrder;

    @Column(name = "four_byte_int_order",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "采集通讯4字节整数顺序")
    private String fourByteIntOrder;

    @Column(name = "float_order",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "单精度浮点数顺序")
    private String floatOrder;

    @Column(name = "analog_quantity_group_packet_interval",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "批量传输模拟量组包间隔")
    private String analogQuantityGroupPacketInterval;

    @Column(name = "analog_quantity_group_packet_max_length",nullable = false)
    @NotNull
    @ApiModelProperty(value = "批量传输模拟量组包最大长度")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long analogQuantityGroupPacketMaxlength;

    @Column(name = "digital_quantity_group_packet_interval",nullable = false)
    @NotNull
    @ApiModelProperty(value = "批量传输数字量组包间隔")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long digitalQuantityGroupPacketInterval;

    @Column(name = "digital_quantity_group_packet_max_length",nullable = false)
    @NotNull
    @ApiModelProperty(value = "批量传输数字量组包最大长度")
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long digitalQuantityGroupPacketMaxlength;

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


    @ApiModelProperty(value = "状态")
    @Column(name = "device_status",nullable = false)
    @NotNull
    private String deviceStatus;

    @ApiModelProperty(value = "关联的分组")
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "device_id")
    private Set<DmModbusGroup> dmModbusGroup;

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