package com.zcxk.evaluation.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcxk.evaluation.dto.EvaluationResultDto;
import com.zcxk.evaluation.enums.EvaluationResultStatus;
import com.zcxk.evaluation.service.EvaluationResultService;
import com.zcxk.evaluation.vo.EvaluationResultVo;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.utils.EvaluationUtil;
import com.zcxk.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName EvaluationResultController
 * @Description: 考评结果
 * @Author lihui
 * @Date 2021/5/7
 * @Version V1.0
 **/
@RestController
@RequestMapping("/evaluation/result")
@Api(tags = "考评结果")
public class EvaluationResultController {

    @Value("${UMIS.sys_excel_path}")
    private String baseDir;

    @Autowired
    private EvaluationResultService evaluationResultService;

    @ApiOperation(value = "查询考评结果")
    @RequestMapping(value = "/find/page", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Pagination<EvaluationResultVo>> findPage(@RequestBody @Valid EvaluationResultDto dto) {
        IPage<EvaluationResultVo> page = evaluationResultService.findPage(dto);
        if (page == null) {
            return new AjaxMessage<>(EvaluationResultStatus.DATA_NOT_FOUND.getStatus(), EvaluationResultStatus.DATA_NOT_FOUND.getMessage(),null);
        }
        return new AjaxMessage<>(ResultStatus.OK, new Pagination<>(page));
    }

    @ApiOperation(value = "考评结果导出")
    @RequestMapping(value = "/export/excel", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<String> exportExcel(@RequestBody @Valid EvaluationResultDto dto) {
        List<EvaluationResultVo> dataList = evaluationResultService.listEvaluationResult(dto);
        if (CollectionUtil.isEmpty(dataList)) {
            return new AjaxMessage<>(EvaluationResultStatus.DATA_NOT_FOUND.getStatus(), EvaluationResultStatus.DATA_NOT_FOUND.getMessage(),null);
        }
        List<List<String>> reportTitle = EvaluationUtil.getExcelTitle();
        String filePath = ExcelUtil.writeSimpleExcelWithHeader(baseDir, "工单绩效报表", reportTitle, EvaluationUtil.getExcelData(dataList));
        return new AjaxMessage<>(ResultStatus.OK, filePath);
    }

}

