| | |
| | | package com.yeshi.fanli.dao.user.invite;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | | import java.text.SimpleDateFormat;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Date;
|
| | | import java.util.HashMap;
|
| | | import java.util.List;
|
| | | import java.util.Locale;
|
| | | import java.util.Map;
|
| | |
|
| | | 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 com.mongodb.BasicDBList;
|
| | | import com.mongodb.BasicDBObject;
|
| | | import com.mongodb.DBCollection;
|
| | | import com.mongodb.DBObject;
|
| | | import com.mongodb.GroupCommand;
|
| | | import com.yeshi.fanli.dao.MongodbBaseDao;
|
| | | import com.yeshi.fanli.entity.bus.user.invite.TeamDailyRecord;
|
| | |
|
| | | @Repository
|
| | | public class TeamDailyRecordDao extends MongodbBaseDao<TeamDailyRecord> {
|
| | |
|
| | | private static String collectionName = "teamDailyRecord";
|
| | |
|
| | | |
| | | /**
|
| | | * 选择性更新
|
| | | * @param record
|
| | | */
|
| | | public void updateSelective(TeamDailyRecord record) {
|
| | | Query query = new Query();
|
| | | query.addCriteria(Criteria.where("id").is(record.getId()));
|
| | |
|
| | | Update update = new Update();
|
| | | if (record.getFirstNum() != null)
|
| | | update.set("firstNum", record.getFirstNum());
|
| | | if (record.getSecondNum() != null)
|
| | | update.set("secondNum", record.getSecondNum());
|
| | | if (record.getBeyondNum() != null)
|
| | | update.set("beyondNum", record.getBeyondNum());
|
| | | if (record.getFirstDaRen() != null)
|
| | | update.set("firstDaRen", record.getFirstDaRen());
|
| | | if (record.getSecondDaRen() != null)
|
| | | update.set("secondDaRen", record.getSecondDaRen());
|
| | | if (record.getBeyondDaRen() != null)
|
| | | update.set("beyondDaRen", record.getBeyondDaRen());
|
| | | if (record.getFirstHighVIP() != null)
|
| | | update.set("firstHighVIP", record.getFirstHighVIP());
|
| | | if (record.getSecondHighVIP() != null)
|
| | | update.set("secondHighVIP", record.getSecondHighVIP());
|
| | | if (record.getBeyondHighVIP() != null)
|
| | | update.set("beyondHighVIP", record.getBeyondHighVIP());
|
| | | if (record.getFirstSuperVIP() != null)
|
| | | update.set("firstSuperVIP", record.getFirstSuperVIP());
|
| | | if (record.getSecondSuperVIP() != null)
|
| | | update.set("secondSuperVIP", record.getSecondSuperVIP());
|
| | | if (record.getBeyondSuperVIP() != null)
|
| | | update.set("beyondSuperVIP", record.getBeyondSuperVIP());
|
| | | update.set("updateTime", new Date());
|
| | | update(query, update);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 根据id查询数据
|
| | | * |
| | | * @param id
|
| | | * @return
|
| | | */
|
| | | public TeamDailyRecord getById(String id) {
|
| | | Query query = new Query();
|
| | | query.addCriteria(Criteria.where("id").is(id));
|
| | | return mongoTemplate.findOne(query, TeamDailyRecord.class);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 查询
|
| | | * |
| | | * @return
|
| | | */
|
| | | public List<TeamDailyRecord> getByUid(Long uid) {
|
| | | Query query = new Query();
|
| | | query.addCriteria(Criteria.where("uid").is(uid));
|
| | | return findList(query);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 查询
|
| | | * |
| | | * @return
|
| | | */
|
| | | public List<TeamDailyRecord> listByUid(Long uid, Date minDate, Date maxDate) {
|
| | | List<Criteria> list = new ArrayList<Criteria>();
|
| | | list.add(Criteria.where("uid").is(uid));
|
| | | if (minDate != null)
|
| | | list.add(Criteria.where("countDay").gte(minDate));
|
| | | if (maxDate != null)
|
| | | list.add(Criteria.where("countDay").lte(maxDate));
|
| | | |
| | | Query query = new Query();
|
| | | if (list.size() > 0) {
|
| | | Criteria[] cas = new Criteria[list.size()];
|
| | | for (int i = 0; i < list.size(); i++)
|
| | | cas[i] = list.get(i);
|
| | | query.addCriteria(new Criteria().andOperator(cas));
|
| | | }
|
| | | return findList(query);
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * 统计年月
|
| | | * @param uid
|
| | | * @param minDate
|
| | | * @param maxDate
|
| | | * @return
|
| | | */
|
| | | public List<TeamDailyRecord> sumGroupByYearMonth(Long uid, Date minDate, Date maxDate) {
|
| | | List<TeamDailyRecord> listDailyCount = new ArrayList<>();
|
| | | // 统计方法
|
| | | String reduce = "function(doc, aggr){"
|
| | | + " aggr.firstDaRen += doc.firstDaRen;"
|
| | | + " aggr.secondDaRen += doc.secondDaRen;"
|
| | | + " aggr.firstHighVIP += doc.firstHighVIP;"
|
| | | + " aggr.secondHighVIP += doc.secondHighVIP;"
|
| | | + " aggr.firstSuperVIP += doc.firstSuperVIP;"
|
| | | + " aggr.secondSuperVIP += doc.secondSuperVIP;"
|
| | | + "}";
|
| | |
|
| | | // 查询条件
|
| | | List<Criteria> list = new ArrayList<Criteria>();
|
| | | list.add(Criteria.where("uid").is(uid));
|
| | | if (minDate != null)
|
| | | list.add(Criteria.where("countDay").gte(minDate));
|
| | | if (maxDate != null)
|
| | | list.add(Criteria.where("countDay").lte(maxDate));
|
| | |
|
| | | Query query = new Query();
|
| | | if (list.size() > 0) {
|
| | | Criteria[] cas = new Criteria[list.size()];
|
| | | for (int i = 0; i < list.size(); i++)
|
| | | cas[i] = list.get(i);
|
| | | query.addCriteria(new Criteria().andOperator(cas));
|
| | | }
|
| | |
|
| | | BasicDBObject agg = new BasicDBObject("yearMonth", "");
|
| | |
|
| | | Map<String, Object> map = new HashMap<>();
|
| | | map.put("firstDaRen", 0);
|
| | | map.put("firstHighVIP", 0);
|
| | | map.put("firstSuperVIP", 0);
|
| | | map.put("secondDaRen", 0);
|
| | | map.put("secondHighVIP", 0);
|
| | | map.put("secondSuperVIP", 0);
|
| | | BasicDBObject dbObject = new BasicDBObject(map);
|
| | | DBObject result = mongoTemplate.getCollection(collectionName).group(agg, query.getQueryObject(), dbObject, reduce);
|
| | | Map<String, BasicDBObject> mapResult = result.toMap();
|
| | | if (mapResult.size() > 0) {
|
| | | for (int i = 0; i < mapResult.size(); i++) {
|
| | | BasicDBObject object = mapResult.get(i + "");
|
| | | |
| | | TeamDailyRecord record = new TeamDailyRecord();
|
| | | record.setFirstDaRen(new BigDecimal(object.get("firstDaRen").toString()).intValue());
|
| | | record.setFirstHighVIP(new BigDecimal(object.get("firstHighVIP").toString()).intValue());
|
| | | record.setFirstSuperVIP(new BigDecimal(object.get("firstSuperVIP").toString()).intValue());
|
| | | record.setSecondDaRen(new BigDecimal(object.get("secondDaRen").toString()).intValue());
|
| | | record.setSecondHighVIP(new BigDecimal(object.get("secondHighVIP").toString()).intValue());
|
| | | record.setSecondSuperVIP(new BigDecimal(object.get("secondSuperVIP").toString()).intValue());
|
| | | listDailyCount.add(record);
|
| | | }
|
| | | }
|
| | | return listDailyCount;
|
| | | }
|
| | | |
| | | |
| | | private String getTeamNumReduce() {
|
| | | StringBuilder builder = new StringBuilder();
|
| | | builder.append("function(doc, aggr){ ");
|
| | | builder.append(" if(doc.firstNum > 0){ aggr.firstTotal += doc.firstNum;}");
|
| | | builder.append(" aggr.secondTotal += doc.secondNum;");
|
| | | builder.append(" aggr.beyondTotal += doc.beyondNum;");
|
| | | builder.append(" }");
|
| | | return builder.toString();
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 统计每月订单收入、数量
|
| | | * @param uid
|
| | | * @param minDate
|
| | | * @param maxDate
|
| | | * @return
|
| | | */
|
| | | public List<TeamDailyRecord> sumTeamNumGroupByCountDay(Long uid, Date minDate, Date maxDate) {
|
| | | // 查询条件
|
| | | List<Criteria> list = new ArrayList<Criteria>();
|
| | | list.add(Criteria.where("uid").is(uid));
|
| | | if (minDate != null)
|
| | | list.add(Criteria.where("countDay").gte(minDate));
|
| | | if (maxDate != null)
|
| | | list.add(Criteria.where("countDay").lte(maxDate));
|
| | |
|
| | | Query query = new Query();
|
| | | if (list.size() > 0) {
|
| | | Criteria[] cas = new Criteria[list.size()];
|
| | | for (int i = 0; i < list.size(); i++)
|
| | | cas[i] = list.get(i);
|
| | | query.addCriteria(new Criteria().andOperator(cas));
|
| | | }
|
| | |
|
| | | Map<String, Object> map = new HashMap<>();
|
| | | map.put("firstTotal", 0);
|
| | | map.put("secondTotal", 0);
|
| | | map.put("beyondTotal", 0);
|
| | | BasicDBObject initial = new BasicDBObject(map);
|
| | |
|
| | | // 进行按天、周、月分组
|
| | | BasicDBObject agg = new BasicDBObject("countDay", "");
|
| | | DBCollection collection = mongoTemplate.getCollection(collectionName);
|
| | | GroupCommand xx = new GroupCommand(collection, agg, query.getQueryObject(), initial, getTeamNumReduce(), null);
|
| | | BasicDBList objects = (BasicDBList) collection.group(xx);
|
| | |
|
| | | List<TeamDailyRecord> results = new ArrayList<>();
|
| | | if (objects != null) {
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
|
| | | for (int i = 0; i < objects.size(); i++) {
|
| | | BasicDBObject dbObject = (BasicDBObject) objects.get(i);
|
| | | if (dbObject == null) {
|
| | | continue;
|
| | | }
|
| | | |
| | | Object object = dbObject.get("countDay");
|
| | | if (object == null) {
|
| | | continue;
|
| | | }
|
| | | |
| | | try {
|
| | | String date = object.toString();
|
| | | Date dtime = sdf.parse(date);
|
| | | TeamDailyRecord dailyCount = new TeamDailyRecord();
|
| | | dailyCount.setCountDay(dtime);
|
| | | dailyCount.setFirstNum(new BigDecimal(dbObject.get("firstTotal").toString()).intValue());
|
| | | dailyCount.setSecondNum(new BigDecimal(dbObject.get("secondTotal").toString()).intValue());
|
| | | dailyCount.setBeyondNum(new BigDecimal(dbObject.get("beyondTotal").toString()).intValue());
|
| | | |
| | | results.add(dailyCount);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | }
|
| | | return results;
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 统计每月订单收入、数量
|
| | | * @param uid
|
| | | * @param minDate
|
| | | * @param maxDate
|
| | | * @return
|
| | | */
|
| | | public List<TeamDailyRecord> sumTeamNumGroupByYearMonth(Long uid, Date minDate, Date maxDate) {
|
| | | // 查询条件
|
| | | List<Criteria> list = new ArrayList<Criteria>();
|
| | | list.add(Criteria.where("uid").is(uid));
|
| | | if (minDate != null)
|
| | | list.add(Criteria.where("countDay").gte(minDate));
|
| | | if (maxDate != null)
|
| | | list.add(Criteria.where("countDay").lte(maxDate));
|
| | |
|
| | | Query query = new Query();
|
| | | if (list.size() > 0) {
|
| | | Criteria[] cas = new Criteria[list.size()];
|
| | | for (int i = 0; i < list.size(); i++)
|
| | | cas[i] = list.get(i);
|
| | | query.addCriteria(new Criteria().andOperator(cas));
|
| | | }
|
| | |
|
| | | Map<String, Object> map = new HashMap<>();
|
| | | map.put("firstTotal", 0);
|
| | | map.put("secondTotal", 0);
|
| | | map.put("beyondTotal", 0);
|
| | | BasicDBObject initial = new BasicDBObject(map);
|
| | |
|
| | | // 进行按天、周、月分组
|
| | | BasicDBObject agg = new BasicDBObject("yearMonth", "");
|
| | | DBCollection collection = mongoTemplate.getCollection(collectionName);
|
| | | GroupCommand xx = new GroupCommand(collection, agg, query.getQueryObject(), initial, getTeamNumReduce(), null);
|
| | | BasicDBList objects = (BasicDBList) collection.group(xx);
|
| | |
|
| | | List<TeamDailyRecord> results = new ArrayList<>();
|
| | | if (objects != null) {
|
| | | for (int i = 0; i < objects.size(); i++) {
|
| | | BasicDBObject dbObject = (BasicDBObject) objects.get(i);
|
| | | if (dbObject == null) {
|
| | | continue;
|
| | | }
|
| | | |
| | | Object object = dbObject.get("yearMonth");
|
| | | if (object == null) {
|
| | | continue;
|
| | | }
|
| | | |
| | | TeamDailyRecord dailyCount = new TeamDailyRecord();
|
| | | dailyCount.setYearMonth(object.toString());
|
| | | dailyCount.setFirstNum(new BigDecimal(dbObject.get("firstTotal").toString()).intValue());
|
| | | dailyCount.setSecondNum(new BigDecimal(dbObject.get("secondTotal").toString()).intValue());
|
| | | dailyCount.setBeyondNum(new BigDecimal(dbObject.get("beyondTotal").toString()).intValue());
|
| | | |
| | | results.add(dailyCount);
|
| | | }
|
| | | }
|
| | | return results;
|
| | | }
|
| | | |
| | | /**
|
| | | * 统计每月订单收入、数量
|
| | | * @param uid
|
| | | * @param minDate
|
| | | * @param maxDate
|
| | | * @return
|
| | | */
|
| | | public List<TeamDailyRecord> sumTeamNumGroupByUid(Long uid, Date minDate, Date maxDate) {
|
| | | // 查询条件
|
| | | List<Criteria> list = new ArrayList<Criteria>();
|
| | | list.add(Criteria.where("uid").is(uid));
|
| | | if (minDate != null)
|
| | | list.add(Criteria.where("countDay").gte(minDate));
|
| | | if (maxDate != null)
|
| | | list.add(Criteria.where("countDay").lte(maxDate));
|
| | |
|
| | | Query query = new Query();
|
| | | if (list.size() > 0) {
|
| | | Criteria[] cas = new Criteria[list.size()];
|
| | | for (int i = 0; i < list.size(); i++)
|
| | | cas[i] = list.get(i);
|
| | | query.addCriteria(new Criteria().andOperator(cas));
|
| | | }
|
| | |
|
| | | Map<String, Object> map = new HashMap<>();
|
| | | map.put("firstTotal", 0);
|
| | | map.put("secondTotal", 0);
|
| | | map.put("beyondTotal", 0);
|
| | | BasicDBObject initial = new BasicDBObject(map);
|
| | |
|
| | | // 进行按天、周、月分组
|
| | | BasicDBObject agg = new BasicDBObject("uid", "");
|
| | | DBCollection collection = mongoTemplate.getCollection(collectionName);
|
| | | GroupCommand xx = new GroupCommand(collection, agg, query.getQueryObject(), initial, getTeamNumReduce(), null);
|
| | | BasicDBList objects = (BasicDBList) collection.group(xx);
|
| | |
|
| | | List<TeamDailyRecord> results = new ArrayList<>();
|
| | | if (objects != null) {
|
| | | for (int i = 0; i < objects.size(); i++) {
|
| | | BasicDBObject dbObject = (BasicDBObject) objects.get(i);
|
| | | if (dbObject == null) {
|
| | | continue;
|
| | | }
|
| | | |
| | | Object object = dbObject.get("uid");
|
| | | if (object == null) {
|
| | | continue;
|
| | | }
|
| | | |
| | | TeamDailyRecord dailyCount = new TeamDailyRecord();
|
| | | dailyCount.setFirstNum(new BigDecimal(dbObject.get("firstTotal").toString()).intValue());
|
| | | dailyCount.setSecondNum(new BigDecimal(dbObject.get("secondTotal").toString()).intValue());
|
| | | dailyCount.setBeyondNum(new BigDecimal(dbObject.get("beyondTotal").toString()).intValue());
|
| | | results.add(dailyCount);
|
| | | }
|
| | | }
|
| | | return results;
|
| | | }
|
| | | }
|
| | | package com.yeshi.fanli.dao.user.invite; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | 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.DBCollection; |
| | | import com.mongodb.DBObject; |
| | | import com.mongodb.GroupCommand; |
| | | import com.yeshi.common.MongodbBaseDao; |
| | | import com.yeshi.fanli.entity.bus.user.invite.TeamDailyRecord; |
| | | import com.yeshi.fanli.vo.order.OrderRankingVO; |
| | | |
| | | @Repository |
| | | public class TeamDailyRecordDao extends MongodbBaseDao<TeamDailyRecord> { |
| | | |
| | | private static String collectionName = "teamDailyRecord"; |
| | | |
| | | |
| | | /** |
| | | * 选择性更新 |
| | | * @param record |
| | | */ |
| | | public void updateSelective(TeamDailyRecord record) { |
| | | Query query = new Query(); |
| | | query.addCriteria(Criteria.where("id").is(record.getId())); |
| | | |
| | | Update update = new Update(); |
| | | if (record.getFirstNum() != null) |
| | | update.set("firstNum", record.getFirstNum()); |
| | | if (record.getSecondNum() != null) |
| | | update.set("secondNum", record.getSecondNum()); |
| | | if (record.getBeyondNum() != null) |
| | | update.set("beyondNum", record.getBeyondNum()); |
| | | if (record.getFirstDaRen() != null) |
| | | update.set("firstDaRen", record.getFirstDaRen()); |
| | | if (record.getSecondDaRen() != null) |
| | | update.set("secondDaRen", record.getSecondDaRen()); |
| | | if (record.getBeyondDaRen() != null) |
| | | update.set("beyondDaRen", record.getBeyondDaRen()); |
| | | if (record.getFirstHighVIP() != null) |
| | | update.set("firstHighVIP", record.getFirstHighVIP()); |
| | | if (record.getSecondHighVIP() != null) |
| | | update.set("secondHighVIP", record.getSecondHighVIP()); |
| | | if (record.getBeyondHighVIP() != null) |
| | | update.set("beyondHighVIP", record.getBeyondHighVIP()); |
| | | if (record.getFirstSuperVIP() != null) |
| | | update.set("firstSuperVIP", record.getFirstSuperVIP()); |
| | | if (record.getSecondSuperVIP() != null) |
| | | update.set("secondSuperVIP", record.getSecondSuperVIP()); |
| | | if (record.getBeyondSuperVIP() != null) |
| | | update.set("beyondSuperVIP", record.getBeyondSuperVIP()); |
| | | update.set("updateTime", new Date()); |
| | | update(query, update); |
| | | } |
| | | |
| | | /** |
| | | * 根据id查询数据 |
| | | * |
| | | * @param id |
| | | * @return |
| | | */ |
| | | public TeamDailyRecord getById(String id) { |
| | | Query query = new Query(); |
| | | query.addCriteria(Criteria.where("id").is(id)); |
| | | return mongoTemplate.findOne(query, TeamDailyRecord.class); |
| | | } |
| | | |
| | | /** |
| | | * 查询 |
| | | * |
| | | * @return |
| | | */ |
| | | public List<TeamDailyRecord> getByUid(Long uid) { |
| | | Query query = new Query(); |
| | | query.addCriteria(Criteria.where("uid").is(uid)); |
| | | return findList(query); |
| | | } |
| | | |
| | | /** |
| | | * 查询 |
| | | * |
| | | * @return |
| | | */ |
| | | public List<TeamDailyRecord> listByUid(Long uid, Date minDate, Date maxDate) { |
| | | List<Criteria> list = new ArrayList<Criteria>(); |
| | | list.add(Criteria.where("uid").is(uid)); |
| | | if (minDate != null) |
| | | list.add(Criteria.where("countDay").gte(minDate)); |
| | | if (maxDate != null) |
| | | list.add(Criteria.where("countDay").lt(maxDate)); |
| | | |
| | | Query query = new Query(); |
| | | if (list.size() > 0) { |
| | | Criteria[] cas = new Criteria[list.size()]; |
| | | for (int i = 0; i < list.size(); i++) |
| | | cas[i] = list.get(i); |
| | | query.addCriteria(new Criteria().andOperator(cas)); |
| | | } |
| | | return findList(query); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 统计年月 |
| | | * @param uid |
| | | * @param minDate |
| | | * @param maxDate |
| | | * @return |
| | | */ |
| | | public List<TeamDailyRecord> sumGroupByYearMonth(Long uid, Date minDate, Date maxDate) { |
| | | List<TeamDailyRecord> listDailyCount = new ArrayList<>(); |
| | | // 统计方法 |
| | | String reduce = "function(doc, aggr){" |
| | | + " aggr.firstDaRen += doc.firstDaRen;" |
| | | + " aggr.secondDaRen += doc.secondDaRen;" |
| | | + " aggr.firstHighVIP += doc.firstHighVIP;" |
| | | + " aggr.secondHighVIP += doc.secondHighVIP;" |
| | | + " aggr.firstSuperVIP += doc.firstSuperVIP;" |
| | | + " aggr.secondSuperVIP += doc.secondSuperVIP;" |
| | | + "}"; |
| | | |
| | | // 查询条件 |
| | | List<Criteria> list = new ArrayList<Criteria>(); |
| | | list.add(Criteria.where("uid").is(uid)); |
| | | if (minDate != null) |
| | | list.add(Criteria.where("countDay").gte(minDate)); |
| | | if (maxDate != null) |
| | | list.add(Criteria.where("countDay").lt(maxDate)); |
| | | |
| | | Query query = new Query(); |
| | | if (list.size() > 0) { |
| | | Criteria[] cas = new Criteria[list.size()]; |
| | | for (int i = 0; i < list.size(); i++) |
| | | cas[i] = list.get(i); |
| | | query.addCriteria(new Criteria().andOperator(cas)); |
| | | } |
| | | |
| | | BasicDBObject agg = new BasicDBObject("yearMonth", ""); |
| | | |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("firstDaRen", 0); |
| | | map.put("firstHighVIP", 0); |
| | | map.put("firstSuperVIP", 0); |
| | | map.put("secondDaRen", 0); |
| | | map.put("secondHighVIP", 0); |
| | | map.put("secondSuperVIP", 0); |
| | | BasicDBObject dbObject = new BasicDBObject(map); |
| | | DBObject result = mongoTemplate.getCollection(collectionName).group(agg, query.getQueryObject(), dbObject, reduce); |
| | | Map<String, BasicDBObject> mapResult = result.toMap(); |
| | | if (mapResult.size() > 0) { |
| | | for (int i = 0; i < mapResult.size(); i++) { |
| | | BasicDBObject object = mapResult.get(i + ""); |
| | | |
| | | TeamDailyRecord record = new TeamDailyRecord(); |
| | | record.setFirstDaRen(new BigDecimal(object.get("firstDaRen").toString()).intValue()); |
| | | record.setFirstHighVIP(new BigDecimal(object.get("firstHighVIP").toString()).intValue()); |
| | | record.setFirstSuperVIP(new BigDecimal(object.get("firstSuperVIP").toString()).intValue()); |
| | | record.setSecondDaRen(new BigDecimal(object.get("secondDaRen").toString()).intValue()); |
| | | record.setSecondHighVIP(new BigDecimal(object.get("secondHighVIP").toString()).intValue()); |
| | | record.setSecondSuperVIP(new BigDecimal(object.get("secondSuperVIP").toString()).intValue()); |
| | | listDailyCount.add(record); |
| | | } |
| | | } |
| | | return listDailyCount; |
| | | } |
| | | |
| | | |
| | | private String getTeamNumReduce() { |
| | | StringBuilder builder = new StringBuilder(); |
| | | builder.append("function(doc, aggr){ "); |
| | | builder.append(" if(doc.firstNum > 0){ aggr.firstTotal += doc.firstNum;}"); |
| | | builder.append(" aggr.secondTotal += doc.secondNum;"); |
| | | builder.append(" aggr.beyondTotal += doc.beyondNum;"); |
| | | builder.append(" }"); |
| | | return builder.toString(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 统计每月订单收入、数量 |
| | | * @param uid |
| | | * @param minDate |
| | | * @param maxDate |
| | | * @return |
| | | */ |
| | | public List<TeamDailyRecord> sumTeamNumGroupByCountDay(Long uid, Date minDate, Date maxDate) { |
| | | // 查询条件 |
| | | List<Criteria> list = new ArrayList<Criteria>(); |
| | | list.add(Criteria.where("uid").is(uid)); |
| | | if (minDate != null) |
| | | list.add(Criteria.where("countDay").gte(minDate)); |
| | | if (maxDate != null) |
| | | list.add(Criteria.where("countDay").lt(maxDate)); |
| | | |
| | | Query query = new Query(); |
| | | if (list.size() > 0) { |
| | | Criteria[] cas = new Criteria[list.size()]; |
| | | for (int i = 0; i < list.size(); i++) |
| | | cas[i] = list.get(i); |
| | | query.addCriteria(new Criteria().andOperator(cas)); |
| | | } |
| | | |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("firstTotal", 0); |
| | | map.put("secondTotal", 0); |
| | | map.put("beyondTotal", 0); |
| | | BasicDBObject initial = new BasicDBObject(map); |
| | | |
| | | // 进行按天、周、月分组 |
| | | BasicDBObject agg = new BasicDBObject("countDay", ""); |
| | | DBCollection collection = mongoTemplate.getCollection(collectionName); |
| | | GroupCommand xx = new GroupCommand(collection, agg, query.getQueryObject(), initial, getTeamNumReduce(), null); |
| | | BasicDBList objects = (BasicDBList) collection.group(xx); |
| | | |
| | | List<TeamDailyRecord> results = new ArrayList<>(); |
| | | if (objects != null) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US); |
| | | for (int i = 0; i < objects.size(); i++) { |
| | | BasicDBObject dbObject = (BasicDBObject) objects.get(i); |
| | | if (dbObject == null) { |
| | | continue; |
| | | } |
| | | |
| | | Object object = dbObject.get("countDay"); |
| | | if (object == null) { |
| | | continue; |
| | | } |
| | | |
| | | try { |
| | | String date = object.toString(); |
| | | Date dtime = sdf.parse(date); |
| | | TeamDailyRecord dailyCount = new TeamDailyRecord(); |
| | | dailyCount.setCountDay(dtime); |
| | | dailyCount.setFirstNum(new BigDecimal(dbObject.get("firstTotal").toString()).intValue()); |
| | | dailyCount.setSecondNum(new BigDecimal(dbObject.get("secondTotal").toString()).intValue()); |
| | | dailyCount.setBeyondNum(new BigDecimal(dbObject.get("beyondTotal").toString()).intValue()); |
| | | |
| | | results.add(dailyCount); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return results; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 统计每月订单收入、数量 |
| | | * @param uid |
| | | * @param minDate |
| | | * @param maxDate |
| | | * @return |
| | | */ |
| | | public List<TeamDailyRecord> sumTeamNumGroupByYearMonth(Long uid, Date minDate, Date maxDate) { |
| | | // 查询条件 |
| | | List<Criteria> list = new ArrayList<Criteria>(); |
| | | list.add(Criteria.where("uid").is(uid)); |
| | | if (minDate != null) |
| | | list.add(Criteria.where("countDay").gte(minDate)); |
| | | if (maxDate != null) |
| | | list.add(Criteria.where("countDay").lt(maxDate)); |
| | | |
| | | Query query = new Query(); |
| | | if (list.size() > 0) { |
| | | Criteria[] cas = new Criteria[list.size()]; |
| | | for (int i = 0; i < list.size(); i++) |
| | | cas[i] = list.get(i); |
| | | query.addCriteria(new Criteria().andOperator(cas)); |
| | | } |
| | | |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("firstTotal", 0); |
| | | map.put("secondTotal", 0); |
| | | map.put("beyondTotal", 0); |
| | | BasicDBObject initial = new BasicDBObject(map); |
| | | |
| | | // 进行按天、周、月分组 |
| | | BasicDBObject agg = new BasicDBObject("yearMonth", ""); |
| | | DBCollection collection = mongoTemplate.getCollection(collectionName); |
| | | GroupCommand xx = new GroupCommand(collection, agg, query.getQueryObject(), initial, getTeamNumReduce(), null); |
| | | BasicDBList objects = (BasicDBList) collection.group(xx); |
| | | |
| | | List<TeamDailyRecord> results = new ArrayList<>(); |
| | | if (objects != null) { |
| | | for (int i = 0; i < objects.size(); i++) { |
| | | BasicDBObject dbObject = (BasicDBObject) objects.get(i); |
| | | if (dbObject == null) { |
| | | continue; |
| | | } |
| | | |
| | | Object object = dbObject.get("yearMonth"); |
| | | if (object == null) { |
| | | continue; |
| | | } |
| | | |
| | | TeamDailyRecord dailyCount = new TeamDailyRecord(); |
| | | dailyCount.setYearMonth(object.toString()); |
| | | dailyCount.setFirstNum(new BigDecimal(dbObject.get("firstTotal").toString()).intValue()); |
| | | dailyCount.setSecondNum(new BigDecimal(dbObject.get("secondTotal").toString()).intValue()); |
| | | dailyCount.setBeyondNum(new BigDecimal(dbObject.get("beyondTotal").toString()).intValue()); |
| | | |
| | | results.add(dailyCount); |
| | | } |
| | | } |
| | | return results; |
| | | } |
| | | |
| | | /** |
| | | * 统计每月订单收入、数量 |
| | | * @param uid |
| | | * @param minDate |
| | | * @param maxDate |
| | | * @return |
| | | */ |
| | | public List<TeamDailyRecord> sumTeamNumGroupByUid(Long uid, Date minDate, Date maxDate) { |
| | | // 查询条件 |
| | | List<Criteria> list = new ArrayList<Criteria>(); |
| | | list.add(Criteria.where("uid").is(uid)); |
| | | if (minDate != null) |
| | | list.add(Criteria.where("countDay").gte(minDate)); |
| | | if (maxDate != null) |
| | | list.add(Criteria.where("countDay").lt(maxDate)); |
| | | |
| | | Query query = new Query(); |
| | | if (list.size() > 0) { |
| | | Criteria[] cas = new Criteria[list.size()]; |
| | | for (int i = 0; i < list.size(); i++) |
| | | cas[i] = list.get(i); |
| | | query.addCriteria(new Criteria().andOperator(cas)); |
| | | } |
| | | |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("firstTotal", 0); |
| | | map.put("secondTotal", 0); |
| | | map.put("beyondTotal", 0); |
| | | BasicDBObject initial = new BasicDBObject(map); |
| | | |
| | | // 进行按天、周、月分组 |
| | | BasicDBObject agg = new BasicDBObject("uid", ""); |
| | | DBCollection collection = mongoTemplate.getCollection(collectionName); |
| | | GroupCommand xx = new GroupCommand(collection, agg, query.getQueryObject(), initial, getTeamNumReduce(), null); |
| | | BasicDBList objects = (BasicDBList) collection.group(xx); |
| | | |
| | | List<TeamDailyRecord> results = new ArrayList<>(); |
| | | if (objects != null) { |
| | | for (int i = 0; i < objects.size(); i++) { |
| | | BasicDBObject dbObject = (BasicDBObject) objects.get(i); |
| | | if (dbObject == null) { |
| | | continue; |
| | | } |
| | | |
| | | Object object = dbObject.get("uid"); |
| | | if (object == null) { |
| | | continue; |
| | | } |
| | | |
| | | TeamDailyRecord dailyCount = new TeamDailyRecord(); |
| | | dailyCount.setFirstNum(new BigDecimal(dbObject.get("firstTotal").toString()).intValue()); |
| | | dailyCount.setSecondNum(new BigDecimal(dbObject.get("secondTotal").toString()).intValue()); |
| | | dailyCount.setBeyondNum(new BigDecimal(dbObject.get("beyondTotal").toString()).intValue()); |
| | | results.add(dailyCount); |
| | | } |
| | | } |
| | | 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)); |
| | | list.add(Criteria.where("result.firstNum").gt(0)); |
| | | if (minDate != null) { |
| | | minDate = DateUtil.reduceDay(minDate, 1); |
| | | list.add(Criteria.where("result.countDay").gt(minDate)); |
| | | } |
| | | if (maxDate != null) { |
| | | maxDate = DateUtil.reduceDay(maxDate, 1); |
| | | list.add(Criteria.where("result.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; |
| | | } |
| | | } |