admin
2022-10-28 0e9b6603d4ae9d11c1fbc90257ce816c5807b8ff
app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornGetRecordServiceImpl.java
@@ -2,6 +2,7 @@
import com.yeshi.makemoney.app.dao.goldcorn.GoldCornGetRecordDao;
import com.yeshi.makemoney.app.dao.goldcorn.GoldCornGetRecordDao.DaoQuery;
import com.yeshi.makemoney.app.entity.SystemEnum;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetRecord;
import com.yeshi.makemoney.app.entity.goldcorn.GoldCornGetType;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornGetRecordException;
@@ -17,9 +18,12 @@
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.yeshi.utils.TimeUtil;
import org.yeshi.utils.statistic.BaseStatisticTimeQuery;
import org.yeshi.utils.statistic.StatisticNumberResult;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class GoldCornGetRecordServiceImpl implements GoldCornGetRecordService {
@@ -203,5 +207,85 @@
        return goldCornGetRecordDao.sumEventCount(createDaoQuery(goldCornGetRecordQuery));
    }
    @Override
    public Map<String, Long> sumGoldCornNum(GoldCornGetRecordQuery goldCornGetRecordQuery) {
        List<AggregationOperation> list = new ArrayList<>();
        list.add(Aggregation.match(goldCornGetRecordDao.getCriteria(createDaoQuery(goldCornGetRecordQuery))));
        list.add(Aggregation.group("day").sum("cornNum").as("cornNum"));
        AggregationResults<Map<String, Object>> results = goldCornGetRecordDao.aggregate(list, Map.class);
        List<Map<String, Object>> resultList = results.getMappedResults();
        Map<String, Long> resultMap = new HashMap<>();
        for (Map<String, Object> result : resultList) {
            String day = result.get("_id") + "";
            Long count = Long.parseLong(result.get("cornNum") + "");
            resultMap.put(day, count);
        }
        return resultMap;
    }
    @Override
    public List<Long> listUids(Date minCreateTime, Date maxCreateTime, int page, int pageSize) {
        List<AggregationOperation> list = new ArrayList<>();
        list.add(Aggregation.match(Criteria.where("createTime").gte(minCreateTime).lt(maxCreateTime)));
        list.add(Aggregation.group("uid"));
        list.add(Aggregation.sort(Sort.Direction.DESC, "_id"));
        list.add(Aggregation.skip(Long.parseLong((page - 1) * pageSize + "")));
        list.add(Aggregation.limit(pageSize));
        AggregationResults<Map<String, Object>> results = goldCornGetRecordDao.aggregate(list, Map.class);
        List<Map<String, Object>> resultList = results.getMappedResults();
        List<Long> uidList = new ArrayList<>();
        for (Map<String, Object> result : resultList) {
            Long uid = Long.parseLong(result.get("_id") + "");
            uidList.add(uid);
        }
        return uidList;
    }
    @Override
    public Map<Long, Boolean> isSignIned(List<Long> uidList, String day) {
        DaoQuery daoQuery = new DaoQuery();
        daoQuery.type = GoldCornGetType.signIn;
        daoQuery.day = day;
        daoQuery.uidList = uidList;
        List<AggregationOperation> list = new ArrayList<>();
        list.add(Aggregation.match(goldCornGetRecordDao.getCriteria(daoQuery)));
        list.add(Aggregation.group("uid"));
        AggregationResults<Map<String, Object>> results = goldCornGetRecordDao.aggregate(list, Map.class);
        List<Map<String, Object>> resultList = results.getMappedResults();
        Set<Long> signInedList = new HashSet<>();
        for (Map<String, Object> result : resultList) {
            Long uid = Long.parseLong(result.get("_id") + "");
            signInedList.add(uid);
        }
        return uidList.stream().collect(Collectors.toMap(uid -> uid, uid -> signInedList.contains(uid)));
    }
    @Override
    public List<StatisticNumberResult> statistic(SystemEnum system, BaseStatisticTimeQuery query) {
        GoldCornGetRecordQuery q = new GoldCornGetRecordQuery();
        q.setStartTime(TimeUtil.getGernalTime(query.getStartTime().getTime(), "yyyy.MM.dd HH:mm:ss"));
        q.setEndTime(TimeUtil.getGernalTime(query.getEndTime().getTime(), "yyyy.MM.dd HH:mm:ss"));
        q.setFormat("yyyy.MM.dd HH:mm:ss");
        Map<String, Long> map = sumGoldCornNum(q);
        List<StatisticNumberResult> resultList = new ArrayList<>();
        for (String key : map.keySet()) {
            StatisticNumberResult numberResult = new StatisticNumberResult();
            numberResult.setTime(key);
            numberResult.setNumber(map.get(key));
            resultList.add(numberResult);
        }
        Comparator<StatisticNumberResult> cm = (o1, o2) ->
                GoldCornUtil.convertFormatDay(o1.getTime()).getTime() - GoldCornUtil.convertFormatDay(o2.getTime()).getTime() > 0 ? 1 : -1;
        Collections.sort(resultList, cm);
        return resultList;
    }
}