package com.jwipc.spe.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.spe.entity.SupAppraisePlan;
import com.jwipc.spe.logic.SupAppraisePlanLogic;
import com.jwipc.spe.model.dto.ScoreReportDTO;
import com.jwipc.spe.model.query.ScoreReportQuery;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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.spe.constant.ModuleConst.MODULE_ROOT;

/**
 * @Author: swq
 * @Date: 2024/3/26 11:32
 */
@RestController
@RequestMapping(MODULE_ROOT + "/appraisalScoreReport/rest/")
@RequiredArgsConstructor
public class AppraisalScoreReportRestController {
    private final SupAppraisePlanLogic supAppraisePlanLogic;
    /**
     * 绩效考核报表查询
     *
     * @param query 查询参数
     * @return
     */
    @GetMapping("list")
    public PageView list(ScoreReportQuery query) {
        return supAppraisePlanLogic.scoreReportList(query);
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping("/planYearList")
    public ResponseEntity<List<SupAppraisePlan>> planList() {

        return ResponseEntity.ok(supAppraisePlanLogic.selectPlanYearList());
    }

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