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.TbGisChargeLog;
import com.gci.boot.modules.gis.entity.TbGisFenceAlarm;
import com.gci.boot.modules.gis.query.GisChargeLogQuery;
import com.gci.boot.modules.gis.query.GisFenceAlarmQuery;
import com.gci.boot.modules.gis.service.ITbGisFenceAlarmService;
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>
 * 围栏告警记录表 前端控制器
 * </p>
 *
 * @author 2464
 * @since 2023-09-01
 */
@RestController
@RequestMapping("/gis/fenceAlarm")
public class TbGisFenceAlarmController {
    @Autowired
    private ITbGisFenceAlarmService tbGisFenceAlarmService;

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

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

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

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

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

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