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

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.mqttrecord.vo.MqttRecordPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.mqttrecord.vo.MqttRecordRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.mqttrecord.vo.MqttRecordSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.mqttrecord.MqttRecordDO;
import cn.iocoder.yudao.module.iot.service.mqttrecord.MqttRecordService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jodd.util.StringUtil;
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.util.ArrayList;
import java.util.List;
import java.util.Map;

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-record")
@Validated
public class MqttRecordController {

    @Resource
    private MqttRecordService mqttRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建数采记录")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-record:create')")
    public CommonResult<Long> createMqttRecord(@Valid @RequestBody MqttRecordSaveReqVO createReqVO) {
        return success(mqttRecordService.createMqttRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数采记录")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-record:update')")
    public CommonResult<Boolean> updateMqttRecord(@Valid @RequestBody MqttRecordSaveReqVO updateReqVO) {
        mqttRecordService.updateMqttRecord(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得数采记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-record:query')")
    public CommonResult<MqttRecordRespVO> getMqttRecord(@RequestParam("id") Long id) {
        MqttRecordDO mqttRecord = mqttRecordService.getMqttRecord(id);
        return success(BeanUtils.toBean(mqttRecord, MqttRecordRespVO.class));
    }
    @GetMapping("/getDeviceAttr")
    @Operation(summary = "")
    @Parameter(name = "code", description = "设备编码", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-record:query')")
    public CommonResult<List> getDeviceAttr(@RequestParam("code") String code) {
        MqttRecordDO mqttRecord = mqttRecordService.getDeviceAttr(code);
        List<String> attrList = new ArrayList<>();
        if(mqttRecord!=null && StringUtil.isNotEmpty(mqttRecord.getDeviceData())){
            Map attrMap = JSONObject.parseObject(mqttRecord.getDeviceData(),Map.class);
            attrList = new ArrayList<>(attrMap.keySet());
        }

        return success(attrList);
    }
    @GetMapping("/page")
    @Operation(summary = "获得数采记录分页")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-record:query')")
    public CommonResult<PageResult<MqttRecordRespVO>> getMqttRecordPage(@Valid MqttRecordPageReqVO pageReqVO) {
        PageResult<MqttRecordDO> pageResult = mqttRecordService.getMqttRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MqttRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数采记录 Excel")
    @PreAuthorize("@ss.hasPermission('iot:mqtt-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMqttRecordExcel(@Valid MqttRecordPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MqttRecordDO> list = mqttRecordService.getMqttRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数采记录.xls", "数据", MqttRecordRespVO.class,
                BeanUtils.toBean(list, MqttRecordRespVO.class));
    }

}