package com.sptg.permissions.biz.receiver;

import com.sptg.common.core.constant.OperationConstant;
import com.sptg.permissions.api.dto.OrgDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.DeviceAuthorize;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.entity.Role;
import com.sptg.permissions.api.stream.OrgModifyNameReceiverInterface;
import com.sptg.permissions.biz.service.AccountServiceInterface;
import com.sptg.permissions.biz.service.DeviceAuthorizeServiceInterface;
import com.sptg.permissions.biz.service.OrgServiceInterface;
import com.sptg.permissions.biz.service.RoleServiceInterface;
import com.sptg.permissions.biz.stream.OrgModifyNameProduceInterface;
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: OrgModifyNameReceiver
 * description: 机构名称修改消费者
 *
 *                  
 *            
 */
@Component
@Slf4j
@RequiredArgsConstructor
@EnableBinding(value = { OrgModifyNameReceiverInterface.class, OrgModifyNameProduceInterface.class})
public class OrgModifyNameReceiver {

    private final AccountServiceInterface accountServiceInterface;
    private final OrgServiceInterface orgServiceInterface;
    private final RoleServiceInterface roleServiceInterface;
    private final DeviceAuthorizeServiceInterface deviceAuthorizeServiceInterface;

    @StreamListener(value = OrgModifyNameReceiverInterface.INPUT)
    public void input(OrgDto dto){
        //  机构Id
        Long orgId = dto.getId();
        //  机构新名称
        String orgName = dto.getName();

        //  修改子机构列表的父级名称
        List<Organization> subOrganizationList = this.orgServiceInterface.selectByPid(orgId);
        if (subOrganizationList.size() > OperationConstant.ZERO) {
            subOrganizationList.forEach(entity -> entity.setParentName(orgName));
            this.orgServiceInterface.updateBatchById(subOrganizationList);
        }
        //  修改关联账号的机构名称
        List<Account> accountList = this.accountServiceInterface.selectByOrgId(orgId);
        if (accountList.size() > OperationConstant.ZERO){
            accountList.forEach(account -> account.setOrgName(orgName));
            this.accountServiceInterface.updateBatch(accountList);
        }
        //  修改关联角色的机构名称
        List<Role> roleList = this.roleServiceInterface.getListByOrgId(orgId);
        if (roleList.size() > OperationConstant.ZERO){
            roleList.forEach(entity -> entity.setOrgName(orgName));
            this.roleServiceInterface.updateBatch(roleList);
        }
        //  修改设备权限表中的设备跟机构名称
        List<DeviceAuthorize> deviceAuthorizeList = this.deviceAuthorizeServiceInterface.selectByRootOrgId(orgId);
        if (deviceAuthorizeList.size() > OperationConstant.ZERO){
            deviceAuthorizeList.forEach(entity -> entity.setDeviceRootOrgName(orgName));
            this.deviceAuthorizeServiceInterface.updateBatchById(deviceAuthorizeList);
        }
        //  其他关联表 ....




    }

}
