package cn.iocoder.yudao.module.mes.controller.admin.mqtt;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.controller.admin.mqttrecord.vo.MqttData;
import cn.iocoder.yudao.module.iot.controller.admin.mqttrecord.vo.MqttDataUtils;
import cn.iocoder.yudao.module.iot.controller.admin.mqttrecord.vo.MqttRecordSaveReqVO;
import cn.iocoder.yudao.module.iot.service.mqttrecord.MqttRecordService;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 数采记录")
@RestController
@RequestMapping("/iot/mqtt-server")
@Validated
public class MqttServerController {

    @Resource
    private MqttRecordService mqttRecordService;

    @PostMapping("/createMap")
    @Operation(summary = "创建数采记录")
    public CommonResult<Boolean> createMap(@RequestBody Map<String, Object> param) {
        MqttData mqttData  = MqttDataUtils.parseMap(param);
        if(mqttData!=null){
            MqttRecordSaveReqVO createReqVO = new MqttRecordSaveReqVO();
            createReqVO.setDeviceCode(mqttData.getDeviceID());
            createReqVO.setGatewayCode(mqttData.getGatewayID());
            createReqVO.setDeviceDataTime(mqttData.getDeviceDataTime());
            createReqVO.setDeviceData(JSON.toJSONString(param));
            mqttRecordService.createMqttRecord(createReqVO);
        }
        return success(true);
    }
}