admin
2022-04-07 e1cc0d03fadc2d251d36c0dc3650f75e830d5363
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
package com.yeshi.makemoney.app.service.impl.goldcorn;
 
import com.yeshi.makemoney.app.entity.config.SystemConfigKey;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetFrequencyConfig;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetPrice;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetRecord;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetType;
import com.yeshi.makemoney.app.entity.user.UserInfo;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornGetFrequencyConfigException;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornGetPriceException;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornGetRecordException;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornMakeException;
import com.yeshi.makemoney.app.exception.user.UserInfoException;
import com.yeshi.makemoney.app.service.inter.config.SystemConfigService;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetFrequencyConfigService;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetPriceService;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetRecordService;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornMakeService;
import com.yeshi.makemoney.app.service.inter.team.TeamInviteRelationService;
import com.yeshi.makemoney.app.service.inter.user.UserInfoService;
import com.yeshi.makemoney.app.service.query.goldcorn.GoldCornGetRecordQuery;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.yeshi.utils.TimeUtil;
import org.yeshi.utils.annotation.RequestSerializableByKey;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
/**
 * @author hxh
 * @title: GoldCornMakeServiceImpl
 * @description: 挣金币服务
 * @date 2022/4/6 16:56
 */
@Service
public class GoldCornMakeServiceImpl implements GoldCornMakeService {
 
    @Resource
    private GoldCornGetFrequencyConfigService goldCornGetFrequencyConfigService;
 
    @Resource
    private GoldCornGetRecordService goldCornGetRecordService;
 
    @Resource
    private RedisTemplate<String, String> redisTemplate;
 
    @Resource
    private GoldCornGetPriceService goldCornGetPriceService;
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private TeamInviteRelationService teamInviteRelationService;
 
    @Resource
    private SystemConfigService systemConfigService;
 
 
    private void frequencyVerify(UserInfo user, GoldCornGetType type, Date time) throws GoldCornGetFrequencyConfigException, GoldCornMakeException {
        GoldCornGetFrequencyConfig config = goldCornGetFrequencyConfigService.getConfig(user.getSystem(), type, time);
        if (config == null) {
            throw new GoldCornGetFrequencyConfigException(GoldCornGetFrequencyConfigException.CODE_NOT_EXIST, "频率配置不存在");
        }
 
 
        GoldCornGetRecordQuery query = new GoldCornGetRecordQuery();
        query.setUid(user.getId());
        query.setType(type);
 
        //间隔判断
        if (config.getMinSpaceTime() != null) {
            List<GoldCornGetRecord> list = goldCornGetRecordService.list(query, 1, 1);
            if (list != null && list.size() > 0 && System.currentTimeMillis() - list.get(0).getCreateTime().getTime() < config.getMinSpaceTime() * 1000L) {
                String msg = String.format("需要间隔%s秒以上", config.getMinSpaceTime());
                throw new GoldCornMakeException(GoldCornMakeException.CODE_GET_FREQUENCY_LIMIT, msg);
            }
        }
 
        Date startTime = null;
        Date endTime = time;
        if (config.getTimeUnit() == GoldCornGetFrequencyConfig.GoldCornGetFrequencyTimeUnit.minute) {
            startTime = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(endTime.getTime(), "yyyyMMdd HHmm"), "yyyyMMdd HHmm"));
        } else if (config.getTimeUnit() == GoldCornGetFrequencyConfig.GoldCornGetFrequencyTimeUnit.hour) {
            startTime = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(endTime.getTime(), "yyyyMMdd HH"), "yyyyMMdd HH"));
        } else if (config.getTimeUnit() == GoldCornGetFrequencyConfig.GoldCornGetFrequencyTimeUnit.day) {
            startTime = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(endTime.getTime(), "yyyyMMdd"), "yyyyMMdd"));
        } else if (config.getTimeUnit() == GoldCornGetFrequencyConfig.GoldCornGetFrequencyTimeUnit.week) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(endTime.getTime());
            int subDay = (Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 2 + 7) % 7;
            startTime = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(endTime.getTime() - 1000 * 60 * 60 * 24L * subDay, "yyyyMMdd"), "yyyyMMdd"));
        } else if (config.getTimeUnit() == GoldCornGetFrequencyConfig.GoldCornGetFrequencyTimeUnit.month) {
            startTime = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(endTime.getTime(), "yyyyMM"), "yyyyMM"));
        } else if (config.getTimeUnit() == GoldCornGetFrequencyConfig.GoldCornGetFrequencyTimeUnit.year) {
            startTime = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(endTime.getTime(), "yyyy"), "yyyy"));
        }
        query.setFormat("yyyyMMdd HH:mm:ss");
        query.setStartTime(TimeUtil.getGernalTime(startTime.getTime(), query.getFormat()));
        //service里面有减去1天
        query.setEndTime(TimeUtil.getGernalTime(endTime.getTime() - 1000 * 60 * 60 * 24L, query.getFormat()));
 
        long count = goldCornGetRecordService.count(query);
        if (count >= config.getLimitCount()) {
            String msg = String.format("超出%s次/%s的限制", config.getLimitCount(), config.getTimeUnit().getName());
            throw new GoldCornMakeException(GoldCornMakeException.CODE_GET_FREQUENCY_LIMIT, msg);
        }
 
    }
 
 
    @RequestSerializableByKey(key = "'addgoldcorn-'+#uid+'-'+#type")
    @Override
    public void addGoldCorn(Long uid, GoldCornGetType type, Date currentTime) throws UserInfoException, GoldCornGetPriceException, GoldCornGetFrequencyConfigException, GoldCornMakeException {
        if (uid == null || type == null || currentTime == null) {
            throw new GoldCornMakeException(GoldCornMakeException.CODE_PARAMS_NOT_ENOUGH, "参数不完整");
        }
        UserInfo user = userInfoService.get(uid);
        if (user == null) {
            throw new UserInfoException(UserInfoException.CODE_NOT_EXIST, "用户不存在");
        }
        //判断是否超出了次数限制
        frequencyVerify(user, type, currentTime);
        //获取价格
        GoldCornGetPrice price = goldCornGetPriceService.getPrice(user.getSystem(), type, currentTime);
        if (price == null) {
            throw new GoldCornGetPriceException(GoldCornGetPriceException.CODE_NOT_EXIST, "价格信息缺失");
        }
        GoldCornGetRecord record = new GoldCornGetRecord();
        record.setCornNum(price.getCornNum());
        record.setDay(TimeUtil.getGernalTime(currentTime.getTime(), "yyyy-MM-dd"));
        record.setType(type);
        record.setUid(uid);
        record.setLevel(GoldCornGetRecord.LEVEL_OWN);
        record.setCreateTime(currentTime);
 
        try {
            goldCornGetRecordService.add(record);
            //是否有上级
            Long bossUid = teamInviteRelationService.getBossUid(uid);
            if (bossUid != null) {
                BigDecimal rate = new BigDecimal(systemConfigService.getValueCache(user.getSystem(), SystemConfigKey.teamFirstDivideRate));
                GoldCornGetRecord cRecord = new GoldCornGetRecord();
                cRecord.setCornNum(price.getCornNum());
                cRecord.setDay(record.getDay());
                cRecord.setType(type);
                cRecord.setUid(bossUid);
                cRecord.setCornNum(new BigDecimal(record.getCornNum()).multiply(rate).intValue());
                cRecord.setFromId(record.getId());
                cRecord.setFromUid(record.getUid());
                cRecord.setLevel(GoldCornGetRecord.LEVEL_FIRST);
                cRecord.setCreateTime(currentTime);
                if (cRecord.getCornNum() > 0) {
                    goldCornGetRecordService.add(cRecord);
                }
                //是否有上上级
                bossUid = teamInviteRelationService.getBossUid(bossUid);
                if (bossUid != null) {
                    rate = new BigDecimal(systemConfigService.getValueCache(user.getSystem(), SystemConfigKey.teamSecondDivideRate));
                    GoldCornGetRecord ccRecord = new GoldCornGetRecord();
                    ccRecord.setCornNum(price.getCornNum());
                    ccRecord.setDay(record.getDay());
                    ccRecord.setType(type);
                    ccRecord.setUid(bossUid);
                    ccRecord.setFromId(record.getId());
                    ccRecord.setFromUid(record.getUid());
                    ccRecord.setCornNum(new BigDecimal(record.getCornNum()).multiply(rate).intValue());
                    ccRecord.setLevel(GoldCornGetRecord.LEVEL_SECOND);
                    ccRecord.setCreateTime(currentTime);
                    if (ccRecord.getCornNum() > 0) {
                        goldCornGetRecordService.add(ccRecord);
                    }
                }
            }
        } catch (GoldCornGetRecordException e) {
            throw new GoldCornMakeException(GoldCornMakeException.CODE_ADD_RECORD_ERROR, e.getMsg());
        }
    }
 
    @Override
    public Long getGoldCornByDay(Long uid, String day) {
        //TODO redis缓存
        return goldCornGetRecordService.getGoldCornByDay(uid, day);
    }
}