| | |
| | | import java.util.Locale;
|
| | | import java.util.Map;
|
| | |
|
| | | import org.springframework.data.domain.Sort;
|
| | | 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.aggregation.LookupOperation;
|
| | | import org.springframework.data.mongodb.core.query.Criteria;
|
| | | import org.springframework.data.mongodb.core.query.Query;
|
| | | import org.springframework.data.mongodb.core.query.Update;
|
| | | import org.springframework.stereotype.Repository;
|
| | | import org.yeshi.utils.DateUtil;
|
| | |
|
| | | import com.mongodb.BasicDBList;
|
| | | import com.mongodb.BasicDBObject;
|
| | |
| | | import com.mongodb.GroupCommand;
|
| | | import com.yeshi.fanli.dao.MongodbBaseDao;
|
| | | import com.yeshi.fanli.entity.bus.user.invite.TeamDailyRecord;
|
| | | import com.yeshi.fanli.vo.order.OrderRankingVO;
|
| | |
|
| | | @Repository
|
| | | public class TeamDailyRecordDao extends MongodbBaseDao<TeamDailyRecord> {
|
| | |
| | | }
|
| | | return results;
|
| | | }
|
| | | |
| | | |
| | | |
| | |
|
| | | /**
|
| | | * 根据订单排行榜进行uid分组订单数量排序
|
| | | * @param uid
|
| | | * @param minDate
|
| | | * @param maxDate
|
| | | * @return
|
| | | */
|
| | | public List<OrderRankingVO> getRankingByFirstNum(Long uid, Date minDate, Date maxDate) {
|
| | | LookupOperation lookupToLots = LookupOperation.newLookup().
|
| | | from("teamDailyRecord").//关联表名,多方
|
| | | localField("workerUid").// 主表关联字段
|
| | | foreignField("uid").// 次表字段关联字段
|
| | | as("result"); // 次表的别名
|
| | |
|
| | | // 查询条件
|
| | | List<Criteria> list = new ArrayList<Criteria>();
|
| | | list.add(Criteria.where("bossUid").is(uid));
|
| | | if (minDate != null) {
|
| | | minDate = DateUtil.reduceDay(minDate, 1);
|
| | | list.add(Criteria.where("orders.countDay").gt(minDate));
|
| | | }
|
| | | if (maxDate != null) {
|
| | | maxDate = DateUtil.reduceDay(maxDate, 1);
|
| | | list.add(Criteria.where("orders.countDay").lte(maxDate));
|
| | | }
|
| | | |
| | | // 转换数组
|
| | | Criteria[] cas = new Criteria[list.size()];
|
| | | for (int i = 0; i < list.size(); i++)
|
| | | cas[i] = list.get(i);
|
| | | // 查询参数
|
| | | Criteria criteria = new Criteria().andOperator(cas);
|
| | | |
| | | |
| | | List<AggregationOperation> aggs = new ArrayList<>(); |
| | | // 左连接
|
| | | aggs.add(lookupToLots); |
| | | // 拆分子数组
|
| | | aggs.add(Aggregation.unwind("result"));
|
| | | // 条件匹配
|
| | | aggs.add(Aggregation.match(criteria)); |
| | | // 需要返回的字段
|
| | | aggs.add(Aggregation.project().and("workerUid").as("workerUid").and("level").as("level")
|
| | | .and("result.firstNum").as("number"));
|
| | | // 分组求和并返回
|
| | | aggs.add(Aggregation.group("workerUid").first("workerUid").as("workerUid").first("level").as("level")
|
| | | .sum("number").as("number")); |
| | | // 排序
|
| | | aggs.add(Aggregation.sort(new Sort(Sort.Direction.DESC, "number"))); |
| | | aggs.add(Aggregation.limit(10)); |
| | | |
| | | // 组织条件
|
| | | Aggregation agg = Aggregation.newAggregation(aggs); |
| | | // 执行查询
|
| | | AggregationResults<BasicDBObject> results = mongoTemplate.aggregate(agg, "threeSaleDetail", BasicDBObject.class);
|
| | | |
| | | // 返回结果处理
|
| | | List<BasicDBObject> livevideo = results.getMappedResults();
|
| | | |
| | | List<OrderRankingVO> resultList = new ArrayList<>();
|
| | | for (BasicDBObject obj : livevideo) {
|
| | | Object object = obj.get("workerUid");
|
| | | if (object == null) {
|
| | | continue;
|
| | | }
|
| | | |
| | | OrderRankingVO vo = new OrderRankingVO();
|
| | | vo.setUid(new BigDecimal(obj.get("workerUid").toString()).longValue());
|
| | | vo.setGrade(new BigDecimal(obj.get("level").toString()).intValue());
|
| | | vo.setNumber(new BigDecimal(obj.get("number").toString()).intValue());
|
| | | resultList.add(vo); |
| | | }
|
| | | return resultList;
|
| | | }
|
| | | }
|