TRX 1 жил өмнө
parent
commit
58dc1e894e

+ 4 - 4
FullCardClient/src/main/java/com/zhongshu/card/client/model/payment/ExpenseFlowModel.java

@@ -65,11 +65,11 @@ public class ExpenseFlowModel extends SuperModel {
     @Schema(description = "消费类型,如:餐饮消费")
     private String payType;
 
-    @Schema(description = "年份,如2024")
-    private String year;
+    @Schema(description = "年份,如: 2024")
+    private Integer year;
 
-    @Schema(description = "月份,如06")
-    private String month;
+    @Schema(description = "月份,如: 6")
+    private Integer month;
 
     @Schema(description = "消费订单号")
     private String orderNo;

+ 13 - 15
FullCardClient/src/main/java/com/zhongshu/card/client/model/payment/ExpenseFlowSearch.java

@@ -41,24 +41,19 @@ public class ExpenseFlowSearch extends SuperSearch {
     private String payType;
 
     @Schema(description = "年份,如:2024")
-    private String year;
+    private Integer year;
 
-    @Schema(description = "月份,如06")
-    private String month;
+    @Schema(description = "月份,如 6")
+    private Integer month;
 
-    @Schema(description = "第几周")
-    private String week;
+    @Schema(description = "第几周,如:32")
+    private Integer week;
 
-    public String getMonth() {
-        if (StringUtils.isNotEmpty(month)) {
-            if (month.length() == 2) {
-                return month;
-            } else {
-                return String.format("0%s", month);
-            }
-        }
-        return month;
-    }
+    @Schema(description = "当前月的第几天")
+    private Integer dayOfMonth;
+
+    @Schema(description = "当前年的第几天")
+    private Integer dayOfYear;
 
     @Schema(description = "消费订单号")
     private String orderNo;
@@ -104,4 +99,7 @@ public class ExpenseFlowSearch extends SuperSearch {
     @Schema(description = "统计类型,按年、月、周", hidden = true)
     private StatisticType statisticType;
 
+    @Schema(description = "group 字段名称", hidden = true)
+    private String fieldName = "";
+
 }

+ 2 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/payment/statistic/StatisticItem.java

@@ -2,6 +2,7 @@ package com.zhongshu.card.client.model.payment.statistic;
 
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
+import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
@@ -14,6 +15,7 @@ import java.math.BigDecimal;
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
+@Builder
 public class StatisticItem {
 
     @Schema(description = "统计项")

+ 5 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/service/payment/ExpenseFlowService.java

@@ -4,6 +4,8 @@ import com.zhongshu.card.client.model.payment.ExpenseFlowCount;
 import com.zhongshu.card.client.model.payment.ExpenseFlowModel;
 import com.zhongshu.card.client.model.payment.ExpenseFlowSearch;
 import com.zhongshu.card.client.model.payment.ExpenseRefundParam;
+import com.zhongshu.card.client.model.payment.statistic.BusinessMainStatisticModel;
+import com.zhongshu.card.client.model.payment.statistic.StatisticSearch;
 import com.zhongshu.card.client.ret.ResultContent;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
@@ -30,4 +32,7 @@ public interface ExpenseFlowService {
     // 商户统计
     ResultContent<ExpenseFlowCount> countBuinessMainPayment(ExpenseFlowSearch param);
 
+    // 商户小程序首页统计
+    ResultContent<BusinessMainStatisticModel> businessMainPageStatistic(StatisticSearch statisticSearch);
+
 }

+ 8 - 4
FullCardClient/src/main/java/com/zhongshu/card/client/utils/type/StatisticType.java

@@ -6,15 +6,19 @@ import lombok.Getter;
  * 统计类型
  */
 public enum StatisticType {
-    Year("年"),
-    Month("月"),
-    Week("周"),
+    Year("年", "year"),
+    Month("月", "month"),
+    Week("周", "week"),
     ;
 
     @Getter
     private String remark;
 
-    StatisticType(String remark) {
+    @Getter
+    private String fieldName;
+
+    StatisticType(String remark, String fieldName) {
         this.remark = remark;
+        this.fieldName = fieldName;
     }
 }

+ 10 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/controller/payment/ExpenseFlowController.java

@@ -8,6 +8,8 @@ import com.zhongshu.card.client.model.payment.ExpenseFlowCount;
 import com.zhongshu.card.client.model.payment.ExpenseFlowModel;
 import com.zhongshu.card.client.model.payment.ExpenseFlowSearch;
 import com.zhongshu.card.client.model.payment.ExpenseRefundParam;
+import com.zhongshu.card.client.model.payment.statistic.BusinessMainStatisticModel;
+import com.zhongshu.card.client.model.payment.statistic.StatisticSearch;
 import com.zhongshu.card.client.model.school.BookInfoAddParam;
 import com.zhongshu.card.client.model.school.BookInfoModel;
 import com.zhongshu.card.client.model.school.BookInfoSearch;
@@ -99,6 +101,14 @@ public class ExpenseFlowController {
         return expenseFlowService.countBuinessMainPayment(param);
     }
 
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "商户小程序首页统计", description = "商户小程序首页统计")
+    @RequestMapping(value = {"businessMainPageStatistic"}, method = {RequestMethod.POST})
+    public ResultContent<BusinessMainStatisticModel> businessMainPageStatistic(@RequestBody StatisticSearch param) {
+        return expenseFlowService.businessMainPageStatistic(param);
+    }
+
+
     //------------------------商户订单相关功能 end----------------------------
 
 }

+ 7 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/dao/payment/extend/ExpenseFlowDaoExtend.java

@@ -2,12 +2,17 @@ package com.zhongshu.card.server.core.dao.payment.extend;
 
 import com.zhongshu.card.client.model.payment.ExpenseFlowCount;
 import com.zhongshu.card.client.model.payment.ExpenseFlowSearch;
+import com.zhongshu.card.client.model.payment.statistic.StatisticItem;
 import com.zhongshu.card.client.model.school.BookInfoSearch;
 import com.zhongshu.card.server.core.domain.payment.ExpenseFlow;
 import com.zhongshu.card.server.core.domain.school.BookInfo;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 
+import java.math.BigDecimal;
+import java.util.List;
+import java.util.Map;
+
 /**
  * @Author TRX
  * @CreateDate: 2023/7/7
@@ -45,5 +50,7 @@ public interface ExpenseFlowDaoExtend {
 
     ExpenseFlow init(String mqttDataId, String token);
 
+    // 分组统计
+    Map<String, BigDecimal> statisticItems(ExpenseFlowSearch param);
 
 }

+ 51 - 10
FullCardServer/src/main/java/com/zhongshu/card/server/core/dao/payment/impl/ExpenseFlowDaoImpl.java

@@ -3,6 +3,7 @@ package com.zhongshu.card.server.core.dao.payment.impl;
 import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
 import com.zhongshu.card.client.model.payment.ExpenseFlowCount;
 import com.zhongshu.card.client.model.payment.ExpenseFlowSearch;
+import com.zhongshu.card.client.model.payment.statistic.StatisticItem;
 import com.zhongshu.card.client.model.school.BookInfoSearch;
 import com.zhongshu.card.server.core.dao.BaseImpl;
 import com.zhongshu.card.server.core.dao.payment.extend.ExpenseFlowDaoExtend;
@@ -29,6 +30,7 @@ import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.regex.Pattern;
@@ -56,19 +58,26 @@ public class ExpenseFlowDaoImpl extends BaseImpl implements ExpenseFlowDaoExtend
         }
 
         // 年
-        if (StringUtils.isNotEmpty(param.getYear())) {
+        if (param.getYear() != null) {
             criteria.and("year").is(param.getYear());
         }
         // 月
-        if (StringUtils.isNotEmpty(param.getMonth())) {
-            criteria.and("month").is(CommonUtil.turnMonthSearch(param.getMonth()));
+        if (param.getMonth() != null) {
+            criteria.and("month").is(param.getMonth());
         }
-
         // 周
-        if (StringUtils.isNotEmpty(param.getWeek())) {
+        if (param.getWeek() != null) {
             criteria.and("week").is(param.getWeek());
         }
 
+        if (param.getDayOfMonth() != null) {
+            criteria.and("dayOfMonth").is(param.getDayOfMonth());
+        }
+
+        if (param.getDayOfYear() != null) {
+            criteria.and("dayOfYear").is(param.getDayOfYear());
+        }
+
         // 是否支付成功
         if (param.getIsPaySuccess() != null) {
             criteria.and("isPaySuccess").is(param.getIsPaySuccess());
@@ -98,11 +107,6 @@ public class ExpenseFlowDaoImpl extends BaseImpl implements ExpenseFlowDaoExtend
             criteria.and("shopOids").in(param.getShopOids());
         }
 
-        // 是否支付成功
-        if (param.getIsPaySuccess() != null) {
-            criteria.and("isPaySuccess").is(param.getIsPaySuccess());
-        }
-
         // 退款状态
         if (param.getRefundState() != null) {
             criteria.and("refundState").is(param.getRefundState());
@@ -188,6 +192,43 @@ public class ExpenseFlowDaoImpl extends BaseImpl implements ExpenseFlowDaoExtend
         return expenseFlowCount;
     }
 
+    /**
+     * 分组统计
+     *
+     * @param param
+     * @return
+     */
+    public Map<String, BigDecimal> statisticItems(ExpenseFlowSearch param) {
+        Map<String, BigDecimal> map = new HashMap<>();
+        Criteria criteria = buildSearcCriteria(param);
+
+        String fieldName = param.getFieldName();
+
+        List<AggregationOperation> operations = new ArrayList<>(3);
+        // 条件
+        AggregationOperation match = Aggregation.match(criteria);
+        operations.add(match);
+
+        // 分组
+        AggregationOperation group = Aggregation.group("$" + fieldName).sum("$payAmount").as("count");
+        operations.add(group);
+
+        // 排序
+        AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, fieldName);
+        operations.add(sort);
+
+        Aggregation groupAggregation = Aggregation.newAggregation(operations);
+        List<Map> countObjs = mongoTemplate.aggregate(groupAggregation, ExpenseFlow.class, Map.class).getMappedResults();
+        if (countObjs.size() > 0) {
+            countObjs.stream().forEach(it -> {
+                String name = (String) it.get(fieldName);
+                BigDecimal amount = CommonUtil.getBigDecimalByObj(it.get("count"));
+                map.put(name, amount);
+            });
+        }
+        return map;
+    }
+
     /**
      * 商户收入总数
      *

+ 11 - 5
FullCardServer/src/main/java/com/zhongshu/card/server/core/domain/payment/ExpenseFlow.java

@@ -82,13 +82,19 @@ public class ExpenseFlow extends SuperMain {
     private String payType;
 
     @Schema(description = "年份,如:2024")
-    private String year;
+    private Integer year;
 
-    @Schema(description = "月份,如06")
-    private String month;
+    @Schema(description = "月份,如6")
+    private Integer month;
 
-    @Schema(description = "第几周")
-    private String week;
+    @Schema(description = "第几周,如:32")
+    private Integer week;
+
+    @Schema(description = "当前月的第几天")
+    private Integer dayOfMonth;
+
+    @Schema(description = "当前年的第几天")
+    private Integer dayOfYear;
 
     @Schema(description = "消费订单号")
     private String orderNo;

+ 57 - 6
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/payment/ExpenseFlowServiceImpl.java

@@ -10,6 +10,7 @@ import com.github.microservice.models.type.PaymentDeviceType;
 import com.github.microservice.models.type.PaymentType;
 import com.zhongshu.card.client.model.payment.*;
 import com.zhongshu.card.client.model.payment.statistic.BusinessMainStatisticModel;
+import com.zhongshu.card.client.model.payment.statistic.StatisticItem;
 import com.zhongshu.card.client.model.payment.statistic.StatisticSearch;
 import com.zhongshu.card.client.ret.ResultContent;
 import com.zhongshu.card.client.ret.ResultMessage;
@@ -50,7 +51,9 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 /**
@@ -113,9 +116,11 @@ public class ExpenseFlowServiceImpl extends SuperService implements ExpenseFlowS
     public ResultContent<ExpenseFlow> createExpenseFlowByHxz(ConsumTransactionsModel iotParam, ExpenseFlow expenseFlow) {
         expenseFlow.setMqttDataId(iotParam.getMqttDataId());
 
-        expenseFlow.setYear(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyy));
-        expenseFlow.setMonth(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternMM));
-        expenseFlow.setWeek(DateUtils.getCurrentWeekInYear() + "");
+        expenseFlow.setYear(DateUtils.getCurrentYear());
+        expenseFlow.setMonth(DateUtils.getCurrentMonthInYear());
+        expenseFlow.setWeek(DateUtils.getCurrentWeekInYear());
+        expenseFlow.setDayOfMonth(DateUtils.getCurrentDayInMonth());
+        expenseFlow.setDayOfYear(DateUtils.getCurrentDayInYear());
         expenseFlow.setPaymentTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyySSS));
 
         // 消费订单号
@@ -211,8 +216,11 @@ public class ExpenseFlowServiceImpl extends SuperService implements ExpenseFlowS
         expenseFlow.setOrderFromType(OrderFromType.QrCode);
         expenseFlow.setUserId(userId);
 
-        expenseFlow.setYear(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyy));
-        expenseFlow.setMonth(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternMM));
+        expenseFlow.setYear(DateUtils.getCurrentYear());
+        expenseFlow.setMonth(DateUtils.getCurrentMonthInYear());
+        expenseFlow.setWeek(DateUtils.getCurrentWeekInYear());
+        expenseFlow.setDayOfMonth(DateUtils.getCurrentDayInMonth());
+        expenseFlow.setDayOfYear(DateUtils.getCurrentDayInYear());
         expenseFlow.setPaymentTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyySSS));
 
         // 消费订单号
@@ -584,14 +592,57 @@ public class ExpenseFlowServiceImpl extends SuperService implements ExpenseFlowS
      * @param statisticSearch
      * @return
      */
-    public ResultContent businessMainPageStatistic(StatisticSearch statisticSearch) {
+    public ResultContent<BusinessMainStatisticModel> businessMainPageStatistic(StatisticSearch statisticSearch) {
         BusinessMainStatisticModel statisticModel = new BusinessMainStatisticModel();
 
         ExpenseFlowSearch param = new ExpenseFlowSearch();
         param.setIsPaySuccess(Boolean.TRUE);
         BeanUtils.copyProperties(statisticSearch, param);
+        List<String> shopOids = getSearchAllShopOid(param);
+        if (ObjectUtils.isEmpty(shopOids)) {
+            return ResultContent.buildFail("请选择查询商户或所有的");
+        }
+        param.setShopOids(shopOids);
+
+        param.setYear(DateUtils.getCurrentYear());
         // 统计模型,按 月、周
         StatisticType statisticType = param.getStatisticType();
+        if (statisticType != null) {
+            param.setFieldName(statisticType.getFieldName());
+        }
+
+        List<StatisticItem> items = new ArrayList<>();
+        // 统计项
+        Map<String, BigDecimal> map = expenseFlowDao.statisticItems(param);
+        if (statisticType != null) {
+            // 构建item
+            if (statisticType == StatisticType.Month) {
+                // 按月
+                int month = DateUtils.getCurrentMonthInYear();
+                for (int i = 0; i <= month; i++) {
+                    String key = String.valueOf(i);
+                    BigDecimal value = BigDecimal.ZERO;
+                    if (map.containsKey(key)) {
+                        value = map.get(key);
+                    }
+                    StatisticItem item = StatisticItem.builder().name(key).amount(value).build();
+                    items.add(item);
+                }
+            } else if (statisticType == StatisticType.Week) {
+                // 按月
+                int week = DateUtils.getCurrentWeekInYear();
+                for (int i = 0; i <= week; i++) {
+                    String key = String.valueOf(i);
+                    BigDecimal value = BigDecimal.ZERO;
+                    if (map.containsKey(key)) {
+                        value = map.get(key);
+                    }
+                    StatisticItem item = StatisticItem.builder().name(key).amount(value).build();
+                    items.add(item);
+                }
+            }
+        }
+        statisticModel.setItems(items);
 
 
         return ResultContent.buildSuccess(statisticModel);

+ 1 - 1
FullCardServer/src/main/java/com/zhongshu/card/server/core/test/Test.java

@@ -39,7 +39,7 @@ public class Test {
         BigDecimal b2 = BigDecimal.valueOf(54.32);
         System.out.println("m= " + b1.divide(b2, 5, RoundingMode.HALF_UP));
 
-        System.out.println("weekDays: " + DateUtils.getCurrentYear());
+        System.out.println("weekDays: " + DateUtils.getCurrentDayInYear());
 
         GroovyShell groovyShell = new GroovyShell();
         String res = " println 'Hello, Groovy!' ";

+ 20 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/util/DateUtils.java

@@ -221,6 +221,26 @@ public class DateUtils {
         return date.getYear();
     }
 
+    /**
+     * 在月份的第几天
+     *
+     * @return
+     */
+    public static int getCurrentDayInMonth() {
+        LocalDate date = LocalDate.now();
+        return date.getDayOfMonth();
+    }
+
+    /**
+     * 在年的第几天
+     *
+     * @return
+     */
+    public static int getCurrentDayInYear() {
+        LocalDate date = LocalDate.now();
+        return date.getDayOfYear();
+    }
+
     /**
      * 获得本小时的开始时间
      *