package com.bz.rmcp.dap.controller;

import com.alibaba.fastjson.JSON;
import com.bz.rmcp.dap.model.DeviceOrigDataDTO;
import com.bz.rmcp.dap.model.OfflineData;
import com.bz.rmcp.dap.mq.MqMessageSender;
import com.bz.rmcp.dap.service.DeviceDataService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author linqingwei
 * @date 2021-08-09 17:40
 */
@Slf4j
@RequestMapping("rabbitmq")
@RestController
public class RabbitMqMsgController {
    @Autowired
    private MqMessageSender sender;
    @Autowired
    private DeviceDataService deviceDataService;

    @PostMapping("sendmsg")
    public void sendMsg(OfflineData data){
        log.info("当前时间：{},收到请求，data:{}", new Date(), data);
        sender.sendDelayMsg(data);
    }

    @PostMapping("sendDeviceData")
    public void sendDeviceData(){
        Map<String,Object> param = new HashMap<>();

        param.put("WSV",90);
        param.put("VOL",3.56);
        param.put("FORCED","0");
        param.put("DISTURB","0");
        param.put("OVERDRAFT","0");
        param.put("ALARM","0");
        param.put("VOL_STATUS","0");
        param.put("VALVE","01");

        /*param.put("WSV",90);
        param.put("VOL_STATUS","0");
        param.put("VALVE","01");
        param.put("DISTURB","0");
        param.put("REMOVE_STATUS","0");*/

        Map<Date,String> map = new HashMap<>();
        map.put(new Date(), JSON.toJSONString(param));

        DeviceOrigDataDTO dto = new DeviceOrigDataDTO();
        dto.setId(1L);
        dto.setAgentIdentifier("");
        dto.setDeviceNo("77777777");
        dto.setMode("");
        dto.setType("");
        dto.setManufacturer("");
        dto.setOriginalDataFormat("");
        dto.setOriginalData("");
        dto.setParsedData(map);
        dto.setEventTime(new Date());
        dto.setReceiveTime(new Date());


        deviceDataService.saveDeviceData(dto);
    }

    @PostMapping("sendDeviceLoraData")
    public void sendDeviceLoraData(
            @ApiParam(value = "设备编号", required = true) @RequestParam String deviceNo,
            @ApiParam(value = "json", required = true) @RequestParam(defaultValue = "{\"VOL\":3.56,\"ALARM\":\"0\",\"VOL_STATUS\":\"0\",\"VALVE\":\"01\",\"WSV\":90,\"FORCED\":\"0\",\"DISTURB\":\"0\",\"OVERDRAFT\":\"0\"}") String json
    ){
        Map<String,Object> param  = JSON.parseObject(json, HashMap.class);
        Map<Date,String> map = new HashMap<>();
        map.put(new Date(), JSON.toJSONString(param));

        DeviceOrigDataDTO dto = new DeviceOrigDataDTO();
        dto.setId(1L);
        dto.setAgentIdentifier("");
        dto.setDeviceNo(deviceNo);
        dto.setMode("");
        dto.setType("");
        dto.setManufacturer("");
        dto.setOriginalDataFormat("");
        dto.setOriginalData("");
        dto.setParsedData(map);
        dto.setEventTime(new Date());
        dto.setReceiveTime(new Date());
        deviceDataService.saveDeviceData(dto);

    }

    @PostMapping("sendDeviceNbData")
    public void sendDeviceNbData(
            @ApiParam(value = "设备编号", required = true) @RequestParam String deviceNo,
            @ApiParam(value = "json", required = true) @RequestParam(defaultValue = "{\"REMOVE_STATUS\":\"0\",\"VOL_STATUS\":\"0\",\"VALVE\":\"01\",\"WSV\":90,\"DISTURB\":\"0\"}") String json
    ){

        Map<String,Object> param  = JSON.parseObject(json, HashMap.class);
        Map<Date,String> map = new HashMap<>();
        map.put(new Date(), JSON.toJSONString(param));

        DeviceOrigDataDTO dto = new DeviceOrigDataDTO();
        dto.setId(1L);
        dto.setAgentIdentifier("");
        dto.setDeviceNo(deviceNo);
        dto.setMode("");
        dto.setType("");
        dto.setManufacturer("");
        dto.setOriginalDataFormat("");
        dto.setOriginalData("");
        dto.setParsedData(map);
        dto.setEventTime(new Date());
        dto.setReceiveTime(new Date());
        deviceDataService.saveDeviceData(dto);
    }
}
