BaseGridFactory.java 4.7 KB
Newer Older
S
smallchill 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/**
 * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.core.toolbox.grid;

import org.springblade.core.aop.AopContext;
import org.springblade.core.base.controller.BladeController;
import org.springblade.core.constant.Const;
import org.springblade.core.meta.IQuery;
import org.springblade.core.plugins.dao.Db;
import org.springblade.core.plugins.dao.Md;
import org.springblade.core.toolbox.Func;
import org.springblade.core.toolbox.kit.JsonKit;
import org.springblade.core.toolbox.kit.StrKit;
import org.springblade.core.toolbox.support.Convert;
import org.springblade.core.toolbox.support.SqlKeyword;

import java.util.HashMap;
import java.util.Map;

/**
 * grid工厂基类,封装通用分页方法
35
 * @author zhuangqian
S
smallchill 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
 */
public abstract class BaseGridFactory implements IGrid {

	/**
	 * 封装grid返回数据类型
	 * 
	 * @param dbName
	 *            数据库别名
	 * @param page
	 *            当前页号
	 * @param rows
	 *            每页的数量
	 * @param para
	 *            额外条件 {"id":1,"title":"test"}
	 * @param sort
	 *            排序列名 (id)
	 * @param order
	 *            排序方式 (desc)
	 * @param intercept
	 *            业务拦截器
	 * @param ctrl
	 *            控制器
	 * @return Object
	 */
	protected Object basePaginate(String dbName, Integer page, Integer rows, String source, String para, String sort, String order, IQuery intercept, BladeController ctrl) {
		if (source.toLowerCase().indexOf("select") == -1) {
			return paginateById(dbName, page, rows, source, para, sort, order, intercept, ctrl);
		} else {
			return paginateBySql(dbName, page, rows, source, para, sort, order, intercept, ctrl);
		}
	}
	
	private Object paginateById(String dbName, Integer page, Integer rows, String sqlId, String para, String sort, String order, IQuery intercept, BladeController ctrl) {	
		String sqlTemplate = Md.getSql(sqlId);
		return paginateBySql(dbName, page, rows, sqlTemplate, para, sort, order, intercept, ctrl);
	}

	private Object paginateBySql(String dbName, Integer page, Integer rows, String sqlTemplate, String para, String sort, String order, IQuery intercept, BladeController ctrl) {
		String statement = "select * from (" + sqlTemplate + ") blade_statement";
		String sqlex = SqlKeyword.getWhere(para);
		Map<String, Object> map = getSqlMap(para, sort, order);	
		String orderBy = (Func.isEmpty(map.get(Const.ORDER_BY_STR))) ? " " : (" order by " + Func.toStr(map.get(Const.ORDER_BY_STR)));
		String sqlCount = "";
		
		// 查询前拦截
		AopContext ac = null;
		if (null != intercept) {
			ac = new AopContext(ctrl);
			ac.setSql(sqlTemplate);
			ac.setSqlEx(sqlex);
			ac.setCondition("");
			ac.setOrderBy(orderBy);
			ac.setSqlStatement("");
			ac.setSqlCount("");
			ac.setParam(map);
			intercept.queryBefore(ac);
			sqlCount = ac.getSqlCount();
			sqlex = ac.getSqlEx();
			orderBy = ac.getOrderBy();
			statement = (StrKit.isBlank(ac.getSqlStatement()) ? (statement + (StrKit.isBlank(ac.getWhere()) ? (sqlex + ac.getCondition()) : ac.getWhere()) + orderBy) : ac.getSqlStatement());
		} else {
			statement = statement + sqlex + orderBy;
		}

		Object list = (null == ac) ? null : ac.getObject();
		if (null == list) {
			if(StrKit.notBlank(dbName)) {
				list = Db.init(dbName).paginate(statement, sqlCount, map, page, rows);			
			} else {
				list = Db.paginate(statement, sqlCount, map, page, rows);
			}
		}

		// 查询后拦截
		if (null != intercept) {
			if(null == ac.getObject()) {
				ac.setObject(list);
			}
			intercept.queryAfter(ac);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	private static Map<String, Object> getSqlMap(String para, String sort, String order){
		Map<String, Object> map = JsonKit.parse(Func.isEmpty(Func.decodeUrl(para)) ? null : Func.decodeUrl(para), HashMap.class);
		if (Func.isEmpty(map)) {
123
			map = new HashMap<>(16);
S
smallchill 已提交
124
		}
125 126
        for (String key : map.keySet()) {
            if (key.startsWith(SqlKeyword.TOINT) || key.startsWith(SqlKeyword.IT) || key.startsWith(SqlKeyword.F_IT)) {
127
                map.put(key, Convert.toInt(map.get(key)));
128 129
            }
        }
S
smallchill 已提交
130 131 132 133
		map.put(Const.ORDER_BY_STR, Func.isAllEmpty(sort, order) ? "" : (sort + " " + order));
		return map;
	}
}