package com.gxwebsoft.common.system.entity;

import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.security.core.userdetails.UserDetails;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户
 *
 * @author WebSoft
 * @since 2018-12-24 16:10:13
 */
@Data
@Schema(description = "用户")
@TableName("sys_user")
public class User implements UserDetails {
    private static final long serialVersionUID = 1L;

    @Schema(description = "用户id")
    @TableId(type = IdType.AUTO)
    private Integer userId;

    @Schema(description = "用户类型, 0普通用户")
    private Integer type;

    @Schema(description = "用户编码")
    private String userCode;

    @Schema(description = "第三方系统的用户ID")
    private Integer uid;

    @Schema(description = "账号")
    private String username;

    @Schema(description = "密码")
    private String password;

    @Schema(description = "昵称")
    private String nickname;

    @Schema(description = "头像")
    private String avatar;

    @Schema(description = "头像")
    private String bgImage;

    @Schema(description = "性别, 字典标识")
    private String sex;

    @Schema(description = "手机号")
    private String phone;

    @Schema(description = "邮箱")
    private String email;

    @Schema(description = "邮箱验证码（非数据库字段，仅用于绑定/修改邮箱时校验）")
    @TableField(exist = false)
    private String emailCode;

    @Schema(description = "短信验证码（非数据库字段，仅用于绑定/修改手机号时校验）")
    @TableField(exist = false)
    private String smsCode;

    @Schema(description = "新手机号短信验证码（非数据库字段，仅用于换绑手机号时二次验证新号归属）")
    @TableField(exist = false)
    private String smsCodeNew;

    @Schema(description = "资质")
    private String aptitude;

    @Schema(description = "头衔")
    private String title;

    @Schema(description = "特长")
    private String speciality;

    @Schema(description = "支付密码")
    private String payPassword;

    @Schema(description = "职务")
    private String position;

    @Schema(description = "邮箱是否验证, 0否, 1是")
    private Integer emailVerified;

    @Schema(description = "别名")
    private String alias;

    @Schema(description = "真实姓名")
    private String realName;

    @Schema(description = "身份证号(脱敏)")
    private String idCard;

    @Schema(description = "身份证号")
    @TableField(exist = false)
    private String idCardNo;

    @Schema(description = "出生日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDateTime birthday;

    @Schema(description = "年龄")
    private Integer age;

    @Schema(description = "所在国家")
    private String country;

    @Schema(description = "所在省份")
    private String province;

    @Schema(description = "所在城市")
    private String city;

    @Schema(description = "所在辖区")
    private String region;

    @Schema(description = "街道地址")
    private String address;

    @Schema(description = "店名")
    private String shopName;

    @Schema(description = "行业类型(父级)")
    private String industryParent;

    @Schema(description = "行业类型(子级)")
    private String industryChild;

    @Schema(description = "经度")
    private String longitude;

    @Schema(description = "纬度")
    private String latitude;

    @Schema(description = "用户可用余额")
    private BigDecimal balance;

    @Schema(description = "已提现金额")
    private BigDecimal cashedMoney;

    @Schema(description = "用户可用积分")
    private Integer points;

    @Schema(description = "用户总支付的金额")
    private String payMoney;

    @Schema(description = "实际消费的金额(不含退款)")
    private String expendMoney;

    @Schema(description = "会员等级ID")
    private Integer gradeId;

    @Schema(description = "个人简介")
    private String introduction;

    @Schema(description = "机构ID")
    private Integer organizationId;

    @Schema(description = "会员分组ID")
    private Integer groupId;

    @Schema(description = "会员分组")
    @TableField(exist = false)
    private String groupName;

    @Schema(description = "企业ID")
    private Integer companyId;

    @Schema(description = "注册来源客户端")
    private String platform;

    @Schema(description = "是否下线会员")
    private Integer offline;

    @Schema(description = "关注数")
    private Integer followers;

    @Schema(description = "粉丝数")
    private Integer fans;

    @Schema(description = "获赞数")
    private Integer likes;

    @Schema(description = "客户端ID")
    private String clientId;

    @Schema(description = "可管理的商户")
    private String merchants;

    @Schema(description = "商户ID")
    private Long merchantId;

    @Schema(description = "商户名称")
    private String merchantName;

    @Schema(description = "商户LOGO")
    private String merchantAvatar;

    @Schema(description = "是否管理员")
    private Boolean isAdmin;

    @Schema(description = "是否企业管理员")
    private Integer isOrganizationAdmin;

    @Schema(description = "是否超级管理员")
    private Boolean isSuperAdmin;

    @Schema(description = "是否开发者")
    private Boolean isDeveloper;

    @Schema(description = "租户管理员ID")
    @TableField(exist = false)
    private Integer adminId;

    @Schema(description = "用于一键登录控制台")
    @TableField(exist = false)
    private String adminToken;

    @Schema(description = "评论数")
    private Integer commentNumbers;

    @Schema(description = "是否推荐")
    private Integer recommend;

    @Schema(description = "备注")
    private String comments;

    @Schema(description = "专家角色")
    private Integer expertType;

    @Schema(description = "审核状态: 0待审核, 1已通过, 2已拒绝")
    private Integer auditStatus;

    @Schema(description = "审核驳回原因")
    private String rejectReason;

    @Schema(description = "状态, 0正常, 1冻结")
    private Integer status;

    @Schema(description = "是否删除, 0否, 1是")
    @TableLogic
    private Integer deleted;

    @Schema(description = "租户id")
    private Integer tenantId;

    @Schema(description = "租户名称")
    @TableField(exist = false)
    private String tenantName;

    @Schema(description = "最后结算时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime settlementTime;

    @Schema(description = "注册时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "修改时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "公司名称")
    @TableField(exist = false)
    private String companyName;

    @Schema(description = "是否已实名认证")
    private Boolean certification;

    @Schema(description = "机构名称")
    @TableField(exist = false)
    private String organizationName;

    @Schema(description = "性别名称")
    @TableField(exist = false)
    private String sexName;

    @Schema(description = "会员等级")
    @TableField(exist = false)
    private String gradeName;

    @Schema(description = "默认注册的角色ID")
    @TableField(exist = false)
    private Integer roleId;

    @Schema(description = "角色列表")
    @TableField(exist = false)
    private List<Role> roles;

    @Schema(description = "权限列表")
    @TableField(exist = false)
    private List<Menu> authorities;

    @Schema(description = "微信凭证")
    @TableField(exist = false)
    private String code;

    @Schema(description = "推荐人ID")
    @TableField(exist = false)
    private Integer dealerId;

    @Schema(description = "微信openid")
    private String openid;

    @Schema(description = "微信公众号openid")
    private String officeOpenid;

    @Schema(description = "微信unionid")
    private String unionid;

    @Schema(description = "累计登录次数")
    private Integer loginNum;

    @Schema(description = "ico文件")
    @TableField(exist = false)
    private String logo;

    @Schema(description = "创建的应用数量")
    @TableField(exist = false)
    private Double apps;

    @Schema(description = "租户设置信息")
    @TableField(exist = false)
    private String setting;

    @Schema(description = "手机号(脱敏)")
    @TableField(exist = false)
    private String mobile;

    @Schema(description = "当期登录的商户账号")
    @TableField(exist = false)
    private MerchantAccount merchantAccount;

    @Schema(description = "推荐人用户信息")
    @TableField(exist = false)
    private User referee;

    @Schema(description = "手机号登录校验码")
    @TableField(exist = false)
    private String phoneLoginCode;

    @Schema(description = "校验码")
    @TableField(exist = false)
    private String authCode;

    @Schema(description = "是否有上级")
    @TableField(exist = false)
    private Boolean hasParent;

    @Schema(description = "同一个手机号码存在多个管理员账号")
    @TableField(exist = false)
    private Boolean hasAdminsByPhone;

    @Schema(description = "是否已设置密码（未设置过密码的用户可免旧密码直接设置）")
    @TableField(exist = false)
    private Boolean hasPassword;

    @Schema(description = "模板ID")
    private Integer templateId;

    @Schema(description = "插件安装状态")
    private Boolean installed;

//    @Schema(description = "企业信息")
//    @TableField(exist = false)
//    private Company companyInfo;

//    @Schema(description = "系统配置信息")
//    @TableField(exist = false)
//    private Object system;

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status != null && this.status == 0;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public String getMobile(){
      return DesensitizedUtil.mobilePhone(this.phone);
    }

    public String getIdCardNo(){
      return DesensitizedUtil.idCardNum(this.idCard,4,4);
    }

}
