package com.izouma.wenlvju.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.izouma.wenlvju.converter.StringArrayConverter;
import com.izouma.wenlvju.enums.MessageType;
import com.izouma.wenlvju.security.Authority;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Where(clause = "del = 0")
public class Message extends BaseEntity {

    @Enumerated(EnumType.STRING)
    private MessageType type;

    @Column(columnDefinition = "TEXT")
    private String content;

    @Convert(converter = StringArrayConverter.class)
    @Column(columnDefinition = "TEXT")
    @ApiModelProperty(value = "发送的手机号")
    private List<String> phone;

    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH})
    @JoinTable(
            name = "message_authority",
            joinColumns = {@JoinColumn(name = "message_id", referencedColumnName = "id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))},
            inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))})
    @BatchSize(size = 20)
    @ExcelIgnore
    private Set<Authority> authorities = new HashSet<>();

    @ApiModelProperty(value = "返回内容")
    private String responseBody;

    @ApiModelProperty(value = "是否已发送")
    private boolean send;
}
