package com.purui.prims.headless.server.rest;

import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.utils.UserHolder;
import com.purui.prims.headless.api.pojo.request.StatisticsPeriodReq;
import com.purui.prims.headless.api.pojo.response.StatisticsPeriodResp;
import com.purui.prims.headless.server.service.StatisticsPeriodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping({"/api/semantic/period"})
@Api(tags = "统计周期")
public class StatPeriodController {

    private final StatisticsPeriodService statisticsPeriodService;

    public StatPeriodController(StatisticsPeriodService statisticsPeriodService) {
        this.statisticsPeriodService = statisticsPeriodService;
    }

    @ApiOperation(value = "新建/修改统计周期")
    @PostMapping
    public boolean save(@RequestBody StatisticsPeriodReq periodReq, HttpServletRequest request,
                        HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return statisticsPeriodService.saveOrUpdate(periodReq, user);
    }

    @ApiOperation(value = "统计周期列表")
    @GetMapping("/list")
    public List<StatisticsPeriodResp> getPeriods() {
        return statisticsPeriodService.getStatisticsPeriods();
    }
}
