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

import cn.iocoder.yudao.module.custom.entity.CarBrand;
import cn.iocoder.yudao.module.custom.service.CarBrandService;
import cn.iocoder.yudao.module.custom.vo.CarBrandReqVO;
import cn.iocoder.yudao.module.custom.vo.CarBrandRespVO;
import cn.iocoder.yudao.module.custom.vo.CarBrandReqVO;
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.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/carBrand")
@Validated
public class CarBrandController {

    @Resource
    private CarBrandService carBrandService;

    /**
     * 创建车辆品牌
     *
     * @param createReqVO
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "创建车辆品牌")
    @PreAuthorize("@ss.hasPermission('base:car-brand:create')")
    public CommonResult<String> createCarBrand(@RequestBody CarBrandReqVO createReqVO) {
        return success(carBrandService.createCarBrand(createReqVO));
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得车辆品牌")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:car-brand:query')")
    public CommonResult<CarBrandRespVO> getCarBrand(@RequestParam("id") String id) {
        CarBrand carBrand = carBrandService.getCarBrand(id);
        return success(BeanUtils.toBean(carBrand, CarBrandRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆品牌分页")
    @PreAuthorize("@ss.hasPermission('base:car-brand:query')")
    public CommonResult<IPage<CarBrandRespVO>> getCarBrandPage(CarBrandReqVO pageReqVO) {
        IPage<CarBrandRespVO> pageResult = carBrandService.getCarBrandPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆品牌 Excel")
    @PreAuthorize("@ss.hasPermission('base:car-brand:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCarBrandExcel(CarBrandReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CarBrandRespVO> list = carBrandService.getCarBrandPage(pageReqVO).getRecords();
        // 导出 Excel
        ExcelUtils.write(response, "车辆品牌维护.xls", "数据", CarBrandRespVO.class, list);
    }

}