|
@@ -16,8 +16,13 @@ import javax.annotation.Resource;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
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.Date;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import com.etotem.cfc.util.SortUtil;
|
|
import com.etotem.cfc.util.SortUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -69,10 +74,144 @@ public class PointsService implements PointsServiceInterface {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Page<PointsLog> getPointsLogs(Long memberId, Integer page, Integer size) {
|
|
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)
|
|
.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);
|
|
wrapper.orderByDesc(PointsLog::getCreatedAt);
|
|
|
SortUtil.applySort(wrapper);
|
|
SortUtil.applySort(wrapper);
|
|
|
- return pointsLogMapper.selectPage(pageParam, wrapper);
|
|
|
|
|
|
|
+ Page<PointsLog> result = pointsLogMapper.selectPage(pageParam, wrapper);
|
|
|
|
|
+ enrichSourceNames(result != null ? result.getRecords() : null);
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|