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
package com.yeshi.makemoney.app.service.impl.goldcorn;
 
import com.yeshi.makemoney.app.dao.goldcorn.GoldCornGetRecordDao;
import com.yeshi.makemoney.app.dao.goldcorn.GoldCornGetRecordDao.DaoQuery;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetRecord;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetType;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornGetRecordException;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetRecordService;
import com.yeshi.makemoney.app.service.query.goldcorn.GoldCornGetRecordQuery;
import com.yeshi.makemoney.app.utils.goldcorn.GoldCornUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.yeshi.utils.TimeUtil;
 
import javax.annotation.Resource;
import java.util.*;
 
@Service
public class GoldCornGetRecordServiceImpl implements GoldCornGetRecordService {
 
    @Resource
    private GoldCornGetRecordDao goldCornGetRecordDao;
 
    private DaoQuery createDaoQuery(GoldCornGetRecordQuery goldCornGetRecordQuery) {
        DaoQuery daoQuery = new DaoQuery();
        daoQuery.type = goldCornGetRecordQuery.getType();
        daoQuery.uid = goldCornGetRecordQuery.getUid();
        daoQuery.day = goldCornGetRecordQuery.getDay();
        daoQuery.doubles = goldCornGetRecordQuery.getDoubles();
        daoQuery.dayList = goldCornGetRecordQuery.getDayList();
        daoQuery.minCreateTime = goldCornGetRecordQuery.toStartTime();
        daoQuery.maxCreateTime = goldCornGetRecordQuery.toEndTime();
        return daoQuery;
    }
 
    @Override
    public List<GoldCornGetRecord> list(GoldCornGetRecordQuery goldCornGetRecordQuery, int page, int pageSize) {
        DaoQuery daoQuery = createDaoQuery(goldCornGetRecordQuery);
 
        daoQuery.start = (page - 1) * pageSize;
        daoQuery.count = pageSize;
        daoQuery.sortList = Arrays.asList(new Sort.Order[]{Sort.Order.desc("createTime")});
        return goldCornGetRecordDao.list(daoQuery);
    }
 
    @Override
    public long count(GoldCornGetRecordQuery goldCornGetRecordQuery) {
        DaoQuery daoQuery = createDaoQuery(goldCornGetRecordQuery);
        return goldCornGetRecordDao.count(daoQuery);
    }
 
    @Override
    public GoldCornGetRecord get(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        return goldCornGetRecordDao.findOne(query);
    }
 
    @Override
    public void add(GoldCornGetRecord goldCornGetRecord) throws GoldCornGetRecordException {
 
        if (goldCornGetRecord.getCreateTime() == null) {
            goldCornGetRecord.setCreateTime(new Date());
        }
 
        goldCornGetRecord.setId(goldCornGetRecord.toId());
 
        //查询主键ID是否存在
        if (goldCornGetRecordDao.get(goldCornGetRecord.getId()) != null) {
            throw new GoldCornGetRecordException(GoldCornGetRecordException.CODE_EXIST, "已存在");
        }
 
 
        //保存
        goldCornGetRecordDao.save(goldCornGetRecord);
    }
 
    @Override
    public void update(GoldCornGetRecord goldCornGetRecord) {
        if (goldCornGetRecord.getUpdateTime() == null) {
            goldCornGetRecord.setUpdateTime(new Date());
        }
        //更新
        goldCornGetRecordDao.updateSelective(goldCornGetRecord);
    }
 
    @Override
    public Long getGoldCornByDay(Long uid, Date day) {
        List<Criteria> andList = new ArrayList<>();
        if (uid != null) {
            andList.add(Criteria.where("uid").is(uid));
        }
 
        if (day != null) {
            andList.add(Criteria.where("day").is(GoldCornUtil.getFormatDay(day)));
        }
 
        Criteria[] ands = new Criteria[andList.size()];
        andList.toArray(ands);
        List<AggregationOperation> list = new ArrayList<>();
        if (ands.length > 0) {
            list.add(Aggregation.match(new Criteria().andOperator(ands)));
        }
        list.add(Aggregation.group().sum("cornNum").as("goldcorn"));
        AggregationResults<Map> results = goldCornGetRecordDao.aggregate(list, Map.class);
        return results.getUniqueMappedResult() == null ? 0L : Long.parseLong(results.getUniqueMappedResult().get("goldcorn") + "");
    }
 
    @Override
    public List<Long> getUidsByDay(String day, int page, int pageSize) {
        List<Long> uidList = new ArrayList<>();
        List<AggregationOperation> list = new ArrayList<>();
        list.add(Aggregation.match(Criteria.where("day").is(day)));
        list.add(Aggregation.group("uid"));
        list.add(Aggregation.project("uid"));
        list.add(Aggregation.skip((long) ((page - 1) * pageSize)));
        list.add(Aggregation.limit((long) pageSize));
        AggregationResults<Map> results = goldCornGetRecordDao.aggregate(list, Map.class);
        List<Map> resultList = results.getMappedResults();
        if (resultList != null) {
            for (Map map : resultList) {
                uidList.add(Long.parseLong(map.get("_id") + ""));
            }
        }
        return uidList;
    }
 
    @Override
    public long countUidsByDay(String day) {
        List<AggregationOperation> list = new ArrayList<>();
        list.add(Aggregation.match(Criteria.where("day").is(day)));
        list.add(Aggregation.group("uid"));
        list.add(Aggregation.group().count().as("count"));
        AggregationResults<Map> results = goldCornGetRecordDao.aggregate(list, Map.class);
        return results.getUniqueMappedResult() == null ? 0L : Long.parseLong(results.getUniqueMappedResult().get("count") + "");
    }
 
    @Override
    public Map<Long, Integer> sumGoldCornByFromUids(Long uid, List<Long> fromUidList, String day) {
        if (fromUidList == null || fromUidList.size() == 0) {
            return new HashMap<>();
        }
        Criteria[] ands = new Criteria[3];
        ands[0] = Criteria.where("day").is(day);
        ands[1] = Criteria.where("uid").is(uid);
 
        Criteria[] ors = new Criteria[fromUidList.size()];
        for (int i = 0; i < ors.length; i++) {
            ors[i] = Criteria.where("fromUid").is(fromUidList.get(i));
        }
        ands[2] = new Criteria().orOperator(ors);
 
        List<AggregationOperation> list = new ArrayList<>();
        list.add(Aggregation.match(new Criteria().andOperator(ands)));
        list.add(Aggregation.group("fromUid").sum("cornNum").as("cornNum"));
        AggregationResults<Map> results = goldCornGetRecordDao.aggregate(list, Map.class);
        List<Map> mapList = results.getMappedResults();
        Map<Long, Integer> map = new HashMap<>();
        if (mapList != null) {
            for (Map m : mapList) {
                map.put((long) m.get("_id"), (int) m.get("cornNum"));
            }
        }
        return map;
    }
 
    @Override
    public int getContinueDay(Long uid, GoldCornGetType type, Date deadDate) {
        List<AggregationOperation> list = new ArrayList<>();
        list.add(Aggregation.match(Criteria.where("uid").is(uid).and("type").is(type).and("createTime").lte(deadDate)));
        list.add(Aggregation.group("day").max("createTime").as("createTime"));
        list.add(Aggregation.sort(Sort.Direction.DESC, "createTime"));
        //最大限制为365天
        list.add(Aggregation.limit(365));
        list.add(Aggregation.project("day"));
        AggregationResults<Map<String, String>> results = goldCornGetRecordDao.aggregate(list, Map.class);
        List<Map<String, String>> resultList = results.getMappedResults();
        //获取连续的天数
        String format = "yyyy-MM-dd";
        Date lastDay = new Date(TimeUtil.convertToTimeTemp(TimeUtil.getGernalTime(deadDate.getTime(), format), format));
        int continueDay = 0;
        for (Map<String, String> day : resultList) {
            Long timestamp = TimeUtil.convertToTimeTemp(day.get("_id"), format);
            long cha = lastDay.getTime() - timestamp;
            if (cha <= 1000 * 60 * 60 * 24L) {
                continueDay++;
            } else {
                break;
            }
            lastDay = new Date(timestamp);
        }
        return continueDay;
    }
 
    @Override
    public Map<GoldCornGetType, Long> sumEventCount(GoldCornGetRecordQuery goldCornGetRecordQuery) {
        return goldCornGetRecordDao.sumEventCount(createDaoQuery(goldCornGetRecordQuery));
    }
 
 
}