From dcb96821f905f3fd93c059d8703517bdcac4d336 Mon Sep 17 00:00:00 2001
From: 刘帅 <rr.iii@qq.com>
Date: 星期二, 29 九月 2026 20:02:02 +0800
Subject: [PATCH] 排序问题及门店手机号展示问题

---
 cloud-server-activity/src/main/java/com/dsh/activity/controller/PointMercharsController.java |  184 +++++++++++++++++++++++++++++++--------------
 1 files changed, 127 insertions(+), 57 deletions(-)

diff --git a/cloud-server-activity/src/main/java/com/dsh/activity/controller/PointMercharsController.java b/cloud-server-activity/src/main/java/com/dsh/activity/controller/PointMercharsController.java
index f251986..63ef586 100644
--- a/cloud-server-activity/src/main/java/com/dsh/activity/controller/PointMercharsController.java
+++ b/cloud-server-activity/src/main/java/com/dsh/activity/controller/PointMercharsController.java
@@ -31,6 +31,7 @@
 
 import javax.annotation.Resource;
 import javax.xml.crypto.Data;
+import java.math.BigDecimal;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.LocalDate;
@@ -124,13 +125,37 @@
 
     @RequestMapping("/base/pointMerchars/writeOff")
     public Object writeOff(@RequestBody WriteOffDTO dto) {
-        UserPointsMerchandise byId = userPointsMerchandiseService.getById(dto.getId());
-        byId.setStoreId(dto.getVerifiStoreId());
-        byId.setStatus(2);
-        byId.setVerificationTime(new Date());
-        byId.setUserId(null);
-        byId.setVerificationUserId(dto.getVerificationUserId());
-        return userPointsMerchandiseService.updateById(byId);
+        if (dto == null || dto.getId() == null) {
+            throw new IllegalArgumentException("门票记录不能为空");
+        }
+        if (!Integer.valueOf(1).equals(dto.getGoType()) && !Integer.valueOf(2).equals(dto.getGoType())) {
+            throw new IllegalArgumentException("入园方式参数不正确");
+        }
+
+        UserPointsMerchandise ticket = userPointsMerchandiseService.getById(dto.getId());
+        if (ticket == null) {
+            throw new IllegalArgumentException("门票记录不存在");
+        }
+        if (!Integer.valueOf(1).equals(ticket.getStatus())) {
+            throw new IllegalStateException("该门票已核销");
+        }
+
+        // 以待核销状态作为更新条件,避免并发请求重复核销同一张门票。
+        UserPointsMerchandise updatedTicket = new UserPointsMerchandise();
+        updatedTicket.setId(ticket.getId());
+        updatedTicket.setVerifiStoreId(dto.getVerifiStoreId());
+        updatedTicket.setStatus(2);
+        updatedTicket.setVerificationTime(new Date());
+        updatedTicket.setVerificationUserId(dto.getVerificationUserId());
+        updatedTicket.setGoType(dto.getGoType());
+        boolean updated = userPointsMerchandiseService.update(updatedTicket,
+                new LambdaQueryWrapper<UserPointsMerchandise>()
+                        .eq(UserPointsMerchandise::getId, ticket.getId())
+                        .eq(UserPointsMerchandise::getStatus, 1));
+        if (!updated) {
+            throw new IllegalStateException("该门票已核销");
+        }
+        return true;
     }
 
     @Autowired
@@ -284,8 +309,8 @@
      */
     @ResponseBody
     @RequestMapping("/base/pointMerchars/getAmount")
-    public List<PointMerchandiseIncomeVo> getAmount() {
-        return userPointsMerchandiseService.getAmount();
+    public List<PointMerchandiseIncomeVo> getAmount(@RequestBody List<Integer> storeIds) {
+        return userPointsMerchandiseService.getAmount(storeIds);
     }
 
 
@@ -421,6 +446,7 @@
                 .le("startTime", new Date())
                 .ge("endTime", new Date())
                 .eq("status", 1)
+                .orderByDesc("insertTime", "id")
         );
         if (request.getShopId()!=null){
             List<Integer> storeId = new ArrayList<>();
@@ -439,7 +465,9 @@
                 List<PointsMerchandise> pointsMerchandiseList = pmdsService.lambdaQuery()
                         .eq(PointsMerchandise::getState, 1)
                         .eq(PointsMerchandise::getShelves,1)
-                        .in(PointsMerchandise::getId, pointsMerchandiseIds).list();
+                        .in(PointsMerchandise::getId, pointsMerchandiseIds)
+                        .orderByDesc(PointsMerchandise::getInsertTime, PointsMerchandise::getId)
+                        .list();
 
                 list = pointsMerchandiseList;
             }
@@ -869,7 +897,8 @@
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
         List<UserPointsMerchandise> pointsMerchandises = upmseService.list(new QueryWrapper<UserPointsMerchandise>()
-                .eq("userId", appUserId).eq("payStatus", 2));
+                .eq("userId", appUserId).eq("payStatus", 2)
+                .orderByDesc("insertTime", "id"));
         List<PointsMerchandiseStore> pointsMerchandiseStoreList = pointsMerchandiseStoreService.list();
         Map<Integer, List<PointsMerchandiseStore>> pointsMerchandiseStoreMap = pointsMerchandiseStoreList.stream().collect(Collectors.groupingBy(PointsMerchandiseStore::getPointsMerchandiseId));
 
@@ -883,7 +912,8 @@
         List<UserCoupon> userCoupons = new ArrayList<>();
         if (ids.size() > 0) {
             userCoupons = userCouponService.list(new QueryWrapper<UserCoupon>().eq("userId", appUserId).in("couponId", ids)
-                    .ne("status", 3));
+                    .ne("status", 3)
+                    .orderByDesc("insertTime", "id"));
         }
         if (pointsMerchandises.size() > 0) {
             for (UserPointsMerchandise pointsMerchandise : pointsMerchandises) {
@@ -1117,7 +1147,10 @@
 
         // 2.0新增会员权益赠送门票
         List<VipDetail> list = vipDetailService.lambdaQuery()
-                .eq(VipDetail::getAppUserId, appUserId).eq(VipDetail::getType, 2).list();
+                .eq(VipDetail::getAppUserId, appUserId)
+                .eq(VipDetail::getType, 2)
+                .orderByDesc(VipDetail::getInsertTime, VipDetail::getId)
+                .list();
         for (VipDetail vipDetail : list) {
             ExchangeDetailsResponse detailsResponse = new ExchangeDetailsResponse();
             // 添加-用于区分会员权益门票
@@ -1172,11 +1205,11 @@
                 }
                 responses.add(detailsResponse);
             }
-            Comparator<ExchangeDetailsResponse> comparator = Comparator.comparing(ExchangeDetailsResponse::getStartTime).reversed();
-            Collections.sort(responses, comparator);
-            detailsVo.setDetailsResponses(responses);
-
         }
+        responses.sort(Comparator.comparing(ExchangeDetailsResponse::getStartTime,
+                Comparator.nullsLast(Comparator.reverseOrder()))
+                .thenComparing(ExchangeDetailsResponse::getDetailsId, Comparator.reverseOrder()));
+        detailsVo.setDetailsResponses(responses);
         return detailsVo;
     }
 
@@ -1779,6 +1812,7 @@
                 map.put("id", pointsMerchandise.getId());
                 map.put("userId", pointsMerchandise.getUserId());
                 map.put("status", pointsMerchandise.getStatus());
+                map.put("goType", pointsMerchandise.getGoType());
                 map.put("payTime", sdf.format(pointsMerchandise.getPaymentTime()));
                 map.put("orderNumber", pointsMerchandise.getOrderNumber());
                 mapList.add(map);
@@ -2147,64 +2181,100 @@
     @PostMapping("/base/pointMerchars/queryFee")
     @ResponseBody
     public HashMap<String, Object> queryFee(@RequestBody QueryDataFee queryDataFee) {
+        return queryTicketFee(queryDataFee, false);
+    }
+
+    /**
+     * 按购买记录的 storeId 统计门票收入。购买表按 userId 分片,未带 userId 条件时
+     * ShardingSphere 会读取各分表,确保 t_user_points_merchandise1-5 的记录都参与统计。
+     */
+    @PostMapping("/base/pointMerchars/queryTicketFee")
+    @ResponseBody
+    public HashMap<String, Object> queryTicketFee(@RequestBody QueryDataFee queryDataFee) {
+        return queryTicketFee(queryDataFee, true);
+    }
+
+    private HashMap<String, Object> queryTicketFee(QueryDataFee queryDataFee, boolean scopeByStore) {
         HashMap<String, Object> map = new HashMap<>();
         String data = queryDataFee.getData();
-        List<Integer> ids = queryDataFee.getIds();
-        if (ids.size() == 0) {
-            ids.add(-1);
-        } else {
-
-        }
-        List<PointsMerchandise> list1 = pmdsService.list(new LambdaQueryWrapper<PointsMerchandise>().eq(PointsMerchandise::getRedemptionMethod, 3));
+        List<Integer> ids = queryDataFee.getIds() == null
+                ? new ArrayList<>() : new ArrayList<>(queryDataFee.getIds());
+        // cardType 是门票卡种的唯一分类依据:1 日卡、2 月卡、3 季卡、4 年卡。
+        // 不能额外依赖历史商品的 type/redemptionMethod,避免有效门票记录被遗漏。
+        List<PointsMerchandise> tickets = pmdsService.list(new LambdaQueryWrapper<PointsMerchandise>()
+                .in(PointsMerchandise::getCardType, 1, 2, 3, 4));
         LambdaQueryWrapper<UserPointsMerchandise> vipPaymentLambdaQueryWrapper = new LambdaQueryWrapper<>();
         if (ToolUtil.isNotEmpty(data)) {
             String stime = data.split(" - ")[0] + " 00:00:00";
             String etime = data.split(" - ")[1] + " 23:59:59";
-            vipPaymentLambdaQueryWrapper.between(UserPointsMerchandise::getInsertTime, stime, etime);
+            // 实收以支付时间归属;没有支付时间的历史数据才回退到创建时间。
+            vipPaymentLambdaQueryWrapper.and(wrapper -> wrapper
+                    .between(UserPointsMerchandise::getPaymentTime, stime, etime)
+                    .or(item -> item.isNull(UserPointsMerchandise::getPaymentTime)
+                            .between(UserPointsMerchandise::getInsertTime, stime, etime)));
         }
-        vipPaymentLambdaQueryWrapper.in(UserPointsMerchandise::getUserId, ids);
-        List<Integer> collect = list1.stream().map(PointsMerchandise::getId).collect(Collectors.toList());
+        if (!ids.isEmpty()) {
+            if (scopeByStore) {
+                // 门票记录通常写入 storeId;全国/城市通用等历史记录可能未写该字段,
+                // 此时按商品配置的门店关系归属到运营商/门店统计范围。
+                List<Integer> scopedTicketIds = pmdstoService.list(new LambdaQueryWrapper<PointsMerchandiseStore>()
+                                .in(PointsMerchandiseStore::getStoreId, ids))
+                        .stream().map(PointsMerchandiseStore::getPointsMerchandiseId)
+                        .filter(Objects::nonNull).distinct().collect(Collectors.toList());
+                vipPaymentLambdaQueryWrapper.and(wrapper -> {
+                    wrapper.in(UserPointsMerchandise::getStoreId, ids);
+                    if (!scopedTicketIds.isEmpty()) {
+                        wrapper.or().in(UserPointsMerchandise::getPointsMerchandiseId, scopedTicketIds);
+                    }
+                });
+            } else {
+                vipPaymentLambdaQueryWrapper.in(UserPointsMerchandise::getUserId, ids);
+            }
+        }
+        List<Integer> collect = tickets.stream().map(PointsMerchandise::getId).collect(Collectors.toList());
         if (collect.size() == 0) {
             collect.add(-1);
         }
         vipPaymentLambdaQueryWrapper.in(UserPointsMerchandise::getPointsMerchandiseId, collect);
+        // payStatus=2 才表示支付成功;payType 是兑换/支付方式,不能固定为 2,
+        // 否则会把其它已支付门票记录错误排除。
         vipPaymentLambdaQueryWrapper.eq(UserPointsMerchandise::getPayStatus, 2);
-        ArrayList<Integer> objects = new ArrayList<>();
+        vipPaymentLambdaQueryWrapper.and(wrapper -> wrapper.isNull(UserPointsMerchandise::getState)
+                .or().eq(UserPointsMerchandise::getState, 1));
         List<UserPointsMerchandise> list = upmseService.list(vipPaymentLambdaQueryWrapper);
 
-        double a = 0;
-        double d = 0;
-        double m = 0;
-        double j = 0;
-        double y = 0;
+        Map<Integer, PointsMerchandise> ticketMap = tickets.stream()
+                .collect(Collectors.toMap(PointsMerchandise::getId, ticket -> ticket, (first, ignored) -> first));
+        BigDecimal all = BigDecimal.ZERO;
+        BigDecimal day = BigDecimal.ZERO;
+        BigDecimal month = BigDecimal.ZERO;
+        BigDecimal quarter = BigDecimal.ZERO;
+        BigDecimal year = BigDecimal.ZERO;
         for (UserPointsMerchandise userPointsMerchandise : list) {
-            for (PointsMerchandise pointsMerchandise : list1) {
-                if (pointsMerchandise.getId() == userPointsMerchandise.getPointsMerchandiseId()) {
-                    a += pointsMerchandise.getCash().doubleValue();
-                }
-                if (pointsMerchandise.getType() == 3) {
-                    if (pointsMerchandise.getCardType() == 1) {
-                        d += pointsMerchandise.getCash().doubleValue();
-                    }
-                    if (pointsMerchandise.getCardType() == 2) {
-                        m += pointsMerchandise.getCash().doubleValue();
-                    }
-                    if (pointsMerchandise.getCardType() == 3) {
-                        j += pointsMerchandise.getCash().doubleValue();
-                    }
-                    if (pointsMerchandise.getCardType() == 4) {
-                        y += pointsMerchandise.getCash().doubleValue();
-                    }
-                }
+            PointsMerchandise ticket = ticketMap.get(userPointsMerchandise.getPointsMerchandiseId());
+            if (ticket == null || userPointsMerchandise.getCash() == null) {
+                continue;
             }
-
+            // 以 t_user_points_merchandise1-5 中实际支付的 cash 为准,
+            // 不使用商品当前价格,避免商品改价影响历史营收。
+            BigDecimal ticketAmount = userPointsMerchandise.getCash();
+            all = all.add(ticketAmount);
+            if (Integer.valueOf(1).equals(ticket.getCardType())) {
+                day = day.add(ticketAmount);
+            } else if (Integer.valueOf(2).equals(ticket.getCardType())) {
+                month = month.add(ticketAmount);
+            } else if (Integer.valueOf(3).equals(ticket.getCardType())) {
+                quarter = quarter.add(ticketAmount);
+            } else if (Integer.valueOf(4).equals(ticket.getCardType())) {
+                year = year.add(ticketAmount);
+            }
         }
 
-        map.put("all", a);
-        map.put("day", d);
-        map.put("month", m);
-        map.put("quarter", j);
-        map.put("year", y);
+        map.put("all", all);
+        map.put("day", day);
+        map.put("month", month);
+        map.put("quarter", quarter);
+        map.put("year", year);
         return map;
 
 

--
Gitblit v1.7.1