package com.sky.ioc.message;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.ioc.entity.domain.energy.DeviceMessage;
import com.sky.ioc.entity.domain.energy.EnergyDevice;
import com.sky.ioc.entity.domain.security.SecurityDevice;
import com.sky.ioc.mapper.energy.DeviceMessageMapper;
import com.sky.ioc.mapper.energy.EnergyDeviceMapper;
import com.sky.ioc.mapper.security.SecurityDeviceMapper;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Configuration
public class PushCallback implements MqttCallback {

    @Autowired
    private MQTTSubsribe mqttSubsribe;

    @Autowired
    private RedisTemplate redisTemplate;

    //    定时任务——定时缓存查询的数据
    private ScheduledExecutorService scheduled;

    @Autowired
    DeviceMessageMapper deviceMessageMapper;

    @Autowired
    SecurityDeviceMapper securityDeviceMapper;

    @Autowired
    EnergyDeviceMapper energyDeviceMapper;


    @Override
    public void connectionLost(Throwable throwable) {
        // 连接丢失后，一般在这里面进行重连
        System.out.println("连接断开，可以做重连");
        this.mqttSubsribe.startReconnect();
    }

    @Override

    public void messageArrived(String topic, MqttMessage message) throws Exception {
        // subscribe后得到的消息会执行到这里面
        System.out.println("接收消息主题 : " + topic);
        System.out.println("接收消息Qos : " + message.getQos());
        System.out.println("接收消息内容 : " + new String(message.getPayload()));
        String value = new String(message.getPayload());
        JSONObject jsonObject = JSONObject.parseObject(value);
        DeviceMessage deviceMessage = new DeviceMessage();
        deviceMessage.setMessage(jsonObject.getString("message"));
        deviceMessage.setService(jsonObject.getString("service"));
        deviceMessage.setModule(jsonObject.getString("module"));
        deviceMessage.setApiUrl(jsonObject.getString("apiUrl"));
        deviceMessage.setSupplierId(jsonObject.getString("supplierId"));
        deviceMessage.setDeviceId(jsonObject.getInteger("id"));
        deviceMessage.setCreateTime(new Date()+"");
        Integer deviceId = deviceMessage.getDeviceId();
        String service = deviceMessage.getService();
        String name = "";
        if(service.equals("energy")){
            EnergyDevice energyDevice = energyDeviceMapper.selectOne(new LambdaQueryWrapper<EnergyDevice>().eq(EnergyDevice::getId,deviceId));
            name = energyDevice==null?"":energyDevice.getDeviceName();
        }else if(service.equals("safety")){
            SecurityDevice securityDevice = securityDeviceMapper.selectOne(new LambdaQueryWrapper<SecurityDevice>().eq(SecurityDevice::getThirdId,deviceId));
            name = securityDevice==null?"":securityDevice.getDeviceName();
        }
        deviceMessage.setDeviceName(name);
        deviceMessage.setType(1);
        deviceMessageMapper.insert(deviceMessage);

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + token.isComplete());
    }
}

