刘光辉
14 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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;
    }
}