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

import cn.iocoder.yudao.module.system.controller.admin.statistics.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.statistics.RobotWorkingHoursStatisticsDO;
import cn.iocoder.yudao.module.system.service.statistics.RobotWorkingHoursStatisticsService;
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/statistics/robot-working-hours-statistics")
@Validated
public class RobotWorkingHoursStatisticsController {

    @Resource
    private RobotWorkingHoursStatisticsService robotWorkingHoursStatisticsService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆工作时长统计")
    @PreAuthorize("@ss.hasPermission('statistics:robot-working-hours-statistics:create')")
    public CommonResult<Long> createRobotWorkingHoursStatistics(@Valid @RequestBody RobotWorkingHoursStatisticsSaveReqVO createReqVO) {
        return success(robotWorkingHoursStatisticsService.createRobotWorkingHoursStatistics(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆工作时长统计")
    @PreAuthorize("@ss.hasPermission('statistics:robot-working-hours-statistics:update')")
    public CommonResult<Boolean> updateRobotWorkingHoursStatistics(@Valid @RequestBody RobotWorkingHoursStatisticsSaveReqVO updateReqVO) {
        robotWorkingHoursStatisticsService.updateRobotWorkingHoursStatistics(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆工作时长统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('statistics:robot-working-hours-statistics:delete')")
    public CommonResult<Boolean> deleteRobotWorkingHoursStatistics(@RequestParam("id") Long id) {
        robotWorkingHoursStatisticsService.deleteRobotWorkingHoursStatistics(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆工作时长统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('statistics:robot-working-hours-statistics:query')")
    public CommonResult<RobotWorkingHoursStatisticsRespVO> getRobotWorkingHoursStatistics(@RequestParam("id") Long id) {
        RobotWorkingHoursStatisticsDO robotWorkingHoursStatistics = robotWorkingHoursStatisticsService.getRobotWorkingHoursStatistics(id);
        return success(BeanUtils.toBean(robotWorkingHoursStatistics, RobotWorkingHoursStatisticsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆工作时长统计分页")
    @PreAuthorize("@ss.hasPermission('statistics:robot-working-hours-statistics:query')")
    public CommonResult<PageResult<RobotWorkingHoursStatisticsRespVO>> getRobotWorkingHoursStatisticsPage(@Valid RobotWorkingHoursStatisticsPageReqVO pageReqVO) {
        PageResult<RobotWorkingHoursStatisticsDO> pageResult = robotWorkingHoursStatisticsService.getRobotWorkingHoursStatisticsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotWorkingHoursStatisticsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆工作时长统计 Excel")
    @PreAuthorize("@ss.hasPermission('statistics:robot-working-hours-statistics:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRobotWorkingHoursStatisticsExcel(@Valid RobotWorkingHoursStatisticsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotWorkingHoursStatisticsDO> list = robotWorkingHoursStatisticsService.getRobotWorkingHoursStatisticsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆工作时长统计.xls", "数据", RobotWorkingHoursStatisticsRespVO.class,
                        BeanUtils.toBean(list, RobotWorkingHoursStatisticsRespVO.class));
    }

}