master
冯凯 2023-11-05 09:46:53 +08:00
parent 575fcaba2d
commit c8cd2aeaa7
6 changed files with 191 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package com.health.system.common.domain;
import lombok.Data;
import java.util.Date;
/**
* @author
* @version 1.0
* @description:
* @date 2023/11/5 9:08
*/
@Data
public class Sign {
/*
id
*/
private Integer signId;
/*
id
*/
private Long userId;
/*
*/
private Date signTime;
/*
*/
private Integer signAmount;
}

View File

@ -0,0 +1,42 @@
package com.health.system.server.controller;
import com.health.common.core.domain.Result;
import com.health.common.security.utils.SecurityUtils;
import com.health.system.common.domain.Sign;
import com.health.system.server.service.SysSignService;
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;
/**
* @author
* @version 1.0
* @description:
* @date 2023/11/5 9:01
*/
@RestController
@RequestMapping("/sign")
public class SysSignController {
@Autowired
private SysSignService sysSignService;
@GetMapping("/search/sign")
public Result searchSignByUserId(){
//获取当前登录人userId
Long userId = SecurityUtils.getUserId();
Sign sign= sysSignService.searchSignByUserId(userId);
return Result.success(sign);
}
@PostMapping("/sign/byUserId")
public Result signByUserId(){
Long userId = SecurityUtils.getUserId();
sysSignService.signByUserId(userId);
return Result.success("","签到成功");
}
}

View File

@ -0,0 +1,20 @@
package com.health.system.server.mapper;
import com.health.system.common.domain.Sign;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @author
* @version 1.0
* @description:
* @date 2023/11/5 9:03
*/
@Mapper
public interface SysSignMapper {
Sign searchSignByUserId(@Param("userId") Long userId);
void updateSingAmountByUserId(Sign sign);
void signByUserId(Sign sign);
}

View File

@ -0,0 +1,15 @@
package com.health.system.server.service;
import com.health.system.common.domain.Sign;
/**
* @author
* @version 1.0
* @description:
* @date 2023/11/5 9:02
*/
public interface SysSignService {
Sign searchSignByUserId(Long userId);
void signByUserId(Long userId);
}

View File

@ -0,0 +1,61 @@
package com.health.system.server.service.impl;
import com.health.common.redis.service.RedisService;
import com.health.system.common.domain.Sign;
import com.health.system.server.mapper.SysSignMapper;
import com.health.system.server.service.SysSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* @author
* @version 1.0
* @description:
* @date 2023/11/5 9:02
*/
@Service
public class SysSignServiceImpl implements SysSignService {
@Autowired
private SysSignMapper sysSignMapper;
@Autowired
private RedisService redisService;
@Override
public Sign searchSignByUserId(Long userId) {
//先从redis中获取连续签到次数
if (redisService.hasKey("sign:"+userId)){
Sign redisSign = redisService.getCacheObject("sign:" + userId);
return redisSign;
}
//redis没有从数据库中获取
//查询出当前登录人的最近一条的签到记录
Sign sign = sysSignMapper.searchSignByUserId(userId);
//获取当天的日期 是第几日
int today = new Date().getDay();
//获取最近一次签到的日期 是第几日
int recentDay = sign.getSignTime().getDay();
//判断是否已经断签了
if (today-recentDay>1){
sign.setSignAmount(0);
//其次将签到次数signAmount改为0
sysSignMapper.updateSingAmountByUserId(sign);
redisService.deleteObject("sign:"+userId);
redisService.setCacheObject("sign:"+userId,sign);
return sign;
}
//没有断签 直接返回当前查询出来的签到记录
return sign;
}
@Override
public void signByUserId(Long userId) {
Sign sign = this.searchSignByUserId(userId);
sign.setSignAmount(sign.getSignAmount()+1);
sysSignMapper.signByUserId(sign);
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.health.system.server.mapper.SysSignMapper">
<insert id="signByUserId">
insert into tb_sign
(user_id,sign_time,sign_amount)
values
(#{userId},now(),#{signAmount})
</insert>
<update id="updateSingAmountByUserId">
update tb_sign set sing_amount=0 where user_id=#{userId} and sign_time=#{signTime}
</update>
<select id="searchSignByUserId" resultType="com.health.system.common.domain.Sign">
SELECT * from tb_sign where user_id=#{userId} ORDER BY sign_time DESC LIMIT 1
</select>
</mapper>