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

import cn.iocoder.yudao.module.system.controller.admin.remote.vo.RemoteControllerInformationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.remote.vo.RemoteControllerInformationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.remote.vo.RemoteControllerInformationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.remote.RemoteControllerInformationDO;
import cn.iocoder.yudao.module.system.service.remote.RemoteControllerInformationService;
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/remote/controller-information") //不能修改/删除
@Validated
public class RemoteControllerInformationController {

    @Resource
    private RemoteControllerInformationService controllerInformationService;

    @PostMapping("/create")
    @Operation(summary = "创建远遥设备信息")
    @PreAuthorize("@ss.hasPermission('remote:controller-information:create')")
    public CommonResult<Long> createControllerInformation(@Valid @RequestBody RemoteControllerInformationSaveReqVO createReqVO) {
        return success(controllerInformationService.createControllerInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新远遥设备信息")
    @PreAuthorize("@ss.hasPermission('remote:controller-information:update')")
    public CommonResult<Boolean> updateControllerInformation(@Valid @RequestBody RemoteControllerInformationSaveReqVO updateReqVO) {
        controllerInformationService.updateControllerInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除远遥设备信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('remote:controller-information:delete')")
    public CommonResult<Boolean> deleteControllerInformation(@RequestParam("id") Long id) {
        controllerInformationService.deleteControllerInformation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得远遥设备信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('remote:controller-information:query')")
    public CommonResult<RemoteControllerInformationRespVO> getControllerInformation(@RequestParam("id") Long id) {
        RemoteControllerInformationDO controllerInformation = controllerInformationService.getControllerInformation(id);
        return success(BeanUtils.toBean(controllerInformation, RemoteControllerInformationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得远遥设备信息分页")
    @PreAuthorize("@ss.hasPermission('remote:controller-information:query')")
    public CommonResult<PageResult<RemoteControllerInformationRespVO>> getControllerInformationPage(@Valid RemoteControllerInformationPageReqVO pageReqVO) {
        PageResult<RemoteControllerInformationDO> pageResult = controllerInformationService.getControllerInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RemoteControllerInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出远遥设备信息 Excel")
    @PreAuthorize("@ss.hasPermission('remote:controller-information:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportControllerInformationExcel(@Valid RemoteControllerInformationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RemoteControllerInformationDO> list = controllerInformationService.getControllerInformationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "远遥设备信息.xls", "数据", RemoteControllerInformationRespVO.class,
                        BeanUtils.toBean(list, RemoteControllerInformationRespVO.class));
    }

    /**
     * 不能修改/删除
     * @return
     */
    @PostMapping("/checkAddress")
    @Operation(summary = "校验远遥的地址")
    @PermitAll
    public String checkAddress(@RequestBody RemoteControllerInformationSaveReqVO createReqVO) {
        return createReqVO.getSystemIp();
    }
}