package com.skr.mdm.api.statistics.controller;

import com.skr.mdm.api.statistics.service.StatisticsService;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.bean.vo.statistics.AppInstallCountVO;
import com.skr.mdm.bean.vo.statistics.ClientUpgradeStatisticsVO;
import com.skr.mdm.bean.vo.statistics.CmdDayStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

/**
 * @author nfq
 * @date 2021/1/25 10:43
 */
@RestController
@RequestMapping("statistics")
@Api(tags = "数据统计")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    @PostMapping("app/install")
    @ApiOperation(value = "应用安装统计")
    @PreAuthorize("hasAnyAuthority('sys:audit:statistics')")
    @ApiResponses(@ApiResponse(code = HttpStatus.SC_OK, message = "成功", response = AppInstallCountVO.class))
    public ResponseResult appInstallStatistics(@RequestParam String startTime, @RequestParam String endTime, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return statisticsService.appInstallStatistics(loginAdminDTO.getCompanyId(), startTime, endTime);
    }


    @PostMapping("client/upgrade")
    @ApiOperation(value = "客户端升级统计")
    @PreAuthorize("hasAnyAuthority('sys:audit:statistics')")
    @ApiResponses(@ApiResponse(code = HttpStatus.SC_OK, message = "成功", response = ClientUpgradeStatisticsVO.class))
    public ResponseResult ClientUpgradeStatistics(@RequestParam String startTime, @RequestParam String endTime, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return statisticsService.clientUpgradeStatistics(loginAdminDTO.getCompanyId(), startTime, endTime);
    }

    @PostMapping("cmd")
    @ApiOperation(value = "指令/策略统计")
    @PreAuthorize("hasAnyAuthority('sys:audit:statistics')")
    @ApiResponses(@ApiResponse(code = HttpStatus.SC_OK, message = "成功", response = CmdDayStatisticsVO.class))
    public ResponseResult cmdStatistics(@RequestParam String startTime, @RequestParam String endTime, @ApiIgnore Authentication authentication) throws ParseException {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        return statisticsService.cmdStatistics(loginAdminDTO.getCompanyId(), startTime, endTime);
    }

    @PostMapping("export")
    @ApiOperation(value = "导出")
    @PreAuthorize("hasAnyAuthority('sys:audit:statistics:export')")
    public void export(@RequestParam String startTime, @RequestParam String endTime, HttpServletResponse response,
                                 @ApiIgnore Authentication authentication) throws ParseException, IOException {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        statisticsService.export(loginAdminDTO, startTime, endTime, response);
    }

}