yujian
2019-11-29 444da68b958e53c7fa122db62723fce25368ee1d
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.yeshi.fanli.service.impl.user.vip;
 
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
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.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.vip.UserVIPInfo;
import com.yeshi.fanli.entity.shop.BanLiShopOrder;
import com.yeshi.fanli.exception.user.vip.UserVIPInfoException;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.integral.IntegralDetailService;
import com.yeshi.fanli.service.inter.user.vip.UserVIPInfoService;
import com.yeshi.fanli.service.inter.user.vip.UserVipConfigService;
 
@Service
public class UserVIPInfoServiceImpl implements UserVIPInfoService {
 
    @Resource
    private UserVIPInfoMapper userVIPInfoMapper;
    
    @Resource
    private UserInfoExtraService userInfoExtraService;
    
    @Resource
    private UserVipConfigService userVipConfigService;
    
    @Resource
    private HongBaoV2CountService hongBaoV2CountService;
    
    @Resource
    private IntegralDetailService integralDetailService;
    
    @Resource
    private BanLiShopOrderService banLiShopOrderService;
    
 
    @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;
    }
 
    
    @Override
    public boolean verifyConform(Long uid) {
        if (uid == null || uid <= 0)
            return false;
        
        UserInfoExtra extra = userInfoExtraService.getUserInfoExtra(uid);
        if (extra == null) 
            return false;
        
        String limtDate = userVipConfigService.getValueByKey("vip_execute_time");
        Date executeDate = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
            executeDate = format.parse(limtDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (executeDate == null)
            return false;
        
        // 1、直接粉丝(从 2020 年 1 月 1 日起直接粉丝产生有效订单)
        BigDecimal payMoney = new BigDecimal(userVipConfigService.getValueByKey("require_order_pay"));
        long teamNum = hongBaoV2CountService.countValidOrderTeamUserByUid(uid, executeDate.getTime(), payMoney);
        if (extra.getFirstLoginTime() == null || extra.getFirstLoginTime().getTime() < executeDate.getTime()) {
            long limitNum = Long.parseLong(userVipConfigService.getValueByKey("require_invite_num_old_user"));
            if (teamNum < limitNum)
                return false;
        } else {
            long limitNum = Long.parseLong(userVipConfigService.getValueByKey("require_invite_num_new_user"));
            if (teamNum < limitNum)
                return false;
        }
        
        // 2、累计自购返利≥100 元(从注册板栗快省起);
        BigDecimal fanMoney = new BigDecimal(userVipConfigService.getValueByKey("require_fan_money"));
        BigDecimal purchase = hongBaoV2CountService.getRewardMoneyBySelf(uid);
        if (purchase.compareTo(fanMoney) < 0)
            return false;
            
        // 3、累计金币≥10000 枚(从注册板栗快省起);
        BigDecimal goldCoin = integralDetailService.getCumulativeMoney(uid);
        BigDecimal limitGoldCoin = new BigDecimal(userVipConfigService.getValueByKey("require_gold_coin"));
        if (goldCoin.compareTo(limitGoldCoin) < 0)
            return false;
            
        // 4、使用红包≥1 次
        List<Integer> list = new ArrayList<Integer>();
        list.add(BanLiShopOrder.STATE_SUCCESS);
        long useHongBao = banLiShopOrderService.countByUidAndState(uid, list);
        long limitHongBao = Long.parseLong(userVipConfigService.getValueByKey("require_shop_buy"));
        if (useHongBao < limitHongBao)
            return false;
            
        // 符合条件
        return true;
    }
}