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


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
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.TbGisGpsStatistics;
import com.gci.boot.modules.gis.service.ITbGisGpsStatisticsService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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.Arrays;

/**
 * <p>
 * 车辆gps相关数据统计 前端控制器
 * </p>
 *
 * @author 2464
 * @since 2023-10-31
 */
@RestController
@RequestMapping("/gis/gpsStatistics")
public class TbGisGpsStatisticsController {
    @Autowired
    private ITbGisGpsStatisticsService iTbGisGpsStatisticsService;

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

    /**
     * 添加
     */
//    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody TbGisGpsStatistics tbGisGpsStatistics) {
        iTbGisGpsStatisticsService.save(tbGisGpsStatistics);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     */
//    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody TbGisGpsStatistics tbGisGpsStatistics) {
        iTbGisGpsStatisticsService.updateById(tbGisGpsStatistics);
        return Result.ok("编辑成功！");
    }

    /**
     * 删除
     */
//    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        iTbGisGpsStatisticsService.removeById(id);
        return Result.ok("删除成功！");
    }

    /**
     * 批量删除
     */
//    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        iTbGisGpsStatisticsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 提供一个方法外部生成
     */
    @GetMapping(value = "/init")
    public Result<?> init(@RequestParam(name = "time") String time) {
        iTbGisGpsStatisticsService.init(time);
        return Result.ok("生成成功");
    }

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