更新开始时间

This commit is contained in:
xuelijun
2026-01-23 14:39:29 +08:00
parent 15b0fff86d
commit 96fa89f2a3
4 changed files with 65 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
package com.tem.bocai.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.tem.bocai.util.SQLiteDateConverter;
import jakarta.persistence.*;
import lombok.Data;
import net.sourceforge.tess4j.TesseractException;
@@ -44,11 +46,33 @@ public class LoginInfoResult {
/* @Column(name = "current_num", nullable = false)
private Integer currentNum;*/
@Column(name = "create_time", nullable = false, updatable = false)
/*@Column(name = "create_time", nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date createTime;
@Column(name = "update_time", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date updateTime;*/
/* @Column(name = "create_time", nullable = false, updatable = false)
@CreationTimestamp // Hibernate注解自动设置创建时间
private Date createTime;
@Column(name = "update_time", nullable = false)
@UpdateTimestamp // Hibernate注解自动更新时间
private Date updateTime;*/
@Column(name = "create_time", nullable = false, updatable = false)
@CreationTimestamp
@Convert(converter = SQLiteDateConverter.class)
private Date createTime;
@Column(name = "update_time", nullable = false)
@UpdateTimestamp
@Convert(converter = SQLiteDateConverter.class)
private Date updateTime;
@Column(name = "start_time")
@Convert(converter = SQLiteDateConverter.class)
private Date startTime;
}

View File

@@ -13,6 +13,7 @@ import org.springframework.stereotype.Service;
import net.sourceforge.tess4j.Tesseract;
import org.apache.http.client.methods.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -31,6 +32,7 @@ public class LoginServiceImpl implements LoginService {
private LoginInfoRepository loginInfoRepository;
private static final int MAX_CRA = 3;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public String loginAutomatic(LoginInfoParam loginInfoParam) {
String token = "";
@@ -70,7 +72,7 @@ public class LoginServiceImpl implements LoginService {
e.printStackTrace();
}
}
return "";
return "";
}
/* @Override
@@ -158,7 +160,6 @@ public class LoginServiceImpl implements LoginService {
public String saveUserInfo(LoginInfoResult loginInfoResult) {
try {
Optional<LoginInfoResult> existingUser = loginInfoRepository.findFirstByOrderByCreateTimeDesc();
Date now = new Date();
if (existingUser.isPresent()) {
// 获取数据库中已有的用户
@@ -182,18 +183,19 @@ public class LoginServiceImpl implements LoginService {
}
if (loginInfoResult.getOnOff() != null) {
dbUser.setOnOff(loginInfoResult.getOnOff());
if (loginInfoResult.getOnOff().equals(1)) {
dbUser.setStartTime(new Date());
}
}
// 更新修改时间
dbUser.setUpdateTime(now);
// 不需要手动设置updateTime@UpdateTimestamp会自动处理
// 保存更新后的实体
loginInfoRepository.save(dbUser);
} else {
// 新增逻辑
loginInfoResult.setCreateTime(now);
loginInfoResult.setUpdateTime(now);
// 不需要手动设置createTime和updateTime注解会自动处理
if (loginInfoResult.getOnOff() != null && loginInfoResult.getOnOff().equals(1)) {
loginInfoResult.setStartTime(new Date());
}
loginInfoRepository.save(loginInfoResult);
}

View File

@@ -0,0 +1,29 @@
package com.tem.bocai.util;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
// SQLite时间转换器
@Converter
public class SQLiteDateConverter implements AttributeConverter<Date, String> {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public String convertToDatabaseColumn(Date date) {
return date == null ? null : sdf.format(date);
}
@Override
public Date convertToEntityAttribute(String dateString) {
try {
return dateString == null ? null : sdf.parse(dateString);
} catch (ParseException e) {
return null;
}
}
}