package com.yuchen.saas.park.service.pull.input;


import com.alibaba.fastjson.JSON;
import com.yuchen.saas.message.api.enums.MessageTypeEnum;
import com.yuchen.saas.message.api.feign.dingding.MonitorDingFeignServer;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.fault.ConnectEvent;
import com.yuchen.saas.park.service.fault.service.EquipmentStatusService;
import com.yuchen.saas.park.service.service.PmgBatParkPerceiveService;
import com.yuchen.saas.park.service.service.PmgBatSmartEquipmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class DeviceConnectInput {

    private final Logger logger = LoggerFactory.getLogger(DeviceConnectInput.class);

    private Map<String, ConnectEvent> alarmDevice = new HashMap<>();

    @Resource
    private PmgBatSmartEquipmentService pmgBatSmartEquipmentService;

    @Resource
    private MonitorDingFeignServer monitorDing;

    /**
     * 注入设备缓存操作接口
     */
    @Resource
    private PmgBatParkPerceiveService pmgBatParkPerceiveService;

    @Resource
    private EquipmentStatusService equipmentStatusService;

/*
    */
/**
     * 三秒检查一下是否有掉线设备。将其恢复
     *//*

    public void scheduledOps() {
        this.alarmDevice.forEach((deviceName, connectEvent) -> {
            if (!connectEvent.getOnline()) {
                pushMessage(connectEvent);
            }
        });
    }
*/

    /**
     * 接收设备在离线事件数据
     * @param connectEvent
     */
    public void inputData(ConnectEvent connectEvent) {
        // 首先将在在离线数据存入 map集合 定时推送
        this.alarmDevice.put(connectEvent.getDeviceName(), connectEvent);
        // 发送钉钉消息
        pushMessage(connectEvent);
        // 修改设备在离线状态
        updateIsOnline(connectEvent);
        // 保存设备离线故障信息
        equipmentStatusService.saveEquipmentEvent(connectEvent);
    }

    /**
     * 清除报警设备信息
     */
    public void clearAlarmDevice(){
        System.out.println("清除前："+ JSON.toJSONString(this.alarmDevice));
        this.alarmDevice.clear();
        System.out.println("清除后："+ JSON.toJSONString(this.alarmDevice));
    }

    /**
     * 发送钉钉消息
     * @param connectEvent 设备在离线事件数据
     */
    private void pushMessage(ConnectEvent connectEvent){
        // 获取设备详细信息
        PmgBatSmartEquipment result = pmgBatSmartEquipmentService.getDetails(connectEvent.getProductKey(),
                connectEvent.getDeviceName());

        // 如果设备数据为空，不发送消息提醒
        if (null == result){
            return ;
        }

        String operation = "设备与平台";
        if (connectEvent.getOnline()){
            operation += "建立连接";
        }else{
            operation += "断开连接";
        }
        // 拼接设备详细信息
        String detail = "\n↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓" +
                "\n#### **【所属园区】** " + result.getParkId() +
                "\n#### **【设备名称】** " + result.getName() +
                "\n#### **【ProductKey】** " + result.getProductKey() +
                "\n#### **【DeviceName】** " + result.getDeviceName();
        // 发送钉钉
        monitorDing.pushMessage(MessageTypeEnum.HIGH_UP.getValue(),
                "设备在离线提醒",
                "智慧园区",
                "园区设备状态变动",
                operation,
                detail,
                null);
    }

    /**
     * 修改设备在离线状态
     * @param connectEvent 设备在离线事件数据
     */
    private void updateIsOnline(ConnectEvent connectEvent){
        PmgBatSmartEquipment result = pmgBatSmartEquipmentService.getDetails(connectEvent.getProductKey(),
                connectEvent.getDeviceName());
        if (null != result && result.getId() > 0) {
            PmgBatSmartEquipment pmgBatSmartEquipment = new PmgBatSmartEquipment();
            pmgBatSmartEquipment.setId(result.getId());
            if (connectEvent.getOnline()) {
                pmgBatSmartEquipment.setIsOnline(1);
            } else if (!connectEvent.getOnline()) {
                pmgBatSmartEquipment.setIsOnline(0);
            }
            pmgBatSmartEquipmentService.updateById(pmgBatSmartEquipment);
            // 修改缓存内的设备在离线状态
            pmgBatParkPerceiveService.upOrDownDevice(pmgBatSmartEquipment.getIsOnline().toString(),result.getProductKey(),
                    result.getDeviceName(),result.getParkId());
        }
    }
}
