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

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.gis.entity.RefuelAlarm;
import com.gci.boot.modules.gis.service.IRefuelAlarmService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * 油耗异常
 * @author 24642
 */
@Slf4j
@RestController
@RequestMapping("/gis/refuelAlarm")
public class RefuelAlarmController {

    @Autowired
    private IRefuelAlarmService refuelAlarmService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(RefuelAlarm query,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<RefuelAlarm> page = new Page<RefuelAlarm>(pageNo, pageSize);
        IPage<RefuelAlarm> pageList = refuelAlarmService.pageBuild(page, query);
        return Result.ok(pageList);
    }

    /**
     * 导出
     * @return
     */
    @GetMapping("/export")
    public Result<?> export(RefuelAlarm query, 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);
            Page<RefuelAlarm> page = new Page<RefuelAlarm>(1, -1);
            IPage<RefuelAlarm> pageList = refuelAlarmService.pageBuild(page, query);
            ExportParams exportParams = new ExportParams();
            exportParams.setDictHandler(new MyExcelDictHandleImpl());
            Workbook sheets = ExcelExportUtil.exportExcel(exportParams, RefuelAlarm.class, pageList.getRecords());
            sheets.write(output);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return Result.error("导出错误！");
        }
        return Result.ok();
    }

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/count")
    public Result<?> queryCount(RefuelAlarm query,
                                   HttpServletRequest req) {
        JSONObject entries = new JSONObject();
        if (ObjectUtil.isAllEmpty(query.getStartTime(),query.getEndTime())) {

        }
        return Result.ok();
    }
}
