package com.qmth.paper.library.api;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.boot.tools.excel.ExcelWriter;
import com.qmth.boot.tools.excel.enums.ExcelType;
import com.qmth.paper.library.business.bean.result.ScanStatExportResult;
import com.qmth.paper.library.business.service.PaperScanStatService;
import com.qmth.paper.library.common.contant.ApiPrefixConstant;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.enums.ExceptionResultEnum;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import io.swagger.annotations.*;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Description 扫描统计控制类
 */
@Api(tags = "扫描统计Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + ApiPrefixConstant.PREFIX_BASIC + "/scan/stat")
public class PaperScanStatController {

    @Resource
    private PaperScanStatService paperScanStatService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = Result.class) })
    public Result page(@ApiParam(value = "扫描日期") @RequestParam(value = "scanDate", required = false) Long scanDate,
            @ApiParam(value = "扫描员") @RequestParam(value = "scanner", required = false) String scanner,
            @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
            @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(paperScanStatService.pageScanStat(scanDate, scanner, pageNumber, pageSize));
    }

    @ApiOperation(value = "导出")
    @PostMapping(value = "/export")
    public void export(@ApiParam(value = "扫描日期") @RequestParam(value = "scanDate", required = false) Long scanDate,
            @ApiParam(value = "扫描员") @RequestParam(value = "scanner", required = false) String scanner, HttpServletResponse response) {
        try {
            String fileName = URLEncoder.encode("扫描统计", "UTF-8");
            response.setHeader("Content-Disposition", "inline; filename=" + fileName + ".xlsx");
            response.setContentType("application/vnd.ms-excel");
            ExcelWriter writer = ExcelWriter.create(ExcelType.XLSX);
            List<ScanStatExportResult> scanStatList = paperScanStatService.listScanStat(scanDate, scanner);
            if (CollectionUtils.isEmpty(scanStatList)) {
                throw ExceptionResultEnum.ERROR.exception("未开始扫描");
            }
            writer.writeObjects("扫描统计", null, ScanStatExportResult.class, scanStatList.iterator());
            writer.output(response.getOutputStream());
        } catch (IOException e) {
            throw ExceptionResultEnum.ERROR.exception(e.getMessage());
        }
    }

}
