ソースを参照

牦牛资产档案

wwh 1 ヶ月 前
コミット
e0cf1434d4

+ 60 - 0
baqing-admin/src/main/java/com/ruoyi/web/modules/industryservice/support/YakHerdInventoryMonthSupport.java

@@ -0,0 +1,60 @@
1
+package com.ruoyi.web.modules.industryservice.support;
2
+
3
+import java.time.YearMonth;
4
+import java.util.ArrayList;
5
+import java.util.Collections;
6
+import java.util.List;
7
+import com.ruoyi.common.exception.ServiceException;
8
+
9
+/**
10
+ * 填报月份选项与年月合法性校验
11
+ */
12
+public final class YakHerdInventoryMonthSupport
13
+{
14
+    public static final int MIN_MONTH = 1;
15
+
16
+    public static final int MAX_MONTH = 12;
17
+
18
+    private YakHerdInventoryMonthSupport()
19
+    {
20
+    }
21
+
22
+    public static List<Integer> resolveMonthOptions()
23
+    {
24
+        List<Integer> months = new ArrayList<>();
25
+        for (int m = MIN_MONTH; m <= MAX_MONTH; m++)
26
+        {
27
+            months.add(m);
28
+        }
29
+        return Collections.unmodifiableList(months);
30
+    }
31
+
32
+    public static boolean isValidMonth(Integer statMonth)
33
+    {
34
+        return statMonth != null && statMonth >= MIN_MONTH && statMonth <= MAX_MONTH;
35
+    }
36
+
37
+    /**
38
+     * 校验填报年月:年份在可选范围内、月份 1–12、不可填报未来月份。
39
+     */
40
+    public static void validateStatPeriod(Integer statYear, Integer statMonth)
41
+    {
42
+        if (statYear == null)
43
+        {
44
+            throw new ServiceException("所属年份不能为空");
45
+        }
46
+        if (!YakHerdInventoryYearSupport.isAllowedYear(statYear))
47
+        {
48
+            throw new ServiceException("所属年份不在可选范围内");
49
+        }
50
+        if (!isValidMonth(statMonth))
51
+        {
52
+            throw new ServiceException("所属月份不合法");
53
+        }
54
+        YearMonth selected = YearMonth.of(statYear, statMonth);
55
+        if (selected.isAfter(YearMonth.now()))
56
+        {
57
+            throw new ServiceException("不能填报未来月份");
58
+        }
59
+    }
60
+}

+ 35 - 0
baqing-admin/src/test/java/com/ruoyi/web/modules/industryservice/support/YakHerdInventoryMonthSupportTest.java

@@ -0,0 +1,35 @@
1
+package com.ruoyi.web.modules.industryservice.support;
2
+
3
+import static org.junit.jupiter.api.Assertions.assertEquals;
4
+import static org.junit.jupiter.api.Assertions.assertThrows;
5
+
6
+import org.junit.jupiter.api.DisplayName;
7
+import org.junit.jupiter.api.Test;
8
+import com.ruoyi.common.exception.ServiceException;
9
+
10
+@DisplayName("YakHerdInventoryMonthSupport")
11
+class YakHerdInventoryMonthSupportTest
12
+{
13
+    @Test
14
+    @DisplayName("月份选项 12 个")
15
+    void monthOptions()
16
+    {
17
+        assertEquals(12, YakHerdInventoryMonthSupport.resolveMonthOptions().size());
18
+        assertEquals(1, YakHerdInventoryMonthSupport.resolveMonthOptions().get(0).intValue());
19
+        assertEquals(12, YakHerdInventoryMonthSupport.resolveMonthOptions().get(11).intValue());
20
+    }
21
+
22
+    @Test
23
+    @DisplayName("未来月份拒绝")
24
+    void rejectFutureMonth()
25
+    {
26
+        int year = java.time.Year.now().getValue();
27
+        int futureMonth = java.time.YearMonth.now().getMonthValue() + 1;
28
+        if (futureMonth > 12)
29
+        {
30
+            return;
31
+        }
32
+        assertThrows(ServiceException.class,
33
+                () -> YakHerdInventoryMonthSupport.validateStatPeriod(year, futureMonth));
34
+    }
35
+}