package cn.iocoder.yudao.module.custom.controller.admin;

import cn.iocoder.yudao.module.custom.entity.CarMain;
import cn.iocoder.yudao.module.custom.service.CarMainService;
import cn.iocoder.yudao.module.custom.vo.CarMainReqVO;
import cn.iocoder.yudao.module.custom.vo.CarMainRespVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 车辆信息")
@RestController
@RequestMapping("/base/carMain")
@Validated
public class CarMainController {

    @Resource
    private CarMainService carMainService;

    /**
     * 创建车辆信息
     *
     * @param createReqVO
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "创建车辆信息")
    @PreAuthorize("@ss.hasPermission('base:car-main:create')")
    public CommonResult<String> createCarMain(@RequestBody CarMainReqVO createReqVO) {
        return carMainService.createCarMain(createReqVO);
    }

    /**
     * 更新车辆信息
     *
     * @param updateReqVO
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "更新车辆信息")
    @PreAuthorize("@ss.hasPermission('base:car-main:update')")
    public CommonResult<String> updateCarMain(@RequestBody CarMainReqVO updateReqVO) {
        return carMainService.updateCarMain(updateReqVO);
    }

    /**
     * 删除车辆信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:car-main:delete')")
    public CommonResult<Boolean> deleteCarMain(@RequestParam("id") String id) {
        carMainService.deleteCarMain(id);
        return success(true);
    }

    /**
     * 获得车辆信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "获得车辆信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:car-main:query')")
    public CommonResult<CarMainRespVO> getCarMain(@RequestParam("id") String id) {
        CarMain carMain = carMainService.getCarMain(id);
        return success(BeanUtils.toBean(carMain, CarMainRespVO.class));
    }

    /**
     * 获得车辆信息分页
     *
     * @param pageReqVO
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "获得车辆信息分页")
    @PreAuthorize("@ss.hasPermission('base:car-main:query')")
    public CommonResult<IPage<CarMainRespVO>> getCarMainPage(CarMainReqVO pageReqVO) {
        IPage<CarMainRespVO> pageResult = carMainService.getCarMainPage(pageReqVO);
        return success(pageResult);
    }

    /**
     * 导出车辆信息
     *
     * @param pageReqVO
     * @param response
     * @throws IOException
     */
    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆信息 Excel")
    @PreAuthorize("@ss.hasPermission('base:car-main:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCarMainExcel(CarMainReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CarMainRespVO> list = carMainService.getCarMainPage(pageReqVO).getRecords();
        // 导出 Excel
        ExcelUtils.write(response, "车辆信息.xls", "数据", CarMainRespVO.class,
                BeanUtils.toBean(list, CarMainRespVO.class));
    }

}