package com.mortals.xhx.base.framework.listener;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.mortals.framework.util.UuidUtil;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.module.alarm.model.AlarmConfigEntity;
import com.mortals.xhx.module.alarm.model.AlarmConfigQuery;
import com.mortals.xhx.module.alarm.model.AlarmSmsSendEntity;
import com.mortals.xhx.module.alarm.service.AlarmConfigService;
import com.mortals.xhx.module.alarm.service.AlarmSmsSendService;
import com.mortals.xhx.module.device.model.DeviceAlarmInfoEntity;
import com.mortals.xhx.module.device.model.DeviceAlarmInfoQuery;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.service.DeviceAlarmInfoService;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.platform.model.PlatformEntity;
import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * 离线通知
 *
 * @author: zxfei
 * @date: 2022/6/22 16:40
 * @description:
 **/
@Slf4j
@Component
public class CustomerKeyExpirationListener implements MessageListener {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ProductService productService;
    @Autowired
    private PlatformService platformService;
    @Autowired
    private AlarmConfigService alarmConfigService;
    @Autowired
    private DeviceAlarmInfoService deviceAlarmInfoService;
    @Autowired
    private AlarmSmsSendService alarmSmsSendService;


    @Override
    public void onMessage(Message message, byte[] bytes) {
        String key = message.toString();
        String subStr = StrUtil.removePrefix(key, RedisKey.KEY_DEVICE_ONLINE_CACHE);
        if(!subStr.equals(key)){
            DeviceEntity deviceEntity = deviceService.getExtCache(subStr);
            if (!ObjectUtils.isEmpty(deviceEntity)) {
                if (deviceEntity.getDeviceStatus() == DeviceStatusEnum.在线.getValue()) {
                    deviceEntity.setOfflineTime(new Date());
                    deviceEntity.setDeviceStatus(DeviceStatusEnum.离线.getValue());
                    deviceService.update(deviceEntity);

                    PlatformEntity platformEntity = platformService.get(deviceEntity.getPlatformId());
                    ProductEntity productEntity = productService.get(deviceEntity.getProductId());
                    if (!ObjectUtils.isEmpty(platformEntity) && !ObjectUtils.isEmpty(productEntity)) {
                        //通知第三方平台
                        deviceService.sendThirdParty(deviceEntity, productEntity, platformEntity, DeviceMethodEnum.OFFLINE);
                    }

                    // TODO: 2022/6/23 告警信息保存与发送
                    AlarmConfigEntity alarmConfigEntity = alarmConfigService.selectOne(new AlarmConfigQuery().productId(productEntity.getId()));
                    if (!ObjectUtils.isEmpty(alarmConfigEntity)) {
                        if(alarmConfigEntity.getAlarmPusW1ay()== AlarmPusW1ayEnum.短信.getValue()){
                            // TODO: 2022/7/4 发送短信
                            DeviceAlarmInfoEntity deviceAlarmInfoEntity = deviceAlarmInfoService.selectOne(new DeviceAlarmInfoQuery().alarmDevice(deviceEntity.getId()));
                            if(!ObjectUtils.isEmpty(deviceAlarmInfoEntity)){
                                AlarmSmsSendEntity alarmSmsSendEntity = new AlarmSmsSendEntity();
                                alarmSmsSendEntity.initAttrValue();
                                alarmSmsSendEntity.setId(IdUtil.getSnowflake().nextId());
                                alarmSmsSendEntity.setMobile(deviceAlarmInfoEntity.getReceivePersonnelTelephone());
                                alarmSmsSendEntity.setReceiver(deviceAlarmInfoEntity.getAlarmReceivePersonnel());
                                alarmSmsSendEntity.setSendMess(deviceAlarmInfoEntity.getAlarmContent());
                                alarmSmsSendEntity.setSendStatus(SendStatusEnum.未发送.getValue());
                                alarmSmsSendService.save(alarmSmsSendEntity,null);
                            }

                        }
                    }
                }
            }
        }
        log.info("监听到key:" + key + "过期");
    }
}