GenerateWeeklyRangesController.java
1.97 KB
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
package cn.com.glsx.dj.controller;
import cn.com.glsx.framework.rop.response.RopResponse;
import com.rop.AbstractRopRequest;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@ServiceMethodBean(version = "1.0.0", needInSession = NeedInSessionType.NO)
@Api("生成最近一年所有周一到周日")
public class GenerateWeeklyRangesController {
/**
* 生成最近一年所有周一到周日
*
* @return
*/
@ApiOperation(value = "生成最近一年所有周一到周日")
@ServiceMethod(method = "dj.api.sale.get.generateWeeklyRanges", needInSession = NeedInSessionType.NO)
public RopResponse<List<Map>> generateWeeklyRanges(AbstractRopRequest request) {
LocalDate now = LocalDate.now();
LocalDate thisMonday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
LocalDate oneYearAgo = now.minusYears(1);
LocalDate oneYearAgoMonday = oneYearAgo.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
List<Map> result = new ArrayList<>();
LocalDate currentMonday = thisMonday;
while (!currentMonday.isBefore(oneYearAgoMonday)) {
LocalDate sunday = currentMonday.plusDays(6);
Map<String, String> weekRange = new HashMap<>();
weekRange.put("startDate", currentMonday.toString()); // 周一
weekRange.put("endDate", sunday.toString()); // 周日
result.add(weekRange); // Map 是 Object 的子类
currentMonday = currentMonday.minusWeeks(1); // 上一周
}
return RopResponse.success(result);
}
}