package com.hz.pm.api.todocenter.utils;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Sets;
import com.hz.pm.api.user.manage.UserInfoManage;
import com.ningdatech.basic.util.CollUtils;
import com.wflow.workflow.bean.dto.ProcessInstanceUserDto;
import com.wflow.workflow.bean.process.ProgressNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 装配节点用户信息工具类
 *
 * @author CMM
 * @since 2023/03/08 11:52
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class BuildUserUtils {

    private final UserInfoManage userInfoManage;

    public void buildUserByProcessInfo(List<ProgressNode> progressInfo) {
        Set<String> userSet = Sets.newHashSet();
        progressInfo.forEach(node -> {
            if (CollUtil.isNotEmpty(node.getChildren())) {
                for (ProgressNode innerNode : node.getChildren()) {
                    userSet.add(innerNode.getUserId());
                }
            } else {
                userSet.add(node.getUserId());
            }
        });
        List<Long> tmpUserIds = CollUtils.convert(userSet, Long::parseLong);
        Map<String, ProcessInstanceUserDto> userMap = userInfoManage.getUserMapByUserIds(tmpUserIds);
        buildUser(progressInfo, userMap);
    }

    private void buildUser(List<ProgressNode> progressInfo, Map<String, ProcessInstanceUserDto> userMap) {
        for (ProgressNode progressNode : progressInfo) {
            progressNode.setUser(userMap.get(progressNode.getUserId()));
            if (CollUtil.isNotEmpty(progressNode.getChildren())) {
                buildUser(progressNode.getChildren(), userMap);
            }
        }
    }


}
