package cn.axzo.workflow.core.deletage.approverscope;

import cn.axzo.workflow.common.enums.WorkspaceType;
import cn.axzo.workflow.common.model.dto.CooperationOrgDTO;
import cn.axzo.workflow.common.model.dto.CooperationOrgDTO.OrgScope;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

import static cn.axzo.workflow.common.constant.BpmnConstants.BIZ_ORG_RELATION;

/**
 * "审批范围"选择"所在单位"
 *
 * @author syl
 * @date 2023/11/21
 */
@Component
public class GovWorkspaceProcessor implements ApproverScopeProcessor {

    @Override
    public ApproverScopeDTO build(FlowElement flowElement, DelegateExecution execution) {
        // 获取组织信息
        CooperationOrgDTO orgDTO = (CooperationOrgDTO) execution.getVariables().get(BIZ_ORG_RELATION);
        ApproverScopeDTO build = ApproverScopeDTO.builder().build();
        if (Objects.isNull(orgDTO)) {
            return build;
        }

        if (!CollectionUtils.isEmpty(orgDTO.getOrgScopes())) {
            List<OrgScope> orgScopes = orgDTO.getOrgScopes().stream()
                    .filter(o -> Objects.equals(WorkspaceType.GOVERNMENT, WorkspaceType.getType(o.getWorkspaceType())))
                    .collect(Collectors.toList());
            build.setOrgScopes(orgScopes);
        }

        return build;
    }

}
