package com.gci.boot.modules.customer.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.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.vo.Result;
import com.gci.boot.enums.FileExtension;
import com.gci.boot.modules.customer.entity.TbCustomerTraining;
import com.gci.boot.modules.customer.entity.TbInspectionTask;
import com.gci.boot.modules.customer.query.CustomerTrainingQuery;
import com.gci.boot.modules.customer.service.ITbInspectionTaskService;
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-10-16
 */
@RestController
@RequestMapping("/customer/inspectionTask")
public class TbInspectionTaskController {
    @Autowired
    private ITbInspectionTaskService iTbInspectionTaskService;
    @Autowired
    private MinioUtils minioUtils;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AddSysLog(value = "巡检任务-分页列表查询")
    @GetMapping(value = "/list")
    @DataPermission(item = "v.projectId",sqlIds = {"com.gci.boot.modules.customer.mapper.TbInspectionTaskMapper.pageBuild"})
    public Result<?> queryPageList(TbInspectionTask inspectionTask,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        Page<TbInspectionTask> page = new Page<TbInspectionTask>(pageNo, pageSize);
        IPage<TbInspectionTask> pageList = iTbInspectionTaskService.pageBuild(page, inspectionTask);
        return Result.ok(pageList);
    }

    /**
     * 添加
     */
    @AddSysLog(value = "巡检任务-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Validated(SaveValidated.class) TbInspectionTask tbInspectionTask) {
        iTbInspectionTaskService.saveItem(tbInspectionTask);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     */
    @AddSysLog(value = "巡检任务-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody TbInspectionTask tbInspectionTask) {
        iTbInspectionTaskService.updateItem(tbInspectionTask);
        return Result.ok("编辑成功！");
    }

    /**
     * 获取信息
     */
    @AddSysLog(value = "巡检任务-获取信息")
    @GetMapping(value = "/getById")
    public Result<?> getById(@RequestParam(name = "id") String id) {
        return Result.ok(iTbInspectionTaskService.getItemById(id));
    }

    /**
     * 删除
     */
    @AddSysLog(value = "巡检任务-删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        iTbInspectionTaskService.removeItem(id);
        return Result.ok("删除成功！");
    }

    /**
     * 批量删除
     */
    @AddSysLog(value = "巡检任务-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        iTbInspectionTaskService.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.uploadFileValidNotIn(file, FileExtension.EXE.getNameList()));
    }

    /**
     *  导出
     */
    @AddSysLog(value = "巡检任务-导出")
    @GetMapping("/export")
    @DataPermission(item = "v.projectId",sqlIds = {"com.gci.boot.modules.customer.mapper.TbInspectionTaskMapper.pageBuild"})
    public Result<?> export(HttpServletResponse response, TbInspectionTask inspectionTask){
        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<TbInspectionTask> page = new Page<TbInspectionTask>(1, -1);
            IPage<TbInspectionTask> pageList = iTbInspectionTaskService.pageBuild(page, inspectionTask);
            ExportParams exportParams = new ExportParams();
            exportParams.setDictHandler(new MyExcelDictHandleImpl());
            Workbook sheets = ExcelExportUtil.exportExcel(exportParams, TbInspectionTask.class, pageList.getRecords());
            sheets.write(output);
        } catch (IOException e) {
            return Result.error("导出错误！");
        }
        return Result.ok();
    }
}
