package com.goafanti.user.service.impl;

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

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goafanti.app.bo.UserBasicInfo;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.OrganizationIdentityMapper;
import com.goafanti.common.dao.UserIdentityMapper;
import com.goafanti.common.dao.UserMapper;
import com.goafanti.common.model.User;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.user.bo.OrgUnitNames;
import com.goafanti.user.bo.UserBaseBo;
import com.goafanti.user.bo.UserDownLoadBo;
import com.goafanti.user.bo.UserNames;
import com.goafanti.user.bo.UserPageHomeBo;
import com.goafanti.user.service.UserService;

@Service
public class UserServiceImpl extends BaseMybatisDao<UserMapper> implements UserService {
	@Autowired
	private UserMapper						userMapper;
	@Autowired
	private OrganizationIdentityMapper		organizationIdentityMapper;
	@Autowired
	private UserIdentityMapper				userIdentityMapper;
	@Override
	@Transactional
	public User insertRegister(User user, String unitName, String userName) {
		userMapper.insert(user);
		/*if (null != user.getType() && UserType.ORGANIZATION.getCode().equals(user.getType())) {
			OrganizationIdentity organizationIdentity = new OrganizationIdentity();
			organizationIdentity.setUid(user.getId());
			organizationIdentity.setUnitName(StringUtils.isBlank(unitName) ? "" : unitName);
			organizationIdentity.setId(UUID.randomUUID().toString());
			organizationIdentity.setIdentityType(0);
			organizationIdentity.setAuditStatus(0);
			organizationIdentity.setProcess(0);
			organizationIdentity.setWrongCount(0);
			organizationIdentity.setListed(0);
			organizationIdentity.setListedType(0);
			organizationIdentityMapper.insert(organizationIdentity);
		}
		if (null != user.getType() && UserType.PERSONAL.getCode().equals(user.getType())) {
			UserIdentity ui = new UserIdentity();
			ui.setId(UUID.randomUUID().toString());
			ui.setUid(user.getId());
			ui.setAuditStatus(0);
			ui.setProcess(0);
			ui.setWrongCount(0);
			ui.setUsername(StringUtils.isBlank(userName) ? "" : userName);
			userIdentityMapper.insert(ui);
		}*/
		return user;
	}

	@Override
	public User selectByMobieAndType(String mobile, Integer type) {
		return userMapper.selectByMobieAndType(mobile, type);
	}

	@Override
	public User selectByNameAndType(String username, Integer type){
		return userMapper.selectByNameAndType(username,type);
	}
	
	@Override
	public User selectByPrimaryKey(String id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(User u) {
		return userMapper.updateByPrimaryKeySelective(u);
	}

	@Override
	public User insert(User user) {
		userMapper.insert(user);
		return user;
	}

	@Override
	public UserPageHomeBo selectUserPageHomeBoByUserId(String uid) {
		UserBaseBo baseBo = userMapper.selectUserBaseByUserId(uid);
		UserPageHomeBo homeBo = new UserPageHomeBo();
		/*homeBo.setPatentNum(patentInfoMapper.findPatentInfoNumByUid(uid));
		homeBo.setCopyrightNum(copyrightInfoMapper.findCopyrightInfoNumByUid(uid));
		homeBo.setTechProjectNum(techProjectMapper.findTechProjectNumByUid(uid));
		homeBo.setIntellectualPropertyNum(orgIntellectualPropertyMapper.findIntellectualPropertyNum(uid));*/
		BeanUtils.copyProperties(baseBo, homeBo);
		return homeBo;
	}

	@Override
	public UserDownLoadBo selectUserDownLoadBoByUserId(String id) {
		return userMapper.selectUserDownLoadBoByUserId(id);
	}

	@Override
	public List<OrgUnitNames> selectDemandUnitNames() {
		String aid = TokenManager.getAdminId();
		if (TokenManager.hasRole(AFTConstants.MANAGERADMIN)) {
			return organizationIdentityMapper.selectManagerUnitNames(aid);
		} else {
			if (TokenManager.hasRole(AFTConstants.SUPERADMIN)) {
				aid = null;
			}
			return organizationIdentityMapper.selectAllOrgIndentity(aid);
		}
	}

	@Override
	public List<UserNames> selectDemandUserNames() {
		String aid = TokenManager.getAdminId();
		if (TokenManager.hasRole(AFTConstants.MANAGERADMIN)) {
			return userIdentityMapper.selectManagerUserNames(aid);
		} else {
			if (TokenManager.hasRole(AFTConstants.SUPERADMIN)) {
				aid = null;
			}
			return userIdentityMapper.selectAllUserNames(aid);
		}
	}

	@Override
	public Map<String, String> selectAchievementUserOwner(String name) {
		String aid = TokenManager.getAdminId();
		if (TokenManager.hasRole(AFTConstants.MANAGERADMIN)) {
			return disposeAchievementUserOwner(userIdentityMapper.selectManagerAchievementUserOwner(aid, name));
		} else {
			if (TokenManager.hasRole(AFTConstants.SUPERADMIN) || TokenManager.hasRole(AFTConstants.TECHBROKER)
					|| TokenManager.hasRole(AFTConstants.AUDITORADMIN)) {
				aid = null;
			}
			return disposeAchievementUserOwner(userIdentityMapper.selectAchievementUserOwner(aid, name));
		}
	}

	public List<OrgUnitNames> selectAchievementOrgOwner(String name) {
		String aid = TokenManager.getAdminId();
		if (TokenManager.hasRole(AFTConstants.MANAGERADMIN)) {
			return organizationIdentityMapper.selectManagerAchievementOrgOwner(aid, name);
		} else {
			if (TokenManager.hasRole(AFTConstants.SUPERADMIN) || TokenManager.hasRole(AFTConstants.TECHBROKER)
					|| TokenManager.hasRole(AFTConstants.AUDITORADMIN)) {
				aid = null;
			}
			return organizationIdentityMapper.selectAchievementOrgOwner(aid, name);
		}
	}

	private Map<String, String> disposeAchievementUserOwner(List<UserNames> list) {
		Map<String, String> map = new HashMap<>();
		if (null != list && list.size() > 0) {
			for (UserNames u : list) {
				map.put(u.getUid(), u.getUsername() + " " + u.getMobile());
			}
		}
		return map;
	}

	@Override
	public String findUserNameByNameAndMobile(String name, String mobile) {
		return userMapper.findUserByNameAndMobile(name, mobile);
	}

	@Override
	public String findOrgNameByNameAndMobile(String name, String mobile) {
		return userMapper.findOrgByNameAndMobile(name, mobile);
	}


	@Override
	@Cacheable(value = "UserNumberCache", key = "'UserNumberCache:UID:'+#userId")
	public String selectNumberByPrimaryKey(String userId) {
		return String.valueOf(userMapper.selectNumberByPrimaryKey(userId));
	}	

	@Override
	public UserBasicInfo selectBaseInfo() {
		if(TokenManager.getToken() instanceof User)return userMapper.selectBaseInfo(TokenManager.getUserId());
		return null;
	}
	@Override
	public List<User> selectUserByRoleName(String roleName){
		return userMapper.selectUserByRoleName(roleName);
	}

	@Override
	public User selectByNumber(String easemobName) {
		return userMapper.selectByNumber(easemobName);
	}

	@Override
	public UserBaseBo selectUserBase(String userId) {
		 
		return userMapper.selectUserBaseByUserId(userId);
	}
}
