简介
使用redis中的bitMap实现 功能
1. 用户签到, 可以补签
2. 用户当前签到情况
3. 用户当月签到情况
SignController
import com.example.signdemo.service.SignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("sign")
public class SignController {
@Autowired
private SignService signService;
/***
* 用户签到, 可以补签
*/
@PostMapping
public Map<String, Object> doSign(Integer userId, String dateStr){
return signService.doSign(userId, dateStr);
}
/**
* 获取用户当前签到情况
*
* @param userId
* @param dateStr
* @return
*/
@GetMapping("/today")
public Map<String, Object> today(Integer userId, String dateStr){
return signService.today(userId, dateStr);
}
/**
* 获取用户当月签到情况
*
* @param userId
* @param dateStr
* @return
*/
@GetMapping("/getSignInfo")
public Map<String, Object> getSignInfo(Integer userId, String dateStr){
return signService.getSignInfo(userId, dateStr);
}
}
import cn.hutool.core.date.DateUtil;
import com.example.signdemo.utils.SignUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
@Service
public class SignService {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private SignUtils signUtils;
public Map<String, Object> doSign(Integer userId, String dateStr) {
Map<String, Object> result = new HashMap<>();
//获取日期
Date date = signUtils.getDate(dateStr);
// 获取日期对应的天数, 多少号
int day = DateUtil.dayOfMonth(date) - 1;
// 构建redis ke
String signKey = signUtils.buildSignKey(userId, date);
// 获取对应的日期是否已签到
Boolean isSigned = redisTemplate.opsForValue().getBit(signKey, day);
if (isSigned) {
result.put("code", 400);
result.put("message", "用户该日期已签到, 无需再签");
return result;
}
// 签到
redisTemplate.opsForValue().setBit(signKey, day, true);
// 根据当前日期统计签到次数
Date todate = new Date();
// 统计连续签到
int contniuousSignCount = signUtils.getContniuousSignCount(userId, todate);
// 统计总签到数
long sumSignCount = signUtils.getSumSignCount(userId, todate);
result.put("code", 200);
result.put("message", "签到成功");
result.put("contniuousSignCount", contniuousSignCount);
result.put("sumSignCount", sumSignCount);
return result;
}
/**
* 获取用户当前签到情况
*
* @param userId
* @param dateStr
* @return
*/
public Map<String, Object> today(Integer userId, String dateStr) {
Map<String, Object> result = new HashMap<>();
// TODO
return result;
}
/**
* 获取用户当月签到情况
*
* @param userId
* @param dateStr
* @return
*/
public Map<String, Object> getSignInfo(Integer userId, String dateStr) {
Map<String, Object> signInfo = new TreeMap<>();
// TODO
return signInfo;
}
}
发送内容: "签到功能", 获取提取码