package com.retdata.yaoyibi.web;

import com.retdata.yaoyibi.domain.*;
import com.retdata.yaoyibi.service.WmScorePackageService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.uuid.UUID;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping("/score-packages")
public class WmScorePackageController {

    private final WmScorePackageService wmScorePackageService;

    public WmScorePackageController(WmScorePackageService wmScorePackageService) {
        this.wmScorePackageService = wmScorePackageService;
    }

    @GetMapping
    public Page<WmScorePackageSearchDto> search(@ParameterObject WmScorePackageSearchParams searchParams,
                                                @ParameterObject Pageable pageable) {
        return wmScorePackageService.search(searchParams, pageable);
    }

    @GetMapping("/root-score-package-senders")
    public List<CsoSysDeptSummary> listRootScorePackageSenders(@RequestParam(value = "keyword", required = false) String keyword) {
        return wmScorePackageService.listRootScorePackageSenders(keyword);
    }

    @GetMapping("/export")
    public String export(@ParameterObject WmScorePackageSearchParams searchParams) throws IOException {
        try (XSSFWorkbook workbook = wmScorePackageService.export(searchParams)) {
            String filename = UUID.randomUUID() + "_积分包数据.xlsx";
            Path path = Paths.get(RuoYiConfig.getDownloadPath(), filename);
            Files.createDirectories(path.getParent());
            try (OutputStream fos = Files.newOutputStream(path)) {
                workbook.write(fos);
            }
            return filename;
        }
    }

    @GetMapping("/score-stat")
    public ScoreStat getScoreStatByParams(@ParameterObject WmScorePackageSearchParams searchParams) {
        return wmScorePackageService.getScoreStatByParams(searchParams);
    }

    @GetMapping("/score-time-stat")
    public List<ScoreTimeStat> getScoreTimeStatByParams(@ParameterObject WmScorePackageSearchParams searchParams,
                                                        @RequestParam("statUnit") String statUnit) {
        return wmScorePackageService.getScoreTimeStatByParams(searchParams, statUnit);
    }

    @GetMapping("/approved-score-stat")
    public ScoreStat getApprovedScoreStatByParams(@ParameterObject WmScorePackageSearchParams searchParams) {
        return wmScorePackageService.getApprovedScoreStatByParams(searchParams);
    }

    @GetMapping("/approved-score-time-stat")
    public GetApprovedScoreTimeStatByParamsResult getApprovedScoreTimeStatByParams(
            @ParameterObject WmScorePackageSearchParams searchParams,
            @RequestParam("statUnit") String statUnit) {
        return wmScorePackageService.getApprovedScoreTimeStatByParams(searchParams, statUnit);
    }

    @GetMapping("/settled-stat")
    public SettledStat getSettledStatByParams(@ParameterObject WmScorePackageSearchParams searchParams) {
        return wmScorePackageService.getSettledStatByParams(searchParams);
    }

    @GetMapping("/settled-time-stat")
    public SettledTimeStatResult getSettledTimeStatByParams(@ParameterObject WmScorePackageSearchParams searchParams,
                                                            @RequestParam("statUnit") String statUnit) {
        return wmScorePackageService.getSettledTimeStatByParams(searchParams, statUnit);
    }

    @GetMapping("/receiver-names")
    public List<String> listReceiverNames(@RequestParam(value = "keyword", required = false) String keyword,
                                          @RequestParam("limit") int limit) {
        return wmScorePackageService.listReceiverNames(keyword, limit);
    }

}
