package cn.axzo.workflow.server.controller.delegate;

import cn.axzo.tyr.client.feign.TyrSaasRoleUserApi;
import cn.axzo.tyr.client.model.roleuser.dto.SaasRoleUserDTO;
import cn.axzo.tyr.client.model.roleuser.req.RoleUserParam;
import cn.axzo.workflow.common.enums.ApproverSpecifyEnum;
import cn.axzo.workflow.common.model.dto.CooperationOrgDTO.OrgScope;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskDelegateAssigner;
import cn.axzo.workflow.core.deletage.approverscope.ApproverScopeDTO;
import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 基于"角色"查询审批人
 *
 * @author wangli
 * @since 2023/11/16 11:37
 */
@Slf4j
@Component
public class BasedRoleTaskAssigneeSelector extends AbstractBpmnTaskAssigneeSelector {

    @Autowired
    private TyrSaasRoleUserApi tyrSaasRoleUserApi;

    @Override
    public String getType() {
        return ApproverSpecifyEnum.role.getType();
    }

    @Override
    public boolean support(String param) {
        return getType().equals(param);
    }

    @Override
    public List<BpmnTaskDelegateAssigner> select(FlowElement flowElement, DelegateExecution execution,
                                                 Boolean throwException) {
        return super.select(flowElement, execution, throwException);
    }

    @Override
    protected List<BpmnTaskDelegateAssigner> invokeService(FlowElement flowElement, DelegateExecution execution,
                                                           ApproverScopeDTO scopeDto) {
        Set<Long> workspaceIdSet = ListUtils.emptyIfNull(scopeDto.getOrgScopes()).stream()
                .map(OrgScope::getWorkspaceId)
                .collect(Collectors.toSet());


        // ouIds、workspaceIds 只能传其中一个  然后代码过滤
        RoleUserParam param = RoleUserParam.builder()
                .roleIds(super.getTypes(flowElement).stream().map(Long::valueOf).collect(Collectors.toSet()))
                .ouIds(ListUtils.emptyIfNull(scopeDto.getOrgScopes()).stream()
                        .map(OrgScope::getOuId)
                        .collect(Collectors.toList()))
                .build();
        List<SaasRoleUserDTO> flowTaskAssigners = parseApiResult(() -> tyrSaasRoleUserApi.roleUserList(param),
                "审批节点: " + flowElement.getId() + ", 通过角色查询审批人",
                "cn.axzo.tyr.client.feign.TyrSaasRoleUserApi.roleUserList", param)
                .stream().filter(f -> workspaceIdSet.contains(f.getWorkspaceId()))
                .collect(Collectors.toList());

        if (CollUtil.isEmpty(flowTaskAssigners)) {
            return super.invokeService(flowElement, execution, scopeDto);
        }
        return flowTaskAssigners.stream().map(u -> BpmnTaskDelegateAssigner.builder()
                        .assignee(String.valueOf(u.getIdentityId()))
                        .assigneeType(String.valueOf(u.getIdentityType()))
                        .tenantId(String.valueOf(u.getWorkspaceId()))
                        .ouId(String.valueOf(u.getOuId()))
                        .personId(String.valueOf(u.getNaturalPersonId()))
                        .build())
                .collect(Collectors.toList());
    }

}
