package net.siufung.bzgzpt.persistence.uap.service.impl;

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

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

import cn.jbinfo.platform.core.basic.exception.ServiceException;
import cn.jbinfo.platform.core.framework.page.MyPage;
import cn.jbinfo.platform.core.utils.StringUtils;
import net.siufung.bzgzpt.persistence.collect.domain.Aba31;
import net.siufung.bzgzpt.persistence.uap.dao.IDepartDao;
import net.siufung.bzgzpt.persistence.uap.dao.IRoleDao;
import net.siufung.bzgzpt.persistence.uap.dao.ISysXzqhQdDao;
import net.siufung.bzgzpt.persistence.uap.dao.IUserDao;
import net.siufung.bzgzpt.persistence.uap.domain.Depart;
import net.siufung.bzgzpt.persistence.uap.domain.Role;
import net.siufung.bzgzpt.persistence.uap.domain.SysXzqhQd;
import net.siufung.bzgzpt.persistence.uap.domain.User;
import net.siufung.bzgzpt.persistence.uap.service.IUserService;

@Service("userService")
@Transactional(readOnly = true)
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private IUserDao userDao;
	@Autowired
	private IRoleDao roleDao;
	
	@Autowired
	private IDepartDao departDao;
	
	@Autowired
	private ISysXzqhQdDao sysXzqhQdDao;
	
	@Override
	public User get(User paramT) throws ServiceException {
		// TODO Auto-generated method stub
		return userDao.get(paramT.getId());
	}

	@Override
	public List<User> findList(User paramT) throws ServiceException {
		// TODO Auto-generated method stub
		return userDao.findList(paramT);
	}

	@Override
	@Transactional(readOnly = false)
	public void insert(User paramT) throws ServiceException {
		// TODO Auto-generated method stub
		userDao.insert(paramT);
		/*if (StringUtils.isNotBlank(paramT.getId())){
			userDao.deleteUserRole(paramT);
			if (paramT.getRoleList() != null && paramT.getRoleList().size() > 0){
				userDao.insertUserRole(paramT);
			}
			else
			{
				throw new ServiceException(paramT.getLoginName() + "没有设置角色！");
			}
		}*/
	}

	@Override
	@Transactional(readOnly = false)
	public void update(User paramT) throws ServiceException {
		// TODO Auto-generated method stub
		userDao.update(paramT);
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(User paramT) throws ServiceException {
		// TODO Auto-generated method stub
		userDao.delete(paramT);
	}

	@Override
	public User getUserByLoginName(String loginName) throws ServiceException {
		// TODO Auto-generated method stub
		User user = userDao.getByLoginName(new User(null, loginName));
		if (user == null){
			return null;
		}
		user.setRoleList(roleDao.findList(new Role(user)));
		return user;
	}
	
	@Override
	public User getUserByEmpid(String empid) throws ServiceException {
		// TODO Auto-generated method stub
		User user2 = new User();
		user2.setEmpId(empid);
		User user = userDao.getByEmpid(user2);
		if (user == null){
			return null;
		}
		return user;
	}

	@Override
	public List<User> findUserByDepartId(String departId) {
		// TODO Auto-generated method stub
		User user=new User();
		user.setDepartId(departId);
		return userDao.findUserByDepartId(user);
	}

	@Override
	@Transactional(readOnly = false)
	public void updatePasswordById(User user) {
		// TODO Auto-generated method stub
		userDao.updatePasswordById(user);
		
	}

	@Override
	@Transactional(readOnly = false)
	public void updateUserLoginInfo(User user) {
		// TODO Auto-generated method stub
		userDao.updateLoginInfo(user);
	}
	
	@Override
	@Transactional(readOnly = false)
	public void updateUserInfo(User user) {
		// TODO Auto-generated method stub
		userDao.updateUserInfo(user);
	}
	@Override
	@Transactional(readOnly = false)
	public User assignUserToRole(Role role, User user) {
		// TODO Auto-generated method stub
		if (user == null){
			return null;
		}
		List<String> roleIds = user.getRoleIdList();
		if (roleIds.contains(role.getId())) {
			return null;
		}
		user.getRoleList().add(role);
		userDao.insert(user);
		return user;
	}

	@Override
	public User get(String id) throws ServiceException {
		// TODO Auto-generated method stub
		return userDao.get(id);
	}

	@Override
	public User getById(String id) throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}

	//获取分页数据
	public MyPage<Map<String, Object>> getPage(User user) {
		MyPage<Map<String, Object>> pageView = new MyPage<Map<String, Object>>(user.getPageSize(), user.getPageIndex());
	    Map<String, Object> map = new HashMap<String, Object>();
	    map.put("paging", pageView);
	    map.put("o", user);
	    List<Map<String, Object>> objectList = userDao.queryForPage(map);
	    pageView.setRecords(objectList);
	    return pageView;
	}
	public HashMap getUserAndDeptByLoginName(String username,
			String deptCode) {
		HashMap<String,Object> map = new HashMap<String,Object>();
		User buziUser = null;
		if(StringUtils.isNotBlank(username)){
			buziUser = userDao.getByLoginNameHis(username);
		}
		if(buziUser!=null){
			Depart buziDept = departDao.getById(buziUser.getDepartId());
			map.put("buziUser", buziUser);
			map.put("buziDept", buziDept);
		}else{
			SysXzqhQd buziXzqh = sysXzqhQdDao.getById(StringUtils.toLong(deptCode));
			map.put("buziXzqh", buziXzqh);
		}
		return map;
	}

	

}
