package com.usky.fire.controller.web;


import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.fire.domain.DemVehicle;
import com.usky.fire.service.DemVehicleService;
import com.usky.fire.service.vo.DemVehicleExportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 车辆信息 前端控制器
 * </p>
 *
 * @author JCB
 * @since 2022-10-19
 */
@RestController
@RequestMapping("/demVehicle")
public class DemVehicleController {

    @Autowired
    private DemVehicleService demVehicleService;

    /**
     * 消防车辆管理-列表查询
     *
     * @param vehicleCode      车辆编号
     * @param vehicleAbbreviat 车辆简称
     * @param id               主键ID
     * @param pageNum          当前页
     * @param pageSize         每页条数
     * @return
     */
    @GetMapping("vehicleList")
    public ApiResult<CommonPage<Object>> vehicleList(@RequestParam(value = "vehicleCode", required = false) String vehicleCode,
                                                     @RequestParam(value = "vehicleAbbreviat", required = false) String vehicleAbbreviat,
                                                     @RequestParam(value = "id", required = false, defaultValue = "0") Integer id,
                                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(demVehicleService.vehicleList(vehicleCode, vehicleAbbreviat, id, pageNum, pageSize));
    }

    /**
     * 消防车辆管理-列表查询-导出
     *
     * @param vehicleCode      车辆编号
     * @param vehicleAbbreviat 车辆简称
     * @param exportTitle      文件名
     * @throws java.io.IOException
     */
    @PostMapping("vehicleListExport")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "vehicleCode", required = false) String vehicleCode,
                       @RequestParam(value = "vehicleAbbreviat", required = false) String vehicleAbbreviat,
                       @RequestParam(value = "exportTitle") String exportTitle)throws IOException{
        List<DemVehicleExportVO> list = demVehicleService.vehicleListExport(vehicleCode, vehicleAbbreviat);
        ExcelUtil<DemVehicleExportVO> util = new ExcelUtil<DemVehicleExportVO>(DemVehicleExportVO.class);
        util.exportExcel(response, list, exportTitle, exportTitle);
    }

    /**
     * 消防车辆管理-修改
     *
     * @param demVehicle
     */
    @Log(title = "消防车辆管理-修改", businessType = BusinessType.UPDATE)
    @PutMapping("updateVehicle")
    public ApiResult<Void> updateVehicle(@RequestBody DemVehicle demVehicle) {
        demVehicleService.updateVehicle(demVehicle);
        return ApiResult.success();
    }

    /**
     * 消防车辆管理-删除
     *
     * @param id 主键ID
     */
    @Log(title = "消防车辆管理-删除", businessType = BusinessType.DELETE)
    @DeleteMapping("delVehicle")
    public ApiResult<Void> delVehicle(@RequestParam(value = "id") Integer id) {
        demVehicleService.delVehicle(id);
        return ApiResult.success();
    }

}

