package com.sptg.device.biz.receiver;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sptg.common.core.enums.DeviceInstruct;
import com.sptg.common.core.enums.Instruct;
import com.sptg.device.api.constant.DeviceConstants;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.entity.DeviceLog;
import com.sptg.device.api.entity.MqConf;
import com.sptg.device.api.stream.DeviceDeleteProduceInterface;
import com.sptg.device.api.stream.DeviceDeleteReceiverInterface;
import com.sptg.device.biz.service.*;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.third.api.entity.qinlin.QLinDevice;
import com.sptg.third.api.feigns.qinlin.QLinFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * title: DeviceDeleteReceiver
 * description: 设备删除消费者
 *
 *
 */
@Component
@Slf4j
@RequiredArgsConstructor
@EnableBinding(value = { DeviceDeleteReceiverInterface.class, DeviceDeleteProduceInterface.class})
public class DeviceDeleteReceiver {

    private final DeviceServiceInterface deviceServiceInterface;
    private final MqConfServiceInterface mqConfServiceInterface;
    private final DeviceLogServiceInterface deviceLogServiceInterface;
    private final MsgPublishService msgPublishService;

    @StreamListener(value = DeviceDeleteReceiverInterface.INPUT)
    public void input(List<Long> idList){
        if (null == idList || idList.size() == 0){
            return;
        }
        //  获取设备信息
        List<Device> deviceList = this.deviceServiceInterface.selectBatchIds(idList);
        if (deviceList.size() == 0){
            return;
        }
        List<Device> mqDeviceList = null;
        mqDeviceList = deviceList.stream().filter(device -> device.getMqId() != null).collect(Collectors.toList());

        //  根据设备的队列服务器Id将设备列表进行分组
        Map<Long, List<Device>> mqMap = mqDeviceList.stream().collect(Collectors.groupingBy(Device::getMqId));
        mqMap.keySet().forEach(mqId -> {
            //  获取队列服务器信息
            MqConf mqConf = this.mqConfServiceInterface.selectById(mqId);
            if (null != mqConf){
                //  更新连接数：当前连接数 - 设备数量
                int threads = mqConf.getThreads() - mqMap.get(mqId).size();
                mqConf.setThreads(threads);
                this.mqConfServiceInterface.updateById(mqConf);
            }
        });

        //  删除设备信息
        List<Device> qlDeviceList = new ArrayList<>();
        List<Device> otherDeviceList = new ArrayList<>();
        deviceList.forEach(device -> {
            String deviceFactory = device.getDeviceFactory();
            if(deviceFactory.equals(DeviceConstants.DEVICE_FACTORY_QINLIN)){
                qlDeviceList.add(device);
            }else{
                otherDeviceList.add(device);
            }
        });
        this.deviceServiceInterface.deleteBatchIds(idList);
        Map<String,String> map = new HashMap<>();
        //  下发清空设备数据指令
        this.msgPublishService.batchPushInstruct(null, otherDeviceList, Instruct.format_data,map);
        //  删除设备的日志 device_log
        Wrapper<DeviceLog> logWrapper = new EntityWrapper<>();
        logWrapper.in("l_device_id", idList);
        this.deviceLogServiceInterface.delete(logWrapper);
    }

}
