/**
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.sifang.modules.sys.common.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.sifang.modules.sys.common.persistence.BaseEntity;
import com.sifang.modules.sys.common.utils.excel.ExcelField;
import com.sifang.modules.sys.extention.permission.AbstractPermissionFilter;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.*;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;

import javax.persistence.Entity;
import javax.persistence.*;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 用户Entity
 * @version 2019-12-15
 */
@Entity
@Table(name = "sys_user")
@DynamicInsert @DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends BaseEntity<User> implements Serializable {

	public final static String USER_TYPE_SYS = "0";	   //超级管理员
	public final static String USER_TYPE_YYS = "1";      //运营商
	public final static String USER_TYPE_SIT = "2";      //服务商
	public final static String USER_TYPE_XXY = "3";      //信息员
	public final static String USER_TYPE_WXG = "4";      //平台员工
	public final static String USER_TYPE_HUI = "5";      //服务商人员
	public final static String USER_TYPE_KJ = "6";      //代理商
	public final static String USER_TYPE_ADVISOR = "7"; //代理商人员
	public final static String USER_TYPE_SUPPLI = "8"; //供应商
	public final static String USER_TYPE_PTYG = "4";      //平台员工
	public final static String USER_TYPE_FWSRY = "5";      //服务商人员
	public final static String USER_TYPE_FWS = "3";         //服务商
	public final static String USER_TYPE_GYS="8";          //供应商
	public final static String USER_TYPE_PARTNER="9";//合作商
	public final static String USER_TYPE_HZS="a";          //合作商人员

	public final static Integer USER_CREATE_TYPE_DAORU = 1;//导入类型的用户
	public final static Integer USER_CREATE_TYPE_LURU = 2;//录入类型的用户
	public final static Integer USER_CREATE_TYPE_ZIJIREG = 3;//自己注册类型的用户
	
	private static final long serialVersionUID = 1L;
	private String id;
//	private Office company;	// 归属公司
//	private Office office;	// 归属部门
	private String loginName;// 登录名
	private String password;// 密码
	private String password1;// 用户密码验证
	private String userType;// 用户类型
	private String mobile;
	private String email;	// 邮箱
	private String createBy;
	private Date createDate;
	private Integer createType;//创建类型
	private String remarks;
	private String appType;
	private String status;
	private String resetPwdToken;
	private Date updateTime;
	private String openid;
	private String registrationId;
	private String pushId;
	private String permission;
	private String sfRegisId;
//	private String loginId;//登录ID
//	private Date loginDate;	// 最后登陆日期
	private String departmentId;

	@Transient
	private String departmentName;

	@Transient
	private String traderType;

	@Transient
	private String traderId;
	@Transient
	private String topTraderId;

	@Transient
	public String getTopTraderId() {
		return topTraderId;
	}
	@Transient
	public void setTopTraderId(String topTraderId) {
		this.topTraderId = topTraderId;
	}

	@Transient
	public String getTraderType() {
		return traderType;
	}
	@Transient
	public void setTraderType(String traderType) {
		this.traderType = traderType;
	}
	@Transient
	public String getTraderId() {
		return traderId;
	}
	@Transient
	public void setTraderId(String traderId) {
		this.traderId = traderId;
	}

	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}
	@Transient
	public String getDepartmentName() {
		return departmentName;
	}
	@Transient
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	@Transient
	private AbstractPermissionFilter apf;
	
	
	private List<Role> roleList = Lists.newArrayList(); // 拥有角色列表

	public User() {
		super();
		this.status = BaseEntity.DEL_FLAG_NORMAL;
		this.createDate = new Date();
	}
	
	public User(String id) {
		this();
		this.id = id;
	}

	@Id
	@GeneratedValue(generator = "idGenerator")
	@GenericGenerator(name ="idGenerator" , strategy ="uuid")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
//	@ManyToOne
//	@JoinColumn(name="company_id")
//	@NotFound(action = NotFoundAction.IGNORE)
//	@JsonIgnore
//	@NotNull(message="归属公司不能为空")
//	@ExcelField(title="归属公司", align=2, sort=20)
//	public Office getCompany() {
//		return company;
//	}
//
//	public void setCompany(Office company) {
//		this.company = company;
//	}
	
//	@ManyToOne
//	@JoinColumn(name="office_id")
//	@NotFound(action = NotFoundAction.IGNORE)
//	@JsonIgnore
//	@NotNull(message="归属部门不能为空")
//	@ExcelField(title="归属部门", align=2, sort=25)
//	public Office getOffice() {
//		return office;
//	}
//
//	public void setOffice(Office office) {
//		this.office = office;
//	}

	@Length(min=1, max=100)
	@ExcelField(title="登录名", align=2, sort=30)
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	@JsonIgnore
	@Length(min=1, max=100)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Email @Length(min=0, max=200)
	@ExcelField(title="邮箱", align=1, sort=50)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Integer getCreateType() {
		return createType;
	}

	public void setCreateType(Integer createType) {
		this.createType = createType;
	}

	//@Transient
	@ExcelField(title="备注", align=1, sort=900)
	public String getRemarks() {
		return remarks;
	}
	
	@Length(min=0, max=100)
	@ExcelField(title="用户类型", align=2, sort=80, dictType="sys_user_type")
	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	//@Transient
	@ExcelField(title="创建时间", type=0, align=1, sort=90)
	public Date getCreateDate() {
		return createDate;
	}

	/*@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@ExcelField(title="最后登录日期", type=1, align=1, sort=110)
	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}*/

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "sys_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
	@Where(clause="status='"+BaseEntity.DEL_FLAG_NORMAL+"'")
	@OrderBy("id") @Fetch(FetchMode.SUBSELECT)
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@JsonIgnore
	public List<Role> getRoleList() {
		return roleList;
	}
	
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	@Transient
	@JsonIgnore
	public List<String> getRoleIdList() {
		List<String> roleIdList = Lists.newArrayList();
		for (Role role : roleList) {
			roleIdList.add(role.getId());
		}
		return roleIdList;
	}

	@Transient
	public void setRoleIdList(List<String> roleIdList) {
		roleList = Lists.newArrayList();
		for (String roleId : roleIdList) {
			Role role = new Role();
			role.setId(roleId);
			roleList.add(role);
		}
	}
	
	@Transient
	public boolean isAdmin(){
		return isAdmin(this.id);
	}
	
	@Transient
	public static boolean isAdmin(String id){
		return id != null && id.equals("1");
	}

	@Transient
	public String getPassword1() {
		return password1;
	}
	@Transient
	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getCreateBy() {
		return createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	public String getAppType() {
		return appType;
	}

	public void setAppType(String appType) {
		this.appType = appType;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getResetPwdToken() {
		return resetPwdToken;
	}

	public void setResetPwdToken(String resetPwdToken) {
		this.resetPwdToken = resetPwdToken;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getRegistrationId() {
		return registrationId;
	}

	public void setRegistrationId(String registrationId) {
		this.registrationId = registrationId;
	}

	public String getPushId() {
		return pushId;
	}

	public void setPushId(String pushId) {
		this.pushId = pushId;
	}

	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public String getSfRegisId() {
		return sfRegisId;
	}

	public void setSfRegisId(String sfRegisId) {
		this.sfRegisId = sfRegisId;
	}

	@Transient
	public AbstractPermissionFilter getApf() {
		return apf;
	}

	@Transient
	public void setApf(AbstractPermissionFilter apf) {
		this.apf = apf;
	}

	@Transient
	private String userName;

	@Transient
	public String getUserName() {
		return userName;
	}

	@Transient
	public void setUserName(String userName) {
		this.userName = userName;
	}

    @Transient
	private String roleIds;

	@Transient
	public String getRoleIds() {
		return roleIds;
	}
	@Transient
	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	//	@Override
//	public String toString() {
//		return ToStringBuilder.reflectionToString(this);
//	}
}