package com.izouma.dingdong.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.izouma.dingdong.annotations.Searchable;
import com.izouma.dingdong.config.Constants;
import com.izouma.dingdong.enums.Identity;
import com.izouma.dingdong.security.Authority;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

@EqualsAndHashCode(callSuper = false)
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Where(clause = "enabled = 1")
@ApiModel(value = "用户", description = "用户")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer"})
public class User extends BaseEntity implements Serializable {

    @Pattern(regexp = Constants.Regex.USERNAME)
    @Size(min = 1, max = 50)
    @Column(nullable = false, unique = true)
    @Searchable
    private String username;

    @Searchable
    private String nickname;

    private String avatar;

    @JsonIgnore
    private String password;

    @Builder.Default
    @Column(nullable = false)
    private Boolean enabled = true;

    @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH})
    @JoinTable(
            name = "user_authority",
            joinColumns = {@JoinColumn(name = "user_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<>();

    private String openId;

    private String sex;

    private String language;

    private String city;

    private String province;

    private String country;

    @Searchable
    private String phone;

    private String email;

    @Enumerated(EnumType.STRING)
    @ApiModelProperty(value = "身份", name = "identity")
    private Identity identity;

    /*    @ApiModelProperty(value = "合作申请", name = "apply")
        private String apply;*/

    @Builder.Default
    @Column(nullable = false)
    @ApiModelProperty(value = "黑名单", name = "blacklist")
    private Boolean blacklist = false;

//    @ApiModelProperty(value = "消费金额",name = "consumption")
//    private BigDecimal consumption;

    //用于商家/骑手体现
    @Column(precision = 10, scale = 2)
    @ApiModelProperty(value = "余额", name = "money")
    private BigDecimal money;

}
