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

import cn.iocoder.yudao.module.custom.entity.CarModel;
import cn.iocoder.yudao.module.custom.service.CarModelService;
import cn.iocoder.yudao.module.custom.vo.*;
import cn.iocoder.yudao.module.custom.vo.CarModelReqVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
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.PageResult;
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/carModel")
@Validated
public class CarModelController {

    @Autowired
    private CarModelService carModelService;

    /**
     * 创建车辆品牌型号
     * 
     * @param createReqVO
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "创建车辆品牌型号")
    @PreAuthorize("@ss.hasPermission('base:car-model:create')")
    public CommonResult<String> createCarModel(@RequestBody CarModelReqVO createReqVO) {
        return success(carModelService.createCarModel(createReqVO));
    }

    /**
     * 更新车辆品牌型号
     * 
     * @param updateReqVO
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "更新车辆品牌型号")
    @PreAuthorize("@ss.hasPermission('base:car-model:update')")
    public CommonResult<Boolean> updateCarModel(@RequestBody CarModelReqVO updateReqVO) {
        carModelService.updateCarModel(updateReqVO);
        return success(true);
    }

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

    /**
     * 获得车辆品牌型号
     * 
     * @param id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "获得车辆品牌型号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:car-model:query')")
    public CommonResult<CarModelRespVO> getCarModel(@RequestParam("id") String id) {
        CarModel carModel = carModelService.getCarModel(id);
        return success(BeanUtils.toBean(carModel, CarModelRespVO.class));
    }

    /**
     * 获得车辆品牌型号分页
     * 
     * @param pageReqVO
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "获得车辆品牌型号分页")
    @PreAuthorize("@ss.hasPermission('base:car-model:query')")
    public CommonResult<IPage<CarModelRespVO>> getCarModelPage(CarModelReqVO pageReqVO) {
        IPage<CarModelRespVO> pageResult = carModelService.getCarModelPage(pageReqVO);
        return success(pageResult);
    }

    /**
     * 获得车辆品牌型号分页
     *
     * @param pageReqVO
     * @return
     */
    @GetMapping("/pageById")
    @Operation(summary = "获得车辆品牌型号分页")
    @PreAuthorize("@ss.hasPermission('base:car-model:query')")
    public CommonResult<IPage<CarModelRespVO>> getCarModelPageById(CarModelReqVO pageReqVO) {
        IPage<CarModelRespVO> pageResult = carModelService.getCarModelPageByBrandId(pageReqVO);
        return success(pageResult);
    }

    /**
     * 导出车辆品牌型号
     * 
     * @param pageReqVO
     * @param response
     * @throws IOException
     */
    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆品牌型号 Excel")
    @PreAuthorize("@ss.hasPermission('base:car-model:export')")
    public void searchBrand(CarModelReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CarModelRespVO> list = carModelService.getCarModelPage(pageReqVO).getRecords();
        // 导出 Excel
        ExcelUtils.write(response, "车辆品牌型号.xls", "数据", CarModelRespVO.class, list);
    }

    /**
     * 筛选品牌型号
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/searchBrand")
    @Operation(summary = "获取品牌型号级联下拉")
    @PreAuthorize("@ss.hasPermission('base:car-main:query')")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<CascaderOptionsVO>> searchBrand(@RequestBody CarModelReqVO reqVO) {
        List<CascaderOptionsVO> result = carModelService.searchBrand(reqVO);
        return success(result);
    }

}