Search in sources :

Example 1 with Param

use of org.apache.ibatis.annotations.Param in project judge by zjnu-acm.

the class BestSubmissionsBuilder method bestSubmissions.

public static String bestSubmissions(@Param("form") BestSubmissionForm form, @Param("pageable") Pageable pageable) {
    Long contestId = form.getContestId();
    long problemId = form.getProblemId();
    Set<String> dejaVu = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
    Sort sort = Optional.ofNullable(pageable.getSort()).map(s -> s.and(DEFAULT_SORT)).orElse(DEFAULT_SORT);
    Sort.Order[] orders = StreamSupport.stream(sort.spliterator(), false).filter(order -> ALLOW_COLUMNS.contains(order.getProperty()) && dejaVu.add(order.getProperty())).toArray(Sort.Order[]::new);
    final int length = orders.length;
    log.debug("{}", Arrays.asList(orders));
    StringBuilder sb = new StringBuilder("select " + SubmissionMapper.LIST_COLUMNS + " from solution s where problem_id=").append(problemId).append(" and score=100 ");
    if (contestId != null) {
        sb.append("and contest_id=").append(contestId).append(' ');
    }
    for (int i = length - 1; i >= 0; --i) {
        sb.append("and(user_id");
        for (int j = 0; j <= i; ++j) {
            sb.append(',').append(orders[j].getProperty());
        }
        sb.append(")in(select user_id");
        for (int j = 0; j < i; ++j) {
            sb.append(',').append(orders[j].getProperty());
        }
        sb.append(',').append(orders[i].isAscending() ? "min" : "max").append("(").append(orders[i].getProperty()).append(")").append(orders[i].getProperty()).append(" from solution where problem_id=").append(problemId).append(" and score=100 ");
        if (contestId != null) {
            sb.append("and contest_id=").append(contestId).append(' ');
        }
    }
    for (int i = 0; i < length; ++i) {
        sb.append("group by user_id)");
    }
    if (length > 0) {
        sb.append(" order by ");
        for (int i = 0; i < length; ++i) {
            if (i > 0) {
                sb.append(",");
            }
            sb.append(orders[i].getProperty());
            if (!orders[i].isAscending()) {
                sb.append(" desc");
            }
        }
    }
    return sb.append(" limit ").append(pageable.getOffset()).append(",").append(pageable.getPageSize()).toString();
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Arrays(java.util.Arrays) Slf4j(lombok.extern.slf4j.Slf4j) Param(org.apache.ibatis.annotations.Param) Optional(java.util.Optional) Set(java.util.Set) BestSubmissionForm(cn.edu.zjnu.acm.judge.data.form.BestSubmissionForm) StreamSupport(java.util.stream.StreamSupport) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) TreeSet(java.util.TreeSet) TreeSet(java.util.TreeSet) Sort(org.springframework.data.domain.Sort)

Aggregations

BestSubmissionForm (cn.edu.zjnu.acm.judge.data.form.BestSubmissionForm)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 StreamSupport (java.util.stream.StreamSupport)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Param (org.apache.ibatis.annotations.Param)1 Pageable (org.springframework.data.domain.Pageable)1 Sort (org.springframework.data.domain.Sort)1