package cn.reghao.autodop.dmaster.notification.model.po;

import cn.reghao.autodop.dmaster.util.validator.ValidEnum;
import cn.reghao.autodop.dmaster.util.db.BaseEntity;
import lombok.*;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 通知组
 *
 * @author reghao
 * @date 2020-03-06 21:20:06
 */
@Getter
@Setter
@Entity
public class NotifyGroup extends BaseEntity<Integer> {
    @Column(nullable = false)
    @ValidEnum(value = NotifyType.class, message = "请选择正确的通知类型")
    private String notifyType;
    private String notifyAccountId;
    // TODO 同时会作为 email 的 subject
    @NotBlank(message = "接收组标识不能为空白字符串")
    private String groupId;
    // TODO 对 String 集合的内容进行校验
    @ElementCollection(targetClass = String.class)
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<String> receivers;

    @Override
    public int hashCode() {
        int result = 17;
        result = result * 31 + notifyType.hashCode();
        result = result * 31 + notifyAccountId.hashCode();
        return result;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }

        if (other instanceof NotifyGroup) {
            NotifyGroup o = (NotifyGroup) other;
            return o.notifyType.equals(notifyType) && o.notifyAccountId.equals(notifyAccountId);
        } else {
            return false;
        }
    }
}
