feat: restore work calendar view
This commit is contained in:
@@ -61,6 +61,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
public class PageSchemaController {
|
||||
|
||||
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) */
|
||||
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 = ?",
|
||||
templateId);
|
||||
} catch (DataAccessException ex) {
|
||||
Map<String, Object> builtin = builtinSchemaRow(templateId);
|
||||
if (builtin != null) {
|
||||
return buildSchemaResponse(builtin, ifNoneMatch, "standard", amisEnvelope);
|
||||
}
|
||||
// SYS_PAGE_SCHEMA 表缺失或库异常 → schema 存储不可用
|
||||
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
@@ -117,6 +122,10 @@ public class PageSchemaController {
|
||||
}
|
||||
|
||||
if (rows.isEmpty()) {
|
||||
Map<String, Object> builtin = builtinSchemaRow(templateId);
|
||||
if (builtin != null) {
|
||||
return buildSchemaResponse(builtin, ifNoneMatch, "standard", amisEnvelope);
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body("{\"status\":1,\"msg\":\"模板不存在\"}");
|
||||
@@ -266,6 +275,77 @@ public class PageSchemaController {
|
||||
private static final com.fasterxml.jackson.databind.ObjectMapper SCHEMA_MAPPER =
|
||||
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 *};任一匹配即命中。 */
|
||||
private boolean etagMatches(String ifNoneMatch, String etag) {
|
||||
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 {
|
||||
|
||||
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_OVERLAY = "{\"type\":\"page\",\"title\":\"角色维护(定制)\"}";
|
||||
private static final String ETAG_VALUE = "abc123etag";
|
||||
@@ -150,6 +151,23 @@ class PageSchemaControllerTest {
|
||||
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
|
||||
@DisplayName("5. SYS_PAGE_SCHEMA 缺失/库异常 → 503")
|
||||
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