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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.iot.controller.admin.mqttdatarecord.vo.MqttDataRecordPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.mqttdatarecord.vo.MqttDataRecordRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.mqttdatarecord.vo.MqttDataRecordSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.mqttdatarecord.MqttDataRecordDO;
import cn.iocoder.yudao.module.iot.service.mqttdatarecord.MqttDataRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 设备数据记录")
@RestController
@RequestMapping("/iot/mqtt-data-record")
@Validated
public class MqttDataRecordController {

    @Resource
    private MqttDataRecordService mqttDataRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建设备数据记录")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-data-record:create')")
    public CommonResult<Long> createMqttDataRecord(@Valid @RequestBody MqttDataRecordSaveReqVO createReqVO) {
        return success(mqttDataRecordService.createMqttDataRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备数据记录")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-data-record:update')")
    public CommonResult<Boolean> updateMqttDataRecord(@Valid @RequestBody MqttDataRecordSaveReqVO updateReqVO) {
        mqttDataRecordService.updateMqttDataRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备数据记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:mqtt-data-record:delete')")
    public CommonResult<Boolean> deleteMqttDataRecord(@RequestParam("id") Long id) {
        mqttDataRecordService.deleteMqttDataRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备数据记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-data-record:query')")
    public CommonResult<MqttDataRecordRespVO> getMqttDataRecord(@RequestParam("id") Long id) {
        MqttDataRecordDO mqttDataRecord = mqttDataRecordService.getMqttDataRecord(id);
        return success(BeanUtils.toBean(mqttDataRecord, MqttDataRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备数据记录分页")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-data-record:query')")
    public CommonResult<PageResult<MqttDataRecordRespVO>> getMqttDataRecordPage(@Valid MqttDataRecordPageReqVO pageReqVO) {
        PageResult<MqttDataRecordDO> pageResult = mqttDataRecordService.getMqttDataRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MqttDataRecordRespVO.class));
    }
    @GetMapping("/page2")
    @Operation(summary = "获得设备数据记录分页")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-data-record:query')")
    public CommonResult<PageResult<MqttDataRecordRespVO>> getMqttDataRecordPage2(@Valid MqttDataRecordPageReqVO pageReqVO) {
        PageResult<MqttDataRecordDO> pageResult = mqttDataRecordService.getMqttDataRecordPage2(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MqttDataRecordRespVO.class));
    }
    @GetMapping("/page3")
    @Operation(summary = "获得设备数据最近一个小时的数据")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-data-record:query')")
    public CommonResult<PageResult<MqttDataRecordRespVO>> getMqttDataRecordPage3(@Valid MqttDataRecordPageReqVO pageReqVO) {
        LocalDateTime[] dateTimes= {LocalDateTime.now().plusHours(-1),LocalDateTime.now()};

        pageReqVO.setCreateTime(dateTimes);
        PageResult<MqttDataRecordDO> pageResult = mqttDataRecordService.getMqttDataRecordPage2(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MqttDataRecordRespVO.class));
    }

    @GetMapping("/export-excel2")
    @Operation(summary = "导出设备数据记录 Excel")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-data-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMqttDataRecordExcel2(@Valid MqttDataRecordPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MqttDataRecordDO> list = mqttDataRecordService.getMqttDataRecordPage2(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备数据记录.xls", "数据", MqttDataRecordRespVO.class,
                BeanUtils.toBean(list, MqttDataRecordRespVO.class));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出设备数据记录 Excel")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-data-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMqttDataRecordExcel(@Valid MqttDataRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MqttDataRecordDO> list = mqttDataRecordService.getMqttDataRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备数据记录.xls", "数据", MqttDataRecordRespVO.class,
                        BeanUtils.toBean(list, MqttDataRecordRespVO.class));
    }

}