package com.gci.boot.modules.statis.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.common.dict.excel.MyExcelDictHandleImpl;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.basedata.entity.TbCustomerFollowTemplate;
import com.gci.boot.modules.statis.vo.StatisParamVO;
import com.gci.boot.modules.statis.vo.WeighbridgeStatisVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Api(tags="地磅数据统计")
@Slf4j
@RestController
@RequestMapping("/statis/weighbridge")
public class WeighbridgeStatisController {


    /**
     * 单纯写死的数据
     * @param vo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StatisParamVO vo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<WeighbridgeStatisVo> page = new Page<WeighbridgeStatisVo>(pageNo, pageSize);
        page.setRecords(new ArrayList<WeighbridgeStatisVo>());
        return Result.ok(page);
    }
    /**
     * 导出
     * @return
     */
    @GetMapping("/export")
    public Result<?> export(StatisParamVO vo, HttpServletResponse response){
        try (OutputStream output = response.getOutputStream()){

            String fileName = "地磅数据统计.xlsx";
            try {
                fileName = URLEncoder.encode(fileName, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
            List<WeighbridgeStatisVo> weighbridgeStatisVos = new ArrayList<>();
            ExportParams exportParams = new ExportParams();
            exportParams.setDictHandler(new MyExcelDictHandleImpl());
            Workbook sheets = ExcelExportUtil.exportExcel(exportParams, WeighbridgeStatisVo.class, weighbridgeStatisVos);
            sheets.write(output);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return Result.error("导出错误！");
        }
        return Result.ok();
    }
}
