feat: restore work calendar view

This commit is contained in:
2026-06-18 10:26:07 +08:00
parent 03bf036d87
commit eafd872271
7 changed files with 641 additions and 0 deletions

View File

@@ -0,0 +1,192 @@
package com.dsasp.server.workcalendar.service;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.LocalDate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dsasp.server.common.user.service.LoggedInUser;
import com.dsasp.server.common.util.UserSession;
import com.dsasp.server.common.workcalendar.service.WorkCalendarService;
/**
* EIP-3 工作日历服务实现。
*
* <p>旧框架用 SYS_CALENDAREXCEPTDAY 保存例外日:周末例外=上班日,工作日例外=放假日。
* 这里保留同一张表和同一 bean 名,供页面和服务流脚本共同使用。
*/
@Service("workCalendarService")
@Transactional
public class WorkCalendarServiceImpl implements WorkCalendarService {
private static final String TABLE = "SYS_CALENDAREXCEPTDAY";
private static final String WEEKEND_RESOURCE = "/META-INF/weekends.properties";
private final JdbcTemplate jdbcTemplate;
private final Set<Integer> weekends;
public WorkCalendarServiceImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.weekends = loadWeekends();
}
@Override
public LocalDate getNextWorkDay(LocalDate startDate, int workDays) {
if (startDate == null) {
return null;
}
int step = workDays < 0 ? -1 : 1;
LocalDate current = startDate;
for (int i = 0; i < Math.abs(workDays); i++) {
current = moveByWorkDay(current, step);
}
return current;
}
@Override
@Transactional(readOnly = true)
public List<LocalDate> getExceptionDays(LocalDate startDate, LocalDate endDate) {
if (startDate == null || endDate == null) {
return List.of();
}
LocalDate from = startDate.isAfter(endDate) ? endDate : startDate;
LocalDate to = startDate.isAfter(endDate) ? startDate : endDate;
List<Long> values = jdbcTemplate.queryForList(
"SELECT EXCEPTDATE FROM " + TABLE
+ " WHERE EXCEPTDATE >= ? AND EXCEPTDATE < ? ORDER BY EXCEPTDATE",
Long.class, startOfDayMillis(from), startOfDayMillis(to.plusDays(1)));
List<LocalDate> dates = new ArrayList<>();
for (Long value : values) {
if (value != null) {
dates.add(new LocalDate(value));
}
}
return dates;
}
@Override
public void delete(LocalDate day) {
if (day == null) {
return;
}
jdbcTemplate.update(
"DELETE FROM " + TABLE + " WHERE EXCEPTDATE >= ? AND EXCEPTDATE < ?",
startOfDayMillis(day), startOfDayMillis(day.plusDays(1)));
}
@Override
public void save(LocalDate day) {
if (day == null || containsExceptionDay(day)) {
return;
}
long now = System.currentTimeMillis();
String operator = currentUser();
jdbcTemplate.update(
"INSERT INTO " + TABLE
+ " (SYS_CALENDAREXCEPTDAY_ID, EXCEPTDATE, CREATETIME, CREATEBY,"
+ " UPDATETIME, UPDATEBY, ISACTIVE, VERSION)"
+ " VALUES (?, ?, ?, ?, ?, ?, 1, 0)",
UUID.randomUUID().toString(), startOfDayMillis(day), now, operator, now, operator);
}
@Override
@Transactional(readOnly = true)
public boolean containsExceptionDay(LocalDate day) {
if (day == null) {
return false;
}
Integer count = jdbcTemplate.queryForObject(
"SELECT COUNT(*) FROM " + TABLE + " WHERE EXCEPTDATE >= ? AND EXCEPTDATE < ?",
Integer.class, startOfDayMillis(day), startOfDayMillis(day.plusDays(1)));
return count != null && count > 0;
}
@Override
public boolean isWeekDay(LocalDate day) {
return day != null && weekends.contains(day.getDayOfWeek());
}
@Override
public boolean isWorkDay(DateTime dt) {
return dt != null && isWorkDay(dt.toLocalDate());
}
@Override
@Transactional(readOnly = true)
public boolean isWorkDay(LocalDate day) {
if (day == null) {
return false;
}
boolean exceptionDay = containsExceptionDay(day);
return isWeekDay(day) ? exceptionDay : !exceptionDay;
}
@Override
public void initCalendarCache() {
// EIP4 版不使用旧 CalendarCache,直接查库保证页面和脚本读取一致。
}
private LocalDate moveByWorkDay(LocalDate startDate, int step) {
LocalDate current = startDate.plusDays(step);
while (!isWorkDay(current)) {
current = current.plusDays(step);
}
return current;
}
private static long startOfDayMillis(LocalDate day) {
return day.toDateTimeAtStartOfDay().getMillis();
}
private static Set<Integer> loadWeekends() {
Set<Integer> defaults = new HashSet<>();
defaults.add(DateTimeConstants.SATURDAY);
defaults.add(DateTimeConstants.SUNDAY);
Properties properties = new Properties();
try (InputStream in = WorkCalendarServiceImpl.class.getResourceAsStream(WEEKEND_RESOURCE)) {
if (in == null) {
return defaults;
}
properties.load(in);
} catch (IOException ex) {
return defaults;
}
Set<Integer> configured = new HashSet<>();
addWeekend(configured, properties, "MONDAY", DateTimeConstants.MONDAY);
addWeekend(configured, properties, "TUESDAY", DateTimeConstants.TUESDAY);
addWeekend(configured, properties, "WEDNESDAY", DateTimeConstants.WEDNESDAY);
addWeekend(configured, properties, "THURSDAY", DateTimeConstants.THURSDAY);
addWeekend(configured, properties, "FRIDAY", DateTimeConstants.FRIDAY);
addWeekend(configured, properties, "SATURDAY", DateTimeConstants.SATURDAY);
addWeekend(configured, properties, "SUNDAY", DateTimeConstants.SUNDAY);
return configured.isEmpty() ? defaults : configured;
}
private static void addWeekend(Set<Integer> target, Properties properties, String key, int dayOfWeek) {
if ("true".equalsIgnoreCase(properties.getProperty(key, "").trim())) {
target.add(dayOfWeek);
}
}
private static String currentUser() {
Properties session = UserSession.get();
if (session == null) {
return "ADMIN";
}
String user = session.getProperty(LoggedInUser.CURRENTUSER, "ADMIN").trim();
return user.isEmpty() ? "ADMIN" : user;
}
}

View File

@@ -0,0 +1,7 @@
MONDAY=false
TUESDAY=false
WEDNESDAY=false
THURSDAY=false
FRIDAY=false
SATURDAY=true
SUNDAY=true