yujian
2019-08-21 b0d445fda153e5dac63ad66c8b74191601f0a4ef
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
package com.yeshi.fanli.service.impl.integral;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.integral.IntegralExchangeRecordMapper;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.integral.IntegralExchange;
import com.yeshi.fanli.entity.integral.IntegralExchange.ExchangeTypeEnum;
import com.yeshi.fanli.entity.integral.IntegralExchangeRecord;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.integral.CodePublishRecordService;
import com.yeshi.fanli.service.inter.integral.IntegralExchangeRecordService;
import com.yeshi.fanli.service.inter.integral.IntegralExchangeService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class IntegralExchangeRecordServiceImpl implements IntegralExchangeRecordService {
 
    @Resource
    private IntegralExchangeRecordMapper integralExchangeRecordMapper;
 
    @Resource
    private IntegralExchangeService integralExchangeService;
    
    @Resource
    private UserInfoExtraService userInfoExtraService;
    
    @Resource
    private CodePublishRecordService codePublishRecordService;
    
    
    
    @Override
    public List<IntegralExchange> listExchange(long start, int count, Long uid){
        List<IntegralExchange> listValid = integralExchangeService.listValidCache(start, count);
        if (listValid == null || listValid.size() == 0) {
            return listValid;
        }
        
        boolean hasCode = false;
        try {
            UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
            if (userInfoExtra != null && !StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
                hasCode = true;
            }
        } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
        }
        
        for(int i = 0; i < listValid.size(); i++) {
            IntegralExchange exchange = listValid.get(i);
            
            ExchangeTypeEnum type = exchange.getType();
            if (type == null) {
                listValid.remove(i);
                i --;
                continue;
            }
            
            Long id = exchange.getId();
            if (type == ExchangeTypeEnum.freeCouponBuy) {
                // 注:因自购免单券,一个用户只能兑换一次,则当前用户一旦兑换成功,自购免单券兑换入口永久在当前用户金币兑换列表中消失。
                long num = integralExchangeRecordMapper.countRecordByUid(id, uid, null);
                if (num >= 1) {
                    listValid.remove(i);
                    i --;
                    continue;
                }
            }
            
            // 邀请码激活卡,兑换项,如果当前用户已经激活了邀请功能,则不需要显示该兑换项。
            if (type == ExchangeTypeEnum.inviteCodeActivate && hasCode) {
                listValid.remove(i);
                i --;
                continue;
            }
            
            // 邀请码发布卡,兑换项,如果当前用户未激活邀请功能,则不需要显示该兑换项。
            if (type == ExchangeTypeEnum.inviteCodePublish) {
                if (!hasCode) {
                    listValid.remove(i);
                    i --;
                    continue;
                }
                
                if(codePublishRecordService.countValidRecord(uid) > 0) {
                    exchange.setBtnName("去查看");
                    exchange.setNeedJump(true);
                }
            }
            
            String progress = exchange.getProgress();
            if (StringUtil.isNullOrEmpty(progress))
                continue;
            
            // 今日兑换情况
            long num = integralExchangeRecordMapper.countRecordByUid(id, uid, null);
            Integer upperLimit = exchange.getUpperLimit();
            if (upperLimit == null) {
                progress = progress.replace("{已兑换}", num + "").replace("/{上限数}", "");
            } else {
                progress = progress.replace("{已兑换}", num + "").replace("{上限数}", exchange.getUpperLimit() + "");
            }
            exchange.setProgress(progress);
        }
        return listValid;
    }
    
    @Override
    public long countRecordByUid(long exchangeid, Long uid, Integer today){
        return integralExchangeRecordMapper.countRecordByUid(exchangeid, uid, today);
    }
    
    @Override
    public void addExchangeRecord(long exchangeid, Long uid){
        IntegralExchangeRecord record = new IntegralExchangeRecord();
        record.setExchangeId(exchangeid);
        record.setUid(uid);
        record.setCreateTime(new Date());
        integralExchangeRecordMapper.insertSelective(record);
    }
}