ProjectMutex.java 2.61 KB
package com.ui.project;

import org.apache.commons.lang.StringUtils;

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

/**
 * 有些项目是互斥的
 */
public class ProjectMutex {

    //与其他项目互斥,但是本身并不互斥
    private final static List<List<String>> java_mutex_project_to_other=new ArrayList<>();
    //private final static List<String> java_mutex_project_to_other_rule1= Arrays.asList(new String[]{"yohobuy-activity","yohobuy-product","yohobuy-promotion","yoho-gateway"});
    private final static List<String> java_mutex_project_to_other_rule1= Arrays.asList(new String[]{"yohobuy-activity"});
    private final static List<String> java_mutex_project_to_other_rule2= Arrays.asList(new String[]{"yohobuy-product"});
    private final static List<String> java_mutex_project_to_other_rule3= Arrays.asList(new String[]{"yohobuy-promotion"});
    private final static List<String> java_mutex_project_to_other_rule4= Arrays.asList(new String[]{"yoho-gateway"});
    static {
        //java_mutex_project_to_other.add(java_mutex_project_to_other_rule1);
        //java_mutex_project_to_other.add(java_mutex_project_to_other_rule2);
        //java_mutex_project_to_other.add(java_mutex_project_to_other_rule3);
        //java_mutex_project_to_other.add(java_mutex_project_to_other_rule4);
    }


    /**
     *  检查是否存在互斥项目
     * 差集
     * @param sourceProjects
     * @return
     */
    public static List<Object> checkMutex(String sourceProjects){
        boolean mutex=false;
        String descr="";
        List<String> sourceList=new ArrayList<>();
        for(String str:sourceProjects.split(",")){
            if(StringUtils.isNotBlank(str)){
                sourceList.add(str);
            }
        }
        if(sourceList!=null&&sourceList.size()>0){
            //检查互斥
            for(List<String> lsTmp:java_mutex_project_to_other){
                List<String> checkListList=new ArrayList<>();
                checkListList.addAll(sourceList);
                checkListList.removeAll(lsTmp);

                if(checkListList.size()>0&&checkListList.size()<sourceList.size()){
                    descr += "项目[";
                    descr += StringUtils.join(checkListList,",");
                    descr +="]与[";
                    descr += StringUtils.join(lsTmp,",");
                    descr +="]不允许同时发布,请单独提发布";
                    mutex=true;
                    break;
                }

            }
        }
        List<Object> ls=new ArrayList<>();
        ls.add(mutex);
        ls.add(descr);
        return ls;
    }
}