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

import cn.iocoder.yudao.module.system.controller.admin.robot.chargelog.*;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotChargeLogDO;
import cn.iocoder.yudao.module.system.service.robot.chargelog.RobotChargeLogService;
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/charge-log")
@Validated
public class RobotChargeLogController {

    @Resource
    private RobotChargeLogService chargeLogService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆充电记录")
    @PreAuthorize("@ss.hasPermission('robot:charge-log:create')")
    public CommonResult<Long> createChargeLog(@Valid @RequestBody RobotChargeLogSaveReqVO createReqVO) {
        return success(chargeLogService.createChargeLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆充电记录")
    @PreAuthorize("@ss.hasPermission('robot:charge-log:update')")
    public CommonResult<Boolean> updateChargeLog(@Valid @RequestBody RobotChargeLogSaveReqVO updateReqVO) {
        chargeLogService.updateChargeLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆充电记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:charge-log:delete')")
    public CommonResult<Boolean> deleteChargeLog(@RequestParam("id") Long id) {
        chargeLogService.deleteChargeLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆充电记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:charge-log:query')")
    public CommonResult<RobotChargeLogRespVO> getChargeLog(@RequestParam("id") Long id) {
        RobotChargeLogDO chargeLog = chargeLogService.getChargeLog(id);
        return success(BeanUtils.toBean(chargeLog, RobotChargeLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆充电记录分页")
    @PreAuthorize("@ss.hasPermission('robot:charge-log:query')")
    public CommonResult<PageResult<RobotChargeLogRespVO>> getChargeLogPage(@Valid RobotChargeLogPageReqVO pageReqVO) {
        PageResult<RobotChargeLogDO> pageResult = chargeLogService.getChargeLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotChargeLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆充电记录 Excel")
    @PreAuthorize("@ss.hasPermission('robot:charge-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportChargeLogExcel(@Valid RobotChargeLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotChargeLogDO> list = chargeLogService.getChargeLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆充电记录.xls", "数据", RobotChargeLogRespVO.class,
                        BeanUtils.toBean(list, RobotChargeLogRespVO.class));
    }

}