| | |
| | | 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.yeshi.fanli.dao.MongodbBaseDao;
|
| | | import com.yeshi.fanli.entity.order.OrderMoneyDailyCount;
|
| | | import com.yeshi.fanli.entity.order.OrderMoneyDailyCount.SourceTypeEnum;
|
| | | import com.yeshi.fanli.util.TimeUtil;
|
| | | import com.yeshi.fanli.vo.order.OrderRankingVO;
|
| | |
|
| | | @Repository
|
| | | public class OrderMoneyDailyCountDao extends MongodbBaseDao<OrderMoneyDailyCount> {
|
| | |
| | |
|
| | | for (int i = 0; i < mapResult.size(); i++) {
|
| | | BasicDBObject object = mapResult.get(i + "");
|
| | | if (object == null || object.get("sourceType") == null) {
|
| | | continue;
|
| | | }
|
| | | |
| | | SourceTypeEnum sourceTypeEnum = null;
|
| | | for (int m = 0; m < arrayEnun.length; m++) {
|
| | | if (arrayEnun[m].name().equals(object.get("sourceType").toString())) {
|
| | |
| | | SourceTypeEnum[] arrayEnun = SourceTypeEnum.values();
|
| | | for (int i = 0; i < mapResult.size(); i++) {
|
| | | BasicDBObject object = mapResult.get(i + "");
|
| | | OrderMoneyDailyCount dailyCount = new OrderMoneyDailyCount();
|
| | |
|
| | | if (object == null || object.get("sourceType") == null) {
|
| | | continue;
|
| | | }
|
| | | |
| | | SourceTypeEnum sourceTypeEnum = null;
|
| | | for (int m = 0; m < arrayEnun.length; m++) {
|
| | | if (arrayEnun[m].name().equals(object.get("sourceType").toString())) {
|
| | |
| | | break;
|
| | | }
|
| | | }
|
| | | OrderMoneyDailyCount dailyCount = new OrderMoneyDailyCount();
|
| | | dailyCount.setSourceType(sourceTypeEnum);
|
| | | dailyCount.setIncome(new BigDecimal(object.get("totalIncome").toString()).intValue());
|
| | | dailyCount.setOrderNum(new BigDecimal(object.get("totalNum").toString()).intValue());
|
| | |
| | | return results;
|
| | | }
|
| | |
|
| | | |
| | | |
| | | private String getTeamOrderNumReduce() {
|
| | | StringBuilder builder = new StringBuilder();
|
| | | builder.append("function(doc, aggr){ ");
|
| | | builder.append(" aggr.directOrderNum += doc.directOrderNum;");
|
| | | builder.append(" aggr.inDirectOrderNum += doc.inDirectOrderNum;");
|
| | | builder.append(" aggr.beyondOrderNum += doc.beyondOrderNum;");
|
| | | builder.append(" }");
|
| | | return builder.toString();
|
| | | }
|
| | | |
| | | /**
|
| | | * 统计每月订单收入、数量
|
| | | * @param uid
|
| | | * @param minDate
|
| | | * @param maxDate
|
| | | * @return
|
| | | */
|
| | | public List<OrderMoneyDailyCount> sumTeamOrderNumGroupByCountDay(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("directOrderNum", 0);
|
| | | map.put("inDirectOrderNum", 0);
|
| | | map.put("beyondOrderNum", 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, getTeamOrderNumReduce(), null);
|
| | | BasicDBList objects = (BasicDBList) collection.group(xx);
|
| | |
|
| | | List<OrderMoneyDailyCount> 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);
|
| | | OrderMoneyDailyCount dailyCount = new OrderMoneyDailyCount();
|
| | | dailyCount.setCountDay(dtime);
|
| | | dailyCount.setDirectOrderNum(new BigDecimal(dbObject.get("directOrderNum").toString()).intValue());
|
| | | dailyCount.setInDirectOrderNum(new BigDecimal(dbObject.get("inDirectOrderNum").toString()).intValue());
|
| | | dailyCount.setBeyondOrderNum(new BigDecimal(dbObject.get("beyondOrderNum").toString()).intValue());
|
| | | results.add(dailyCount);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | }
|
| | | return results;
|
| | | }
|
| | | |
| | | |
| | | /**
|
| | | * 统计每月订单收入、数量
|
| | | * @param uid
|
| | | * @param minDate
|
| | | * @param maxDate
|
| | | * @return
|
| | | */
|
| | | public List<OrderMoneyDailyCount> sumTeamOrderNumGroupByYearMonth(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("directOrderNum", 0);
|
| | | map.put("inDirectOrderNum", 0);
|
| | | map.put("beyondOrderNum", 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, getTeamOrderNumReduce(), null);
|
| | | BasicDBList objects = (BasicDBList) collection.group(xx);
|
| | |
|
| | | List<OrderMoneyDailyCount> 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;
|
| | | }
|
| | | |
| | | OrderMoneyDailyCount dailyCount = new OrderMoneyDailyCount();
|
| | | dailyCount.setYearMonth(object.toString());
|
| | | dailyCount.setDirectOrderNum(new BigDecimal(dbObject.get("directOrderNum").toString()).intValue());
|
| | | dailyCount.setInDirectOrderNum(new BigDecimal(dbObject.get("inDirectOrderNum").toString()).intValue());
|
| | | dailyCount.setBeyondOrderNum(new BigDecimal(dbObject.get("beyondOrderNum").toString()).intValue());
|
| | | |
| | | results.add(dailyCount);
|
| | | }
|
| | | }
|
| | | return results;
|
| | | }
|
| | | |
| | | /**
|
| | | * 统计每月订单收入、数量
|
| | | * @param uid
|
| | | * @param minDate
|
| | | * @param maxDate
|
| | | * @return
|
| | | */
|
| | | public List<OrderMoneyDailyCount> sumTeamOrderNumGroupByUid(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("directOrderNum", 0);
|
| | | map.put("inDirectOrderNum", 0);
|
| | | map.put("beyondOrderNum", 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, getTeamOrderNumReduce(), null);
|
| | | BasicDBList objects = (BasicDBList) collection.group(xx);
|
| | |
|
| | | List<OrderMoneyDailyCount> 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;
|
| | | }
|
| | | |
| | | OrderMoneyDailyCount dailyCount = new OrderMoneyDailyCount();
|
| | | dailyCount.setDirectOrderNum(new BigDecimal(dbObject.get("directOrderNum").toString()).intValue());
|
| | | dailyCount.setInDirectOrderNum(new BigDecimal(dbObject.get("inDirectOrderNum").toString()).intValue());
|
| | | dailyCount.setBeyondOrderNum(new BigDecimal(dbObject.get("beyondOrderNum").toString()).intValue());
|
| | | |
| | | results.add(dailyCount);
|
| | | }
|
| | | }
|
| | | return results;
|
| | | }
|
| | | |
| | | |
| | | |
| | | /**
|
| | | * 根据订单排行榜进行uid分组订单数量排序
|
| | | * @param uid
|
| | | * @param minDate
|
| | | * @param maxDate
|
| | | * @return
|
| | | */
|
| | | public List<OrderRankingVO> getRankingByOrderNum(Long uid, Date minDate, Date maxDate) {
|
| | | LookupOperation lookupToLots = LookupOperation.newLookup().
|
| | | from("order_money_daily_count").//关联表名,多方
|
| | | localField("workerUid").// 主表关联字段
|
| | | foreignField("uid").// 次表字段关联字段
|
| | | as("orders"); // 次表的别名
|
| | |
|
| | | // 查询条件
|
| | | 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("orders"));
|
| | | // 条件匹配
|
| | | aggs.add(Aggregation.match(criteria)); |
| | | // 需要返回的字段
|
| | | aggs.add(Aggregation.project().and("workerUid").as("workerUid").and("level").as("level")
|
| | | .and("orders.orderNum").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;
|
| | | }
|
| | | }
|