更新开始时间
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package com.tem.bocai.entity;
|
package com.tem.bocai.entity;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.tem.bocai.util.SQLiteDateConverter;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import net.sourceforge.tess4j.TesseractException;
|
import net.sourceforge.tess4j.TesseractException;
|
||||||
@@ -44,11 +46,33 @@ public class LoginInfoResult {
|
|||||||
|
|
||||||
/* @Column(name = "current_num", nullable = false)
|
/* @Column(name = "current_num", nullable = false)
|
||||||
private Integer currentNum;*/
|
private Integer currentNum;*/
|
||||||
@Column(name = "create_time", nullable = false, updatable = false)
|
/*@Column(name = "create_time", nullable = false, updatable = false)
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
private Date createTime;
|
private Date createTime;
|
||||||
|
|
||||||
@Column(name = "update_time", nullable = false)
|
@Column(name = "update_time", nullable = false)
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@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;
|
private Date updateTime;
|
||||||
|
|
||||||
|
@Column(name = "start_time")
|
||||||
|
@Convert(converter = SQLiteDateConverter.class)
|
||||||
|
private Date startTime;
|
||||||
}
|
}
|
||||||
@@ -13,6 +13,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import net.sourceforge.tess4j.Tesseract;
|
import net.sourceforge.tess4j.Tesseract;
|
||||||
import org.apache.http.client.methods.*;
|
import org.apache.http.client.methods.*;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -31,6 +32,7 @@ public class LoginServiceImpl implements LoginService {
|
|||||||
private LoginInfoRepository loginInfoRepository;
|
private LoginInfoRepository loginInfoRepository;
|
||||||
private static final int MAX_CRA = 3;
|
private static final int MAX_CRA = 3;
|
||||||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String loginAutomatic(LoginInfoParam loginInfoParam) {
|
public String loginAutomatic(LoginInfoParam loginInfoParam) {
|
||||||
String token = "";
|
String token = "";
|
||||||
@@ -158,7 +160,6 @@ public class LoginServiceImpl implements LoginService {
|
|||||||
public String saveUserInfo(LoginInfoResult loginInfoResult) {
|
public String saveUserInfo(LoginInfoResult loginInfoResult) {
|
||||||
try {
|
try {
|
||||||
Optional<LoginInfoResult> existingUser = loginInfoRepository.findFirstByOrderByCreateTimeDesc();
|
Optional<LoginInfoResult> existingUser = loginInfoRepository.findFirstByOrderByCreateTimeDesc();
|
||||||
Date now = new Date();
|
|
||||||
|
|
||||||
if (existingUser.isPresent()) {
|
if (existingUser.isPresent()) {
|
||||||
// 获取数据库中已有的用户
|
// 获取数据库中已有的用户
|
||||||
@@ -182,18 +183,19 @@ public class LoginServiceImpl implements LoginService {
|
|||||||
}
|
}
|
||||||
if (loginInfoResult.getOnOff() != null) {
|
if (loginInfoResult.getOnOff() != null) {
|
||||||
dbUser.setOnOff(loginInfoResult.getOnOff());
|
dbUser.setOnOff(loginInfoResult.getOnOff());
|
||||||
|
if (loginInfoResult.getOnOff().equals(1)) {
|
||||||
|
dbUser.setStartTime(new Date());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// 更新修改时间
|
// 不需要手动设置updateTime,@UpdateTimestamp会自动处理
|
||||||
dbUser.setUpdateTime(now);
|
|
||||||
|
|
||||||
// 保存更新后的实体
|
// 保存更新后的实体
|
||||||
loginInfoRepository.save(dbUser);
|
loginInfoRepository.save(dbUser);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// 新增逻辑
|
// 新增逻辑
|
||||||
loginInfoResult.setCreateTime(now);
|
// 不需要手动设置createTime和updateTime,注解会自动处理
|
||||||
loginInfoResult.setUpdateTime(now);
|
if (loginInfoResult.getOnOff() != null && loginInfoResult.getOnOff().equals(1)) {
|
||||||
|
loginInfoResult.setStartTime(new Date());
|
||||||
|
}
|
||||||
loginInfoRepository.save(loginInfoResult);
|
loginInfoRepository.save(loginInfoResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
29
src/main/java/com/tem/bocai/util/SQLiteDateConverter.java
Normal file
29
src/main/java/com/tem/bocai/util/SQLiteDateConverter.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user