GenerateWeeklyRangesController.java 1.97 KB
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);
    }

}