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

import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotWarnCodeMappingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotWarnCodeMappingRespVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.vo.RobotWarnCodeMappingSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.warn.RobotWarnCodeMappingVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotWarnCodeMappingDO;
import cn.iocoder.yudao.module.system.service.robot.RobotWarnCodeMappingService;
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.annotation.security.PermitAll;
import javax.validation.constraints.*;
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("/system/robot/warn-code-mapping")
@Validated
public class RobotWarnCodeMappingController {

    @Resource
    private RobotWarnCodeMappingService warnCodeMappingService;

    @PostMapping("/create")
    @Operation(summary = "创建机器人告警码值映射")
    @PreAuthorize("@ss.hasPermission('robot:warn-code-mapping:create')")
    public CommonResult<Long> createWarnCodeMapping(@Valid @RequestBody RobotWarnCodeMappingSaveReqVO createReqVO) {
        return success(warnCodeMappingService.createWarnCodeMapping(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人告警码值映射")
    @PreAuthorize("@ss.hasPermission('robot:warn-code-mapping:update')")
    public CommonResult<Boolean> updateWarnCodeMapping(@Valid @RequestBody RobotWarnCodeMappingSaveReqVO updateReqVO) {
        warnCodeMappingService.updateWarnCodeMapping(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器人告警码值映射")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:warn-code-mapping:delete')")
    public CommonResult<Boolean> deleteWarnCodeMapping(@RequestParam("id") Long id) {
        warnCodeMappingService.deleteWarnCodeMapping(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机器人告警码值映射")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:warn-code-mapping:query')")
    public CommonResult<RobotWarnCodeMappingRespVO> getWarnCodeMapping(@RequestParam("id") Long id) {
        RobotWarnCodeMappingDO warnCodeMapping = warnCodeMappingService.getWarnCodeMapping(id);
        return success(BeanUtils.toBean(warnCodeMapping, RobotWarnCodeMappingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机器人告警码值映射分页")
    @PreAuthorize("@ss.hasPermission('robot:warn-code-mapping:query')")
    public CommonResult<PageResult<RobotWarnCodeMappingRespVO>> getWarnCodeMappingPage(@Valid RobotWarnCodeMappingPageReqVO pageReqVO) {
        PageResult<RobotWarnCodeMappingDO> pageResult = warnCodeMappingService.getWarnCodeMappingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotWarnCodeMappingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人告警码值映射 Excel")
    @PreAuthorize("@ss.hasPermission('robot:warn-code-mapping:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarnCodeMappingExcel(@Valid RobotWarnCodeMappingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotWarnCodeMappingDO> list = warnCodeMappingService.getWarnCodeMappingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人告警码值映射.xls", "数据", RobotWarnCodeMappingRespVO.class,
                        BeanUtils.toBean(list, RobotWarnCodeMappingRespVO.class));
    }

    @GetMapping("/agv/query")
    @Operation(summary = "机器人下载告警码值映射文件")
    @PermitAll
    public void agvQueryWarnCode(HttpServletResponse response) {
        warnCodeMappingService.agvQueryWarnCode(response);
    }

}