package com.dcit.authority.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.dcit.authority.dao.SysUserMapper;
import com.dcit.authority.dao.SysUserRolesMapper;
import com.dcit.authority.dto.SysUserBasicInfoDTO;
import com.dcit.authority.dto.SysUserDTO;
import com.dcit.authority.dto.UserRolesInfoDTO;
import com.dcit.authority.model.SysUser;
import com.dcit.authority.model.SysUserRoles;
import com.dcit.authority.service.SysUserService;

@Service
public class SysUserServiceImpl implements SysUserService{

	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Autowired
	private SysUserRolesMapper sysUserRolesMapper;

	@Override
	public String loadAccountRole(String account) {
		// TODO Auto-generated method stub
		return sysUserMapper.selectUserRoles(account);
	}

	@Override
	public List<SysUserDTO> getUserList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return sysUserMapper.selectUserList(map);
	}

	@Override
	public List<SysUser> getUserListByRoleId(String roleId) {
		// TODO Auto-generated method stub
		return sysUserMapper.selectUserListByRoleId(roleId);
	}

	@Override
	public boolean authorityUserRole(String account, String roleId) {
		// TODO Auto-generated method stub
		SysUserRoles sysUserRoles = new SysUserRoles();
		sysUserRoles.setRoleId(roleId);
		sysUserRoles.setUserId(account);
		sysUserRoles.setCreateTime(new Date());
		sysUserRoles.setUpdateTime(new Date());
        return sysUserRolesMapper.insert(sysUserRoles) == 1 ? Boolean.TRUE :Boolean.FALSE;
	}

	@Override
	public boolean deleteAuthorityUserRole(String uid, String roleId) {
		SysUserRoles sysUserRoles = new SysUserRoles();
		sysUserRoles.setRoleId(roleId);
		sysUserRoles.setUserId(uid);
        return sysUserRolesMapper.deleteByUniqueKey(sysUserRoles) == 1 ? Boolean.TRUE : Boolean.FALSE;
	}

	@Override
	public SysUserBasicInfoDTO getUserByAccount(String account) {
		// TODO Auto-generated method stub
		return sysUserMapper.selectByAccount(account);
	}

	@Override
	public List<SysUser> getNotAuthorityUserListByRoleId(String roleId) {
		// TODO Auto-generated method stub
		return sysUserMapper.selectUserListExtendByRoleId(roleId);
	}

	@Override
	public SysUser selectByPrimaryKey(String uid) {
		// TODO Auto-generated method stub
		return sysUserMapper.selectByPrimaryKey(uid);
	}

	@Override
	public int insertSelective(SysUser record) {
		// TODO Auto-generated method stub
		return sysUserMapper.insertSelective(record);
	}

	@Override
	public int updateByPrimaryKeySelective(SysUser record) {
		// TODO Auto-generated method stub
		return sysUserMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<UserRolesInfoDTO> selectUserRolesInfoByUid(String userId) {
		// TODO Auto-generated method stub
		return sysUserRolesMapper.selectUserRolesInfoByUid(userId);
	}

	@Override
	public int addOrEditUserRoles(SysUserRoles sysUserRoles) {
		// TODO Auto-generated method stub
		return sysUserRolesMapper.insertSelective(sysUserRoles);
	}

	@Override
	public int deleteUserRolesById(String rid) {
		// TODO Auto-generated method stub
		return sysUserRolesMapper.deleteByPrimaryKey(rid);
	}

	@Override
	public SysUserRoles selectUserRolesById(String id) {
		// TODO Auto-generated method stub
		return sysUserRolesMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKey(SysUser record) {
		// TODO Auto-generated method stub
		return sysUserMapper.updateByPrimaryKey(record);
	}

	@Override
	public int deleteUserRolesByUserId(String userId) {
		// TODO Auto-generated method stub
		return sysUserRolesMapper.deleteUserRolesByUserId(userId);
	}

	@Override
	public int deleteRoleUsersByRoleId(String roleId) {
		// TODO Auto-generated method stub
		return sysUserRolesMapper.deleteRoleUsersByRoleId(roleId);
	}

	@Override
	public List<SysUser> selectSysUser(String account) throws DataAccessException {
		// TODO Auto-generated method stub
		return sysUserMapper.selectSysUser(account);
	}

	
	
	

}
