package cn.iocoder.yudao.module.system.controller.admin.robot;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotModelPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotModelRespVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotModelSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotModelDO;
import cn.iocoder.yudao.module.system.service.robot.RobotModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 车辆类型")
@RestController
@RequestMapping("/system/robot/model")
@Validated
public class RobotModelController {

    @Resource
    private RobotModelService modelService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆类型")
    @PreAuthorize("@ss.hasPermission('robot:model:create')")
    public CommonResult<Long> createModel(@Valid @RequestBody RobotModelSaveReqVO createReqVO) {
        return success(modelService.createModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆类型")
    @PreAuthorize("@ss.hasPermission('robot:model:update')")
    public CommonResult<Boolean> updateModel(@Valid @RequestBody RobotModelSaveReqVO updateReqVO) {
        modelService.updateModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:model:delete')")
    public CommonResult<Boolean> deleteModel(@RequestParam("id") Long id) {
        modelService.deleteModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:model:query')")
    public CommonResult<RobotModelRespVO> getModel(@RequestParam("id") Long id) {
        RobotModelDO model = modelService.getModel(id);
        return success(BeanUtils.toBean(model, RobotModelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆类型分页")
    @PreAuthorize("@ss.hasPermission('robot:model:query')")
    public CommonResult<PageResult<RobotModelRespVO>> getModelPage(@Valid RobotModelPageReqVO pageReqVO) {
        PageResult<RobotModelDO> pageResult = modelService.getModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotModelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆类型 Excel")
    @PreAuthorize("@ss.hasPermission('robot:model:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportModelExcel(@Valid RobotModelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotModelDO> list = modelService.getModelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆类型.xls", "数据", RobotModelRespVO.class,
                        BeanUtils.toBean(list, RobotModelRespVO.class));
    }

    @GetMapping("/getAllModel")
    @Operation(summary = "获得车辆类型分页")
    @PreAuthorize("@ss.hasPermission('robot:model:getAllModel')")
    public CommonResult<List<RobotModelDO>> getAllModel() {
        List<RobotModelDO> list = modelService.getAllModel();
        return success(list);
    }

}