package com.gree.mall.manager.controller.report;

import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.report.SalesReportLogic;
import com.gree.mall.manager.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@Api(value = "销售报表", tags = {"销售报表"})
@RequestMapping(value = "/report", produces = "application/json; charset=utf-8")
public class SalesReportController {
    @Autowired
    private SalesReportLogic salesReportLogic;

    @GetMapping("/day/statistics")
    @ApiOperation(value = "日实时销售报表")
    public ResponseHelper reportDay(HttpServletRequest request) {
        Date startTime = DateUtils.getStartTime();
        Date endTime = DateUtils.getEndTime();
        return ResponseHelper.success(salesReportLogic.realTimeStatistics(request,DateUtils.formatDate(startTime),DateUtils.formatDate(endTime)));
    }

    @GetMapping("/month/statistics")
    @ApiOperation(value = "月实时销售报表")
    public ResponseHelper reportMonth(HttpServletRequest request) {
        Date startTime = DateUtils.getStartMonth();
        Date endTime = DateUtils.getEndMonth();
        return ResponseHelper.success(salesReportLogic.realTimeStatistics(request,DateUtils.formatDate(startTime),DateUtils.formatDate(endTime)));
    }

    @GetMapping("/year/statistics")
    @ApiOperation(value = "年实时销售报表")
    public ResponseHelper reportYear(HttpServletRequest request) {
        String year = new SimpleDateFormat("yyyy").format(new Date());
        String startTime = year + "-01-01";
        String endTime = DateUtils.formatDate(new Date());
        return ResponseHelper.success(salesReportLogic.realTimeStatistics(request,startTime,endTime));
    }


    @GetMapping("/day/export")
    @ApiOperation(value = "日实时销售导出")
    public void exportDay(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Date startTime = DateUtils.getStartTime();
        Date endTime = DateUtils.getEndTime();
        salesReportLogic.exportData(request,response,DateUtils.formatDate(startTime),DateUtils.formatDate(endTime));
    }

    @GetMapping("/month/export")
    @ApiOperation(value = "月实时销售导出")
    public void exportMonth(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Date startTime = DateUtils.getStartMonth();
        Date endTime = DateUtils.getEndMonth();
        salesReportLogic.exportData(request,response,DateUtils.formatDate(startTime),DateUtils.formatDate(endTime));
    }

    @GetMapping("/year/export")
    @ApiOperation(value = "年实时销售导出")
    public void exportYear(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String year = new SimpleDateFormat("yyyy").format(new Date());
        String startTime = year + "-01-01";
        String endTime = year + "12-31";
        salesReportLogic.exportData(request,response,startTime,endTime);
    }


}
