package com.jwipc.finance.controller.rest;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.finance.logic.BudgetReportLogic;
import com.jwipc.finance.model.dto.BudgetReportDTO;
import com.jwipc.finance.model.query.BudgetReportQuery;
import com.jwipc.page.PageView;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.jwipc.finance.constant.ModuleConst.MODULE_ROOT;

/**
 * @Author: swq
 * @Date: 2024/4/7 10:19
 */
@RestController
@RequestMapping(MODULE_ROOT + "/budgetReport/rest")
@RequiredArgsConstructor
public class BudgetReportRestController {
    private final BudgetReportLogic budgetReportLogic;
    /**
     * 绩效考核报表查询
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("/list")
    public PageView list(BudgetReportQuery query) {
        return budgetReportLogic.budgetReportList(query);
    }

    /**
     * 导出Excel
     *
     * @param response
     * @param exportFiled 导出列
     * @param queryStr    导出查询参数
     */
    @PostMapping("/export")
    public void reportExportExcel(HttpServletResponse response, String exportFiled, String queryStr) throws
            IOException
    {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<BudgetReportDTO> exportData = budgetReportLogic.listAll(JSON.parseObject(queryStr, BudgetReportQuery.class));
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,
                "预算报表-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN), "sheet", exportData,
                exportFiled);
    }
}
