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

import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskLineTimeConsumingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskLineTimeConsumingRespVO;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotTaskLineTimeConsumingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.log.RobotTaskLineTimeConsumingDO;
import cn.iocoder.yudao.module.system.service.log.RobotTaskLineTimeConsumingService;
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/robot/task-line-time-consuming")
@Validated
public class RobotTaskLineTimeConsumingController {

    @Resource
    private RobotTaskLineTimeConsumingService taskLineTimeConsumingService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆行走路线耗时记录")
    @PreAuthorize("@ss.hasPermission('robot:task-line-time-consuming:create')")
    public CommonResult<Long> createTaskLineTimeConsuming(@Valid @RequestBody RobotTaskLineTimeConsumingSaveReqVO createReqVO) {
        return success(taskLineTimeConsumingService.createTaskLineTimeConsuming(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆行走路线耗时记录")
    @PreAuthorize("@ss.hasPermission('robot:task-line-time-consuming:update')")
    public CommonResult<Boolean> updateTaskLineTimeConsuming(@Valid @RequestBody RobotTaskLineTimeConsumingSaveReqVO updateReqVO) {
        taskLineTimeConsumingService.updateTaskLineTimeConsuming(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆行走路线耗时记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:task-line-time-consuming:delete')")
    public CommonResult<Boolean> deleteTaskLineTimeConsuming(@RequestParam("id") Long id) {
        taskLineTimeConsumingService.deleteTaskLineTimeConsuming(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆行走路线耗时记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:task-line-time-consuming:query')")
    public CommonResult<RobotTaskLineTimeConsumingRespVO> getTaskLineTimeConsuming(@RequestParam("id") Long id) {
        RobotTaskLineTimeConsumingDO taskLineTimeConsuming = taskLineTimeConsumingService.getTaskLineTimeConsuming(id);
        return success(BeanUtils.toBean(taskLineTimeConsuming, RobotTaskLineTimeConsumingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆行走路线耗时记录分页")
    @PreAuthorize("@ss.hasPermission('robot:task-line-time-consuming:query')")
    public CommonResult<PageResult<RobotTaskLineTimeConsumingRespVO>> getTaskLineTimeConsumingPage(@Valid RobotTaskLineTimeConsumingPageReqVO pageReqVO) {
        PageResult<RobotTaskLineTimeConsumingDO> pageResult = taskLineTimeConsumingService.getTaskLineTimeConsumingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotTaskLineTimeConsumingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆行走路线耗时记录 Excel")
    @PreAuthorize("@ss.hasPermission('robot:task-line-time-consuming:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaskLineTimeConsumingExcel(@Valid RobotTaskLineTimeConsumingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotTaskLineTimeConsumingDO> list = taskLineTimeConsumingService.getTaskLineTimeConsumingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆行走路线耗时记录.xls", "数据", RobotTaskLineTimeConsumingRespVO.class,
                        BeanUtils.toBean(list, RobotTaskLineTimeConsumingRespVO.class));
    }

}