package cn.iocoder.yudao.module.iot.controller.admin.deviceoperationrecord;

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.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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.*;

import cn.iocoder.yudao.module.iot.controller.admin.deviceoperationrecord.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.deviceoperationrecord.DeviceOperationRecordDO;
import cn.iocoder.yudao.module.iot.service.deviceoperationrecord.DeviceOperationRecordService;

@Tag(name = "管理后台 - 运行记录")
@RestController
@RequestMapping("/iot/device-operation-record")
@Validated
public class DeviceOperationRecordController {

    @Resource
    private DeviceOperationRecordService deviceOperationRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建运行记录")
    @PreAuthorize("@ss.hasPermission('iot:device-operation-record:create')")
    public CommonResult<Long> createDeviceOperationRecord(@Valid @RequestBody DeviceOperationRecordSaveReqVO createReqVO) {
        return success(deviceOperationRecordService.createDeviceOperationRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新运行记录")
    @PreAuthorize("@ss.hasPermission('iot:device-operation-record:update')")
    public CommonResult<Boolean> updateDeviceOperationRecord(@Valid @RequestBody DeviceOperationRecordSaveReqVO updateReqVO) {
        deviceOperationRecordService.updateDeviceOperationRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除运行记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:device-operation-record:delete')")
    public CommonResult<Boolean> deleteDeviceOperationRecord(@RequestParam("id") Long id) {
        deviceOperationRecordService.deleteDeviceOperationRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得运行记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-operation-record:query')")
    public CommonResult<DeviceOperationRecordRespVO> getDeviceOperationRecord(@RequestParam("id") Long id) {
        DeviceOperationRecordDO deviceOperationRecord = deviceOperationRecordService.getDeviceOperationRecord(id);
        return success(BeanUtils.toBean(deviceOperationRecord, DeviceOperationRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得运行记录分页")
    @PreAuthorize("@ss.hasPermission('iot:device-operation-record:query')")
    public CommonResult<PageResult<DeviceOperationRecordRespVO>> getDeviceOperationRecordPage(@Valid DeviceOperationRecordPageReqVO pageReqVO) {
        PageResult<DeviceOperationRecordDO> pageResult = deviceOperationRecordService.getDeviceOperationRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceOperationRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出运行记录 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-operation-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceOperationRecordExcel(@Valid DeviceOperationRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceOperationRecordDO> list = deviceOperationRecordService.getDeviceOperationRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "运行记录.xls", "数据", DeviceOperationRecordRespVO.class,
                        BeanUtils.toBean(list, DeviceOperationRecordRespVO.class));
    }

    @GetMapping("/deviceOperationPage")
    @Operation(summary = "设备运行报表")
    @PreAuthorize("@ss.hasPermission('mes:repair-tems:query')")
    public CommonResult<PageResult<DeviceTotalTimeRecordRespVO>> deviceOperationPage(@Valid DeviceTotalTimeRecordReqVO pageReqVO) {
        PageResult<DeviceTotalTimeRecordRespVO> pageResult = deviceOperationRecordService.deviceOperationPage(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/export-device-operation-report")
    @Operation(summary = "导出设备运行报表记录 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-operation-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceOperationReport(@Valid DeviceTotalTimeRecordReqVO pageReqVO,
                                                 HttpServletResponse response) throws IOException {
        PageResult<DeviceTotalTimeRecordRespVO> pageResult = deviceOperationRecordService.deviceOperationPage(pageReqVO);
        // 设置响应头
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode("设备运行报表记录.xls", "UTF-8"));
        response.setHeader("Content-Encoding", "identity");
        // 导出Excel
        String fileName = String.format("设备运行报表记录_%s.xls", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        // 导出 Excel
        ExcelUtils.write(response, fileName, "数据", DeviceTotalTimeRecordRespVO.class,pageResult.getList());
    }
}