package jnpf.culture.controller;

import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.culture.service.CultureStatService;
import jnpf.model.culture.dto.StatQueryDto;
import jnpf.model.culture.vo.CultureStatVo;
import jnpf.util.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.rmi.server.ExportException;

/**
 * 文化打卡 - 统计
 *
 * @author yanwenfu
 * @create 2025-12-23
 */
@RestController
@RequestMapping(value = "/culture/stat")
public class CultureStatController {

    @Resource
    private CultureStatService cultureStatService;

    @Resource
    private ExportUtil exportUtil;

    /**
     * 打卡统计 - 列表(分页)
     * @param queryDto 查询dto
     * @return jnpf.base.ActionResult<jnpf.base.vo.PageListVO<jnpf.model.culture.vo.CultureStatVo>>
     */
    @PostMapping(value = "/page")
    public ActionResult<PageListVO<CultureStatVo>> getStatPage(@RequestBody StatQueryDto queryDto) {

        PageInfo<CultureStatVo> page = cultureStatService.getStatPage(queryDto);
        return ActionResult.page(page.getList(), FtbUtil.getPagination(page));
    }

    /**
     * 打卡统计 - 导出
     * @param queryDto 查询条件
     * @param response HttpServletResponse
     */
    @PostMapping(value = "/export")
    public void getStatExport(@RequestBody StatQueryDto queryDto, HttpServletResponse response) throws Exception {

        Workbook workbook = cultureStatService.getStatWorkbook(queryDto);
        if (null != workbook) {
            exportUtil.exportTemplateExcel(workbook, response, ConstantUtil.CULTURE_CLOCK_IN_FORM);
        } else {
            throw new ExportException("导出" + ConstantUtil.CULTURE_CLOCK_IN_FORM + "失败");
        }
    }
}
