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.TbAnnualInspectionInfo;
import com.gci.boot.modules.vehicles.entity.TbConsumablesTemplate;
import com.gci.boot.modules.vehicles.query.AnnualInspectionInfoQuery;
import com.gci.boot.modules.vehicles.query.ConsumablesTemplateQuery;
import com.gci.boot.modules.vehicles.service.ITbConsumablesTemplateService;
import com.gci.boot.modules.vehicles.vo.ConsumablesTemplateVo;
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.ArrayList;
import java.util.Arrays;

/**
 * <p>
 * 耗材模板 前端控制器
 * </p>
 *
 * 弃用啦
 *
 * @author 2464
 * @since 2023-09-12
 */
@RestController
@RequestMapping("/vehicles/consumablesTemplate")
public class TbConsumablesTemplateController {
    @Autowired
    private ITbConsumablesTemplateService iTbConsumablesTemplateService;

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

    /**
     * 添加
     */
    @AddSysLog(value = "耗材模板-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Validated(SaveValidated.class) TbConsumablesTemplate tbConsumablesTemplate) {
        iTbConsumablesTemplateService.saveItem(tbConsumablesTemplate);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     */
    @AddSysLog(value = "耗材模板-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody @Validated(UpdateValidated.class) TbConsumablesTemplate tbConsumablesTemplate) {
        iTbConsumablesTemplateService.updateItem(tbConsumablesTemplate);
        return Result.ok("编辑成功！");
    }

    /**
     * 根据id获取信息
     */
    @AddSysLog(value = "耗材模板-根据id获取信息")
    @GetMapping("/getByProjectIdAndVehicleType")
    public Result<?> getByProjectId(@RequestParam("projectId") String projectId,@RequestParam("vehicleType")String vehicleType) {
        TbConsumablesTemplate tbConsumablesTemplate = iTbConsumablesTemplateService.getByProjectIdAndVehicleType(projectId,vehicleType);
        return Result.ok(tbConsumablesTemplate);
    }
    /**
     * 删除
     */
    @AddSysLog(value = "耗材模板-删除")
    @DeleteMapping(value = "/deleteByProjectIdAndVehicleType")
    public Result<?> deleteByProjectId(@RequestParam(name = "projectId", required = true) String projectId,@RequestParam("vehicleType")String vehicleType) {
        iTbConsumablesTemplateService.removeItem(projectId,vehicleType);
        return Result.ok("删除成功！");
    }

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

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