package com.sptg.permissions.biz.receiver;

import com.sptg.permissions.api.dto.RegionAuthorizeDto;
import com.sptg.permissions.api.stream.XqUntyingAuthorizeReceiverInterface;
import com.sptg.permissions.biz.service.DeviceAuthorizeServiceInterface;
import com.sptg.permissions.biz.stream.XqUntyingAuthorizeProduceInterface;
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: XqUntyingAuthorizeReceiver
 * description: 小区取消授权消费者
 *
 *                  
 *            
 */
@Component
@Slf4j
@RequiredArgsConstructor
@EnableBinding(value = { XqUntyingAuthorizeReceiverInterface.class, XqUntyingAuthorizeProduceInterface.class})
public class XqUntyingAuthorizeReceiver {

    private final DeviceAuthorizeServiceInterface deviceAuthorizeServiceInterface;

    @StreamListener(value = XqUntyingAuthorizeReceiverInterface.INPUT)
    public void input(RegionAuthorizeDto dto){
        if (null == dto){
            return;
        }
        //  授权的小区Id列表
        List<Long> orgIdList = dto.getOrgIdList();
        //  根据小区Id删除小区下关联的设备授权信息
        orgIdList.forEach( orgId ->
                this.deviceAuthorizeServiceInterface.deleteByOrgPositionAndResidentialId(dto.getOrgPosition(),orgId)
        );
    }


}
