package com.pictc.jdbc.model;

import java.io.Serializable;
import java.util.List;

import com.pictc.jdbc.model.Criteria.Type;
import com.pictc.utils.CollectionUtils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

@ApiModel
@Accessors(chain = true)
@Data
public class CriteriaGroup implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@ApiModelProperty(name = "条件组")
	private List<Criteria> criterias = CollectionUtils.newArrayList();
	
	@ApiModelProperty(name = "条件组之间的连接方式：默认为AND")
	private Link link = Link.AND;
	
	public boolean hasCriteria() {
		return criterias!=null && !criterias.isEmpty();
	}
	
	public CriteriaGroup andSql(String sql,Object val) {
		return sql(Link.AND, sql, val);
	}
	
	public CriteriaGroup orSql(String sql,Object val) {
		return sql(Link.OR, sql, val);
	}
	
	public CriteriaGroup sql(Link link,String sql,Object val) {
		criterias.add(new Criteria().setLink(link).setType(Type.EQ).setSql(sql).setVal(val));
		return this;
	}
	
	public CriteriaGroup add(Criteria criteria) {
		criterias.add(criteria);
		return this;
	}
	
	public CriteriaGroup andEq(String field,Object val) {
		return eq(Link.AND, field, val);
	}
	
	public CriteriaGroup orEq(String field,Object val) {
		return eq(Link.OR, field, val);
	}
	
	public CriteriaGroup eq(Link link,String field,Object val) {
		criterias.add(new Criteria().setLink(link).setType(Type.EQ).setField(field).setVal(val));
		return this;
	}
	
	public CriteriaGroup andLike(String field,Object val) {
		return like(Link.AND, field, val);
	}
	
	public CriteriaGroup orLike(String field,Object val) {
		return like(Link.OR, field, val);
	}
	
	public CriteriaGroup like(Link link,String field,Object val) {
		criterias.add(new Criteria().setLink(link).setType(Type.LIKE).setField(field).setVal(val));
		return this;
	}
	
	public CriteriaGroup andGt(String field,Object val) {
		return gt(Link.AND, field, val);
	}
	
	public CriteriaGroup orGt(String field,Object val) {
		return gt(Link.OR, field, val);
	}
	
	public CriteriaGroup gt(Link link,String field,Object val) {
		criterias.add(new Criteria().setLink(link).setType(Type.GT).setField(field).setVal(val));
		return this;
	}
	
	public CriteriaGroup andEqLt(String field,Object val) {
		return eqLt(Link.AND, field, val);
	}
	
	public CriteriaGroup orEqLt(String field,Object val) {
		return eqLt(Link.OR, field, val);
	}
	
	public CriteriaGroup eqLt(Link link,String field,Object val) {
		criterias.add(new Criteria().setLink(link).setType(Type.EQ_LT).setField(field).setVal(val));
		return this;
	}
	
	public CriteriaGroup andEqGt(String field,Object val) {
		return eqGt(Link.AND, field, val);
	}
	
	public CriteriaGroup orEqGt(String field,Object val) {
		return eqGt(Link.OR, field, val);
	}
	
	public CriteriaGroup eqGt(Link link,String field,Object val) {
		criterias.add(new Criteria().setLink(link).setType(Type.EQ_GT).setField(field).setVal(val));
		return this;
	}
	
	public CriteriaGroup andBetween(String field,Object val,Object secondVal) {
		return between(Link.AND, field, val, secondVal);
	}
	
	public CriteriaGroup orBetween(String field,Object val,Object secondVal) {
		return between(Link.OR, field, val, secondVal);
	}
	
	public CriteriaGroup between(Link link,String field,Object val,Object secondVal) {
		criterias.add(new Criteria().setLink(link).setType(Type.BETWEEN).setField(field).setVal(val).setSecondVal(secondVal));
		return this;
	}
	
	public CriteriaGroup andIn(String field,List<Object> listVal) {
		return in(Link.AND, field, listVal);
	}
	
	public CriteriaGroup orIn(String field,List<Object> listVal) {
		return in(Link.OR, field, listVal);
	}
	
	public CriteriaGroup in(Link link,String field,List<Object> listVal) {
		criterias.add(new Criteria().setLink(link).setType(Type.IN).setField(field).setListVal(listVal));
		return this;
	}

}
