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


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.util.ObjectUtil;
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.log.annotation.AddSysLog;
import com.gci.boot.common.util.MinioUtils;
import com.gci.boot.common.validated.SaveValidated;
import com.gci.boot.common.validated.UpdateValidated;
import com.gci.boot.common.vo.Result;
import com.gci.boot.enums.FileExtension;
import com.gci.boot.modules.gis.entity.TbGisVehicleFaultAlarm;
import com.gci.boot.modules.gis.query.GisVehicleFaultQuery;
import com.gci.boot.modules.vehicles.entity.TbAccidentInfo;
import com.gci.boot.modules.vehicles.query.AccidentInfoQuery;
import com.gci.boot.modules.vehicles.service.ITbAccidentInfoService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

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-13
 */
@RestController
@RequestMapping("/vehicles/accidentInfo")
public class TbAccidentInfoController {
    @Autowired
    private ITbAccidentInfoService iTbAccidentInfoService;
    @Autowired
    private MinioUtils minioUtils;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AddSysLog(value = "事故信息-分页列表查询")
    @GetMapping(value = "/list")
    @DataPermission(item="tai.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbAccidentInfoMapper.pageBuild"})
    public Result<?> queryPageList(AccidentInfoQuery query,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<TbAccidentInfo> page = new Page<TbAccidentInfo>(pageNo, pageSize);
        IPage<TbAccidentInfo> pageList = iTbAccidentInfoService.pageBuild(page, query);
        return Result.ok(pageList);
    }

    /**
     * 添加
     */
    @AddSysLog(value = "事故信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Validated(SaveValidated.class) TbAccidentInfo tbAccidentInfo) {
        iTbAccidentInfoService.saveItem(tbAccidentInfo);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     */
    @AddSysLog(value = "事故信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody @Validated(UpdateValidated.class) TbAccidentInfo tbAccidentInfo) {
        iTbAccidentInfoService.updateItem(tbAccidentInfo);
        return Result.ok("编辑成功！");
    }

    /**
     * 删除
     */
    @AddSysLog(value = "事故信息-删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        iTbAccidentInfoService.removeItem(id);
        return Result.ok("删除成功！");
    }

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

    /**
     * 文件上传
     */
    @AddSysLog(value = "事故信息-文件上传")
    @PostMapping("/upload")
    public Result<?> upload(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        if (ObjectUtil.isEmpty(file)) {
            return Result.error("请正确上传文件！");
        }
        return Result.ok(minioUtils.uploadFileValid(file, FileExtension.PIC.getNameList()));
    }

    /**
     * 文件上传视频
     */
    @AddSysLog(value = "事故信息-文件上传视频")
    @PostMapping("/uploadVideo")
    public Result<?> uploadVideo(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        if (ObjectUtil.isEmpty(file)) {
            return Result.error("请正确上传文件！");
        }
        return Result.ok(minioUtils.uploadFileValid(file, FileExtension.VID.getNameList()));
    }

    /**
     * 导出
     */
    @AddSysLog(value = "事故信息-导出")
    @GetMapping("/export")
    @DataPermission(item="tai.projectId", sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbAccidentInfoMapper.pageBuild"})
    public Result<?> export(HttpServletResponse response, AccidentInfoQuery 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<TbAccidentInfo> page = new Page<TbAccidentInfo>(1, -1);
            IPage<TbAccidentInfo> pageList = iTbAccidentInfoService.pageBuild(page, query);
            ExportParams exportParams = new ExportParams();
            exportParams.setDictHandler(new MyExcelDictHandleImpl());
            Workbook sheets = ExcelExportUtil.exportExcel(exportParams, TbAccidentInfo.class, pageList.getRecords());
            sheets.write(output);
        } catch (IOException e) {
            return Result.error("导出错误！");
        }
        return Result.ok();
    }
}
