/*
 * 该类主要负责系统主要业务逻辑的实现,如多表处理的事务操作、权限控制等
 * 该类根据具体的业务逻辑来调用该实体对应的Dao或者多个Dao来实现数据库操作
 * 实际的数据库操作在对应的Dao或其他Dao中实现
 */

package com.blueland.bpm.identity.service;

import com.blueland.bpm.identity.RoleService;
import com.blueland.bpm.identity.UserService;
import com.blueland.bpm.identity.dao.UserQueryDao;
import com.blueland.bpm.identity.model.Org;
import com.blueland.bpm.identity.model.Role;
import com.blueland.bpm.identity.model.User;
import com.blueland.bpm.identity.util.IdentityException;
import com.blueland.bpm.identity.util.UserServiceImplCondition;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;

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

/**
 * @author lw
 * @version 1.0
 * @since
 */
@Service("userServiceImpl")
@Conditional(UserServiceImplCondition.class)
public class UserServiceImpl implements UserService {

	private static final Log logger = LogFactory.getLog(UserServiceImpl.class);
	@Autowired
	// User对应的DAO类,主要用于数据库的增删改查等操作
	private UserQueryDao userQueryDao;
	@Autowired
	private RoleService roleService;

	@Override
	public User getUserById(String userId) {
		User user = null;
		try {
			user = userQueryDao.getById(userId);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return user;
	}
	
	@Override
	public User getCenterUserById(String userId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Map<String, Object> pageQueryUserByAttr(String userType, String userName, int pageNumber, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public User getUserByCode(String userCode,String orgId) {
		User user = null;
		try {
			User params=new User();
			params.setUserCode(userCode);
			params.setOrgId(orgId);
			user = userQueryDao.getByEntity(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return user;
	}
	
	@Override
	public User getUserByCode(String userCode) {
		User user = null;
		try {
			User params=new User();
			params.setUserCode(userCode);
			user = userQueryDao.getByEntity(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return user;
	}

	@Override
	public List<User> findUserByOrgId(String orgId) {
		List<User> users = null;
		try {
			Org params = new Org();
			params.setOrgId(orgId);
			users = userQueryDao.findUserByOrg(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}
	
	@Override
	public List<User> findUserByOrgIds(List<Org> orgs) {
		List<User> users = null;
		try {
			Map params=new HashMap();
			params.put("orgs", orgs);
			users = userQueryDao.getSqlSessionTemplate().selectList(userQueryDao.getIbatisMapperNamesapce()+".findUserByOrgs", params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}
	
	@Override
	public List<User> getUserByUserNameOrUserCode(String userName,String userCode){
		List<User> users = null;
		try {
			users = userQueryDao.getUserByUserNameOrUserCode(userName,userCode);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}

	@Override
	public List<User> findUserTreeByOrgId(String orgId) {
		List<User> users = null;
		try {
			Org org = new Org();
			org.setOrgId(orgId);
			users = this.userQueryDao.getSqlSessionTemplate().selectList(this.userQueryDao.getIbatisMapperNamesapce()+".findUserTreeByOrgId",org);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}

	@Override
	public List<User> findUserByRoleId(String roleId) {
		List<User> users = null;
		try {
			Role params = new Role();
			params.setRoleId(roleId);
			users = userQueryDao.findUserByRole(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}

	

	@Override
	public List<User> findUserByEntitiyCriteria(User paramEntity) {
		List<User> users = null;
		try {
			users = userQueryDao.findByExample(paramEntity);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}

	@Override
	public List<User> findUser(String roleId, String orgId) {
		List<User> users = null;
		try {
			Role params = new Role();
			params.setRoleId(roleId);
			users = userQueryDao.findUserByRole(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}


	@Override
	public boolean hasUser(String orgId) {
		boolean hasUserFlag = false;
		try {
			User params = new User();
			params.setOrgId(orgId);
			hasUserFlag = userQueryDao.findUserCount(params)>0;
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return hasUserFlag;
	}

	@Override
	public List<User> findUserIdByRoleCascade(String roleId) {
		List<User> users = null;
		try {
			Role params = new Role();
			params.setRoleId(roleId);
			users = userQueryDao.findUserIdByRoleCascade(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}

	@Override
	public List<User> findAllUserByRole(String roleId) {
		List<User> users = null;
		try {
			Role params = new Role();
			params.setRoleId(roleId);
			users = userQueryDao.findAllUserByRole(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}
	
	
	@Override
	public List<User> findUserByRoleCascade(String roleId, String orgId,
			Integer flag) {
		List<User> users = null;
		try {
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("roleId",roleId);
			params.put("orgId",orgId);
			params.put("rowNumCount",flag);
			users = userQueryDao.findUserByRoleCascade(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}

	
	@Override
	public boolean isUserInOrg(String userId, String orgId) {
		User user = this.getUserById(userId);
		if (user == null) {
			return false;
		}
		return user.getOrgId().equals(orgId);
	}
	@Override
	public boolean isUserInRole(String userId, String roleId) {
		return roleService.isUserInRole(roleId, userId);
	}

	@Override
	public List<User> findUserByRole(String roleId, String companyId,String deptId) {
		List<User> users = null;
		try {
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("roleId",roleId);
			if(StringUtils.isNotEmpty(companyId)){
				params.put("companyId",companyId);
			}if(StringUtils.isNotEmpty(deptId)){
				params.put("deptId",deptId);
			}
			users = userQueryDao.findUserByRoleCascade(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return users;
	}

	@Override
	public User getUserByDeptId(String deptId,String userCode){
		
		User user = null;
		try {
			User params=new User();
			params.setUserCode(userCode);
			params.setDeptId(deptId);
			user = userQueryDao.getByEntity(params);
		} catch (Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return user;
	}

	@Override
	public List<String> findUserMobiles(List<String> userIds) {
		if(userIds==null||userIds.isEmpty()) {
			return null;
		}
		Map<String,List<String>> params=new HashMap<String,List<String>>();
		params.put("userIds",userIds);
		return this.userQueryDao.getSqlSessionTemplate().selectList(this.userQueryDao.getIbatisMapperNamesapce()+".findUserMobiles",params);
	}

	@Override
	public List<String> findUserIdByRoleId(String roleId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> ssoLogin(String bwUcLocalCookieValue){
		// TODO Auto-generated method stub
		return null;
	}
}
