package com.usky.fire.service.mqtt.cy;

import com.usky.common.core.util.JsonUtils;
import com.usky.fire.domain.WaterAj;
import com.usky.fire.service.WaterAjService;
import com.usky.fire.service.api.mhWater.OneCardApi;
import com.usky.fire.service.enums.TopListener;
import com.usky.fire.service.mqtt.MqttStrategy;
import com.usky.fire.service.vo.MqttBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author yq
 * @date 2022/3/4 15:07
 */
@Service("cy")
public class CyStrategy implements MqttStrategy {


    @Autowired
    private OneCardApi oneCardApi;
    @Autowired
    private WaterAjService waterAjService;
    @Override
    public String disposeMessage(MqttBaseVO mqttBaseVO) {
        String topic = mqttBaseVO.getTopic();
        Map map = JsonUtils.fromJson(mqttBaseVO.getData().toString(), Map.class);
        if (TopListener.MH_WATER_INFO.getCode().equals(topic)){
            oneCardApi.callInfoApi(map);
        }else if (TopListener.MH_WATER_ALERT.getCode().equals(topic)){
            oneCardApi.callAlarmApi(map);
        }else if (TopListener.MH_WATER_STATISTICS.getCode().equals(topic)){
            WaterAj waterAj = JsonUtils.fromJson(mqttBaseVO.getData().toString(), WaterAj.class);
            waterAjService.saveOrUpdate(waterAj);
        }
        return null;

    }
}
