package com.ruoyi.bst.app.service.impl;

import com.ruoyi.bst.app.domain.AppQuery;
import com.ruoyi.bst.app.domain.AppVO;
import com.ruoyi.bst.app.service.AppAssembler;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.system.user.domain.UserQuery;
import com.ruoyi.system.user.service.UserService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class AppAssemblerImpl implements AppAssembler {

    private final UserService userService;

    public AppAssemblerImpl(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void assembleUserName(List<AppVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }
        List<Long> userIds = list.stream().map(AppVO::getOwnerIds).flatMap(Collection::stream).collect(Collectors.toList());
        UserQuery userQuery = new UserQuery();
        userQuery.setUserIds(userIds);
        List<UserVO> userList = new ArrayList<>();
        if (CollectionUtils.isNotEmptyElement(userIds)) {
            userList = userService.selectUserList(userQuery);
        }

        for (AppVO app : list) {
            if (app.getOwnerIds() == null) {
                app.setUserList(new ArrayList<>());
                continue;
            }
            List<UserVO> appUserList = userList.stream().filter(item -> {
                return app.getOwnerIds().contains(item.getUserId());
            }).collect(Collectors.toList());
            app.setUserList(appUserList);
        }
    }

}
