package cn.stylefeng.team.project.modular.business.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.sys.api.pojo.menu.ProjectBusinessDTO;
import cn.stylefeng.team.project.api.entity.Business;
import cn.stylefeng.team.project.api.entity.BusinessCategory;

import java.util.ArrayList;
import java.util.List;

public class BusinessTreeFactory {

    public static List<ProjectBusinessDTO> createBusinessTree(List<BusinessCategory> totalBusinessCategory,
                                                              List<Business> totalBusinessList) {

        ArrayList<ProjectBusinessDTO> result = new ArrayList<>();

        if (ObjectUtil.isEmpty(totalBusinessCategory)) {
            return result;
        }
        for (BusinessCategory businessCategory : totalBusinessCategory) {
            ProjectBusinessDTO projectBusinessDTO = new ProjectBusinessDTO();
            projectBusinessDTO.setId(businessCategory.getBusinessCategoryId());
            projectBusinessDTO.setName(businessCategory.getCategoryName());
            projectBusinessDTO.setBusinessList(new ArrayList<>());
            result.add(projectBusinessDTO);
        }

        for (Business business : totalBusinessList) {
            for (ProjectBusinessDTO categoryItem : result) {
                if (business.getBusinessCategoryId().equals(categoryItem.getId())) {
                    ProjectBusinessDTO businessItem = new ProjectBusinessDTO();
                    businessItem.setId(business.getBusinessId());
                    businessItem.setName(business.getBusinessName());
                    categoryItem.getBusinessList().add(businessItem);
                    break;
                }
            }
        }

        return result;
    }

}
