admin
2022-04-29 6cc97918a5a42e37a3c3867cc5b78a0b9fd43a24
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
216
217
package com.yeshi.makemoney.app.service.impl.goldcorn;
 
import java.lang.Exception;
import javax.annotation.Resource;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.makemoney.app.entity.SystemEnum;
import com.yeshi.makemoney.app.entity.config.SystemConfigKey;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetType;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornPriceCountType;
import com.yeshi.makemoney.app.entity.user.UserInfo;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornGetPriceException;
import com.yeshi.makemoney.app.service.inter.config.SystemConfigService;
import com.yeshi.makemoney.app.service.inter.team.TeamInviteRelationService;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
 
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.*;
 
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.TimeUtil;
import org.yeshi.utils.bean.BeanUtil;
 
import com.yeshi.makemoney.app.dao.goldcorn.GoldCornGetPriceDao;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetPrice;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetPriceService;
import com.yeshi.makemoney.app.service.query.goldcorn.GoldCornGetPriceQuery;
import com.yeshi.makemoney.app.dao.goldcorn.GoldCornGetPriceDao.DaoQuery;
 
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
 
@Service
public class GoldCornGetPriceServiceImpl implements GoldCornGetPriceService {
 
    @Resource
    private GoldCornGetPriceDao goldCornGetPriceDao;
 
    @Resource
    private SystemConfigService systemConfigService;
 
    @Resource
    private TeamInviteRelationService teamInviteRelationService;
 
    @Override
    public List<GoldCornGetPrice> list(GoldCornGetPriceQuery goldCornGetPriceQuery, int page, int pageSize) {
        DaoQuery daoQuery = new DaoQuery();
        daoQuery.type = goldCornGetPriceQuery.getType();
        daoQuery.minValidateTime = goldCornGetPriceQuery.toStartTime();
        daoQuery.maxValidateTime = goldCornGetPriceQuery.toEndTime();
 
 
        daoQuery.start = (page - 1) * pageSize;
        daoQuery.count = pageSize;
        return goldCornGetPriceDao.list(daoQuery);
    }
 
    @Override
    public long count(GoldCornGetPriceQuery goldCornGetPriceQuery) {
        DaoQuery daoQuery = new DaoQuery();
        daoQuery.type = goldCornGetPriceQuery.getType();
        daoQuery.minValidateTime = goldCornGetPriceQuery.toStartTime();
        daoQuery.maxValidateTime = goldCornGetPriceQuery.toEndTime();
        return goldCornGetPriceDao.count(daoQuery);
    }
 
    @Override
    public GoldCornGetPrice get(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        return goldCornGetPriceDao.findOne(query);
    }
 
    @Override
    public void add(GoldCornGetPrice goldCornGetPrice) throws Exception {
 
 
        if (goldCornGetPrice.getCreateTime() == null) {
            goldCornGetPrice.setCreateTime(new Date());
        }
 
        if (goldCornGetPrice.getId() == null) {
            goldCornGetPrice.setId(goldCornGetPrice.toId());
        }
        //查询主键ID是否存在
        if (goldCornGetPriceDao.get(goldCornGetPrice.getId()) != null) {
            throw new Exception("已存在");
        }
 
        //保存
        goldCornGetPriceDao.save(goldCornGetPrice);
    }
 
    @Override
    public void update(GoldCornGetPrice goldCornGetPrice) {
        if (goldCornGetPrice.getUpdateTime() == null) {
            goldCornGetPrice.setUpdateTime(new Date());
        }
        //更新
        goldCornGetPriceDao.updateSelective(goldCornGetPrice);
    }
 
    @Override
    public void delete(List<String> idList) {
        for (String id : idList) {
            goldCornGetPriceDao.delete(id);
        }
    }
 
    @Override
    public GoldCornGetPrice getPrice(SystemEnum system, GoldCornGetType type, Date date) {
        DaoQuery daoQuery = new DaoQuery();
        daoQuery.maxValidateTime = date;
        daoQuery.system = system;
        daoQuery.type = type;
        daoQuery.sortList = Arrays.asList(new Sort.Order[]{Sort.Order.desc("validateTime")});
        daoQuery.count = 1;
 
        List<GoldCornGetPrice> list = goldCornGetPriceDao.list(daoQuery);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
 
    @Override
    public Integer getPriceCornNum(SystemEnum system, GoldCornGetType type, Date date, boolean hasBoss) {
        GoldCornGetPrice price = getPrice(system, type, date);
        if (price == null) {
            return null;
        }
        return new BigDecimal(price.getCornNum()).multiply(new BigDecimal(1).add(price.getTeamGainRate())).intValue();
    }
 
    @Override
    public Integer getSingInPrice(SystemEnum system, int continueDay) {
        List<Integer> list = getSingInPriceList(system);
        if (list == null) {
            return null;
        }
 
        if (continueDay >= list.size()) {
            return list.get(list.size() - 1);
        }
 
        if (continueDay <= 0) {
            return list.get(0);
        }
 
        return list.get(continueDay - 1);
    }
 
    @Override
    public List<Integer> getSingInPriceList(SystemEnum system) {
        String value = systemConfigService.getValueCache(system, SystemConfigKey.signInGoldCorn);
        if (StringUtil.isNullOrEmpty(value)) {
            return null;
        }
        Type type = new TypeToken<List<Integer>>() {
        }.getType();
        return new Gson().fromJson(value, type);
    }
 
    @Override
    public Integer getCountPrice(GoldCornGetType type, UserInfo user, SystemEnum system, Date date, long eventCount) throws GoldCornGetPriceException {
        GoldCornGetPrice getPrice = getPrice(system, type, date);
        if (getPrice == null) {
            throw new GoldCornGetPriceException(GoldCornGetPriceException.CODE_NOT_EXIST, "价格信息缺失");
        }
 
        int goldCorn;
        if (getPrice.getCountType() == GoldCornPriceCountType.time) {
            goldCorn = (int) (eventCount * getPrice.getCornNum() / 60.0f);
 
        } else {
            goldCorn = (int) (eventCount * getPrice.getCornNum());
        }
 
        //加上团队增益比例
        if (user != null && teamInviteRelationService.getBossUid(user.getId()) != null) {
            goldCorn = new BigDecimal(goldCorn).multiply(new BigDecimal(1).add(getPrice.getTeamGainRate())).intValue();
        }
        return goldCorn;
    }
 
    @Override
    public Map<GoldCornGetType, GoldCornGetPrice> getCountPrice(List<GoldCornGetType> typeList, UserInfo user, SystemEnum system, Date date) {
        Map<GoldCornGetType, GoldCornGetPrice> map = new HashMap();
        for (GoldCornGetType type : typeList) {
            GoldCornGetPrice getPrice = getPrice(system, type, date);
            if (getPrice == null) {
                continue;
            }
            int goldCorn = getPrice.getCornNum();
            //加上团队增益比例
            if (user != null && teamInviteRelationService.getBossUid(user.getId()) != null) {
                goldCorn = new BigDecimal(goldCorn).multiply(new BigDecimal(1).add(getPrice.getTeamGainRate())).intValue();
            }
            getPrice.setCornNum(goldCorn);
            map.put(type, getPrice);
        }
        return map;
    }
 
    @Override
    public GoldCornGetPrice getCountPrice(GoldCornGetType type, UserInfo user, SystemEnum system, Date date) {
 
        Map<GoldCornGetType, GoldCornGetPrice> map = getCountPrice(Arrays.asList(new GoldCornGetType[]{type}), user, system, date);
 
        return map.get(type);
    }
 
 
}