package com.ks.tool.bkz.service.impl.user;
|
|
import com.ks.tool.bkz.dao.mybatis.user.UserInfoMapper;
|
import com.ks.tool.bkz.entity.user.UserInfo;
|
import com.ks.tool.bkz.exception.UserException;
|
import com.ks.tool.bkz.service.user.UserService;
|
import com.ks.tool.bkz.util.StringUtil;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
|
@Service
|
public class UserServiceImpl implements UserService {
|
|
@Resource
|
private UserInfoMapper userInfoMapper;
|
|
@Transactional
|
@Override
|
public void addUser(UserInfo user) throws UserException {
|
//账户为空
|
if (StringUtil.isNullOrEmpty(user.getAccount()))
|
throw new UserException(1, "账号为空");
|
UserInfo old = selectByAccount(user.getAccount());
|
if (old != null) {
|
throw new UserException(2, "账号已存在");
|
}
|
|
if (user.getCreateTime() == null)
|
user.setCreateTime(new Date());
|
|
user.setLevel(UserInfo.LEVEL_GENERAL);
|
userInfoMapper.insertSelective(user);
|
}
|
|
@Override
|
public UserInfo selectByAccount(String account) {
|
return userInfoMapper.selectByAccount(account);
|
}
|
|
@Override
|
public UserInfo selectValidByPrimaryKey(Long id) {
|
UserInfo user = userInfoMapper.selectByPrimaryKey(id);
|
if (user.getState() == UserInfo.STATE_NORMAL)
|
return user;
|
return null;
|
}
|
|
@Override
|
public void login(String account, String ipInfo) {
|
UserInfo user = selectByAccount(account);
|
if (user != null) {
|
UserInfo updateUser = new UserInfo();
|
updateUser.setId(user.getId());
|
updateUser.setLoginTime(new Date());
|
updateUser.setUpdateTime(new Date());
|
updateUser.setLoginIpInfo(ipInfo);
|
userInfoMapper.updateByPrimaryKeySelective(updateUser);
|
}
|
}
|
|
@Transactional
|
@Override
|
public void upgradeSuper(Long id) throws UserException{
|
UserInfo user = selectValidByPrimaryKey(id);
|
if (user == null)
|
throw new UserException(1,"用户不存在");
|
if(user.getLevel()==UserInfo.LEVEL_SUPER)
|
throw new UserException(2,"已经是共享版");
|
|
UserInfo update = new UserInfo();
|
update.setId(id);
|
update.setUpdateTime(new Date());
|
update.setSuperTime(new Date());
|
update.setLevel(UserInfo.LEVEL_SUPER);
|
userInfoMapper.updateByPrimaryKeySelective(update);
|
}
|
}
|