Browse Source

feat(S-cfc-482): 实现 points.ledger.view 积分流水查看/对账,红单测全部转绿

Sisyphus 1 hour ago
parent
commit
fe32d96bfc

+ 3 - 1
cfc-backend/src/main/java/com/etotem/cfc/controller/family/PointsController.java

@@ -32,7 +32,9 @@ public class PointsController {
         Long memberId = params.get("memberId") != null ? ParamUtils.getLong(params.get("memberId")) : currentMemberId;
         Integer page = params.get("page") != null ? Integer.valueOf(params.get("page").toString()) : 1;
         Integer size = params.get("size") != null ? Integer.valueOf(params.get("size").toString()) : 10;
-        Page<PointsLog> logs = pointsService.getPointsLogs(memberId, page, size);
+        String startTime = params.get("startTime") != null ? params.get("startTime").toString() : null;
+        String endTime = params.get("endTime") != null ? params.get("endTime").toString() : null;
+        Page<PointsLog> logs = pointsService.getPointsLogs(memberId, page, size, startTime, endTime);
         return Result.success(logs);
     }
 

+ 7 - 0
cfc-backend/src/main/java/com/etotem/cfc/entity/PointsLog.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import lombok.Data;
 
@@ -34,6 +35,12 @@ private String category;
 
 private String description;
 
+/** 展示字段(不入库):来源可读名(SC-P-15 积分明细查看:task→完成任务 / wish|spend→心愿兑换,未映射回退原始码) */
+@TableField(exist = false)
+private String sourceName;
+
+/** 创建时间(ISO 8601 序列化,与项目时间显示规范一致) */
+@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
 private Date createdAt;
 
 // POINT-009: 积分过期日期(NULL表示永久有效)

+ 145 - 4
cfc-backend/src/main/java/com/etotem/cfc/service/PointsService.java

@@ -16,8 +16,13 @@ import javax.annotation.Resource;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import com.etotem.cfc.util.SortUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -69,10 +74,144 @@ public class PointsService implements PointsServiceInterface {
     }
 
     public Page<PointsLog> getPointsLogs(Long memberId, Integer page, Integer size) {
-        Page<PointsLog> pageParam = new Page<>(page, size);
-        return pointsLogMapper.selectPage(pageParam, new LambdaQueryWrapper<PointsLog>()
+        return getPointsLogs(memberId, page, size, null, null);
+    }
+
+    /**
+     * 积分流水查看(能力键 points.ledger.view / SC-P-15):分页 + 时间筛选 + 来源可读名。
+     *
+     * @param startTime 起始日期(yyyy-MM-dd,含当日 00:00:00)
+     * @param endTime   结束日期(yyyy-MM-dd,含当日 23:59:59.999)
+     */
+    public Page<PointsLog> getPointsLogs(Long memberId, Integer page, Integer size,
+                                         String startTime, String endTime) {
+        if (page == null || page < 1) page = 1;
+        if (size == null || size < 1) size = 10;
+        Date start = parseTimeBoundary(startTime, false);
+        Date end = parseTimeBoundary(endTime, true);
+
+        LambdaQueryWrapper<PointsLog> wrapper = new LambdaQueryWrapper<PointsLog>()
                 .eq(PointsLog::getMemberId, memberId)
-                .orderByDesc(PointsLog::getCreatedAt));
+                .orderByDesc(PointsLog::getCreatedAt);
+        if (start != null) wrapper.ge(PointsLog::getCreatedAt, start);
+        if (end != null) wrapper.le(PointsLog::getCreatedAt, end);
+
+        Page<PointsLog> result = pointsLogMapper.selectPage(new Page<>(page, size), wrapper);
+        if (result == null) {
+            result = new Page<>(page, size);
+        }
+        List<PointsLog> raw = result.getRecords() == null ? new ArrayList<PointsLog>() : result.getRecords();
+
+        // 内存兜底过滤:Mapper 可能未按时间条件下推(如 mock / 老实现),
+        // 且 total 必须是筛选后的真实条数(先分页后筛选会让前端 noMore 恒为真,只能显示一页)。
+        List<PointsLog> filtered = new ArrayList<PointsLog>();
+        boolean needsClientPaging = raw.size() > size;
+        for (PointsLog log : raw) {
+            Date createdAt = log.getCreatedAt();
+            if (start != null && (createdAt == null || createdAt.before(start))) {
+                needsClientPaging = true;
+                continue;
+            }
+            if (end != null && (createdAt == null || createdAt.after(end))) {
+                needsClientPaging = true;
+                continue;
+            }
+            filtered.add(log);
+        }
+        if (needsClientPaging) {
+            int total = filtered.size();
+            int from = Math.min((page - 1) * size, total);
+            int to = Math.min(from + size, total);
+            result.setTotal(total);
+            result.setRecords(new ArrayList<PointsLog>(filtered.subList(from, to)));
+        }
+        enrichSourceNames(result.getRecords());
+        return result;
+    }
+
+    /** 流水来源展示补齐:task→完成任务 / wish|spend→心愿兑换,未映射回退原始码(不抛错)。 */
+    private void enrichSourceNames(List<PointsLog> logs) {
+        if (logs == null) return;
+        for (PointsLog log : logs) {
+            log.setSourceName(resolveSourceName(log.getType(), log.getCategory()));
+        }
+    }
+
+    private String resolveSourceName(String type, String category) {
+        if ("task".equals(type) || "task".equals(category)) return "完成任务";
+        if ("wish".equals(type) || "spend".equals(type) || "wish".equals(category)) return "心愿兑换";
+        return type != null ? type : category;
+    }
+
+    /** 时间筛选参数解析:yyyy-MM-dd 按整日边界,带时间部分则按原时刻;非法/空值忽略(不抛错)。 */
+    private Date parseTimeBoundary(String value, boolean endOfDay) {
+        if (value == null || value.trim().isEmpty()) return null;
+        String v = value.trim();
+        try {
+            SimpleDateFormat sdf;
+            if (v.length() <= 10) {
+                sdf = new SimpleDateFormat("yyyy-MM-dd");
+            } else if (v.indexOf('T') > 0) {
+                sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+            } else {
+                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+            }
+            sdf.setLenient(false);
+            Date parsed = sdf.parse(v);
+            if (v.length() <= 10 && endOfDay) {
+                Calendar cal = Calendar.getInstance();
+                cal.setTime(parsed);
+                cal.set(Calendar.HOUR_OF_DAY, 23);
+                cal.set(Calendar.MINUTE, 59);
+                cal.set(Calendar.SECOND, 59);
+                cal.set(Calendar.MILLISECOND, 999);
+                return cal.getTime();
+            }
+            return parsed;
+        } catch (ParseException e) {
+            log.warn("积分流水时间筛选参数非法,已忽略: {}", value);
+            return null;
+        }
+    }
+
+    /**
+     * 积分流水对账(能力键 points.ledger.view / SC-P-16):
+     * 对比成员账面总额与流水代数和,不一致时报警,并统计删除/回退产生的负流水条数。
+     */
+    public Map<String, Object> reconcile(Long memberId) {
+        Map<String, Object> result = new HashMap<String, Object>();
+        FamilyMember member = familyMemberMapper.selectById(memberId);
+        int balance = member != null && member.getTotalPoints() != null ? member.getTotalPoints() : 0;
+
+        int logSum = 0;
+        try {
+            Integer sum = pointsLogMapper.selectSum(
+                    new LambdaQueryWrapper<PointsLog>().eq(PointsLog::getMemberId, memberId));
+            if (sum != null) logSum = sum;
+        } catch (Exception e) {
+            log.warn("积分流水合计查询失败 memberId={}", memberId, e);
+        }
+
+        int negativeCount = 0;
+        try {
+            Page<PointsLog> logs = pointsLogMapper.selectPage(new Page<PointsLog>(1, 10000),
+                    new LambdaQueryWrapper<PointsLog>().eq(PointsLog::getMemberId, memberId));
+            if (logs != null && logs.getRecords() != null) {
+                for (PointsLog logItem : logs.getRecords()) {
+                    if (logItem.getAmount() != null && logItem.getAmount() < 0) negativeCount++;
+                }
+            }
+        } catch (Exception e) {
+            log.warn("积分负流水统计失败 memberId={}", memberId, e);
+        }
+
+        boolean consistent = balance == logSum;
+        result.put("balance", balance);
+        result.put("logSum", logSum);
+        result.put("consistent", consistent);
+        result.put("alert", !consistent);
+        result.put("negativeCount", negativeCount);
+        return result;
     }
 
     /**
@@ -194,7 +333,9 @@ public class PointsService implements PointsServiceInterface {
         }
         wrapper.orderByDesc(PointsLog::getCreatedAt);
         SortUtil.applySort(wrapper);
-        return pointsLogMapper.selectPage(pageParam, wrapper);
+        Page<PointsLog> result = pointsLogMapper.selectPage(pageParam, wrapper);
+        enrichSourceNames(result != null ? result.getRecords() : null);
+        return result;
     }
 
     /**

+ 3 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/api/PointsServiceInterface.java

@@ -23,4 +23,7 @@ public interface PointsServiceInterface {
     int deductCfPoints(Long memberId, int amount, String reason);
     int getCfPointsBalance(Long memberId);
     Page<PointsLog> getPointsLogsByCategory(Long memberId, String category, Integer page, Integer size);
+
+    /** 积分流水对账(SC-P-16):balance/logSum/consistent/alert/negativeCount */
+    Map<String, Object> reconcile(Long memberId);
 }