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.common.Rest;
import com.mortals.framework.util.UuidUtil;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.sms.SmsSetPdu;
import com.mortals.xhx.feign.sms.ISmsSetFeign;
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;
import java.util.List;

/**
 * 离线通知
 *
 * @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;
    @Autowired
    private ISmsSetFeign smsSetFeign;


    @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);
                    }

                    //保存离线告警消息

                    DeviceAlarmInfoEntity alarmInfoEntity = new DeviceAlarmInfoEntity();

                    alarmInfoEntity.initAttrValue();
                    alarmInfoEntity.setAlarmDevice(deviceEntity.getId());
                    alarmInfoEntity.setSiteId(deviceEntity.getSiteId());
                    alarmInfoEntity.setAlarmContent(deviceEntity.getDeviceName() + "已离线！");
                    alarmInfoEntity.setAlarmType(AlarmTypeEnum.离线.getValue());
                    alarmInfoEntity.setAlarmLevel(AlarmLevelEnum.一般.getValue());
                    alarmInfoEntity.setAlarmStatus(AlarmStatusEnum.未清除.getValue());
                    alarmInfoEntity.setAlarmReceivePersonnel(deviceEntity.getLeadingOfficial());
                    alarmInfoEntity.setReceivePersonnelTelephone(deviceEntity.getLeadingOfficialTelephone());
                    alarmInfoEntity.setCreateTime(new Date());
                    alarmInfoEntity.setCreateUserId(1L);
                    deviceAlarmInfoService.save(alarmInfoEntity);


                    // 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 发送短信

                            Rest<RespData<List<SmsSetPdu>>> respDataRest = smsSetFeign.list(new SmsSetPdu().siteId(deviceEntity.getSiteId()));
                            if (respDataRest.getCode() == YesNoEnum.YES.getValue()) {
                                List<SmsSetPdu> data = respDataRest.getData().getData();
                                if (!ObjectUtils.isEmpty(data)) {
                                    Integer messageoff = data.get(0).getMessageoff();
                                    if (messageoff == YesNoEnum.YES.getValue()) {
                                        // todo 发送短信

                                        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.setSiteId(deviceAlarmInfoEntity.getSiteId());
                                            alarmSmsSendEntity.setMobile(deviceAlarmInfoEntity.getReceivePersonnelTelephone());
                                            alarmSmsSendEntity.setReceiver(deviceAlarmInfoEntity.getAlarmReceivePersonnel());
                                            alarmSmsSendEntity.setSendMess(deviceAlarmInfoEntity.getAlarmContent());
                                            alarmSmsSendEntity.setSendStatus(SendStatusEnum.未发送.getValue());
                                            alarmSmsSendService.save(alarmSmsSendEntity, null);
                                        }
                                    } else {

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