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

import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskTimeConsumingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskTimeConsumingRespVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskTimeConsumingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.log.RobotTaskTimeConsumingDO;
import cn.iocoder.yudao.module.system.service.log.RobotTaskTimeConsumingService;
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("/system/consuming/robot-task-time-consuming")
@Validated
public class RobotTaskTimeConsumingController {

    @Resource
    private RobotTaskTimeConsumingService robotTaskTimeConsumingService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆任务行程耗时")
    @PreAuthorize("@ss.hasPermission('consuming:robot-task-time-consuming:create')")
    public CommonResult<Long> createRobotTaskTimeConsuming(@Valid @RequestBody RobotTaskTimeConsumingSaveReqVO createReqVO) {
        return success(robotTaskTimeConsumingService.createRobotTaskTimeConsuming(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆任务行程耗时")
    @PreAuthorize("@ss.hasPermission('consuming:robot-task-time-consuming:update')")
    public CommonResult<Boolean> updateRobotTaskTimeConsuming(@Valid @RequestBody RobotTaskTimeConsumingSaveReqVO updateReqVO) {
        robotTaskTimeConsumingService.updateRobotTaskTimeConsuming(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆任务行程耗时")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('consuming:robot-task-time-consuming:delete')")
    public CommonResult<Boolean> deleteRobotTaskTimeConsuming(@RequestParam("id") Long id) {
        robotTaskTimeConsumingService.deleteRobotTaskTimeConsuming(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆任务行程耗时")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('consuming:robot-task-time-consuming:query')")
    public CommonResult<RobotTaskTimeConsumingRespVO> getRobotTaskTimeConsuming(@RequestParam("id") Long id) {
        RobotTaskTimeConsumingDO robotTaskTimeConsuming = robotTaskTimeConsumingService.getRobotTaskTimeConsuming(id);
        return success(BeanUtils.toBean(robotTaskTimeConsuming, RobotTaskTimeConsumingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆任务行程耗时分页")
    @PreAuthorize("@ss.hasPermission('consuming:robot-task-time-consuming:query')")
    public CommonResult<PageResult<RobotTaskTimeConsumingRespVO>> getRobotTaskTimeConsumingPage(@Valid RobotTaskTimeConsumingPageReqVO pageReqVO) {
        PageResult<RobotTaskTimeConsumingDO> pageResult = robotTaskTimeConsumingService.getRobotTaskTimeConsumingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotTaskTimeConsumingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆任务行程耗时 Excel")
    @PreAuthorize("@ss.hasPermission('consuming:robot-task-time-consuming:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRobotTaskTimeConsumingExcel(@Valid RobotTaskTimeConsumingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotTaskTimeConsumingDO> list = robotTaskTimeConsumingService.getRobotTaskTimeConsumingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆任务行程耗时.xls", "数据", RobotTaskTimeConsumingRespVO.class,
                        BeanUtils.toBean(list, RobotTaskTimeConsumingRespVO.class));
    }

}