package com.uas.platform.b2b.model;

import com.uas.platform.core.model.Status;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户简单信息，前台程序使用
 * 
 * @author yingp
 * 
 */
public class UserInfo {

	public UserInfo(User user) {
		this.userUU = user.getUserUU();
		this.userEmail = user.getUserEmail();
		this.userName = user.getUserName();
		this.userTel = user.getUserTel();
		this.userSex = user.getUserSex();
		if (user.getRoles() != null && user.getRoles().size() > 0) {
			StringBuffer sb = new StringBuffer();
			for (Role role : user.getRoles()) {
				if (sb.length() > 0) {
                    sb.append(" ");
                }
				sb.append(role.getDesc());
			}
			this.role = sb.toString();
		}
		this.sys = false;
		if (!CollectionUtils.isEmpty(user.getRoles())) { // 获取用户在该企业的权限
			for (Role role: user.getRoles()) {
				if (!CollectionUtils.isEmpty(user.getEnterprises())) {
					if (null != user.getEnterprise()) {
						if (role.getEnUU().equals(user.getEnterprise().getUu())) {
							if (role.isSys()) {
								this.sys = true;
							}
						}
					}
				}
			}
		} 
		if (!CollectionUtils.isEmpty(user.getEnterprises())) {
			Enterprise current = user.getEnterprise();
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			for (Enterprise enterprise : user.getEnterprises()) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("enName", enterprise.getEnName());
				map.put("uu", enterprise.getUu());
				map.put("logUrl", enterprise.getLogUrl());
				if(enterprise.getEnSaasStatus() != null && enterprise.getEnSaasStatus() == Status.ENABLED.value()) {
					map.put("enSaasUrl", enterprise.getEnSaasUrl());
				}
				if (enterprise.equals(current)) {
                    map.put("current", true);
                }
				list.add(map);
			}
			this.enterprises = list;
		}
	}

	public static Page<UserInfo> getUserInfo(Page<User> page) {
		List<UserInfo> infos = new ArrayList<UserInfo>();
		if (page != null && page.getContent() != null && !page.getContent().isEmpty()) {
			for (User user : page.getContent()) {
				infos.add(new UserInfo(user));
			}
		}
		return new PageImpl<UserInfo>(infos);
	}

	private Long userUU;

	private String userName;

	private String userEmail;

	private String userTel;

	private String userSex;

	private String role;
	
	private boolean sys;

	private List<Map<String, Object>> enterprises;

	public Long getUserUU() {
		return userUU;
	}

	public void setUserUU(Long userUU) {
		this.userUU = userUU;
	}

	public String getUserName() {
		return userName;
	}

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

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public String getUserTel() {
		return userTel;
	}

	public void setUserTel(String userTel) {
		this.userTel = userTel;
	}

	public String getUserSex() {
		return userSex;
	}

	public void setUserSex(String userSex) {
		this.userSex = userSex;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public List<Map<String, Object>> getEnterprises() {
		return enterprises;
	}

	public void setEnterprises(List<Map<String, Object>> enterprises) {
		this.enterprises = enterprises;
	}

	public boolean isSys() {
		return sys;
	}

	public void setSys(boolean sys) {
		this.sys = sys;
	}

}
