package com.unis.statisticalReport.info.modular.grainTempCheck.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.unis.common.pojo.CommonResult;
import com.unis.statisticalReport.info.modular.grainTempCheck.entity.GrainTemp;
import com.unis.statisticalReport.info.modular.grainTempCheck.entity.GrainTempExcel;
import com.unis.statisticalReport.info.modular.grainTempCheck.param.GrainTempPageParam;
import com.unis.statisticalReport.info.modular.grainTempCheck.service.GrainTempService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
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.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * @author qingsong.han
 * @description:
 * @create 2024-06-04 15:17
 */
@Api(tags = "粮情查询")
@ApiSupport(author = "UNIS_TEAM", order = 1)
@RestController
@Validated
@RequestMapping("/client/c")
public class GrainTempController {
    @Resource
    private GrainTempService grainTempService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("分页")
    @GetMapping("/grainTemp/getPage")
    public CommonResult<Page<GrainTemp>> getPage(GrainTempPageParam param) {
        return CommonResult.data(grainTempService.getPage(param));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("导出")
    @GetMapping("/grainTemp/exportExcel")
    public void exportExcel(String fileName, HttpServletResponse response) {
        List<GrainTempExcel> tempList = grainTempService.exportExcel();
        try {
            if (StringUtils.isEmpty(fileName))
                fileName = LocalDate.now().toString();
            fileName = URLEncoder.encode(fileName, "UTF-8");
            // 设置响应信息，让浏览器下载文件
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ExcelTypeEnum.XLSX.getValue());
            EasyExcel.write(response.getOutputStream()) // 或, GrainTempExcel.class
                    .head(GrainTempExcel.class) // write 中, GrainTempExcel.class 二选一
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet(fileName)
                    .doWrite(tempList);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
