Files
nuttyreading-java/src/main/java/com/peanut/modules/book/controller/UserBookClockInController.java
2023-10-22 18:00:14 +08:00

138 lines
5.6 KiB
Java

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 org.springframework.web.bind.annotation.RestController;
import java.util.*;
/**
* @Description: 用户打卡 Controller
* @Author: Cauchy
* @CreateTime: 2023/10/11
*/
@Slf4j
@RestController
@RequestMapping("/book/userClockIn")
public class UserBookClockInController {
@Autowired
UserBookClockService userBookClockService;
/**
* 计算打卡天数
*
* @param bookId book ID
* @param userId user ID
* @return R
*/
@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);
Map<String, Object> result = new HashMap<>();
if (userBookClock == null) {
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>>() {
});
result.put("beginDate", beginDate);
result.put("currentDay", between + 1);
result.put("clockInDayList", clockInDayList);
return R.ok(result);
}
/**
* 用户打卡
*
* @param bookId book ID
* @param userId user ID
* @return R
*/
@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);
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);
return R.ok("打卡成功");
}
/**
* 用户补卡接口
*
* @param bookId book ID
* @param userId user ID
* @param day 补卡 day
* @return R
*/
@RequestMapping(value = "/correctClockIn", method = RequestMethod.GET)
public R correctClockIn(@RequestParam("bookId") Integer bookId,
@RequestParam("userId") Integer userId,
@RequestParam("day") Integer day) {
QueryWrapper<UserBookClockEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("book_id", bookId);
queryWrapper.eq("user_id", userId);
UserBookClockEntity userBookClock = userBookClockService.getOne(queryWrapper);
String clocksStr = userBookClock.getClocks();
List<Integer> clockInDaysList = JSON.parseObject(clocksStr, new TypeReference<List<Integer>>() {
});
clockInDaysList.add(day);
String clockInDaysListStr = JSON.toJSON(clockInDaysList).toString();
userBookClock.setClocks(clockInDaysListStr);
userBookClockService.updateById(userBookClock);
return R.ok("补卡成功");
}
}