package cn.estsh.i3plus.ext.mes.pcn.pojo.context;

import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
 * 生产过程上下文对象-数据变量平铺设备LOG信息（兼容扫描信息）
 */
@Data
public class MesEquipVariableCollectContext implements Serializable {

    private static final long serialVersionUID = -5527066200627610909L;

    @ApiParam("组织代码")
    private String organizeCode;

    @ApiParam("设备变量ID")
    private Long equipVariableId;

    @ApiParam("设备LOG表ID")
    private Long equipmentLogId;

    @ApiParam("设备ID")
    private Integer equipId;

    @ApiParam("OPC注册ID（设备LOG表的主键）")
    private Integer clientHandle;

    @ApiParam("设备代码")
    private String equipmentCode;

    @ApiParam("通道")
    private String channel;

    @ApiParam("终端")
    private String device;

    @ApiParam("tag名称")
    private String tagName;

    @ApiParam("tag地址")
    private String tagAddress;

    @ApiParam("变量配置类型")
    private String variableCfgType;

    @ApiParam("变量类型")
    private Integer variableType;

    @ApiParam("二级变量")
    private String categoryLevelTwo;

    @ApiParam("数据类型")
    private Integer dataType;

    @ApiParam("DB块")
    private String code;

    @ApiParam("轮询时间")
    private Integer scanInterval;

    @ApiParam("读写类型")
    private Integer readWrite;

    @ApiParam("设备变量名称")
    private String equipVariableName;

    @ApiParam("设备变量描述")
    private String equipVariableDesc;

    @ApiParam("读写标记")
    private Integer readWriteFlag;

    @ApiParam("变量当前值[读到的equipVariableValue值如果存在特殊字符, 原始值赋给此字段后再过滤特殊字符]")
    private String realEquipVariableValue;

    @ApiParam("变量当前值[非匹配参考值的读取信息需要过滤特殊字符]")
    private String equipVariableValue;

    @ApiParam("变量状态")
    private Integer equipVariableStatus;

    @ApiParam("设备质量")
    private Integer quality;

    @ApiParam("写入时间")
    private String writeDatetime;

    @ApiParam("信息来源")
    private Integer messageSource;

    @ApiParam("是否被消费/是否被验证")
    private Integer isConsume = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();

    public MesEquipVariableCollectContext() {}

    public MesEquipVariableCollectContext(Long equipVariableId) {
        this.equipVariableId = equipVariableId;
    }

    //扫描/选择信息赋值
    public MesEquipVariableCollectContext(String organizeCode, String equipVariableValue, Integer messageSource) {
        this.organizeCode = organizeCode;
        this.equipVariableValue = equipVariableValue;
        this.messageSource = messageSource;
    }

    //读取信息赋值
    public MesEquipVariableCollectContext copyValue(Object equipVariableValue, Object equipVariableStatus, Object quality, Object modifyDatetime, Object id) {
        if (!StringUtils.isEmpty(equipVariableValue)) this.equipVariableValue = (String) equipVariableValue;
        if (!StringUtils.isEmpty(equipVariableStatus)) this.equipVariableStatus = (Integer) equipVariableStatus;
        if (!StringUtils.isEmpty(quality)) this.quality = (Integer) quality;
        if (!StringUtils.isEmpty(modifyDatetime)) this.writeDatetime = (String) modifyDatetime;
        if (!StringUtils.isEmpty(id)) this.equipmentLogId = Long.valueOf(id.toString());
        this.messageSource = MesExtEnumUtil.CELL_MESSAGE_SOURCE.READ.getValue();
        return this;
    }

    public MesEquipVariableCollectContext isConsume() {
        this.isConsume = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();
        return this;
    }

    public MesEquipVariableCollectContext isConsume(Integer isConsume) {
        this.isConsume = isConsume;
        return this;
    }

    public MesEquipVariableCollectContext trimValue() {
        if (!StringUtils.isEmpty(this.equipVariableValue)) this.equipVariableValue = this.equipVariableValue.trim();
        return this;
    }

    public MesEquipVariableCollectContext replaceValue(String realEquipVariableValue, String equipVariableValue) {
        this.realEquipVariableValue = realEquipVariableValue;
        this.equipVariableValue = equipVariableValue;
        return this;
    }

}
