admin
2020-05-18 c37ba1bdbf2454184889d6ce7bfd80c7aa113424
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.yeshi.fanli.service.impl.user.vip;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.user.vip.GiveVIPApplyInfoDao;
import com.yeshi.fanli.entity.bus.user.UserInfoRegister;
import com.yeshi.fanli.entity.bus.user.vip.GiveVIPApplyInfo;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
import com.yeshi.fanli.entity.bus.user.vip.UserVIPPreInfo;
import com.yeshi.fanli.exception.ParamsException;
import com.yeshi.fanli.exception.user.vip.GiveVIPApplyInfoException;
import com.yeshi.fanli.exception.user.vip.UserVIPPreInfoException;
import com.yeshi.fanli.service.inter.user.UserInfoRegisterService;
import com.yeshi.fanli.service.inter.user.msg.UserAccountMsgNotificationService;
import com.yeshi.fanli.service.inter.user.vip.GiveVIPApplyInfoService;
import com.yeshi.fanli.service.inter.user.vip.UserVIPPreInfoService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.user.UserLevelUtil;
 
@Service
public class GiveVIPApplyInfoServiceImpl implements GiveVIPApplyInfoService {
 
    @Resource
    private GiveVIPApplyInfoDao giveVIPApplyInfoDao;
 
    @Resource
    private UserVIPPreInfoService userVIPPreInfoService;
 
    @Resource
    private UserInfoRegisterService userInfoRegisterService;
 
    @Resource
    private UserAccountMsgNotificationService userAccountMsgNotificationService;
 
    @Async
    @Override
    public void saveImgs(String id, List<String> imgList) {
        giveVIPApplyInfoDao.saveImgs(id, imgList);
    }
 
    @Override
    public void updateGiveVIPApplyInfo(GiveVIPApplyInfo info) {
        giveVIPApplyInfoDao.updateSelective(info);
    }
 
    @Override
    public GiveVIPApplyInfo addGiveVIPApplyInfo(GiveVIPApplyInfo info)
            throws ParamsException, GiveVIPApplyInfoException {
 
        if (info.getTargetUid() == null || info.getSourceUid() == null || info.getLevel() == null)
            throw new ParamsException(1, "参数不完整");
 
        info.setId(StringUtil
                .Md5(String.format("%s#%s#%s", info.getTargetUid(), info.getSourceUid(), info.getLevel().name())));
 
        if (info.getCreateTime() == null)
            info.setCreateTime(new Date());
        if (info.getState() == null)
            info.setState(GiveVIPApplyInfo.STATE_NO_INFO);
 
        GiveVIPApplyInfo oldInfo = giveVIPApplyInfoDao.get(info.getId());
 
        if (oldInfo != null) {
            throw new GiveVIPApplyInfoException(2, "已经提交过申请");
        }
        giveVIPApplyInfoDao.save(info);
        return info;
    }
 
    @Override
    public List<GiveVIPApplyInfo> listByTargetUid(Long uid, int page, int pageSize) {
        return giveVIPApplyInfoDao.listByTargetUid(uid, null, (page - 1) * pageSize, pageSize);
    }
 
    @Override
    public long countByTargetUid(Long uid) {
        return giveVIPApplyInfoDao.countByTargetUid(uid, null);
    }
 
    @Override
    public List<GiveVIPApplyInfo> listByStateAndTargetUid(Long targetUid, Integer state, int page, int pageSize) {
        return giveVIPApplyInfoDao.listByTargetUid(targetUid, state, (page - 1) * pageSize, pageSize);
    }
 
    @Override
    public long countByStateAndTargetUid(Long targetUid, Integer state) {
        return giveVIPApplyInfoDao.countByTargetUid(targetUid, state);
    }
 
    @Override
    public GiveVIPApplyInfo selectByPrimaryKey(String id) {
        return giveVIPApplyInfoDao.get(id);
    }
 
    @Transactional
    @Override
    public void pass(String id, Long adminId) throws GiveVIPApplyInfoException, UserVIPPreInfoException {
        GiveVIPApplyInfo info = selectByPrimaryKey(id);
        if (info == null)
            throw new GiveVIPApplyInfoException(1, "记录不存在");
 
        // if (info.getState() == GiveVIPApplyInfo.STATE_NO_INFO)
        // throw new GiveVIPApplyInfoException(2, "用户未提交资料");
 
        if (info.getState() == GiveVIPApplyInfo.STATE_REJECT)
            throw new GiveVIPApplyInfoException(3, "已经被拒绝");
 
        if (info.getState() == GiveVIPApplyInfo.STATE_SUCCESS)
            throw new GiveVIPApplyInfoException(4, "已经被通过");
 
        Long uid = info.getTargetUid();
        // 查询当前的条件
        UserVIPPreInfo preInfo = userVIPPreInfoService.getLatestProcessInfo(uid);
        if (preInfo == null || preInfo.getProcess() < info.getLevel().getLevel()) {
            // 会员升级
            UserVIPPreInfo newInfo = new UserVIPPreInfo();
            newInfo.setSourceType(UserVIPPreInfo.SOURCE_TYPE_ARTIFICIAL);
            newInfo.setProcess(info.getLevel().getLevel());
            newInfo.setUid(uid);
            userVIPPreInfoService.addUserVIPPreInfo(newInfo);
            // 发送消息
 
            // 计算相隔天数
            // 默认值
            Date lastUpgradeTime = new Date(1577836800000L);
            if (preInfo != null)
                lastUpgradeTime = preInfo.getCreateTime();
            else {
                UserInfoRegister userInfoRegister = userInfoRegisterService.selectByPrimaryKey(uid);
                if (userInfoRegister != null && userInfoRegister.getCreateTime() != null) {
                    lastUpgradeTime = userInfoRegister.getCreateTime();
                }
            }
 
            int days = TimeUtil.getDayDifferenceCount(lastUpgradeTime, new Date());
 
            userAccountMsgNotificationService.artificialVipUpgradePass(uid,
                    preInfo == null ? UserLevelEnum.daRen.getName()
                            : UserLevelUtil.getByLevel(preInfo.getProcess()).getName(),
                    info.getLevel().getName(), days);
        }
 
        GiveVIPApplyInfo update = new GiveVIPApplyInfo();
        update.setId(info.getId());
        update.setState(GiveVIPApplyInfo.STATE_SUCCESS);
        update.setVerifyTime(new Date());
        update.setAdminUserId(adminId);
        giveVIPApplyInfoDao.updateSelective(update);
    }
 
    @Transactional
    @Override
    public void reject(String id, Long adminId, String reason) throws GiveVIPApplyInfoException {
        GiveVIPApplyInfo info = selectByPrimaryKey(id);
        if (info == null)
            throw new GiveVIPApplyInfoException(1, "记录不存在");
 
        if (info.getState() == GiveVIPApplyInfo.STATE_REJECT)
            throw new GiveVIPApplyInfoException(3, "已经被拒绝");
 
        if (info.getState() == GiveVIPApplyInfo.STATE_SUCCESS)
            throw new GiveVIPApplyInfoException(4, "已经被通过");
 
        GiveVIPApplyInfo update = new GiveVIPApplyInfo();
        update.setId(info.getId());
        update.setState(GiveVIPApplyInfo.STATE_REJECT);
        update.setVerifyTime(new Date());
        update.setRejectReson(reason);
        update.setAdminUserId(adminId);
        giveVIPApplyInfoDao.updateSelective(update);
 
        Long uid = info.getTargetUid();
        UserVIPPreInfo preInfo = userVIPPreInfoService.getLatestProcessInfo(uid);
 
        // 拒绝相关消息
        userAccountMsgNotificationService
                .artificialVipUpgradeReject(uid,
                        preInfo == null ? UserLevelEnum.daRen.getName()
                                : UserLevelUtil.getByLevel(preInfo.getProcess()).getName(),
                        info.getLevel().getName(), reason);
    }
 
    @Override
    public List<GiveVIPApplyInfo> listBySourceUid(Long uid, int page, int pageSize) {
        return giveVIPApplyInfoDao.listBySourceUid(uid, null, (page - 1) * pageSize, pageSize);
    }
 
    @Override
    public long countBySourceUid(Long uid) {
        return giveVIPApplyInfoDao.countBySourceUid(uid, null);
    }
 
}