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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -61,6 +61,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
|||||||
public class PageSchemaController {
|
public class PageSchemaController {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(PageSchemaController.class);
|
private static final Logger log = LoggerFactory.getLogger(PageSchemaController.class);
|
||||||
|
private static final String WORK_CALENDAR_TEMPLATE_ID = "a13fa07f-d8c3-46cf-9736-24d6c0e582e3";
|
||||||
|
|
||||||
/** 响应头:标明 schema 来源层(overlay / standard) */
|
/** 响应头:标明 schema 来源层(overlay / standard) */
|
||||||
public static final String HEADER_SCHEMA_LAYER = "X-Schema-Layer";
|
public static final String HEADER_SCHEMA_LAYER = "X-Schema-Layer";
|
||||||
@@ -110,6 +111,10 @@ public class PageSchemaController {
|
|||||||
"SELECT schema_json::text AS schema_json, schema_etag FROM sys_page_schema WHERE template_id = ?",
|
"SELECT schema_json::text AS schema_json, schema_etag FROM sys_page_schema WHERE template_id = ?",
|
||||||
templateId);
|
templateId);
|
||||||
} catch (DataAccessException ex) {
|
} catch (DataAccessException ex) {
|
||||||
|
Map<String, Object> builtin = builtinSchemaRow(templateId);
|
||||||
|
if (builtin != null) {
|
||||||
|
return buildSchemaResponse(builtin, ifNoneMatch, "standard", amisEnvelope);
|
||||||
|
}
|
||||||
// SYS_PAGE_SCHEMA 表缺失或库异常 → schema 存储不可用
|
// SYS_PAGE_SCHEMA 表缺失或库异常 → schema 存储不可用
|
||||||
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
|
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
@@ -117,6 +122,10 @@ public class PageSchemaController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (rows.isEmpty()) {
|
if (rows.isEmpty()) {
|
||||||
|
Map<String, Object> builtin = builtinSchemaRow(templateId);
|
||||||
|
if (builtin != null) {
|
||||||
|
return buildSchemaResponse(builtin, ifNoneMatch, "standard", amisEnvelope);
|
||||||
|
}
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.body("{\"status\":1,\"msg\":\"模板不存在\"}");
|
.body("{\"status\":1,\"msg\":\"模板不存在\"}");
|
||||||
@@ -266,6 +275,77 @@ public class PageSchemaController {
|
|||||||
private static final com.fasterxml.jackson.databind.ObjectMapper SCHEMA_MAPPER =
|
private static final com.fasterxml.jackson.databind.ObjectMapper SCHEMA_MAPPER =
|
||||||
new com.fasterxml.jackson.databind.ObjectMapper();
|
new com.fasterxml.jackson.databind.ObjectMapper();
|
||||||
|
|
||||||
|
private static Map<String, Object> builtinSchemaRow(String templateId) {
|
||||||
|
if (!WORK_CALENDAR_TEMPLATE_ID.equals(templateId)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int year = java.time.Year.now().getValue();
|
||||||
|
String schema = """
|
||||||
|
{
|
||||||
|
"type": "page",
|
||||||
|
"title": "工作日历",
|
||||||
|
"body": {
|
||||||
|
"type": "crud",
|
||||||
|
"id": "work-calendar-crud",
|
||||||
|
"syncLocation": false,
|
||||||
|
"api": "get:/api/work-calendar/year?year=${year}",
|
||||||
|
"filter": {
|
||||||
|
"title": "查询",
|
||||||
|
"mode": "inline",
|
||||||
|
"wrapWithPanel": true,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "input-number",
|
||||||
|
"name": "year",
|
||||||
|
"label": "年度",
|
||||||
|
"value": %d,
|
||||||
|
"min": 1900,
|
||||||
|
"max": 2100,
|
||||||
|
"step": 1
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"headerToolbar": [
|
||||||
|
{
|
||||||
|
"type": "tpl",
|
||||||
|
"tpl": "<span class='label label-default'>工作日</span> <span class='label label-danger'>周休日/放假</span> <span class='label label-info'>调班上班</span>"
|
||||||
|
},
|
||||||
|
"reload"
|
||||||
|
],
|
||||||
|
"columns": [
|
||||||
|
{"name": "date", "label": "日期"},
|
||||||
|
{"name": "week", "label": "星期"},
|
||||||
|
{"name": "stateLabel", "label": "状态"},
|
||||||
|
{
|
||||||
|
"type": "mapping",
|
||||||
|
"name": "workDay",
|
||||||
|
"label": "是否工作日",
|
||||||
|
"map": {"true": "<span class='label label-success'>是</span>", "false": "<span class='label label-danger'>否</span>"}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "operation",
|
||||||
|
"label": "操作",
|
||||||
|
"buttons": [
|
||||||
|
{
|
||||||
|
"type": "button",
|
||||||
|
"label": "切换例外日",
|
||||||
|
"level": "link",
|
||||||
|
"actionType": "ajax",
|
||||||
|
"api": "post:/api/work-calendar/toggle?date=${date}",
|
||||||
|
"reload": "work-calendar-crud"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""".formatted(year);
|
||||||
|
Map<String, Object> row = new LinkedHashMap<>();
|
||||||
|
row.put("schema_json", schema);
|
||||||
|
row.put("schema_etag", sha256(schema));
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
/** If-None-Match 可为逗号分隔多值或 {@code *};任一匹配即命中。 */
|
/** If-None-Match 可为逗号分隔多值或 {@code *};任一匹配即命中。 */
|
||||||
private boolean etagMatches(String ifNoneMatch, String etag) {
|
private boolean etagMatches(String ifNoneMatch, String etag) {
|
||||||
String header = ifNoneMatch.trim();
|
String header = ifNoneMatch.trim();
|
||||||
|
|||||||
@@ -0,0 +1,201 @@
|
|||||||
|
package com.eip4.platform.metadata.web;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.joda.time.LocalDate;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.dsasp.server.common.workcalendar.service.WorkCalendarService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 工作日历保真端点。
|
||||||
|
*
|
||||||
|
* <p>旧页面按年度展示 SYS_CALENDAREXCEPTDAY 例外日,并通过点击日期切换例外状态。
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/work-calendar")
|
||||||
|
public class WorkCalendarController {
|
||||||
|
|
||||||
|
private final WorkCalendarService workCalendarService;
|
||||||
|
|
||||||
|
public WorkCalendarController(WorkCalendarService workCalendarService) {
|
||||||
|
this.workCalendarService = workCalendarService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/year")
|
||||||
|
public Map<String, Object> year(@RequestParam(value = "year", required = false) Integer year) {
|
||||||
|
int safeYear = safeYear(year);
|
||||||
|
return ok(yearData(safeYear));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/toggle")
|
||||||
|
public Map<String, Object> toggle(@RequestParam(value = "date", required = false) String dateParam,
|
||||||
|
@RequestBody(required = false) Map<String, Object> body) {
|
||||||
|
String rawDate = dateParam;
|
||||||
|
if ((rawDate == null || rawDate.isBlank()) && body != null && body.get("date") != null) {
|
||||||
|
rawDate = String.valueOf(body.get("date"));
|
||||||
|
}
|
||||||
|
if (rawDate == null || rawDate.isBlank()) {
|
||||||
|
return fail("缺少日期");
|
||||||
|
}
|
||||||
|
LocalDate day;
|
||||||
|
try {
|
||||||
|
day = LocalDate.parse(rawDate.trim());
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
return fail("日期格式应为 YYYY-MM-DD");
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean existed = workCalendarService.containsExceptionDay(day);
|
||||||
|
if (existed) {
|
||||||
|
workCalendarService.delete(day);
|
||||||
|
} else {
|
||||||
|
workCalendarService.save(day);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object> data = dayRow(day);
|
||||||
|
data.put("toggled", true);
|
||||||
|
data.put("exception", !existed);
|
||||||
|
data.put("workDay", workCalendarService.isWorkDay(day));
|
||||||
|
return ok("切换成功", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> yearData(int year) {
|
||||||
|
LocalDate start = new LocalDate(year, 1, 1);
|
||||||
|
LocalDate end = new LocalDate(year, 12, 31);
|
||||||
|
List<LocalDate> exceptionDays = workCalendarService.getExceptionDays(start, end);
|
||||||
|
Set<String> exceptionSet = new HashSet<>();
|
||||||
|
for (LocalDate exceptionDay : exceptionDays) {
|
||||||
|
exceptionSet.add(exceptionDay.toString());
|
||||||
|
}
|
||||||
|
List<String> exceptions = exceptionSet.stream().sorted().toList();
|
||||||
|
|
||||||
|
List<Map<String, Object>> days = new ArrayList<>();
|
||||||
|
LocalDate current = start;
|
||||||
|
while (!current.isAfter(end)) {
|
||||||
|
days.add(dayRow(current, exceptionSet.contains(current.toString())));
|
||||||
|
current = current.plusDays(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Map<String, Object>> schedules = new ArrayList<>();
|
||||||
|
for (Map<String, Object> day : days) {
|
||||||
|
if (Boolean.TRUE.equals(day.get("exception"))) {
|
||||||
|
schedules.add(Map.of(
|
||||||
|
"startTime", day.get("date"),
|
||||||
|
"endTime", day.get("date"),
|
||||||
|
"content", day.get("stateLabel"),
|
||||||
|
"className", Boolean.TRUE.equals(day.get("weekend")) ? "bg-info" : "bg-danger"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object> data = new LinkedHashMap<>();
|
||||||
|
data.put("year", year);
|
||||||
|
data.put("items", days);
|
||||||
|
data.put("total", days.size());
|
||||||
|
data.put("exceptions", exceptions);
|
||||||
|
data.put("schedules", schedules);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> dayRow(LocalDate day) {
|
||||||
|
boolean weekend = workCalendarService.isWeekDay(day);
|
||||||
|
boolean exception = workCalendarService.containsExceptionDay(day);
|
||||||
|
return dayRow(day, weekend, exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> dayRow(LocalDate day, boolean exception) {
|
||||||
|
boolean weekend = workCalendarService.isWeekDay(day);
|
||||||
|
return dayRow(day, weekend, exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> dayRow(LocalDate day, boolean weekend, boolean exception) {
|
||||||
|
boolean workDay = weekend ? exception : !exception;
|
||||||
|
String state = state(weekend, exception);
|
||||||
|
Map<String, Object> row = new LinkedHashMap<>();
|
||||||
|
row.put("date", day.toString());
|
||||||
|
row.put("month", day.getMonthOfYear());
|
||||||
|
row.put("day", day.getDayOfMonth());
|
||||||
|
row.put("week", weekLabel(day.getDayOfWeek()));
|
||||||
|
row.put("weekend", weekend);
|
||||||
|
row.put("exception", exception);
|
||||||
|
row.put("workDay", workDay);
|
||||||
|
row.put("state", state);
|
||||||
|
row.put("stateLabel", stateLabel(state));
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int safeYear(Integer year) {
|
||||||
|
int current = java.time.Year.now().getValue();
|
||||||
|
if (year == null) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
if (year < 1900 || year > 2100) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
return year;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String state(boolean weekend, boolean exception) {
|
||||||
|
if (weekend && exception) {
|
||||||
|
return "makeup";
|
||||||
|
}
|
||||||
|
if (weekend) {
|
||||||
|
return "weekend";
|
||||||
|
}
|
||||||
|
if (exception) {
|
||||||
|
return "holiday";
|
||||||
|
}
|
||||||
|
return "workday";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String stateLabel(String state) {
|
||||||
|
return switch (state.toLowerCase(Locale.ROOT)) {
|
||||||
|
case "makeup" -> "调班上班";
|
||||||
|
case "weekend" -> "周休日";
|
||||||
|
case "holiday" -> "放假";
|
||||||
|
default -> "工作日";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String weekLabel(int dayOfWeek) {
|
||||||
|
return switch (dayOfWeek) {
|
||||||
|
case 1 -> "星期一";
|
||||||
|
case 2 -> "星期二";
|
||||||
|
case 3 -> "星期三";
|
||||||
|
case 4 -> "星期四";
|
||||||
|
case 5 -> "星期五";
|
||||||
|
case 6 -> "星期六";
|
||||||
|
case 7 -> "星期日";
|
||||||
|
default -> "";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> ok(Map<String, Object> data) {
|
||||||
|
return ok("", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> ok(String msg, Map<String, Object> data) {
|
||||||
|
Map<String, Object> result = new LinkedHashMap<>();
|
||||||
|
result.put("status", 0);
|
||||||
|
result.put("msg", msg);
|
||||||
|
result.put("data", data);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> fail(String msg) {
|
||||||
|
Map<String, Object> result = new LinkedHashMap<>();
|
||||||
|
result.put("status", 1);
|
||||||
|
result.put("msg", msg);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,6 +37,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|||||||
class PageSchemaControllerTest {
|
class PageSchemaControllerTest {
|
||||||
|
|
||||||
private static final String TID = "00511ac1-ee64-4ecd-a550-22842ee5be72";
|
private static final String TID = "00511ac1-ee64-4ecd-a550-22842ee5be72";
|
||||||
|
private static final String WORK_CALENDAR_TID = "a13fa07f-d8c3-46cf-9736-24d6c0e582e3";
|
||||||
private static final String SCHEMA = "{\"type\":\"page\",\"title\":\"角色维护\"}";
|
private static final String SCHEMA = "{\"type\":\"page\",\"title\":\"角色维护\"}";
|
||||||
private static final String SCHEMA_OVERLAY = "{\"type\":\"page\",\"title\":\"角色维护(定制)\"}";
|
private static final String SCHEMA_OVERLAY = "{\"type\":\"page\",\"title\":\"角色维护(定制)\"}";
|
||||||
private static final String ETAG_VALUE = "abc123etag";
|
private static final String ETAG_VALUE = "abc123etag";
|
||||||
@@ -150,6 +151,23 @@ class PageSchemaControllerTest {
|
|||||||
assertTrue(String.valueOf(resp.getBody()).contains("模板不存在"));
|
assertTrue(String.valueOf(resp.getBody()).contains("模板不存在"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("4b. 工作日历 XML 为空时走内置标准 schema fallback")
|
||||||
|
void workCalendarBuiltinSchemaFallback() {
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
when(jdbc.queryForList(anyString(), eq(WORK_CALENDAR_TID)))
|
||||||
|
.thenReturn(List.of())
|
||||||
|
.thenReturn(List.of());
|
||||||
|
PageSchemaController c = new PageSchemaController(jdbc);
|
||||||
|
|
||||||
|
var resp = c.schema(WORK_CALENDAR_TID, null, null, null);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||||
|
assertTrue(String.valueOf(resp.getBody()).contains("工作日历"));
|
||||||
|
assertTrue(String.valueOf(resp.getBody()).contains("/api/work-calendar/year"));
|
||||||
|
assertEquals("standard", resp.getHeaders().getFirst(PageSchemaController.HEADER_SCHEMA_LAYER));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("5. SYS_PAGE_SCHEMA 缺失/库异常 → 503")
|
@DisplayName("5. SYS_PAGE_SCHEMA 缺失/库异常 → 503")
|
||||||
void serviceUnavailable503() {
|
void serviceUnavailable503() {
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.eip4.platform.metadata.web;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.joda.time.LocalDate;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.dsasp.server.common.workcalendar.service.WorkCalendarService;
|
||||||
|
|
||||||
|
class WorkCalendarControllerTest {
|
||||||
|
|
||||||
|
private final WorkCalendarService service = mock(WorkCalendarService.class);
|
||||||
|
private final WorkCalendarController controller = new WorkCalendarController(service);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
void year_返回年度每日状态和例外日集合() {
|
||||||
|
when(service.getExceptionDays(new LocalDate(2026, 1, 1), new LocalDate(2026, 12, 31)))
|
||||||
|
.thenReturn(List.of(new LocalDate(2026, 6, 20)));
|
||||||
|
when(service.isWeekDay(new LocalDate(2026, 6, 20))).thenReturn(true);
|
||||||
|
when(service.containsExceptionDay(new LocalDate(2026, 6, 20))).thenReturn(true);
|
||||||
|
|
||||||
|
Map<String, Object> resp = controller.year(2026);
|
||||||
|
|
||||||
|
assertThat(resp).containsEntry("status", 0);
|
||||||
|
Map<String, Object> data = (Map<String, Object>) resp.get("data");
|
||||||
|
assertThat(data).containsEntry("year", 2026);
|
||||||
|
assertThat((List<Map<String, Object>>) data.get("items")).hasSize(365);
|
||||||
|
assertThat((List<String>) data.get("exceptions")).containsExactly("2026-06-20");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toggle_存在则删除例外日() {
|
||||||
|
LocalDate day = new LocalDate(2026, 6, 20);
|
||||||
|
when(service.containsExceptionDay(day)).thenReturn(true).thenReturn(false);
|
||||||
|
|
||||||
|
Map<String, Object> resp = controller.toggle("2026-06-20", null);
|
||||||
|
|
||||||
|
assertThat(resp).containsEntry("status", 0);
|
||||||
|
verify(service).delete(day);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toggle_不存在则保存例外日() {
|
||||||
|
LocalDate day = new LocalDate(2026, 6, 20);
|
||||||
|
when(service.containsExceptionDay(day)).thenReturn(false);
|
||||||
|
|
||||||
|
Map<String, Object> resp = controller.toggle(null, Map.of("date", "2026-06-20"));
|
||||||
|
|
||||||
|
assertThat(resp).containsEntry("status", 0);
|
||||||
|
verify(service).save(day);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user