package cn.com.liandisys.ycjt.ggcx.admin.utils;

import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.google.common.collect.Lists;

public class DynamicSpecifications {

    public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> clazz) {

        return new Specification<T>() {

            @SuppressWarnings({ "rawtypes", "unchecked" })
            @Override
            public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {

                if (filters != null && !(filters.isEmpty())) {

                    List<Predicate> predicates = Lists.newArrayList();
                    for (SearchFilter filter : filters) {
                        // nested path translate, 如Task的名为"user.name"的filedName,
                        // 转换为Task.user.name属性
                        String[] names = StringUtils.split(filter.fieldName, ".");
                        Path expression = root.get(names[0]);
                        for (int i = 1; i < names.length; i++) {
                            expression = expression.get(names[i]);
                        }
                        // logic operator
                        // 将查询条件中的%和_转译，并在like查询时用escape条件过滤，使得%和_在like查询是不再当做特殊字符处理
                        String pattern = "";
                        if (null != filter.value) {
                            pattern = filter.value.toString().toUpperCase();
                            pattern = pattern.replaceAll("%", "\\\\%");
                            pattern = pattern.replaceAll("_", "\\\\_");
                        }
                        switch (filter.operator) {
                            case EQ:
                                predicates.add(builder.equal(expression, filter.value));
                                break;
                            case LIKE:
                                predicates.add(builder.like(builder.upper(expression), "%" + pattern + "%", '\\'));
                                break;
                            case LIKE_L:
                                predicates.add(builder.like(builder.upper(expression), "%" + pattern, '\\'));
                                break;
                            case LIKE_R:
                                predicates.add(builder.like(builder.upper(expression), pattern + "%", '\\'));
                                break;
                            case GT:
                                predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                                break;
                            case LT:
                                predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                                break;
                            case GTE:
                                predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                                break;
                            case LTE:
                                predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                                break;
                            case NL:
                                predicates.add(builder.isNull(expression));
                                break;
                            case NNL:
                                predicates.add(builder.isNotNull(expression));
                                break;
                            case NEQ:
                                predicates.add(builder.notEqual(expression, filter.value));
                                break;
                            default:
                                break;
                        }
                    }

                    // 将所有条件用 and 联合起来
                    if (predicates.size() > 0) {
                        return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                    }
                }

                return builder.conjunction();
            }
        };
    }
}
