<commit_before>package top.zbeboy.isy.service.plugin

import org.jooq.*
import org.springframework.util.ObjectUtils
import top.zbeboy.isy.web.util.DataTablesUtils

/**
 * Created by zbeboy 2017-11-17 .
 **/
open class DataTablesPlugin<T> {

    companion object {
        @JvmField
        val CONDITION_TYPE = 0

        @JvmField
        val JOIN_TYPE = 1
    }

    /**
     * 查询全部数据
     *
     * @param dataTablesUtils datatables工具类
     * @param create          jooq create.
     * @param table           jooq table.
     * @return 全部数据
     */
    open fun dataPagingQueryAll(dataTablesUtils: DataTablesUtils<T>, create: DSLContext, table: TableLike<*>): Result<Record> {
        val records: Result<Record>
        val a = searchCondition(dataTablesUtils)
        if (ObjectUtils.isEmpty(a)) {
            val selectJoinStep = create.select()
                    .from(table)
            sortCondition(dataTablesUtils, null, selectJoinStep, JOIN_TYPE)
            pagination(dataTablesUtils, null, selectJoinStep, JOIN_TYPE)
            records = selectJoinStep.fetch()
        } else {
            val selectConditionStep = create.select()
                    .from(table)
                    .where(a)
            sortCondition(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            pagination(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            records = selectConditionStep.fetch()
        }
        return records
    }

    /**
     * 查询全部数据 with 额外条件
     *
     * @param dataTablesUtils datatables工具类
     * @param create          jooq create.
     * @param table           jooq table.
     * @param extraCondition  额外条件
     * @return 全部数据
     */
    open fun dataPagingQueryAllWithCondition(dataTablesUtils: DataTablesUtils<T>, create: DSLContext, table: TableLike<*>, extraCondition: Condition): Result<Record> {
        val records: Result<Record>
        val a = searchCondition(dataTablesUtils)
        if (ObjectUtils.isEmpty(a)) {
            val selectConditionStep = create.select()
                    .from(table)
                    .where(extraCondition)
            sortCondition(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            pagination(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            records = selectConditionStep.fetch()
        } else {
            val selectConditionStep = create.select()
                    .from(table)
                    .where(extraCondition.and(a))
            sortCondition(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            pagination(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            records = selectConditionStep.fetch()
        }
        return records
    }

    /**
     * 查询全部数据 with 额外条件
     *
     * @param dataTablesUtils datatables工具类
     * @param create          jooq create.
     * @param table           jooq table.
     * @param extraCondition  额外条件
     * @return 全部数据
     */
    open fun dataPagingQueryAllWithConditionNoPage(dataTablesUtils: DataTablesUtils<T>, create: DSLContext, table: TableLike<*>, extraCondition: Condition): Result<Record> {
        val records: Result<Record>
        val a = searchCondition(dataTablesUtils)
        if (ObjectUtils.isEmpty(a)) {
            val selectConditionStep = create.select()
                    .from(table)
                    .where(extraCondition)
            records = selectConditionStep.fetch()
        } else {
            val selectConditionStep = create.select()
                    .from(table)
                    .where(extraCondition.and(a))
            records = selectConditionStep.fetch()
        }
        return records
    }

    /**
     * 统计全部
     *
     * @param create jooq create.
     * @param table  jooq table.
     * @return 统计
     */
    open fun statisticsAll(create: DSLContext, table: TableLike<*>): Int {
        val count = create.selectCount()
                .from(table)
                .fetchOne()
        return count.value1()
    }

    /**
     * 统计全部 with 额外条件
     *
     * @param create         jooq create.
     * @param table          jooq table.
     * @param extraCondition 额外条件
     * @return 统计
     */
    open fun statisticsAllWithCondition(create: DSLContext, table: TableLike<*>, extraCondition: Condition): Int {
        val count = create.selectCount()
                .from(table)
                .where(extraCondition)
                .fetchOne()
        return count.value1()
    }

    /**
     * 根据条件统计
     *
     * @param dataTablesUtils datatables工具类
     * @param create          jooq create.
     * @param table           jooq table.
     * @return 统计
     */
    open fun statisticsWithCondition(dataTablesUtils: DataTablesUtils<T>, create: DSLContext, table: TableLike<*>): Int {
        val count: Record1<Int>
        val a = searchCondition(dataTablesUtils)
        if (ObjectUtils.isEmpty(a)) {
            val selectJoinStep = create.selectCount()
                    .from(table)
            count = selectJoinStep.fetchOne()
        } else {
            val selectConditionStep = create.selectCount()
                    .from(table)
                    .where(a)
            count = selectConditionStep.fetchOne()
        }
        return count.value1()
    }

    /**
     * 根据条件统计
     *
     * @param dataTablesUtils datatables工具类
     * @param create          jooq create.
     * @param table           jooq table.
     * @param extraCondition  额外条件
     * @return 统计
     */
    open fun statisticsWithCondition(dataTablesUtils: DataTablesUtils<T>, create: DSLContext, table: TableLike<*>, extraCondition: Condition): Int {
        val count: Record1<Int>
        val a = searchCondition(dataTablesUtils)
        if (ObjectUtils.isEmpty(a)) {
            val selectConditionStep = create.selectCount()
                    .from(table)
                    .where(extraCondition)
            count = selectConditionStep.fetchOne()
        } else {
            val selectConditionStep = create.selectCount()
                    .from(table)
                    .where(extraCondition.and(a))
            count = selectConditionStep.fetchOne()
        }
        return count.value1()
    }

    /**
     * 查询条件，需要自行覆盖
     *
     * @param dataTablesUtils datatables工具类<commit_msg>datatables 优化<commit_after>package top.zbeboy.isy.service.plugin

import org.jooq.*
import org.springframework.util.ObjectUtils
import top.zbeboy.isy.web.util.DataTablesUtils

/**
 * Created by zbeboy 2017-11-17 .
 **/
open class DataTablesPlugin<T> {

    companion object {
        @JvmField
        val CONDITION_TYPE = 0

        @JvmField
        val JOIN_TYPE = 1
    }

    /**
     * 查询全部数据
     *
     * @param dataTablesUtils datatables工具类
     * @param create          jooq create.
     * @param table           jooq table.
     * @return 全部数据
     */
    open fun dataPagingQueryAll(dataTablesUtils: DataTablesUtils<T>, create: DSLContext, table: TableLike<*>): Result<Record> {
        val a = searchCondition(dataTablesUtils)
        return if (ObjectUtils.isEmpty(a)) {
            val selectJoinStep = create.select()
                    .from(table)
            sortCondition(dataTablesUtils, null, selectJoinStep, JOIN_TYPE)
            pagination(dataTablesUtils, null, selectJoinStep, JOIN_TYPE)
            selectJoinStep.fetch()
        } else {
            val selectConditionStep = create.select()
                    .from(table)
                    .where(a)
            sortCondition(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            pagination(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            selectConditionStep.fetch()
        }
    }

    /**
     * 查询全部数据 with 额外条件
     *
     * @param dataTablesUtils datatables工具类
     * @param create          jooq create.
     * @param table           jooq table.
     * @param extraCondition  额外条件
     * @return 全部数据
     */
    open fun dataPagingQueryAllWithCondition(dataTablesUtils: DataTablesUtils<T>, create: DSLContext, table: TableLike<*>, extraCondition: Condition): Result<Record> {
        val a = searchCondition(dataTablesUtils)
        return if (ObjectUtils.isEmpty(a)) {
            val selectConditionStep = create.select()
                    .from(table)
                    .where(extraCondition)
            sortCondition(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            pagination(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            selectConditionStep.fetch()
        } else {
            val selectConditionStep = create.select()
                    .from(table)
                    .where(extraCondition.and(a))
            sortCondition(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            pagination(dataTablesUtils, selectConditionStep, null, CONDITION_TYPE)
            selectConditionStep.fetch()
        }
    }

    /**
     * 查询全部数据 with 额外条件，无分页，做导出数据用
     *
     * @param dataTablesUtils datatables工具类
     * @param create          jooq create.
     * @param table           jooq table.
     * @param extraCondition  额外条件
     * @return 全部数据
     */
    open fun dataPagingQueryAllWithConditionNoPage(dataTablesUtils: DataTablesUtils<T>, create: DSLContext, table: TableLike<*>, extraCondition: Condition): Result<Record> {
        val a = searchCondition(dataTablesUtils)
        return if (ObjectUtils.isEmpty(a)) {
            val selectConditionStep = create.select()
                    .from(table)
                    .where(extraCondition)
            selectConditionStep.fetch()
        } else {
            val selectConditionStep = create.select()
                    .from(table)
                    .where(extraCondition.and(a))
            selectConditionStep.fetch()
        }
    }

    /**
     * 统计全部
     *
     * @param create jooq create.
     * @param table  jooq table.
     * @return 统计
     */
    open fun statisticsAll(create: DSLContext, table: TableLike<*>): Int {
        return create.selectCount()
                .from(table)
                .fetchOne().value1()
    }

    /**
     * 统计全部 with 额外条件
     *
     * @param create         jooq create.
     * @param table          jooq table.
     * @param extraCondition 额外条件
     * @return 统计
     */
    open fun statisticsAllWithCondition(create: DSLContext, table: TableLike<*>, extraCondition: Condition): Int {
        return create.selectCount()
                .from(table)
                .where(extraCondition)
                .fetchOne().value1()
    }

    /**
     * 根据条件统计
     *
     * @param dataTablesUtils datatables工具类
     * @param create          jooq create.
     * @param table           jooq table.
     * @return 统计
     */
    open fun statisticsWithCondition(dataTablesUtils: DataTablesUtils<T>, create: DSLContext, table: TableLike<*>): Int {
        val count: Record1<Int>
        val a = searchCondition(dataTablesUtils)
        count = if (ObjectUtils.isEmpty(a)) {
            val selectJoinStep = create.selectCount()
                    .from(table)
            selectJoinStep.fetchOne()
        } else {
            val selectConditionStep = create.selectCount()
                    .from(table)
                    .where(a)
            selectConditionStep.fetchOne()
        }
        return count.value1()
    }

    /**
     * 根据条件统计
     *
     * @param dataTablesUtils datatables工具类
     * @param create          jooq create.
     * @param table           jooq table.
     * @param extraCondition  额外条件
     * @return 统计
     */
    open fun statisticsWithCondition(dataTablesUtils: DataTablesUtils<T>, create: DSLContext, table: TableLike<*>, extraCondition: Condition): Int {
        val count: Record1<Int>
        val a = searchCondition(dataTablesUtils)
        count = if (ObjectUtils.isEmpty(a)) {
            val selectConditionStep = create.selectCount()
                    .from(table)
                    .where(extraCondition)
            selectConditionStep.fetchOne()
        } else {
            val selectConditionStep = create.selectCount()
                    .from(table)
                    .where(extraCondition.and(a))
            selectConditionStep.fetchOne()
        }
        return count.value1()
    }

    /**
     * 查询条件，需要自行覆盖
     *
     * @param dataTablesUtils datatables工具类
{'fraction_non_alphanumeric': 0.09112961914773186, 'fraction_numerical': 0.0025875313333872402, 'mean_word_length': 1.8910705937353904, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '936881', 'n_tokens_mistral': 3510, 'n_tokens_neox': 3485, 'n_words': 726}