fix: Donate历史

This commit is contained in:
Tim
2025-10-17 11:35:29 +08:00
parent 8643446d8b
commit d914579d64
8 changed files with 72 additions and 13 deletions

View File

@@ -29,4 +29,5 @@ public class PostChangeLogDto {
private LocalDateTime newPinnedAt; private LocalDateTime newPinnedAt;
private Boolean oldFeatured; private Boolean oldFeatured;
private Boolean newFeatured; private Boolean newFeatured;
private Integer amount;
} }

View File

@@ -52,6 +52,8 @@ public class PostChangeLogMapper {
} else if (log instanceof PostFeaturedChangeLog f) { } else if (log instanceof PostFeaturedChangeLog f) {
dto.setOldFeatured(f.isOldFeatured()); dto.setOldFeatured(f.isOldFeatured());
dto.setNewFeatured(f.isNewFeatured()); dto.setNewFeatured(f.isNewFeatured());
} else if (log instanceof PostDonateChangeLog d) {
dto.setAmount(d.getAmount());
} }
return dto; return dto;
} }

View File

@@ -10,4 +10,5 @@ public enum PostChangeType {
FEATURED, FEATURED,
VOTE_RESULT, VOTE_RESULT,
LOTTERY_RESULT, LOTTERY_RESULT,
DONATE,
} }

View File

@@ -0,0 +1,19 @@
package com.openisle.model;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "post_donate_change_logs")
public class PostDonateChangeLog extends PostChangeLog {
@Column(nullable = false)
private int amount;
}

View File

@@ -25,6 +25,7 @@ public class PointService {
private final CommentRepository commentRepository; private final CommentRepository commentRepository;
private final PointHistoryRepository pointHistoryRepository; private final PointHistoryRepository pointHistoryRepository;
private final NotificationService notificationService; private final NotificationService notificationService;
private final PostChangeLogService postChangeLogService;
public int awardForPost(String userName, Long postId) { public int awardForPost(String userName, Long postId) {
User user = userRepository.findByUsername(userName).orElseThrow(); User user = userRepository.findByUsername(userName).orElseThrow();
@@ -304,6 +305,7 @@ public class PointService {
null, null,
String.valueOf(amount) String.valueOf(amount)
); );
postChangeLogService.recordDonation(post, donor, amount);
DonationResponse response = buildDonationResponse(post); DonationResponse response = buildDonationResponse(post);
response.setBalance(donor.getPoint()); response.setBalance(donor.getPoint());
return response; return response;
@@ -322,19 +324,38 @@ public class PointService {
); );
List<DonationDto> donations = histories List<DonationDto> donations = histories
.stream() .stream()
.map(history -> { .collect(Collectors.collectingAndThen(Collectors.toMap(
DonationDto dto = new DonationDto(); history -> {
User donor = history.getFromUser(); User donor = history.getFromUser();
if (donor != null) { if (donor != null && donor.getId() != null) {
dto.setUserId(donor.getId()); return "user:" + donor.getId();
dto.setUsername(donor.getUsername()); }
dto.setAvatar(donor.getAvatar()); return "history:" + history.getId();
} },
dto.setAmount(history.getAmount()); history -> {
dto.setCreatedAt(history.getCreatedAt()); DonationDto dto = new DonationDto();
return dto; User donor = history.getFromUser();
}) if (donor != null) {
.collect(Collectors.toList()); dto.setUserId(donor.getId());
dto.setUsername(donor.getUsername());
dto.setAvatar(donor.getAvatar());
}
dto.setAmount(history.getAmount());
dto.setCreatedAt(history.getCreatedAt());
return dto;
},
(left, right) -> {
left.setAmount(left.getAmount() + right.getAmount());
if (
left.getCreatedAt() == null ||
(right.getCreatedAt() != null && right.getCreatedAt().isAfter(left.getCreatedAt()))
) {
left.setCreatedAt(right.getCreatedAt());
}
return left;
},
java.util.LinkedHashMap::new
), map -> new java.util.ArrayList<>(map.values())));
Long total = pointHistoryRepository.sumAmountByPostAndType( Long total = pointHistoryRepository.sumAmountByPostAndType(
post, post,
PointHistoryType.DONATE_RECEIVED PointHistoryType.DONATE_RECEIVED

View File

@@ -115,6 +115,15 @@ public class PostChangeLogService {
logRepository.save(log); logRepository.save(log);
} }
public void recordDonation(Post post, User donor, int amount) {
PostDonateChangeLog log = new PostDonateChangeLog();
log.setPost(post);
log.setUser(donor);
log.setType(PostChangeType.DONATE);
log.setAmount(amount);
logRepository.save(log);
}
public void deleteLogsForPost(Post post) { public void deleteLogsForPost(Post post) {
logRepository.deleteByPost(post); logRepository.deleteByPost(post);
} }

View File

@@ -42,6 +42,9 @@
<span v-else-if="log.type === 'LOTTERY_RESULT'" class="change-log-content" <span v-else-if="log.type === 'LOTTERY_RESULT'" class="change-log-content"
>系统已精密计算抽奖结果 (=゚ω゚)</span >系统已精密计算抽奖结果 (=゚ω゚)</span
> >
<span v-else-if="log.type === 'DONATE'" class="change-log-content"
>为文章打赏了 {{ log.amount ?? 0 }} 积分</span
>
</div> </div>
<div class="change-log-time">{{ log.time }}</div> <div class="change-log-time">{{ log.time }}</div>
<div <div

View File

@@ -412,6 +412,8 @@ const changeLogIcon = (l) => {
return 'check-one' return 'check-one'
} else if (l.type === 'LOTTERY_RESULT') { } else if (l.type === 'LOTTERY_RESULT') {
return 'gift' return 'gift'
} else if (l.type === 'DONATE') {
return 'financing'
} else { } else {
return 'info' return 'info'
} }
@@ -436,6 +438,7 @@ const mapChangeLog = (l) => ({
newCategory: l.newCategory, newCategory: l.newCategory,
oldTags: l.oldTags, oldTags: l.oldTags,
newTags: l.newTags, newTags: l.newTags,
amount: l.amount,
icon: changeLogIcon(l), icon: changeLogIcon(l),
}) })