签到需求相关接口
This commit is contained in:
parent
4184193ee6
commit
d6355e37ed
|
|
@ -1,35 +1,47 @@
|
|||
package com.sqx.modules.userSign.controller;
|
||||
|
||||
|
||||
import com.sqx.modules.userSign.entity.UserSignRecord;
|
||||
import com.sqx.modules.userSign.service.UserSignRecordService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.sqx.common.utils.DateUtils;
|
||||
import com.sqx.common.utils.Result;
|
||||
import com.sqx.modules.sys.controller.AbstractController;
|
||||
import com.sqx.modules.userSign.dto.UserSignDTO;
|
||||
import com.sqx.modules.userSign.service.UserSignRecordService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@Api(value = "用户签到", tags = {"用户签到"})
|
||||
@RequestMapping(value = "/userSignRecord")
|
||||
public class UserSignRecordController {
|
||||
public class UserSignRecordController extends AbstractController {
|
||||
/**
|
||||
* 服务对象
|
||||
*/
|
||||
@Autowired
|
||||
private UserSignRecordService userSignRecordService;
|
||||
|
||||
// @GetMapping("/selectUserSignRecord")
|
||||
// @ApiOperation("查询用户签到表")
|
||||
// public Result selectUserSignRecord(Integer page, Integer limit,@RequestAttribute("userId") Long userId) {
|
||||
// return Result.success().put("data", userSignRecordService.page(new Page<>(page, limit), new QueryWrapper<UserSignRecord>().orderByAsc("create_time")));
|
||||
// }
|
||||
/**
|
||||
* 获取用户连续签到数据
|
||||
*/
|
||||
@GetMapping("/getUserSignData")
|
||||
@ApiOperation("获取用户连续签到数据")
|
||||
public Result getUserSignData() {
|
||||
UserSignDTO data = userSignRecordService.getUserSignData();
|
||||
return Result.success().put("data", data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连续签到奖励配置
|
||||
*/
|
||||
@GetMapping("/getUserSignAwardConfig")
|
||||
@ApiOperation(value = "获取连续签到奖励配置", notes = "如:[7,7] = 连续签到7天奖励7元")
|
||||
public Result getUserSignAwardConfig() {
|
||||
String[] data = userSignRecordService.getUserSignAwardConfig();
|
||||
return Result.success().put("data", data);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package com.sqx.modules.userSign.dto;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户签到数据
|
||||
*
|
||||
* @author tankaikai
|
||||
* @since 2024-12-18 17:34
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(value = "用户签到数据")
|
||||
public class UserSignDTO implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@ApiModelProperty(value = "用户id")
|
||||
private Long userId;
|
||||
|
||||
@ApiModelProperty(value = "用户名")
|
||||
private String username;
|
||||
|
||||
@ApiModelProperty(value = "手机号")
|
||||
private String mobile;
|
||||
|
||||
@ApiModelProperty(value = "连续签到天数")
|
||||
private Integer signDays;
|
||||
|
||||
@ApiModelProperty(value = "该用户能否参加签到活动", example = "1-允许,0-不允许")
|
||||
private Integer enable;
|
||||
|
||||
@ApiModelProperty(value = "连续签到记录")
|
||||
private List<UserSignRecordDTO> recordList;
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package com.sqx.modules.userSign.dto;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户连续签到记录
|
||||
*
|
||||
* @author tankaikai
|
||||
* @since 2024-12-18 17:34
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(value = "用户连续签到记录")
|
||||
public class UserSignRecordDTO implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@ApiModelProperty(value = "签到日期", required = true, example = "2024-12-18")
|
||||
private String signDay;
|
||||
|
||||
@ApiModelProperty(value = "签到状态", required = true, example = "0-未签到, 1-已签到")
|
||||
private String status;
|
||||
|
||||
@ApiModelProperty(value = "展示文本", required = true, example = "未签到, 待签到, 已签到, 第x天")
|
||||
private String showText;
|
||||
|
||||
@ApiModelProperty(value = "签到时间", example = "2024-12-18 17:34:00")
|
||||
private String signDate;
|
||||
|
||||
}
|
||||
|
|
@ -1,11 +1,14 @@
|
|||
package com.sqx.modules.userSign.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.sqx.modules.userSign.dto.UserSignDTO;
|
||||
import com.sqx.modules.userSign.entity.UserSignRecord;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface UserSignRecordService extends IService<UserSignRecord> {
|
||||
|
||||
UserSignDTO getUserSignData();
|
||||
|
||||
String[] getUserSignAwardConfig();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,142 @@
|
|||
package com.sqx.modules.userSign.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.sqx.common.exception.SqxException;
|
||||
import com.sqx.modules.common.dao.CommonInfoDao;
|
||||
import com.sqx.modules.common.entity.CommonInfo;
|
||||
import com.sqx.modules.sys.entity.SysUserEntity;
|
||||
import com.sqx.modules.userSign.dao.UserSignRecordDao;
|
||||
import com.sqx.modules.userSign.dto.UserSignDTO;
|
||||
import com.sqx.modules.userSign.dto.UserSignRecordDTO;
|
||||
import com.sqx.modules.userSign.entity.UserSignRecord;
|
||||
import com.sqx.modules.userSign.service.UserSignRecordService;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class UserSignRecordServiceImpl extends ServiceImpl<UserSignRecordDao, UserSignRecord> implements UserSignRecordService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private CommonInfoDao commonInfoDao;
|
||||
|
||||
@Override
|
||||
public UserSignDTO getUserSignData() {
|
||||
SysUserEntity currentUser = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
|
||||
UserSignDTO dto = new UserSignDTO();
|
||||
dto.setUserId(currentUser.getUserId());
|
||||
dto.setUsername(currentUser.getUsername());
|
||||
dto.setMobile(currentUser.getMobile());
|
||||
dto.setSignDays(0);
|
||||
dto.setEnable(1);
|
||||
CommonInfo config = commonInfoDao.findOne(918);
|
||||
if (config == null) {
|
||||
throw new SqxException("签到活动配置不存在");
|
||||
}
|
||||
// 活动天数
|
||||
int activeDays = Convert.toInt(config.getValue().split(",")[0]);
|
||||
// 当前日期
|
||||
LocalDate beginDay = LocalDate.now();
|
||||
// 签到记录
|
||||
List<UserSignRecordDTO> recordList = new ArrayList<>();
|
||||
// 连续签到日期
|
||||
List<String> flowDays = buildFlowDays(beginDay, activeDays);
|
||||
// 实际签到记录
|
||||
List<UserSignRecord> list = baseMapper.selectList(Wrappers.<UserSignRecord>lambdaQuery().eq(UserSignRecord::getUserId, currentUser.getUserId()).orderByAsc(UserSignRecord::getCreateTime));
|
||||
// 第x天
|
||||
int index = 1;
|
||||
// 连续签到天数
|
||||
int signDays = 0;
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
for (String day : flowDays) {
|
||||
UserSignRecordDTO record = new UserSignRecordDTO();
|
||||
record.setSignDay(day);
|
||||
record.setStatus("0");
|
||||
record.setShowText(StrUtil.format("第{}天", index));
|
||||
record.setSignDate("");
|
||||
recordList.add(record);
|
||||
index++;
|
||||
}
|
||||
dto.setRecordList(recordList);
|
||||
dto.setSignDays(signDays);
|
||||
return dto;
|
||||
}
|
||||
String beginSignDay = list.stream().findFirst().get().getSignDay();
|
||||
flowDays = buildFlowDays(LocalDate.parse(beginSignDay, DateTimeFormatter.ofPattern("yyyy-MM-dd")), activeDays);
|
||||
index = 1;
|
||||
Map<String, Date> signMap = list.stream().collect(Collectors.toMap(UserSignRecord::getSignDay, UserSignRecord::getCreateTime));
|
||||
for (String day : flowDays) {
|
||||
Date date = signMap.get(day);
|
||||
UserSignRecordDTO record = new UserSignRecordDTO();
|
||||
if (date != null) {
|
||||
String signDay = DateUtil.formatDateTime(signMap.get(day));
|
||||
record.setSignDay(day);
|
||||
record.setStatus("1");
|
||||
record.setSignDate(signDay);
|
||||
record.setShowText("已签到");
|
||||
signDays++;
|
||||
} else {
|
||||
record.setSignDay(day);
|
||||
record.setStatus("0");
|
||||
record.setSignDate("");
|
||||
LocalDate thisDay = LocalDate.parse(day);
|
||||
LocalDate currentDay = LocalDate.now();
|
||||
long daysBetween = ChronoUnit.DAYS.between(thisDay, currentDay);
|
||||
if (daysBetween > 0) {
|
||||
signDays = 0;
|
||||
record.setShowText("未签到");
|
||||
} else if (daysBetween == 0) {
|
||||
record.setShowText("待签到");
|
||||
} else {
|
||||
record.setShowText(StrUtil.format("第{}天", index));
|
||||
}
|
||||
}
|
||||
recordList.add(record);
|
||||
index++;
|
||||
}
|
||||
dto.setRecordList(recordList);
|
||||
dto.setSignDays(signDays);
|
||||
// 该用户是否可以继续签到
|
||||
UserSignRecordDTO last = recordList.get(recordList.size() - 1);
|
||||
LocalDate lastDay = LocalDate.parse(last.getSignDay());
|
||||
LocalDate currentDay = LocalDate.now();
|
||||
long daysBetween = ChronoUnit.DAYS.between(currentDay, lastDay);
|
||||
if (daysBetween < 0 || "1".equals(last.getStatus())) {
|
||||
dto.setEnable(0);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getUserSignAwardConfig() {
|
||||
CommonInfo config = commonInfoDao.findOne(918);
|
||||
if (config == null) {
|
||||
throw new SqxException("签到活动配置不存在");
|
||||
}
|
||||
return config.getValue().split(",");
|
||||
}
|
||||
|
||||
private List<String> buildFlowDays(LocalDate beginDay, int activeDays) {
|
||||
List<String> flowDays = new ArrayList<>();
|
||||
for (int i = 0; i < activeDays; i++) {
|
||||
flowDays.add(beginDay.plusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
||||
}
|
||||
return flowDays;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue