package com.lgx8.right.service.impl;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.transaction.annotation.Transactional;
import com.lgx8.gateway.dao.ICardDao;
import com.lgx8.gateway.entities.Card;
import com.lgx8.right.dao.IRoleDao;
import com.lgx8.right.dao.IUserDao;
import com.lgx8.right.entities.Role;
import com.lgx8.right.entities.User;
import com.lgx8.right.service.IUserCardService;
/**
*
* @author lihui
*
*/
public class UserCardService implements IUserCardService {
private IUserDao userDao;
private ICardDao cardDao;
private IRoleDao roleDao;
public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
public ICardDao getCardDao() {
return cardDao;
}
public IRoleDao getRoleDao() {
return roleDao;
}
public void setRoleDao(IRoleDao roleDao) {
this.roleDao = roleDao;
}
public void setCardDao(ICardDao cardDao) {
this.cardDao = cardDao;
}
/**
* 创建一个新用户,自动为其生产卡号,用于网站注册
* @param user
*/
@Transactional
public void createUser(User user) {
Card card = cardDao.createCard();
card.setStatus(Card.CARD_INUSE);
user.setCard(card);
cardDao.updateCard(card);
userDao.createUser(user);
}
/**
* 创建一个新用户,联盟商家制定其卡号,用于联盟商家推广站开卡
*/
public void createUser(User user, Card card) {
card.setStatus(Card.CARD_INUSE);
user.setCard(card);
cardDao.updateCard(card);
userDao.createUser(user);
}
public User findUser(String name, Object value) {
return userDao.findUser(name, new Object[]{value});
}
/**
* 更新用户角色授权
*/
@Transactional
public void authzRole(User user, Set<Integer> ids) {
Set<Role> roles = new HashSet<Role>();
for (Integer i : ids) {
Role role = roleDao.findRole("id", i);
roles.add(role);
}
userDao.updateUser(user);
}
/**
* 登录认证
* */
public User auth(User user) {
// User o = userDao.findUser(user.getUsername(), user.getPassword());
User o = userDao.loginUser(user.getUsername(), user.getPassword());
return o;
}
/**
* 更新用户登录信息
*/
@Transactional
public void updateUser(User user) {
userDao.updateUser(user);
}
public User findUserById(long id) {
return userDao.findUserById(id);
}
@Transactional
public void delUserById(long id) {
User user = userDao.findUserById(id);
userDao.delUser(user);
}
/**
* 创建子帐号
*/
@Transactional
public void createSubUser(User user, User rtUser) {
user.setParent(rtUser);
this.createUser(user);
List<User> childs = rtUser.getChilds();
childs.add(user);
userDao.updateUser(rtUser);
}
@Transactional
public void registerUser(User user) {
Card card = cardDao.createCard();
card.setStatus(Card.CARD_INUSE);
user.setCard(card);
cardDao.updateCard(card);
userDao.createUser(user);
}
}