package com.xy.event.listener;

import cn.hutool.core.util.BooleanUtil;
import com.xy.dto.DeviceNetRecordDto;
import com.xy.dto.DeviceStatusDto;
import com.xy.dto.nfc.UpperDeviceStatusDTO;
import com.xy.entity.DeviceInfo;
import com.xy.entity.DeviceNetWorkSpi;
import com.xy.entity.DeviceSysinfo;
import com.xy.entity.SysDictRedis;
import com.xy.event.DeviceEvent;
import com.xy.service.*;
import com.xy.utils.RedisService;
import com.xy.utils.SpiUtils;
import com.xy.utils.SysDictUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 设备网络状态事件监听
 */
@Slf4j
@Service
@AllArgsConstructor
public class DeviceNetWorkEventListener {

    private DeviceStatusServiceImpl deviceStatusService;

    private DeviceNetRecordServiceImpl deviceNetRecordService;

    private DeviceSysinfoServiceImpl deviceSysinfoService;

    private AlipayDeviceService alipayDeviceService;

    private DeviceInfoServiceImpl deviceInfoService;

    private RedisService<Integer> redisService;

    /**
     * 上报状态
     *
     * @param netWorkEvent
     */
    @EventListener
    public void up(DeviceEvent.NetWorkEvent netWorkEvent) {
        DeviceStatusDto.Up up = new DeviceStatusDto.Up().setDeviceId(netWorkEvent.getValue());
        up.setNetState(netWorkEvent.getNetState());
        deviceStatusService.up(up).getCode();
    }

    /**
     * 添加联网记录
     *
     * @param netWorkEvent
     */
    @EventListener
    public void saveNetRecord(DeviceEvent.NetWorkEvent netWorkEvent) {
        DeviceNetRecordDto.Save save = new DeviceNetRecordDto.Save()
                .setDeviceId(netWorkEvent.getValue());
        save.setNetStatus(netWorkEvent.getNetState());
        deviceNetRecordService.save(save);
    }

    /**
     * nfc处理
     *
     * @param netWorkEvent
     */
    @EventListener
    public void nfcHandle(DeviceEvent.NetWorkEvent netWorkEvent) {
        DeviceSysinfo deviceSysinfo = deviceSysinfoService.getById(netWorkEvent.getValue());
        if (deviceSysinfo != null) {
            Boolean isHaveAlipayNpad = deviceSysinfo.getIsHaveAlipayNpad();
            if (BooleanUtil.isTrue(isHaveAlipayNpad)) {
                //nfc 同步上位机状态
                alipayDeviceService.upperDeviceStatusSync(new UpperDeviceStatusDTO().setTerminalId(String.valueOf(netWorkEvent.getValue())).setOnline(netWorkEvent.getNetState() == 1));
            }
        }
    }

    /**
     * 告警消息处理
     *
     * @param netWorkEvent
     */
    @EventListener
    public void msgHandle(DeviceEvent.NetWorkEvent netWorkEvent) {
        String key = "device:offline:" + netWorkEvent.getValue();
        if (netWorkEvent.getNetState() == 1) {
            //在线
            redisService.remove(key);
        } else {
            //离线
            int minutes = 60;
            Map<String, SysDictRedis> map = SysDictUtils.get("device_offline_time_code");
            if (map != null) {
                minutes = Integer.parseInt(map.get("minutes").getValue());
            }
            redisService.set(key, 0);
            redisService.timeout(key, minutes, TimeUnit.MINUTES);
        }
    }

    /**
     * spi推送
     *
     * @param netWorkEvent
     */
    @Async
    @EventListener
    public void spi(DeviceEvent.NetWorkEvent netWorkEvent) {
        DeviceInfo deviceInfo = deviceInfoService.getById(netWorkEvent.getValue());
        SpiUtils.sendSpi(deviceInfo.getMercId(), DeviceNetWorkSpi.spiKey, () -> new DeviceNetWorkSpi()
                .setDeviceId(deviceInfo.getDeviceId())
                .setNetState(netWorkEvent.getNetState())
        );
    }

}
