From e1cc0d03fadc2d251d36c0dc3650f75e830d5363 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 07 四月 2022 19:12:42 +0800
Subject: [PATCH] 功能完善

---
 app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornGetRecordServiceImpl.java |  110 ++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 72 insertions(+), 38 deletions(-)

diff --git a/app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornGetRecordServiceImpl.java b/app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornGetRecordServiceImpl.java
index b6657a9..cfbba50 100644
--- a/app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornGetRecordServiceImpl.java
+++ b/app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornGetRecordServiceImpl.java
@@ -1,26 +1,22 @@
 package com.yeshi.makemoney.app.service.impl.goldcorn;
 
-import java.lang.Exception;
-import javax.annotation.Resource;
-
-import org.springframework.stereotype.Service;
-
-import java.util.Date;
-
-import org.yeshi.utils.StringUtil;
-import org.yeshi.utils.TimeUtil;
-import org.yeshi.utils.bean.BeanUtil;
-
-import java.util.List;
-
 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.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.dao.goldcorn.GoldCornGetRecordDao.DaoQuery;
-
+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 javax.annotation.Resource;
+import java.util.*;
 
 @Service
 public class GoldCornGetRecordServiceImpl implements GoldCornGetRecordService {
@@ -33,17 +29,12 @@
         DaoQuery daoQuery = new DaoQuery();
         daoQuery.type = goldCornGetRecordQuery.getType();
         daoQuery.uid = goldCornGetRecordQuery.getUid();
-        if (!StringUtil.isNullOrEmpty(goldCornGetRecordQuery.getStartTime())) {
-            daoQuery.minCreateTime = new Date(TimeUtil.convertToTimeTemp(goldCornGetRecordQuery.getStartTime(), "yyyy-MM-dd"));
-        }
-
-        if (!StringUtil.isNullOrEmpty(goldCornGetRecordQuery.getEndTime())) {
-            daoQuery.maxCreateTime = new Date(TimeUtil.convertToTimeTemp(goldCornGetRecordQuery.getEndTime(), "yyyy-MM-dd"));
-        }
-
+        daoQuery.minCreateTime = goldCornGetRecordQuery.toStartTime();
+        daoQuery.maxCreateTime = goldCornGetRecordQuery.toEndTime();
 
         daoQuery.start = (page - 1) * pageSize;
         daoQuery.count = pageSize;
+        daoQuery.sortList = Arrays.asList(new Sort.Order[]{Sort.Order.desc("createTime")});
         return goldCornGetRecordDao.list(daoQuery);
     }
 
@@ -52,13 +43,8 @@
         DaoQuery daoQuery = new DaoQuery();
         daoQuery.type = goldCornGetRecordQuery.getType();
         daoQuery.uid = goldCornGetRecordQuery.getUid();
-        if (!StringUtil.isNullOrEmpty(goldCornGetRecordQuery.getStartTime())) {
-            daoQuery.minCreateTime = new Date(TimeUtil.convertToTimeTemp(goldCornGetRecordQuery.getStartTime(), "yyyy-MM-dd"));
-        }
-
-        if (!StringUtil.isNullOrEmpty(goldCornGetRecordQuery.getEndTime())) {
-            daoQuery.maxCreateTime = new Date(TimeUtil.convertToTimeTemp(goldCornGetRecordQuery.getEndTime(), "yyyy-MM-dd"));
-        }
+        daoQuery.minCreateTime = goldCornGetRecordQuery.toStartTime();
+        daoQuery.maxCreateTime = goldCornGetRecordQuery.toEndTime();
         return goldCornGetRecordDao.count(daoQuery);
     }
 
@@ -70,15 +56,20 @@
     }
 
     @Override
-    public void add(GoldCornGetRecord goldCornGetRecord) throws Exception {
-        //鏌ヨ涓婚敭ID鏄惁瀛樺湪
-        if (goldCornGetRecordDao.get(goldCornGetRecord.getId()) != null) {
-            throw new Exception("宸插瓨鍦�");
-        }
+    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);
     }
@@ -93,10 +84,53 @@
     }
 
     @Override
-    public void delete(List<String> idList) {
-        for (String id : idList) {
-            goldCornGetRecordDao.delete(id);
+    public Long getGoldCornByDay(Long uid, String 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(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") + "");
     }
 
 

--
Gitblit v1.8.0