package com.iformall.domain.po;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.enums.EnumUserAdmin;
import com.iformall.enums.EnumUserWechat;
import com.iformall.utils.Constant;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;


import java.util.Date;
import java.util.List;

@TableName(value = "mall_user_info")
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class MallUserInfo extends BaseEntity {


    protected Long id;


	@io.swagger.annotations.ApiModelProperty(value="租户ID",name="tenantId")
	private String tenantId;
	@io.swagger.annotations.ApiModelProperty(value="登录用户名",name="username")
	private String username;
	@io.swagger.annotations.ApiModelProperty(value="用户名称",name="name")
	private String name;
	@io.swagger.annotations.ApiModelProperty(value="密码",name="password")
	private String password;
	@io.swagger.annotations.ApiModelProperty(value="创建时间",name="createTime")
	private Date createTime;
	@io.swagger.annotations.ApiModelProperty(value="最后登录时间",name="lastLoginTime")
	private Date lastLoginTime;
	@io.swagger.annotations.ApiModelProperty(value="1:有效，0:禁止登录",name="status")
	private Integer status;
	@io.swagger.annotations.ApiModelProperty(value="0 不是超管 1是超管",name="isAdmin")
	private Integer isAdmin;
	@io.swagger.annotations.ApiModelProperty(value="招商用户角色",name="investRule")
	private Integer investRule;
	@io.swagger.annotations.ApiModelProperty(value="昵称",name="nickName")
	private String nickName;

	@io.swagger.annotations.ApiModelProperty(value="手机,系统内手机号唯一",name="phone")
	private String phone;

	@io.swagger.annotations.ApiModelProperty(value="B端用户微信openid，领导看数据塔台用",name="bopenId")
	private String bopenId;

    @io.swagger.annotations.ApiModelProperty(value="admin用户微信登录用，可绑定多个账户",name="webOpenId")
    private String webOpenId;

    @io.swagger.annotations.ApiModelProperty(value="1 未绑定wechat，0 绑定微信",name="withWechat")
    @TableField(exist = false)
    private Integer withWechat;

	@TableField(exist = false)
	private Integer withWechatForQuery;

	@TableField(exist = false)
	private Long roleId;
	
	@TableField(exist = false)
	private String roleName;
    //该用户所有的角色
    @TableField(exist = false)
    private List<MallRole> role;
    //该用户包含的所有权限
    @TableField(exist = false)
    private List<MallPermission> permission;
    //拥有的菜单权限
    @TableField(exist = false)
    private String menus;

	@TableField(exist = false)
	private String captcha;

	@io.swagger.annotations.ApiModelProperty(value = "邮箱", name = "email")
	private String email;

	public Integer getWithWechatNotFormat() {
		return this.withWechat;
	}

	public Integer getWithWechat() {
		if(StringUtils.isNotBlank(this.webOpenId)) {
			this.withWechat = EnumUserWechat.HAD.getCode();
		} else {
			this.withWechat = EnumUserWechat.NO.getCode();
		}
		return this.withWechat;
	}

    public void protectInfos() {
	    if(StringUtils.isNotBlank(this.password)) {
            setPassword("保密");
        }
        if(StringUtils.isNotBlank(this.bopenId)) {
            setBopenId("保密");
        }
        if(StringUtils.isNotBlank(this.webOpenId)) {
            setWebOpenId("保密");
        }
    }

    public boolean isFmSuperAdmin() {
	    if(StringUtils.isBlank(tenantId)) {
	        return false;
        }
        if(tenantId.equalsIgnoreCase(Constant.FM_SYS_ADMIN)) {
            if(id.equals(Constant.SUPER_ADMIN) || isAdmin.equals(EnumUserAdmin.ADMIN.getCode())) {
                return true;
            }
        }
        return false;
    }

}
