admin
2020-06-17 87b391b8a81ee2abdaa4131d245784ecc7a54e9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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);
    }
}