feat: restore work calendar view
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
MONDAY=false
|
||||
TUESDAY=false
|
||||
WEDNESDAY=false
|
||||
THURSDAY=false
|
||||
FRIDAY=false
|
||||
SATURDAY=true
|
||||
SUNDAY=true
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.dsasp.server.workcalendar.service;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
class WorkCalendarServiceImplTest {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
|
||||
private final WorkCalendarServiceImpl service = new WorkCalendarServiceImpl(jdbcTemplate);
|
||||
|
||||
@Test
|
||||
void isWorkDay_周末例外日为调班_工作日例外日为放假() {
|
||||
LocalDate saturday = new LocalDate(2026, 6, 20);
|
||||
LocalDate monday = new LocalDate(2026, 6, 22);
|
||||
when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class), anyLong(), anyLong()))
|
||||
.thenReturn(0)
|
||||
.thenReturn(1)
|
||||
.thenReturn(0)
|
||||
.thenReturn(1);
|
||||
|
||||
assertThat(service.isWorkDay(saturday)).isFalse();
|
||||
assertThat(service.isWorkDay(saturday)).isTrue();
|
||||
assertThat(service.isWorkDay(monday)).isTrue();
|
||||
assertThat(service.isWorkDay(monday)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getNextWorkDay_跳过默认周末() {
|
||||
when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class), anyLong(), anyLong()))
|
||||
.thenReturn(0);
|
||||
|
||||
assertThat(service.getNextWorkDay(new LocalDate(2026, 6, 19), 1))
|
||||
.isEqualTo(new LocalDate(2026, 6, 22));
|
||||
}
|
||||
|
||||
@Test
|
||||
void save_已存在例外日时不重复插入() {
|
||||
when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class), anyLong(), anyLong()))
|
||||
.thenReturn(1);
|
||||
|
||||
service.save(new LocalDate(2026, 6, 19));
|
||||
|
||||
verify(jdbcTemplate).queryForObject(anyString(), eq(Integer.class), anyLong(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void save_不存在例外日时插入审计字段() {
|
||||
when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class), anyLong(), anyLong()))
|
||||
.thenReturn(0);
|
||||
|
||||
service.save(new LocalDate(2026, 6, 19));
|
||||
|
||||
verify(jdbcTemplate).update(anyString(), anyString(), anyLong(), anyLong(),
|
||||
anyString(), anyLong(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void delete_按日期范围删除() {
|
||||
service.delete(new LocalDate(2026, 6, 19));
|
||||
|
||||
verify(jdbcTemplate).update(anyString(), anyLong(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getExceptionDays_把毫秒值转换成LocalDate() {
|
||||
long start = new LocalDate(2026, 6, 19).toDateTimeAtStartOfDay().getMillis();
|
||||
when(jdbcTemplate.queryForList(anyString(), eq(Long.class), anyLong(), anyLong()))
|
||||
.thenReturn(List.of(start));
|
||||
|
||||
assertThat(service.getExceptionDays(new LocalDate(2026, 1, 1), new LocalDate(2026, 12, 31)))
|
||||
.containsExactly(new LocalDate(2026, 6, 19));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user