修复有重复数据

This commit is contained in:
wuchunlei
2025-01-10 09:52:22 +08:00
parent e34f7eab36
commit 6e1dfd832d

View File

@@ -13,6 +13,7 @@ import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* 听书进度表 * 听书进度表
@@ -54,25 +55,14 @@ public class BookListeningController {
public R save( BookListeningEntity booklistening){ public R save( BookListeningEntity booklistening){
Integer bookId = booklistening.getBookId(); Integer bookId = booklistening.getBookId();
Integer userId = booklistening.getUserId(); Integer userId = booklistening.getUserId();
List<BookListeningEntity> bls = bookListeningService.list(new QueryWrapper<BookListeningEntity>() List<Integer> bls = bookListeningService.list(new QueryWrapper<BookListeningEntity>()
.eq("book_id", bookId) .eq("book_id", bookId)
.eq("chapter_id", booklistening.getChapterId()) .eq("user_id", userId))
.eq("user_id", userId)); .stream().map(BookListeningEntity::getId).collect(Collectors.toList());
if (bls.size() > 0) { if (bls.size() > 0) {
Integer id = bls.get(0).getId(); bookListeningService.removeByIds(bls);
// 构建新的BookListeningEntity对象设置需要修改的字段
BookListeningEntity updatedEntity = new BookListeningEntity();
updatedEntity.setId(id);
updatedEntity.setPrecent(booklistening.getPrecent());
updatedEntity.setChapterId(booklistening.getChapterId());
updatedEntity.setChapterName(booklistening.getChapterName());
updatedEntity.setUpdateTime(new Date());
// 执行更新操作
bookListeningService.updateById(updatedEntity);
return R.ok().put("bookListeningId",bls.get(0).getId());
}else {
bookListeningService.save(booklistening);
} }
bookListeningService.save(booklistening);
return R.ok(); return R.ok();
} }
@@ -81,12 +71,14 @@ public class BookListeningController {
*/ */
@RequestMapping("/update") @RequestMapping("/update")
public R update(@RequestBody BookListeningEntity booklistening) { public R update(@RequestBody BookListeningEntity booklistening) {
BookListeningEntity one = bookListeningService.getOne(new QueryWrapper<BookListeningEntity>() List<Integer> blIds = bookListeningService.list(new QueryWrapper<BookListeningEntity>()
.eq("book_id", booklistening.getBookId()) .eq("book_id", booklistening.getBookId())
.eq("user_id", booklistening.getUserId())); .eq("user_id", booklistening.getUserId()))
if (one != null) { .stream().map(BookListeningEntity::getId).collect(Collectors.toList());
Integer id = one.getId(); if (blIds.size() > 1) {
bookListeningService.removeByIds(blIds);
}else if (blIds.size() > 0) {
Integer id = blIds.get(0);
// 构建新的BookListeningEntity对象设置需要修改的字段 // 构建新的BookListeningEntity对象设置需要修改的字段
BookListeningEntity updatedEntity = new BookListeningEntity(); BookListeningEntity updatedEntity = new BookListeningEntity();
updatedEntity.setId(id); updatedEntity.setId(id);
@@ -94,12 +86,11 @@ public class BookListeningController {
updatedEntity.setChapterId(booklistening.getChapterId()); updatedEntity.setChapterId(booklistening.getChapterId());
updatedEntity.setChapterName(booklistening.getChapterName()); updatedEntity.setChapterName(booklistening.getChapterName());
updatedEntity.setUpdateTime(new Date()); updatedEntity.setUpdateTime(new Date());
// 执行更新操作 // 执行更新操作
bookListeningService.updateById(updatedEntity); bookListeningService.updateById(updatedEntity);
return R.ok();
} }
bookListeningService.save(booklistening);
return R.ok(); return R.ok();
} }
/** /**