/*
*  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 com.youchain.jamod.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @website https://eladmin.vip
* @description /
* @author huojin
* @date 2025-03-21
**/
@Entity
@Data
@Table(name="modbus_info")
public class ModbusInfo implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "`id`")
    @ApiModelProperty(value = "自增主键，唯一标识每条记录")
    private Integer id;

    @Column(name = "`device_name`",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "Modbus 设备的名称，用于直观识别设备")
    private String deviceName;

    @Column(name = "`device_ip`",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "Modbus 设备的 IP 地址")
    private String deviceIp;

    @Column(name = "`device_port`",nullable = false)
    @NotNull
    @ApiModelProperty(value = "Modbus 设备的端口号")
    private Integer devicePort;

    @Column(name = "`device_description`")
    @ApiModelProperty(value = "Modbus 设备的详细描述信息，如设备用途、型号等")
    private String deviceDescription;

    @Column(name = "`data_type`",nullable = false)
    @NotNull
    @ApiModelProperty(value = "Modbus 数据的类型")
    private String dataType;

    @Column(name = "`start_address`",nullable = false)
    @NotNull
    @ApiModelProperty(value = "批量读取数据的起始地址")
    private Integer startAddress;

    @Column(name = "`data_length`",nullable = false)
    @NotNull
    @ApiModelProperty(value = "批量读取的数据长度")
    private Integer dataLength;

    @Column(name = "`data_values`")
    @ApiModelProperty(value = "批量读取的数据值集合，以特定格式存储，如逗号分隔的字符串")
    private String dataValues;

    @Column(name = "`last_read_time`")
    @ApiModelProperty(value = "最后一次读取该数据的时间")
    private Timestamp lastReadTime;

    @Column(name = "`last_modify_time`")
    @ApiModelProperty(value = "最后一次修改该数据的时间")
    private Timestamp lastModifyTime;

    @Column(name = "`data_version`")
    @ApiModelProperty(value = "数据的版本号，每次数据修改时版本号递增，用于数据追溯")
    private Integer dataVersion;

    @Column(name = "`data_status`")
    @ApiModelProperty(value = "数据的状态，normal 表示正常，abnormal 表示异常，warning 表示警告，maintenance 表示维护中")
    private String dataStatus;

    @Column(name = "`alarm_status`")
    @ApiModelProperty(value = "是否触发报警，TRUE 表示触发，FALSE 表示未触发")
    private Integer alarmStatus;

    @Column(name = "`is_connected`")
    @ApiModelProperty(value = "记录设备连接状态，TRUE 表示连接成功，FALSE 表示连接失败")
    private Integer isConnected;

    @Column(name = "`max_retries`")
    @ApiModelProperty(value = "异常重试次数")
    private Integer maxRetries;

    @Column(name = "`enabled`")
    @ApiModelProperty(value = "状态：1启用、0禁用")
    private Boolean enabled;

    @Column(name = "`description`")
    @ApiModelProperty(value = "报错原因")
    private String description;

    public List<String> getDataValueList() {
        if (dataValues == null || dataValues.isEmpty()) {
            return new ArrayList<>();
        }
        String trimmedString = dataValues.substring(1, dataValues.length() - 1);
        String[] stringArray = trimmedString.split(",");
        return Arrays.asList(stringArray);
    }

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