|
|
@@ -0,0 +1,164 @@
|
|
|
+package com.sckw.report.service;
|
|
|
+
|
|
|
+import com.sckw.core.common.enums.NumberConstant;
|
|
|
+import com.sckw.core.utils.CollectionUtils;
|
|
|
+import com.sckw.core.utils.DateUtils;
|
|
|
+import com.sckw.core.utils.StringUtils;
|
|
|
+import com.sckw.core.web.context.LoginUserHolder;
|
|
|
+import com.sckw.mongo.model.SckwTradeOrder;
|
|
|
+import com.sckw.product.api.dubbo.GoodsStatisticsService;
|
|
|
+import com.sckw.product.api.model.GoodsNumStsParam;
|
|
|
+import com.sckw.report.model.dto.WorkbenchOrderStsDTO;
|
|
|
+import com.sckw.report.model.vo.TOrderDataStsResVO;
|
|
|
+import com.sckw.report.model.vo.WbTOrderDataStsParam;
|
|
|
+import com.sckw.report.model.vo.WbTOrderDataStsResVO;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+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.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @desc: TODO
|
|
|
+ * @author: yzc
|
|
|
+ * @date: 2023-09-11 14:33
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@AllArgsConstructor
|
|
|
+public class KwOrderStatisticsService {
|
|
|
+
|
|
|
+ private final MongoTemplate mongoTemplate;
|
|
|
+
|
|
|
+ @DubboReference(version = "1.0.0", group = "design", check = false)
|
|
|
+ private GoodsStatisticsService goodsStatisticsService;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc: 工作台贸易订单数据统计
|
|
|
+ * @author: yzc
|
|
|
+ * @date: 2023-09-11 15:32
|
|
|
+ * @Param params:
|
|
|
+ * @return: com.sckw.report.model.vo.WbTOrderDataStsResVO
|
|
|
+ */
|
|
|
+ public WbTOrderDataStsResVO workbenchData(WbTOrderDataStsParam params) {
|
|
|
+ WbTOrderDataStsResVO res = new WbTOrderDataStsResVO();
|
|
|
+ List<TOrderDataStsResVO> list = new ArrayList<>();
|
|
|
+ Long entId = LoginUserHolder.getEntId();
|
|
|
+ String unit = params.getUnit();
|
|
|
+ Integer orderType = params.getOrderType();
|
|
|
+ Integer dateType = params.getDateType();
|
|
|
+ Date currentStart = DateUtils.getStartTimeByDateType(dateType, LocalDateTime.now());
|
|
|
+ Date lastEnd = DateUtils.getEndTimeByDateType(dateType, LocalDateTime.now());
|
|
|
+ Date lastStart = DateUtils.getStartTimeByDateType(dateType, DateUtils.toLocalDateTime(currentStart));
|
|
|
+ //当前数据
|
|
|
+ TOrderDataStsResVO currentDataSts = new TOrderDataStsResVO();
|
|
|
+ //商品数量
|
|
|
+ GoodsNumStsParam goodsNumStsParam = new GoodsNumStsParam();
|
|
|
+ goodsNumStsParam.setTopEntId(entId).setUnit(unit);
|
|
|
+ Long currentGoodsNum = goodsStatisticsService.getOnShelfGoodsNum(goodsNumStsParam);
|
|
|
+ //订单统计
|
|
|
+ WorkbenchOrderStsDTO currentOrderSts = getOrderStsInfo(orderType, unit, currentStart, null);
|
|
|
+ BigDecimal currentAmount = currentOrderSts.getOrderAmount();
|
|
|
+ BigDecimal currentPrice = currentDataSts.getOrderPrice();
|
|
|
+ currentDataSts.setGoodsNum(currentGoodsNum)
|
|
|
+ .setOrderAmount(currentAmount)
|
|
|
+ .setOrderPrice(currentPrice)
|
|
|
+ .setNotArrivedAmount(currentAmount.subtract(currentOrderSts.getOrderActualAmount()));
|
|
|
+ //todo 待付款金额
|
|
|
+ list.add(currentDataSts);
|
|
|
+
|
|
|
+ //上周/上月数据
|
|
|
+ TOrderDataStsResVO lastDataSts = new TOrderDataStsResVO();
|
|
|
+ //商品数量
|
|
|
+ goodsNumStsParam.setEndDate(lastEnd);
|
|
|
+ Long lastGoodsNum = goodsStatisticsService.getOnShelfGoodsNum(goodsNumStsParam);
|
|
|
+ //订单统计
|
|
|
+ WorkbenchOrderStsDTO lastOrderSts = getOrderStsInfo(orderType, unit, lastStart, lastEnd);
|
|
|
+ BigDecimal lastAmount = lastOrderSts.getOrderAmount();
|
|
|
+ BigDecimal lastPrice = lastOrderSts.getOrderPrice();
|
|
|
+ lastDataSts.setGoodsNum(lastGoodsNum)
|
|
|
+ .setOrderAmount(lastAmount)
|
|
|
+ .setOrderPrice(lastPrice)
|
|
|
+ .setNotArrivedAmount(lastAmount.subtract(lastOrderSts.getOrderActualAmount()));
|
|
|
+ //todo 待付款金额
|
|
|
+ list.add(lastDataSts);
|
|
|
+ res.setTOrderDataStsList(list);
|
|
|
+ //环比数据
|
|
|
+ //商品数量环比
|
|
|
+ BigDecimal goodsNumRingRate = Objects.isNull(lastGoodsNum) || lastGoodsNum == 0L ? NumberConstant.ONE_HUNDRED :
|
|
|
+ new BigDecimal((currentGoodsNum - lastGoodsNum)).divide(new BigDecimal(lastGoodsNum), 2, RoundingMode.HALF_UP);
|
|
|
+ //订单总量环比
|
|
|
+ BigDecimal orderAmountRingRate = lastAmount.compareTo(BigDecimal.ZERO) == 0 ? NumberConstant.ONE_HUNDRED :
|
|
|
+ (currentAmount.subtract(lastAmount)).divide(lastAmount, 2, RoundingMode.HALF_UP);
|
|
|
+ //订单总金额环比
|
|
|
+ BigDecimal orderPriceRingRate = lastPrice.compareTo(BigDecimal.ZERO) == 0 ? NumberConstant.ONE_HUNDRED :
|
|
|
+ (currentPrice.subtract(lastPrice)).divide(lastPrice, 2, RoundingMode.HALF_UP);
|
|
|
+ //未到货量环比
|
|
|
+ BigDecimal currentNotArrived = currentDataSts.getNotArrivedAmount();
|
|
|
+ BigDecimal lastNotArrived = lastDataSts.getNotArrivedAmount();
|
|
|
+ BigDecimal notArrivedAmountRingRate = lastNotArrived.compareTo(BigDecimal.ZERO) == 0 ? NumberConstant.ONE_HUNDRED :
|
|
|
+ (currentNotArrived.subtract(lastNotArrived)).divide(lastNotArrived, 2, RoundingMode.HALF_UP);
|
|
|
+ //待付款金额环比
|
|
|
+ BigDecimal currentWaitPrice = currentDataSts.getWaitCollectedPrice();
|
|
|
+ BigDecimal lastWaitPrice = lastDataSts.getWaitCollectedPrice();
|
|
|
+ BigDecimal waitCollectedPriceRingRate = lastWaitPrice.compareTo(BigDecimal.ZERO) == 0 ? NumberConstant.ONE_HUNDRED :
|
|
|
+ (currentWaitPrice.subtract(lastWaitPrice)).divide(lastWaitPrice, 2, RoundingMode.HALF_UP);
|
|
|
+ res.setGoodsNumRingRate(goodsNumRingRate)
|
|
|
+ .setOrderAmountRingRate(orderAmountRingRate)
|
|
|
+ .setOrderPriceRingRate(orderPriceRingRate)
|
|
|
+ .setNotArrivedAmountRingRate(notArrivedAmountRingRate)
|
|
|
+ .setWaitCollectedPriceRingRate(waitCollectedPriceRingRate);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc: 获取订单统计信息
|
|
|
+ * @author: yzc
|
|
|
+ * @date: 2023-09-11 17:55
|
|
|
+ * @Param orderType:
|
|
|
+ * @Param unit:
|
|
|
+ * @Param start:
|
|
|
+ * @Param end:
|
|
|
+ * @return: com.sckw.report.model.dto.WorkbenchOrderStsDTO
|
|
|
+ */
|
|
|
+ private WorkbenchOrderStsDTO getOrderStsInfo(Integer orderType, String unit, Date start, Date end) {
|
|
|
+ WorkbenchOrderStsDTO dto = new WorkbenchOrderStsDTO();
|
|
|
+ Long entId = LoginUserHolder.getEntId();
|
|
|
+ String topEnt = Objects.equals(orderType, 1) ? "procureTopEntId" : "supplyTopEntId";
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
+ criteria.and(topEnt).is(entId).and("status").nin(0, 2).and("delFlag").is(0);
|
|
|
+ if (StringUtils.isNotBlank(unit)) {
|
|
|
+ criteria.and("unit").is(unit);
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(start)) {
|
|
|
+ criteria.and("createTime").gte(start);
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(end)) {
|
|
|
+ criteria.and("createTime").lte(end);
|
|
|
+ }
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(
|
|
|
+ Aggregation.match(criteria),
|
|
|
+ Aggregation.group().sum("amount").as("orderAmount")
|
|
|
+ .sum("price").as("orderPrice")
|
|
|
+ .sum("actualAmount").as("orderActualAmount"));
|
|
|
+ AggregationResults<WorkbenchOrderStsDTO> aggregate =
|
|
|
+ mongoTemplate.aggregate(aggregation, SckwTradeOrder.class, WorkbenchOrderStsDTO.class);
|
|
|
+ List<WorkbenchOrderStsDTO> mappedResults = aggregate.getMappedResults();
|
|
|
+ if (CollectionUtils.isEmpty(mappedResults)) {
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+ return mappedResults.get(0);
|
|
|
+ }
|
|
|
+}
|