package com.wx.application.nebula.graph.query;

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

/**
 * 
 */
public class TermQueryBuilder {
	
	private final static String where = "WHERE ";
	
	private List<TermQuery> termQuerys = new ArrayList<>();
	
	public static TermQuery termMust(String fieldName,TermQuery.Operator operator, Object value) {
		return new TermQuery(fieldName,operator, value);
	}
	
	public static TermQuery termShold(String fieldName, TermQuery.Operator operator, Object value) {
		TermQuery query = new TermQuery(fieldName, operator, value);
		query.logic = TermQuery.Logic.OR;
		return query;
	}
	
	public void add(TermQuery termQuery) {
		termQuerys.add(termQuery);
	}
	
	public String of(String alias) {
		
		if(termQuerys.size() == 0) {
			return null;
		}
		
		StringBuffer sb = new StringBuffer(where);
		for(int i=0;i<termQuerys.size();i++) {
			TermQuery item = termQuerys.get(i);
			sb.append(alias).append(".");
			if(i<termQuerys.size() - 1) {
				sb.append(item.toString()).append(" ")
				.append(item.logic.name()).append(" ");
			} else {
				sb.append(item.toString()).append(" ");
			}
		}
		
		return sb.toString();
	}
	
}
