This commit is contained in:
Cauchy
2023-10-12 13:33:54 +08:00
parent a356cceb62
commit 6bf8fd586c
13 changed files with 138 additions and 34 deletions

View File

@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
@@ -22,6 +23,7 @@ import java.util.*;
* @CreateTime: 2023/10/11
*/
@Slf4j
@RestController
@RequestMapping("/book/userClockIn")
public class UserBookClockController {
@Autowired
@@ -41,17 +43,23 @@ public class UserBookClockController {
queryWrapper.eq("book_id", bookId);
queryWrapper.eq("user_id", userId);
UserBookClockEntity userBookClock = userBookClockService.getOne(queryWrapper);
Map<String, Object> result = new HashMap<>();
if (userBookClock == null) {
return R.error("未获取到该用户的打卡信息");
result.put("beginDate", new Date());
result.put("currentDay", 1);
result.put("clockInDayList", new ArrayList<>());
return R.ok(result);
}
// 1. 获取打卡初始日期
Date beginDate = userBookClock.getBeginDate();
Date today = new Date();
long between = DateUtil.between(beginDate, today, DateUnit.DAY);
// 2. 获取打卡天数列表
String clocksStr = userBookClock.getClocks();
List<Integer> clockInDayList = JSON.parseObject(clocksStr, new TypeReference<List<Integer>>() {
});
Map<String, Object> result = new HashMap<>();
result.put("beginDate", beginDate);
result.put("currentDay", between + 1);
result.put("clockInDayList", clockInDayList);
return R.ok(result);
}
@@ -64,7 +72,8 @@ public class UserBookClockController {
* @return
*/
@RequestMapping(path = "/clockIn", method = RequestMethod.GET)
public R clockIn(@RequestParam("bookId") Integer bookId, @RequestParam("userId") Integer userId) {
public R clockIn(@RequestParam("bookId") Integer bookId,
@RequestParam("userId") Integer userId) {
// 查询是否有该用户打卡记录,如果没有,说明是第一天打卡
QueryWrapper<UserBookClockEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("book_id", bookId);
@@ -90,7 +99,10 @@ public class UserBookClockController {
Date beginDate = userBookClock.getBeginDate();
Date today = new Date();
long between = DateUtil.between(beginDate, today, DateUnit.DAY);
clockInDaysList.add((int) between);
if (clockInDaysList.contains((int) between + 1)) {
return R.error("今天打卡已完成,不可重复打卡!");
}
clockInDaysList.add((int) (between + 1));
String clockInDaysListStr = JSON.toJSON(clockInDaysList).toString();
userBookClock.setClocks(clockInDaysListStr);
userBookClockService.updateById(userBookClock);