_BaseModel.java 4.0 KB
package com.teplot.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * Depiction:
 * <p>
 * Modify:
 * <p>
 * Author: Kevin Lynn
 * <p>
 * Create Date:2016年7月29日 下午9:01:49
 * <p>
 * 
 * @version 1.0
 * @since 1.0
 */
public abstract class _BaseModel<M extends _BaseModel<M>> extends Model<M> {
	private static final long serialVersionUID = 5181566137071912778L;

	/**
	 *  数据库表名
	 * 
	 * @return
	 */
	public String table() {
		return getClass().getSimpleName();
	}

	public java.sql.Timestamp currentTimestamp() {
		java.util.Date date = new java.util.Date();
		java.sql.Timestamp tt = new java.sql.Timestamp(date.getTime());
		return tt;
	}

	public M searchFirst() {
		return findFirst("select * from " + table());
	}

	public List<M> searchAll() {
		return find("select * from " + table());
	}
	public void update(String carNo) {
		Db.update("update "+table()+" Set status=2 WHERE carNo in (?)",carNo);
	}

	private void checkTableName() {
		if (StrKit.isBlank(table()))
			throw new IllegalArgumentException("tableName can not be blank,please setTableName(tableName)");
	}

	public M searchFirst(String key, Object value) {
		List<M> mList = search(key, value, "");
		return mList != null && mList.size() > 0 ? mList.get(0) : null;
	}

	public List<M> search(String key, Object value) {
		return search(key, value, "");
	}

	public List<M> search(String key, Object value, String orderBy) {
		checkTableName();
		String sql = "select * from " + table() + " where " + key + "=? " + orderBy;
		return find(sql, value);
	}

	public M searchFirst(Map<String, Object> maps) {
		List<M> mList = search(maps, "");
		return mList != null && mList.size() > 0 ? mList.get(0) : null;
	}
	public M searchFirst1(Map<String, Object> maps) {
		List<M> mList = search1(maps, "");
		return mList != null && mList.size() > 0 ? mList.get(0) : null;
	}

	public List<M> search(Map<String, Object> maps) {
		return search(maps, "");
	}
	public List<M> search1(Map<String, Object> maps) {
		return search1(maps, "");
	}
	public List<M> search(Map<String, Object> maps, String orderBy) {
		checkTableName();
		StringBuilder sb = new StringBuilder();
		sb.append("select * from ").append(table()).append(" where 1=1 ");
		List<Object> values = new ArrayList<Object>();
		for (Entry<String, Object> entry : maps.entrySet()) {
			if (entry.getValue() != null) {
				sb.append(" and ").append(entry.getKey()).append("=?");
				values.add(entry.getValue());
			}
		}
		sb.append(" ").append(orderBy);
		return find(sb.toString(), values.toArray());
	}
	public List<M> search1(Map<String, Object> maps, String orderBy) {
		checkTableName();
		StringBuilder sb = new StringBuilder();
		sb.append("select * from ").append(table()).append(" where 1=1 ");
		List<Object> values = new ArrayList<Object>();
		for (Entry<String, Object> entry : maps.entrySet()) {
			if (entry.getValue() != null) {
				sb.append(" and ").append(entry.getKey()).append("=?");
				values.add(entry.getValue());
			}
		}
		sb.append(" ").append("order by ID DESC");
		return find(sb.toString(), values.toArray());
	}
	public Page<M> searchPaginate(int pageNumber, int pageSize, Map<String, Object> maps) {
		return this.searchPaginate(pageNumber, pageSize, maps, "");
	}

	public Page<M> searchPaginate(int pageNumber, int pageSize, Map<String, Object> maps, String orderBy) {
		checkTableName();
		StringBuilder sb = new StringBuilder();
		sb.append("from ").append(table()).append(" where 1=1");
		List<Object> values = new ArrayList<Object>();
		for (Entry<String, Object> entry : maps.entrySet()) {
			if (entry.getValue() != null) {
				sb.append(" and ").append(entry.getKey()).append("=?");
				values.add(entry.getValue());
			}
		}
		sb.append(orderBy);
		return paginate(pageNumber, pageSize, "select *", sb.toString(), values.toArray());
	}

	@Override
	public String toString() {
		return toJson();
	}

}