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.TbGisSpeedingAlarm;
import com.gci.boot.modules.gis.entity.TbGisVehicleFaultAlarm;
import com.gci.boot.modules.gis.query.GisSpeedingQuery;
import com.gci.boot.modules.gis.query.GisVehicleFaultQuery;
import com.gci.boot.modules.gis.service.ITbGisVehicleFaultAlarmService;
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-09
 */
@RestController
@RequestMapping("/gis/vehicleFaultAlarm")
public class TbGisVehicleFaultAlarmController {
    @Autowired
    private ITbGisVehicleFaultAlarmService iTbGisVehicleFaultAlarmService;

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

    @DataPermission(item="temp_1.projectId", sqlIds = {"com.gci.boot.modules.gis.mapper.TbGisVehicleFaultAlarmMapper.countFault"})
    @GetMapping(value = "/countFault")
    public Result<?> countFault(@RequestParam(required = false) String projectId,
                                   HttpServletRequest req) {
        return Result.ok(iTbGisVehicleFaultAlarmService.countFault(projectId));
    }

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

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

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

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

    /**
     * 关闭告警
     */
    @DeleteMapping(value = "/close")
    public Result<?> close(@RequestParam(name = "ids") String ids,@RequestParam(name = "remark") String remark) {
        if (remark.length() > 500) {
            return Result.error("备注不能超过500字符！");
        }
        iTbGisVehicleFaultAlarmService.close(ids,remark);
        return Result.ok("关闭告警成功！");
    }

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