package cn.estsh.i3plus.pojo.platform.bean;

import cn.estsh.i3plus.pojo.base.annotation.AnnoOutputColumn;
import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

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

/**
 * @Description : 系统消息
 * @Reference :
 * @Author : wei.peng
 * @Date : 2018-10-22 16:58:43.689
 * @Modify :
 **/
@Data
@Entity
@DynamicInsert
@DynamicUpdate
@EqualsAndHashCode(callSuper = true)
@Table(name = "SYS_MESSAGE")
@Api(value = "系统消息", description = "系统消息")
public class SysMessage extends BaseBean {

    private static final long serialVersionUID = -6090909263221993356L;

    @Column(name = "MESSAGE_TITLE")
    @ApiParam(value = "消息标题", access = "消息标题")
    private String messageTitle;

    @Column(name = "MESSAGE_SOFT_TYPE")
    @ApiParam(value = "消息所属模块")
    private Integer messageSoftType;

    @Column(name = "MESSAGE_TYPE")
    @ApiParam(value = "消息类型(枚举：)", example = "-1")
    @AnnoOutputColumn(refClass = ImppEnumUtil.MESSAGE_TYPE.class)
    private Integer messageType;

    public int getMessageTypeValue() {
        if (this.messageType == null) {
            return 0;
        } else {
            return this.messageType;
        }
    }

    @AnnoOutputColumn(refClass = ImppEnumUtil.MESSAGE_TYPE_CONTENT.class)
    @Column(name = "MESSAGE_TYPE_CONTENT")
    @ApiParam(value = "消息内容类型(枚举：ImppEnumUtil.MESSAGE_TYPE_CONTENT)", example = "-1")
    private Integer messageContentType;

    @Lob
    @Column(name = "MESSAGE_CONTENT")
    @ApiParam(value = "消息内容", access = "消息内容")
    private String messageContent;

    @Column(name = "MESSAGE_SENDER_ID")
    @ApiParam(value = "发送者id", example = "-1")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long messageSenderId;

    @Column(name = "MESSAGE_SENDER_NAME_RDD")
    @ApiParam(value = "发送者名称")
    private String messageSenderNameRdd;

    @Column(name = "MESSAGE_RECEIVER_TYPE")
    @ApiParam(value = "收件人类型(枚举：ImppEnumUtil.MESSAGE_TYPE)", example = "-1")
    private Integer messageReceiverType;

    @Column(name = "MESSAGE_RECEIVERS_ID")
    @ApiParam(value = "收件者id")
    private String messageReceiversId;

    @Column(name = "MESSAGE_RECEIVERS_NAME_RDD")
    @ApiParam(value = "接受者名称集合")
    private String messageReceiversNameRdd;

    @Column(name = "MESSAGE_SEND_TIME")
    @ApiParam(value = "发送时间")
    private String messageSendTime;

    @Column(name = "IS_URGENT")
    @ApiParam(value = "是否紧急")
    private Integer isUrgent;

    @Column(name = "IS_SYSTEM")
    @ApiParam(value = "是否系统邮件")
    private Integer isSystem;

    @Column(name = "SEND_STATUS")
    @ApiParam(value = "发送状态")
    @AnnoOutputColumn(refClass = ImppEnumUtil.MESSAGE_SEND_STATUS.class)
    private Integer sendStatus;

    @Column(name = "ERROR_MESSAGE")
    @ApiParam(value = "错误信息")
    private String errorMessage;

    @Transient
    @ApiParam(value = "阅读统计")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long readCount;

    public long getReadCountVal() {
        if (readCount == null) {
            return 0;
        } else {
            return readCount.longValue();
        }
    }

    public SysMessage() {
    }

    public SysMessage(
            Long id, String messageTitle, Integer messageSoftType, Integer messageType, Integer messageContentType,
            String messageContent, Long messageSenderId, String messageSenderNameRdd, Integer messageReceiverType,
            String messageReceiversId, String messageReceiversNameRdd, String messageSendTime, Integer isUrgent,
            Integer isSystem, Integer isValid, Integer isDeleted, String createUser, String createDatetime,
            String modifyUser, String modifyDatetime, Long readCount) {
        this.id = id;
        this.messageTitle = messageTitle;
        this.messageSoftType = messageSoftType;
        this.messageType = messageType;
        this.messageContentType = messageContentType;
        this.messageContent = messageContent;
        this.messageSenderId = messageSenderId;
        this.messageSenderNameRdd = messageSenderNameRdd;
        this.messageReceiverType = messageReceiverType;
        this.messageReceiversId = messageReceiversId;
        this.messageReceiversNameRdd = messageReceiversNameRdd;
        this.messageSendTime = messageSendTime;
        this.isUrgent = isUrgent;
        this.isSystem = isSystem;
        this.readCount = readCount;
        this.isValid = isValid;
        this.isDeleted = isDeleted;
        this.createUser = createUser;
        this.createDatetime = createDatetime;
        this.modifyUser = modifyUser;
        this.modifyDatetime = modifyDatetime;
    }

    @Transient
    @ApiParam(value = "附件集合")
    private List<SysFileAttach> sysFileAttachList;

    @Transient
    @ApiParam(value = "附件id集合")
    private List<Long> fileAttachIdList;

    public void addFileAttachId(Long attachId){
        if(fileAttachIdList == null){
            fileAttachIdList = new ArrayList<>();
        }

        fileAttachIdList.add(attachId);
    }

}
