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

import cn.iocoder.yudao.module.system.controller.admin.remote.vo.ControllerTaskTransferLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.remote.vo.ControllerTaskTransferLogRespVO;
import cn.iocoder.yudao.module.system.controller.admin.remote.vo.ControllerTaskTransferLogSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.remote.ControllerTaskTransferLogDO;
import cn.iocoder.yudao.module.system.service.remote.ControllerTaskTransferLogService;
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.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("/remote/controller-task-transfer-log")
@Validated
public class ControllerTaskTransferLogController {

    @Resource
    private ControllerTaskTransferLogService controllerTaskTransferLogService;

    @PostMapping("/create")
    @Operation(summary = "创建远遥任务转移记录")
    @PreAuthorize("@ss.hasPermission('remote:controller-task-transfer-log:create')")
    public CommonResult<Long> createControllerTaskTransferLog(@Valid @RequestBody ControllerTaskTransferLogSaveReqVO createReqVO) {
        return success(controllerTaskTransferLogService.createControllerTaskTransferLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新远遥任务转移记录")
    @PreAuthorize("@ss.hasPermission('remote:controller-task-transfer-log:update')")
    public CommonResult<Boolean> updateControllerTaskTransferLog(@Valid @RequestBody ControllerTaskTransferLogSaveReqVO updateReqVO) {
        controllerTaskTransferLogService.updateControllerTaskTransferLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除远遥任务转移记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('remote:controller-task-transfer-log:delete')")
    public CommonResult<Boolean> deleteControllerTaskTransferLog(@RequestParam("id") Long id) {
        controllerTaskTransferLogService.deleteControllerTaskTransferLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得远遥任务转移记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('remote:controller-task-transfer-log:query')")
    public CommonResult<ControllerTaskTransferLogRespVO> getControllerTaskTransferLog(@RequestParam("id") Long id) {
        ControllerTaskTransferLogDO controllerTaskTransferLog = controllerTaskTransferLogService.getControllerTaskTransferLog(id);
        return success(BeanUtils.toBean(controllerTaskTransferLog, ControllerTaskTransferLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得远遥任务转移记录分页")
    @PreAuthorize("@ss.hasPermission('remote:controller-task-transfer-log:query')")
    public CommonResult<PageResult<ControllerTaskTransferLogRespVO>> getControllerTaskTransferLogPage(@Valid ControllerTaskTransferLogPageReqVO pageReqVO) {
        PageResult<ControllerTaskTransferLogDO> pageResult = controllerTaskTransferLogService.getControllerTaskTransferLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ControllerTaskTransferLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出远遥任务转移记录 Excel")
    @PreAuthorize("@ss.hasPermission('remote:controller-task-transfer-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportControllerTaskTransferLogExcel(@Valid ControllerTaskTransferLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ControllerTaskTransferLogDO> list = controllerTaskTransferLogService.getControllerTaskTransferLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "远遥任务转移记录.xls", "数据", ControllerTaskTransferLogRespVO.class,
                        BeanUtils.toBean(list, ControllerTaskTransferLogRespVO.class));
    }

}