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


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
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.validated.SaveValidated;
import com.gci.boot.common.validated.UpdateValidated;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.vehicles.entity.TbCustomizedTemplate;
import com.gci.boot.modules.vehicles.query.ConsumablesTemplateQuery;
import com.gci.boot.modules.vehicles.query.CustomizedTemplateQuery;
import com.gci.boot.modules.vehicles.service.ITbCustomizedTemplateService;
import com.gci.boot.modules.vehicles.vo.ConsumablesTemplateVo;
import com.gci.boot.modules.vehicles.vo.CustomizedTemplateVo;
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 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-12
 */
@RestController
@RequestMapping("/vehicles/customizedTemplate")
public class TbCustomizedTemplateController {
    @Autowired
    private ITbCustomizedTemplateService iTbCustomizedTemplateService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AddSysLog(value = "定制件模板-分页列表查询")
    @GetMapping(value = "/list")
    @DataPermission(item = "tct.projectId",sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbCustomizedTemplateMapper.listVo"})
    public Result<?> queryPageList(CustomizedTemplateQuery query,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<CustomizedTemplateVo> page = new Page<CustomizedTemplateVo>(pageNo, pageSize);
        IPage<CustomizedTemplateVo> list =  iTbCustomizedTemplateService.pageBuild(page,query);
        return Result.ok(list);
    }

    /**
     * 添加
     */
    @AddSysLog(value = "定制件模板-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Validated(SaveValidated.class) TbCustomizedTemplate tbCustomizedTemplate) {
        iTbCustomizedTemplateService.saveItem(tbCustomizedTemplate);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     */
    @AddSysLog(value = "定制件模板-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody @Validated(UpdateValidated.class) TbCustomizedTemplate tbCustomizedTemplate) {
        iTbCustomizedTemplateService.updateItem(tbCustomizedTemplate);
        return Result.ok("编辑成功！");
    }

    /**
     * 根据id获取信息
     */
    @AddSysLog(value = "定制件模板-根据id获取信息")
    @GetMapping("/getByProjectIdAndVehicleType")
    public Result<?> getByProjectIdAndVehicleType(@RequestParam("projectId") String projectId,@RequestParam("vehicleType") String vehicleType) {
        TbCustomizedTemplate tbCustomizedTemplate = iTbCustomizedTemplateService.getByProjectIdAndVehicleType(projectId,vehicleType);
        return Result.ok(tbCustomizedTemplate);
    }

    /**
     * 删除
     */
    @AddSysLog(value = "定制件模板-删除")
    @DeleteMapping(value = "/deleteByProjectIdAndVehicleType")
    public Result<?> deleteByProjectIdAndVehicleType(@RequestParam(name = "projectId") String projectId,@RequestParam("vehicleType") String vehicleType) {
        iTbCustomizedTemplateService.removeItem(projectId,vehicleType);
        return Result.ok("删除成功！");
    }

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

    /**
     * 导出
     */
    @AddSysLog(value = "定制件模板-导出")
    @GetMapping("/export")
    @DataPermission(item = "tct.projectId",sqlIds = {"com.gci.boot.modules.vehicles.mapper.TbCustomizedTemplateMapper.listVo"})
    public Result<?> export(HttpServletResponse response, CustomizedTemplateQuery 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<CustomizedTemplateVo> page = new Page<CustomizedTemplateVo>(1, -1);
            IPage<CustomizedTemplateVo> pageList = iTbCustomizedTemplateService.pageBuild(page, query);
            ExportParams exportParams = new ExportParams();
            exportParams.setDictHandler(new MyExcelDictHandleImpl());
            exportParams.setAutoSize(true);
            Workbook sheets = ExcelExportUtil.exportExcel(exportParams, CustomizedTemplateVo.class, pageList.getRecords());
            sheets.write(output);
        } catch (IOException e) {
            return Result.error("导出错误！");
        }
        return Result.ok();
    }
}
