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

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.annotation.Query.handle.QueryGenerator;
import com.gci.boot.annotation.permission.DataPermission;
import com.gci.boot.common.dict.excel.MyExcelDictHandleImpl;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.gis.entity.TbGisStoppingAlarm;
import com.gci.boot.modules.gis.query.GisStoppingQuery;
import com.gci.boot.modules.gis.service.ITbGisStoppingAlarmService;
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;

/**
 * <p>
 * 停车超时记录表 前端控制器
 * </p>
 *
 * @author 2464
 * @since 2023-09-01
 */
@RestController
@RequestMapping("/gis/stoppingAlarm")
public class TbGisStoppingAlarmController {

    @Autowired
    ITbGisStoppingAlarmService iTbGisStoppingAlarmService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @DataPermission(item="tgsa.projectId", sqlIds = {"com.gci.boot.modules.gis.mapper.TbGisStoppingAlarmMapper.pageBuild"})
    @GetMapping(value = "/list")
    public Result<?> queryPageList(GisStoppingQuery query,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<TbGisStoppingAlarm> page = new Page<TbGisStoppingAlarm>(pageNo, pageSize);
        LambdaQueryWrapper<TbGisStoppingAlarm> lambdaQueryWrapper = QueryGenerator.initQueryWrapper(TbGisStoppingAlarm.class, query);
        IPage<TbGisStoppingAlarm> pageList = iTbGisStoppingAlarmService.pageBuild(page, lambdaQueryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    @DataPermission(item="tgsa.projectId", sqlIds = {"com.gci.boot.modules.gis.mapper.TbGisStoppingAlarmMapper.pageBuild"})
    public Result<?> export(HttpServletResponse response, GisStoppingQuery query) {
        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<TbGisStoppingAlarm> page = new Page<TbGisStoppingAlarm>(1, -1);
            LambdaQueryWrapper<TbGisStoppingAlarm> lambdaQueryWrapper = QueryGenerator.initQueryWrapper(TbGisStoppingAlarm.class, query);
            IPage<TbGisStoppingAlarm> pageList = iTbGisStoppingAlarmService.pageBuild(page, lambdaQueryWrapper);
            ExportParams exportParams = new ExportParams();
            exportParams.setDictHandler(new MyExcelDictHandleImpl());
            Workbook sheets = ExcelExportUtil.exportExcel(exportParams, TbGisStoppingAlarm.class, pageList.getRecords());
            sheets.write(output);
        } catch (IOException e) {
            return Result.error("导出错误！");
        }
        return Result.ok();
    }
}
