|
|
@@ -19,6 +19,8 @@ import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
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.AggregationResults;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -66,20 +68,22 @@ public class KwOrderService {
|
|
|
* @return: com.sckw.core.model.page.PageResult
|
|
|
*/
|
|
|
public PageResult tradeOrderSelect(TradeOrderListSelectParam params) {
|
|
|
- Query query = getQuery(params, false);
|
|
|
- return getResult(query, params.getPage(), params.getPageSize());
|
|
|
+ Criteria criteria = buildCriteria(params, false);
|
|
|
+ return getResult(criteria, params.getPage(), params.getPageSize());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @desc: 获取分页结果
|
|
|
* @author: yzc
|
|
|
* @date: 2023-07-20 15:02
|
|
|
- * @Param query:
|
|
|
+ * @Param criteria:
|
|
|
* @Param page:
|
|
|
* @Param pageSize:
|
|
|
* @return: com.sckw.core.model.page.PageResult
|
|
|
*/
|
|
|
- private PageResult getResult(Query query, int page, int pageSize) {
|
|
|
+ private PageResult getResult(Criteria criteria, int page, int pageSize) {
|
|
|
+ Query query = new Query();
|
|
|
+ query.addCriteria(criteria);
|
|
|
long count = mongoTemplate.count(query, SckwTradeOrder.class);
|
|
|
Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
|
|
|
SpringDataPageAble pageAble = new SpringDataPageAble(page, pageSize, sort);
|
|
|
@@ -121,17 +125,16 @@ public class KwOrderService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @desc: 组装query
|
|
|
+ * @desc: 组装Criteria
|
|
|
* @author: yzc
|
|
|
* @date: 2023-07-17 18:08
|
|
|
* @Param params:
|
|
|
* @Param isStatistic:
|
|
|
- * @return: org.springframework.data.mongodb.core.query.Query
|
|
|
+ * @return: org.springframework.data.mongodb.core.query.Criteria
|
|
|
*/
|
|
|
- private Query getQuery(TradeOrderListStatisticParam params, Boolean isStatistic) {
|
|
|
+ private Criteria buildCriteria(TradeOrderListStatisticParam params, Boolean isStatistic) {
|
|
|
Long entId = LoginUserHolder.getEntId();
|
|
|
Long userId = LoginUserHolder.getUserId();
|
|
|
- Query query = new Query();
|
|
|
Criteria criteria = new Criteria();
|
|
|
String topEnt = Objects.equals(params.getOrderType(), 1) ? "procureTopEntId" : "supplyTopEntId";
|
|
|
criteria.and(topEnt).is(entId).and("delFlag").is(0);
|
|
|
@@ -207,7 +210,7 @@ public class KwOrderService {
|
|
|
if (CollectionUtils.isNotEmpty(orOperators)) {
|
|
|
criteria.andOperator(orOperators);
|
|
|
}
|
|
|
- return query.addCriteria(criteria);
|
|
|
+ return criteria;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -219,25 +222,37 @@ public class KwOrderService {
|
|
|
*/
|
|
|
public TableStatisticRes tradeOrderStatistic(TradeOrderListStatisticParam params) {
|
|
|
TableStatisticRes res = new TableStatisticRes();
|
|
|
- Query query = getQuery(params, true);
|
|
|
- List<SckwTradeOrder> orders = mongoTemplate.find(query, SckwTradeOrder.class);
|
|
|
- Map<Integer, List<SckwTradeOrder>> map = orders.stream().collect(Collectors.groupingBy(SckwTradeOrder::getStatus));
|
|
|
- List<TableTop> tableTops = new ArrayList<>();
|
|
|
- TableTop all = new TableTop();
|
|
|
- all.setName("全部").setTotal(CollectionUtils.isEmpty(orders) ? 0 : orders.size());
|
|
|
- tableTops.add(all);
|
|
|
+ Criteria criteria = buildCriteria(params, true);
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(
|
|
|
+ Aggregation.match(criteria),
|
|
|
+ Aggregation.group("status").count().as("total"),
|
|
|
+ Aggregation.project("total").and("value").previousOperation()
|
|
|
+ );
|
|
|
+ AggregationResults<TableTop> aggregate = mongoTemplate
|
|
|
+ .aggregate(aggregation, SckwTradeOrder.class, TableTop.class);
|
|
|
+ List<TableTop> tableTops = aggregate.getMappedResults();
|
|
|
+ Map<Integer, Integer> map = new HashMap<>();
|
|
|
+ if (CollectionUtils.isNotEmpty(tableTops)) {
|
|
|
+ map = tableTops.stream().collect(Collectors.toMap
|
|
|
+ (TableTop::getValue, TableTop::getTotal, (k1, k2) -> k2));
|
|
|
+ }
|
|
|
+ List<TableTop> list = new ArrayList<>();
|
|
|
List<DictEnum> tOrderStatusEnums = DictEnum.getEnumsByType(DictTypeEnum.TORDER_STATUS.getType());
|
|
|
- tOrderStatusEnums.forEach(e -> {
|
|
|
+ int count = 0;
|
|
|
+ for (DictEnum e : tOrderStatusEnums) {
|
|
|
Integer value = Integer.valueOf(e.getValue());
|
|
|
- List<SckwTradeOrder> list = map.get(value);
|
|
|
- int total = CollectionUtils.isEmpty(list) ? 0 : list.size();
|
|
|
+ int total = Objects.isNull(map.get(value)) ? 0 : map.get(value);
|
|
|
TableTop tableTop = new TableTop();
|
|
|
tableTop.setName(e.getLabel()).setValue(value).setTotal(total);
|
|
|
- tableTops.add(tableTop);
|
|
|
- });
|
|
|
+ list.add(tableTop);
|
|
|
+ count = count + total;
|
|
|
+ }
|
|
|
+ TableTop all = new TableTop();
|
|
|
+ all.setName("全部").setTotal(count);
|
|
|
+ list.add(all);
|
|
|
TableBottom tableBottom = new TableBottom();
|
|
|
- tableBottom.setTotal(CollectionUtils.isEmpty(orders) ? 0 : orders.size());
|
|
|
- res.setTableTops(tableTops).setTableBottom(tableBottom);
|
|
|
+ tableBottom.setTotal(count);
|
|
|
+ res.setTableTops(list).setTableBottom(tableBottom);
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
@@ -255,17 +270,26 @@ public class KwOrderService {
|
|
|
Query query = new Query();
|
|
|
Criteria criteria = new Criteria();
|
|
|
criteria.and(topEnt).is(entId).and("status").in(status).and("delFlag").is(0);
|
|
|
- query.addCriteria(criteria);
|
|
|
- List<SckwTradeOrder> orders = mongoTemplate.find(query, SckwTradeOrder.class);
|
|
|
- Map<Integer, List<SckwTradeOrder>> map = orders.stream().collect(Collectors.groupingBy(SckwTradeOrder::getStatus));
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(
|
|
|
+ Aggregation.match(criteria),
|
|
|
+ Aggregation.group("status").count().as("total"),
|
|
|
+ Aggregation.project("total").and("value").previousOperation()
|
|
|
+ );
|
|
|
+ AggregationResults<TradeOrderAppStatisticVO> aggregate = mongoTemplate
|
|
|
+ .aggregate(aggregation, SckwTradeOrder.class, TradeOrderAppStatisticVO.class);
|
|
|
+ List<TradeOrderAppStatisticVO> tableTops = aggregate.getMappedResults();
|
|
|
+ Map<Integer, Integer> map = new HashMap<>();
|
|
|
+ if (CollectionUtils.isNotEmpty(tableTops)) {
|
|
|
+ map = tableTops.stream().collect(Collectors.toMap
|
|
|
+ (TradeOrderAppStatisticVO::getValue, TradeOrderAppStatisticVO::getTotal, (k1, k2) -> k2));
|
|
|
+ }
|
|
|
List<TradeOrderAppStatisticVO> statistics = new ArrayList<>();
|
|
|
- status.forEach(e -> {
|
|
|
- List<SckwTradeOrder> list = map.get(e);
|
|
|
- int total = CollectionUtils.isEmpty(list) ? 0 : list.size();
|
|
|
+ for (Integer e : status) {
|
|
|
+ int total = Objects.isNull(map.get(e)) ? 0 : map.get(e);
|
|
|
TradeOrderAppStatisticVO statistic = new TradeOrderAppStatisticVO();
|
|
|
statistic.setValue(e).setTotal(total);
|
|
|
statistics.add(statistic);
|
|
|
- });
|
|
|
+ }
|
|
|
return statistics;
|
|
|
}
|
|
|
|
|
|
@@ -287,7 +311,7 @@ public class KwOrderService {
|
|
|
criteria.and("_id").in(ids).and(topEnt).is(entId).and("delFlag").is(0);
|
|
|
query.addCriteria(criteria);
|
|
|
} else {
|
|
|
- query = getQuery(params, false);
|
|
|
+ query.addCriteria(buildCriteria(params, false));
|
|
|
}
|
|
|
query.with(Sort.by(Sort.Order.desc("createTime")));
|
|
|
List<SckwTradeOrder> orders = mongoTemplate.find(query, SckwTradeOrder.class);
|
|
|
@@ -333,7 +357,6 @@ public class KwOrderService {
|
|
|
* @return: com.sckw.core.model.page.PageResult
|
|
|
*/
|
|
|
public PageResult tradeOrderStatementList(TradeOrderStatementList params) {
|
|
|
- Query query = new Query();
|
|
|
Long entId = LoginUserHolder.getEntId();
|
|
|
Criteria criteria = new Criteria();
|
|
|
String topEnt = Objects.equals(params.getOrderType(), 1) ? "procureTopEntId" : "supplyTopEntId";
|
|
|
@@ -349,7 +372,7 @@ public class KwOrderService {
|
|
|
criteria.and("procureEntId").is(params.getProcureEntId());
|
|
|
}
|
|
|
//交易方式
|
|
|
- if(StringUtils.isNotBlank(params.getTrading())){
|
|
|
+ if (StringUtils.isNotBlank(params.getTrading())) {
|
|
|
criteria.and("trading").is(params.getTrading());
|
|
|
}
|
|
|
//计划开始时间
|
|
|
@@ -369,8 +392,7 @@ public class KwOrderService {
|
|
|
Pattern pattern = Pattern.compile("^.*" + params.getGoodsName() + ".*$", Pattern.CASE_INSENSITIVE);
|
|
|
criteria.and("goodsName").regex(pattern);
|
|
|
}
|
|
|
- query.addCriteria(criteria);
|
|
|
- return getResult(query, params.getPage(), params.getPageSize());
|
|
|
+ return getResult(criteria, params.getPage(), params.getPageSize());
|
|
|
}
|
|
|
|
|
|
/**
|