fix: 申报可用时间按东八区判断,写入时显式使用北京时间
避免容器 JVM/库会话仍为 UTC 时 createTime 偏 8 小时,以及 usable 查询把刚开放的申报滤掉。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -30,8 +30,15 @@ import java.util.TimeZone;
|
|||||||
@EnableWebSocket
|
@EnableWebSocket
|
||||||
public class WebSoftApplication {
|
public class WebSoftApplication {
|
||||||
|
|
||||||
|
static {
|
||||||
|
// 必须在任何 LocalDateTime.now() / JDBC 初始化之前生效。
|
||||||
|
// 注意:1Panel 启动命令里 -Duser.timezone 必须写在 -jar 前面,写在后面会被当成程序参数忽略。
|
||||||
|
System.setProperty("user.timezone", "Asia/Shanghai");
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Docker 默认 UTC,未设置 TZ 时 LocalDateTime.now() 会比北京时间慢 8 小时
|
System.setProperty("user.timezone", "Asia/Shanghai");
|
||||||
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
|
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
|
||||||
SpringApplication.run(WebSoftApplication.class, args);
|
SpringApplication.run(WebSoftApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,6 +32,7 @@ import java.util.List;
|
|||||||
@RequestMapping("/api/gxmu/declare")
|
@RequestMapping("/api/gxmu/declare")
|
||||||
public class DeclareController extends BaseController {
|
public class DeclareController extends BaseController {
|
||||||
private static final String TZBCY_MODULE = "gxmu_tzbcy_form";
|
private static final String TZBCY_MODULE = "gxmu_tzbcy_form";
|
||||||
|
private static final ZoneId BEIJING = ZoneId.of("Asia/Shanghai");
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private DeclareService declareService;
|
private DeclareService declareService;
|
||||||
@@ -75,6 +78,10 @@ public class DeclareController extends BaseController {
|
|||||||
if (validateMessage != null) {
|
if (validateMessage != null) {
|
||||||
return fail(validateMessage);
|
return fail(validateMessage);
|
||||||
}
|
}
|
||||||
|
// 显式写北京时间,避免容器 JVM/库会话仍为 UTC 时 DEFAULT CURRENT_TIMESTAMP 偏 8 小时
|
||||||
|
LocalDateTime now = LocalDateTime.now(BEIJING);
|
||||||
|
declare.setCreateTime(now);
|
||||||
|
declare.setUpdateTime(now);
|
||||||
if (declareService.save(declare)) {
|
if (declareService.save(declare)) {
|
||||||
return success("添加成功");
|
return success("添加成功");
|
||||||
}
|
}
|
||||||
@@ -88,6 +95,7 @@ public class DeclareController extends BaseController {
|
|||||||
if (validateMessage != null) {
|
if (validateMessage != null) {
|
||||||
return fail(validateMessage);
|
return fail(validateMessage);
|
||||||
}
|
}
|
||||||
|
declare.setUpdateTime(LocalDateTime.now(BEIJING));
|
||||||
if (declareService.updateById(declare)) {
|
if (declareService.updateById(declare)) {
|
||||||
return success("修改成功");
|
return success("修改成功");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,8 +52,10 @@
|
|||||||
<if test="param.createTimeEnd != null">
|
<if test="param.createTimeEnd != null">
|
||||||
AND a.create_time <= #{param.createTimeEnd}
|
AND a.create_time <= #{param.createTimeEnd}
|
||||||
</if>
|
</if>
|
||||||
<if test="param.usable != null">
|
<!-- start/end 存的是北京时间字符串;库会话若为 UTC,直接 now() 会把「刚开始」的申报滤掉 -->
|
||||||
AND a.start_time <= now() AND a.end_time >= now()
|
<if test="param.usable != null and param.usable == true">
|
||||||
|
AND a.start_time <= DATE_FORMAT(CONVERT_TZ(UTC_TIMESTAMP(), '+00:00', '+08:00'), '%Y-%m-%d %H:%i:%s')
|
||||||
|
AND a.end_time >= DATE_FORMAT(CONVERT_TZ(UTC_TIMESTAMP(), '+00:00', '+08:00'), '%Y-%m-%d %H:%i:%s')
|
||||||
</if>
|
</if>
|
||||||
<if test="param.deleted != null">
|
<if test="param.deleted != null">
|
||||||
AND a.deleted = #{param.deleted}
|
AND a.deleted = #{param.deleted}
|
||||||
|
|||||||
Reference in New Issue
Block a user