clock in and comment finished waitting for test
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package com.peanut.modules.book.controller;
|
||||
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.peanut.common.utils.R;
|
||||
import com.peanut.modules.book.entity.UserBookClockEntity;
|
||||
import com.peanut.modules.book.service.UserBookClockService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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 java.util.*;
|
||||
|
||||
/**
|
||||
* @Description: 用户打卡 Controller
|
||||
* @Author: Cauchy
|
||||
* @CreateTime: 2023/10/11
|
||||
*/
|
||||
@Slf4j
|
||||
@RequestMapping("/book/userClockIn")
|
||||
public class UserBookClockController {
|
||||
@Autowired
|
||||
UserBookClockService userBookClockService;
|
||||
|
||||
/**
|
||||
* 计算打卡天数
|
||||
*
|
||||
* @param bookId
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(path = "/clockInDays", method = RequestMethod.GET)
|
||||
public R clockInDays(@RequestParam("bookId") Integer bookId,
|
||||
@RequestParam("userId") Integer userId) {
|
||||
QueryWrapper<UserBookClockEntity> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("book_id", bookId);
|
||||
queryWrapper.eq("user_id", userId);
|
||||
UserBookClockEntity userBookClock = userBookClockService.getOne(queryWrapper);
|
||||
if (userBookClock == null) {
|
||||
return R.error("未获取到该用户的打卡信息");
|
||||
}
|
||||
// 1. 获取打卡初始日期
|
||||
Date beginDate = userBookClock.getBeginDate();
|
||||
// 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("clockInDayList", clockInDayList);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户打卡
|
||||
*
|
||||
* @param bookId
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(path = "/clockIn", method = RequestMethod.GET)
|
||||
public R clockIn(@RequestParam("bookId") Integer bookId, @RequestParam("userId") Integer userId) {
|
||||
// 查询是否有该用户打卡记录,如果没有,说明是第一天打卡
|
||||
QueryWrapper<UserBookClockEntity> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("book_id", bookId);
|
||||
queryWrapper.eq("user_id", userId);
|
||||
UserBookClockEntity userBookClock = userBookClockService.getOne(queryWrapper);
|
||||
// 第一天打卡
|
||||
if (userBookClock == null) {
|
||||
UserBookClockEntity entity = new UserBookClockEntity();
|
||||
java.sql.Date beginDate = new java.sql.Date(new Date().getTime());
|
||||
List<Integer> clockInDaysList = new ArrayList<>();
|
||||
clockInDaysList.add(1);
|
||||
String clockInDaysListStr = JSON.toJSON(clockInDaysList).toString();
|
||||
entity.setBookId(bookId);
|
||||
entity.setUserId(userId);
|
||||
entity.setBeginDate(beginDate);
|
||||
entity.setClocks(clockInDaysListStr);
|
||||
userBookClockService.save(entity);
|
||||
return R.ok("打卡成功");
|
||||
}
|
||||
String clocksStr = userBookClock.getClocks();
|
||||
List<Integer> clockInDaysList = JSON.parseObject(clocksStr, new TypeReference<List<Integer>>() {
|
||||
});
|
||||
Date beginDate = userBookClock.getBeginDate();
|
||||
Date today = new Date();
|
||||
long between = DateUtil.between(beginDate, today, DateUnit.DAY);
|
||||
clockInDaysList.add((int) between);
|
||||
String clockInDaysListStr = JSON.toJSON(clockInDaysList).toString();
|
||||
userBookClock.setClocks(clockInDaysListStr);
|
||||
userBookClockService.updateById(userBookClock);
|
||||
return R.ok("打卡成功");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user