yujian
2019-11-25 aa552facf6d833acab0d3e3e29bda2a0fb826ffe
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.yeshi.fanli.service.impl.user.vip;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.user.vip.UserVIPInfoMapper;
import com.yeshi.fanli.entity.bus.user.vip.UserVIPInfo;
import com.yeshi.fanli.exception.user.vip.UserVIPInfoException;
import com.yeshi.fanli.service.inter.user.vip.UserVIPInfoService;
 
@Service
public class UserVIPInfoServiceImpl implements UserVIPInfoService {
 
    @Resource
    private UserVIPInfoMapper userVIPInfoMapper;
 
    @Override
    public void addUserVIPInfo(UserVIPInfo info) throws UserVIPInfoException {
        if (info.getId() == null) {
            throw new UserVIPInfoException(1, "信息不完整");
        }
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKey(info.getId());
        if (userInfo != null) {
            throw new UserVIPInfoException(1, "信息已存在");
        }
        // 初始化状态
        info.setState(UserVIPInfo.STATE_INVALID);
        info.setCreateTime(new Date());
        userVIPInfoMapper.insert(info);
    }
 
    @Transactional
    @Override
    public void passVIPApply(Long uid) throws UserVIPInfoException {
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userInfo == null) {
            throw new UserVIPInfoException(1, "用户信息不存在");
        }
        if (userInfo.getState() != UserVIPInfo.STATE_VERIFING) {
            throw new UserVIPInfoException(2, "申请未处于审核状态");
        }
 
        UserVIPInfo info = new UserVIPInfo();
        info.setId(userInfo.getId());
        info.setSuccessTime(new Date());
        info.setState(UserVIPInfo.STATE_SUCCESS);
        info.setUpdateTime(new Date());
        userVIPInfoMapper.updateByPrimaryKeySelective(info);
    }
 
    @Transactional
    @Override
    public void rejectVIPApply(Long uid, String reason) throws UserVIPInfoException {
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userInfo == null) {
            throw new UserVIPInfoException(1, "用户信息不存在");
        }
        if (userInfo.getState() != UserVIPInfo.STATE_VERIFING) {
            throw new UserVIPInfoException(2, "申请未处于审核状态");
        }
 
        UserVIPInfo info = new UserVIPInfo();
        info.setId(userInfo.getId());
        info.setState(UserVIPInfo.STATE_INVALID);
        info.setUpdateTime(new Date());
        userVIPInfoMapper.updateByPrimaryKeySelective(info);
    }
 
    @Transactional
    @Override
    public void applyVIP(Long uid) throws UserVIPInfoException {
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKeyForUpdate(uid);
        if (userInfo == null) {
            throw new UserVIPInfoException(1, "用户信息不存在");
        }
 
        if (userInfo.getState() != UserVIPInfo.STATE_INVALID) {
            throw new UserVIPInfoException(2, "已经申请过");
        }
 
        UserVIPInfo info = new UserVIPInfo();
        info.setId(userInfo.getId());
        info.setApplyTime(new Date());
        info.setState(UserVIPInfo.STATE_VERIFING);
        info.setUpdateTime(new Date());
        userVIPInfoMapper.updateByPrimaryKeySelective(info);
    }
 
    @Override
    public boolean isVIP(Long uid) {
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKey(uid);
        if (userInfo != null && userInfo.getState() == UserVIPInfo.STATE_SUCCESS)
            return true;
        else
            return false;
    }
 
    @Override
    public UserVIPInfo selectByUid(Long uid) {
        UserVIPInfo userInfo = userVIPInfoMapper.selectByPrimaryKey(uid);
        return userInfo;
    }
 
    @Override
    public Map<Long, Boolean> listByUids(List<Long> uids) {
        if (uids == null || uids.size() == 0)
            return new HashMap<>();
        List<UserVIPInfo> infoList = userVIPInfoMapper.listByUids(uids);
        // 整理数据
        Map<Long, Boolean> map = new HashMap<>();
        if (infoList != null)
            for (UserVIPInfo info : infoList)
                if (info != null && info.getState() == UserVIPInfo.STATE_SUCCESS)
                    map.put(info.getId(), true);
                else
                    map.put(info.getId(), false);
        for (Long uid : uids) {
            if (map.get(uid) == null)
                map.put(uid, false);
        }
        return map;
    }
 
}