package jnpf.limsController;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
import jnpf.util.R;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
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.RestController;
|
|
import java.time.LocalDate;
|
import java.time.format.DateTimeFormatter;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* LIMS通用接口(Java版JNPF 6.1)
|
* 提供周期、日期等通用能力。
|
*/
|
@Slf4j
|
@Tag(name = "LIMS通用接口", description = "周期、日期等通用能力")
|
@RestController
|
@RequestMapping("/lims/common")
|
public class CommonController {
|
|
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
/**
|
* 按当前日期计算周期对应日期。
|
*
|
* @param param 周期参数
|
* @return 周期日期字典
|
*/
|
@Operation(summary = "计算周期日期", description = "根据周期单位和周期数值,基于当前日期计算目标日期")
|
@PostMapping("/zhouqi/calculate")
|
public R<Map<String, String>> calculateZhouqi(@RequestBody ZhouqiCalculateParam param) {
|
if (param == null || param.getZhouqi() == null || param.getZhouqi().isEmpty()) {
|
return R.error("周期参数zhouqi不能为空");
|
}
|
|
LocalDate now = LocalDate.now();
|
Map<String, String> result = new LinkedHashMap<>();
|
for (ZhouqiItem item : param.getZhouqi()) {
|
if (item == null || isBlank(item.getKey()) || item.getValue() == null) {
|
return R.error("周期子项key和value不能为空");
|
}
|
|
try {
|
LocalDate calculateDate = calculateDate(now, item.getKey(), item.getValue());
|
result.put(item.getKey() + item.getValue(), calculateDate.format(DATE_FORMATTER));
|
} catch (IllegalArgumentException e) {
|
return R.error(e.getMessage());
|
}
|
}
|
return R.success("计算成功", result);
|
}
|
|
private LocalDate calculateDate(LocalDate baseDate, String key, Integer value) {
|
switch (key) {
|
case "day":
|
return baseDate.plusDays(value);
|
case "week":
|
return baseDate.plusWeeks(value);
|
case "month":
|
return baseDate.plusMonths(value);
|
case "quarter":
|
return baseDate.plusMonths(value * 3L);
|
case "year":
|
return baseDate.plusYears(value);
|
default:
|
throw new IllegalArgumentException("不支持的周期单位: " + key);
|
}
|
}
|
|
private boolean isBlank(String value) {
|
return value == null || value.trim().isEmpty();
|
}
|
|
@Data
|
public static class ZhouqiCalculateParam {
|
private List<ZhouqiItem> zhouqi;
|
}
|
|
@Data
|
public static class ZhouqiItem {
|
/**
|
* 周期单位:day-天、week-周、month-月、quarter-季度、year-年。
|
*/
|
private String key;
|
|
/**
|
* 周期数值。
|
*/
|
private Integer value;
|
}
|
}
|