package com.tcm.training.admin.app.points.service;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.tcm.training.admin.app.auth.service.AppAuthSessionService;
|
import com.tcm.training.admin.app.points.vo.AppPointsLedgerItemVO;
|
import com.tcm.training.admin.common.PageResponse;
|
import com.tcm.training.admin.points.row.PointsLedgerRow;
|
import com.tcm.training.admin.points.service.PointsLedgerAppService;
|
import com.tcm.training.admin.points.mapper.PointsLedgerMapper;
|
import org.springframework.stereotype.Service;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* APP 积分明细服务。
|
*/
|
@Service
|
public class AppPointsLedgerService {
|
|
private final AppAuthSessionService appAuthSessionService;
|
private final PointsLedgerMapper pointsLedgerMapper;
|
private final PointsLedgerAppService pointsLedgerAppService;
|
|
/**
|
* 注入 APP 积分明细所需依赖。
|
*/
|
public AppPointsLedgerService(AppAuthSessionService appAuthSessionService,
|
PointsLedgerMapper pointsLedgerMapper,
|
PointsLedgerAppService pointsLedgerAppService) {
|
this.appAuthSessionService = appAuthSessionService;
|
this.pointsLedgerMapper = pointsLedgerMapper;
|
this.pointsLedgerAppService = pointsLedgerAppService;
|
}
|
|
/**
|
* 分页查询当前登录用户的积分明细。
|
*/
|
public PageResponse<AppPointsLedgerItemVO> page(int page, int pageSize, String incomeExpenseType) {
|
Long userId = appAuthSessionService.requireLoginUserId();
|
int normalizedPage = page <= 0 ? 1 : page;
|
int normalizedPageSize = pageSize <= 0 ? 20 : pageSize;
|
pointsLedgerAppService.syncLedger();
|
IPage<PointsLedgerRow> result = pointsLedgerMapper.selectAppLedgerPage(
|
new Page<PointsLedgerRow>(normalizedPage, normalizedPageSize),
|
userId,
|
trimToNull(incomeExpenseType)
|
);
|
return PageResponse.of(
|
toItems(result.getRecords()),
|
result.getTotal(),
|
result.getCurrent(),
|
result.getSize()
|
);
|
}
|
|
/**
|
* 将积分流水行转换为 APP 页面需要的列表结构。
|
*/
|
private List<AppPointsLedgerItemVO> toItems(List<PointsLedgerRow> rows) {
|
List<AppPointsLedgerItemVO> list = new ArrayList<AppPointsLedgerItemVO>();
|
if (rows == null) {
|
return list;
|
}
|
for (PointsLedgerRow row : rows) {
|
AppPointsLedgerItemVO item = new AppPointsLedgerItemVO();
|
item.setId(row.getId());
|
item.setTitle(buildTitle(row));
|
item.setIncomeExpenseType(trimToEmpty(row.getIncomeExpenseType()));
|
item.setPointsDelta(row.getPointsDelta() == null ? 0 : row.getPointsDelta());
|
item.setOperatedAt(row.getOperatedAt());
|
list.add(item);
|
}
|
return list;
|
}
|
|
/**
|
* 构建 APP 积分明细标题,保持与静态稿一致的业务文案。
|
*/
|
private String buildTitle(PointsLedgerRow row) {
|
String contentName = trimToEmpty(row == null ? null : row.getContentName());
|
if (contentName.isEmpty()) {
|
contentName = "-";
|
}
|
String businessType = trimToEmpty(row == null ? null : row.getBusinessType());
|
if (PointsLedgerAppService.BUSINESS_LIVE_WATCH.equals(businessType)) {
|
return "观看课程【" + contentName + "】";
|
}
|
if (PointsLedgerAppService.BUSINESS_POINTS_EXCHANGE.equals(businessType)) {
|
return "兑换礼品【" + contentName + "】";
|
}
|
if (PointsLedgerAppService.BUSINESS_POINTS_SETTLEMENT.equals(businessType)) {
|
return "活动结束积分清零【" + contentName + "】";
|
}
|
return "积分变动【" + contentName + "】";
|
}
|
|
/**
|
* 将可空文本归一化为空字符串。
|
*/
|
private String trimToEmpty(String value) {
|
if (value == null) {
|
return "";
|
}
|
return value.trim();
|
}
|
|
/**
|
* 将空白筛选值归一化为 null,便于 MyBatis 条件判断。
|
*/
|
private String trimToNull(String value) {
|
String text = trimToEmpty(value);
|
return text.isEmpty() ? null : text;
|
}
|
}
|