package net.wicp.tams.duckula.ops;

import org.apache.tapestry5.http.services.Request;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import net.wicp.tams.common.Conf;
import net.wicp.tams.common.thread.threadlocal.PerThreadValue;
import net.wicp.tams.common.thread.threadlocal.PerthreadManager;

public abstract class WebTools {
	public static <T> Page<T> buildPage(Request request) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		return new Page<T>(page, rows);
	}

	/**
	 * 跳过sql拦截检查，如：在未登陆时没办法拿到拦截数据，需要跳过,单次有效
	 * 
	 * @param force 是否强制跳过，false:为避免数据混乱超级用户只有查询才跳过，true:不管新增还是查询，都跳过，适用于导数据，如创建租户时的初始化
	 */
	public static void skipSQLInterceptor(boolean force) {
		PerThreadValue<Boolean> superUserValue = PerthreadManager.getInstance().createValue(
				Conf.get(force ? "common.jdbc.sqlIntercept.skip" : "common.jdbc.sqlIntercept.superUser"),
				Boolean.class);
		superUserValue.set(true);
	}

}
