package com.sptg.permissions.biz.receiver;

import com.sptg.common.core.constant.OperationConstant;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.stream.OrgDeleteReceiverInterface;
import com.sptg.permissions.biz.service.AccountServiceInterface;
import com.sptg.permissions.biz.service.RegionAuthorizeServiceInterface;
import com.sptg.permissions.biz.stream.OrgDeleteProduceInterface;
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: OrgDeleteConsume
 * description: 机构删除消费者
 *
 *                  
 *            
 */
@Component
@Slf4j
@RequiredArgsConstructor
@EnableBinding(value = { OrgDeleteReceiverInterface.class, OrgDeleteProduceInterface.class})
public class OrgDeleteReceiver {


    private final RegionAuthorizeServiceInterface regionAuthorizeServiceInterface;
    private final AccountServiceInterface accountServiceInterface;

    @StreamListener(value = OrgDeleteReceiverInterface.INPUT)
    public void input(Organization org){
        //  获取机构下的账号信息
        List<Account> accountList = this.accountServiceInterface.selectByOrgPosition(org.getPosition());
        if (accountList.size() > OperationConstant.ZERO){
            accountList.forEach(account -> account.setStatus(OperationConstant.MINUS_ONE));
            this.accountServiceInterface.updateBatch(accountList);
        }
        //  删除此机构的区域授权信息
        this.regionAuthorizeServiceInterface.deleteByOrgPositionOrResidentialId(org.getPosition(),org.getId());

    }

}
