package com.sptg.permissions.biz.receiver;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sptg.device.api.dto.DeviceOptionsDto;
import com.sptg.device.api.entity.DeviceOptions;
import com.sptg.device.api.feigns.DeviceOptionsFeign;
import com.sptg.device.api.stream.DeviceDeleteReceiverInterface;
import com.sptg.permissions.api.entity.DeviceAuthorize;
import com.sptg.permissions.biz.service.DeviceAuthorizeServiceInterface;
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.List;

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

    private final DeviceAuthorizeServiceInterface deviceAuthorizeServiceInterface;
    private final DeviceOptionsFeign deviceOptionsFeign;

    @StreamListener(value = DeviceDeleteReceiverInterface.INPUT)
    public void input(List<Long> idList){
        if (null == idList || idList.size() == 0){
            return;
        }
        //  删除设备配置
        DeviceOptionsDto deviceOptionsDto = new DeviceOptionsDto();
        deviceOptionsDto.setDeviceIds(idList);
        this.deviceOptionsFeign.deleteOptions(deviceOptionsDto);
        //  删除设备的所有授权记录
        Wrapper<DeviceAuthorize> wrapper = new EntityWrapper<>();
        wrapper.in("l_device_id",idList);
        this.deviceAuthorizeServiceInterface.delete(wrapper);
    }

}
