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.conditions.query.LambdaQueryWrapper;
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.validated.UpdateValidated;
import com.gci.boot.common.vo.Result;
import com.gci.boot.enums.FileExtension;
import com.gci.boot.modules.basedata.entity.TbInspectionTemplate;
import com.gci.boot.modules.customer.entity.TbCustomerTraining;
import com.gci.boot.modules.customer.query.CustomerTrainingQuery;
import com.gci.boot.modules.customer.service.ITbCustomerTrainingService;
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-12
 */
@RestController
@RequestMapping("/customer/customerTraining")
public class TbCustomerTrainingController {
    @Autowired
    private ITbCustomerTrainingService iTbCustomerTrainingService;
    @Autowired
    private MinioUtils minioUtils;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AddSysLog(value = "客户培训-分页列表查询")
    @GetMapping(value = "/list")
    @DataPermission(item="tb_customer_training.projectId", sqlIds = {"com.gci.boot.modules.customer.mapper.TbCustomerTrainingMapper.selectPage"})
    public Result<?> queryPageList(CustomerTrainingQuery query,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<TbCustomerTraining> page = new Page<TbCustomerTraining>(pageNo, pageSize);
        IPage<TbCustomerTraining> pageList = iTbCustomerTrainingService.pageBuild(page, query);
        return Result.ok(pageList);
    }

    /**
     * 添加
     */
    @AddSysLog(value = "客户培训-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Validated(SaveValidated.class) TbCustomerTraining tbCustomerTraining) {
        iTbCustomerTrainingService.saveItem(tbCustomerTraining);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     */
    @AddSysLog(value = "客户培训-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody @Validated(UpdateValidated.class) TbCustomerTraining tbCustomerTraining) {
        iTbCustomerTrainingService.updateItem(tbCustomerTraining);
        return Result.ok("编辑成功！");
    }

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

    /**
     * 批量删除
     */
    @AddSysLog(value = "客户培训-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        iTbCustomerTrainingService.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="tb_customer_training.projectId", sqlIds = {"com.gci.boot.modules.customer.mapper.TbCustomerTrainingMapper.selectPage"})
    public Result<?> export(HttpServletResponse response, CustomerTrainingQuery 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<TbCustomerTraining> page = new Page<TbCustomerTraining>(1, -1);
            IPage<TbCustomerTraining> pageList = iTbCustomerTrainingService.pageBuild(page, query);
            ExportParams exportParams = new ExportParams();
            exportParams.setDictHandler(new MyExcelDictHandleImpl());
            Workbook sheets = ExcelExportUtil.exportExcel(exportParams, TbCustomerTraining.class, pageList.getRecords());
            sheets.write(output);
        } catch (IOException e) {
            return Result.error("导出错误！");
        }
        return Result.ok();
    }
}
