package cn.hmsoft.frame.data.dao;

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

import org.springframework.stereotype.Repository;

import cn.hmsoft.frame.data.model.FrameParam;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.jdbc.core.PlatformDaoSupport;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.jdbc.entity.QueryOrder;

@Repository
public class FrameParamDao extends PlatformDaoSupport<FrameParam>{

	private final static String LIST_PARAM_SQL = "select * from frame_param where project_owner like ? or project_owner is null or project_owner='' order by param_name";

	/***********************
	 * 查询参数列表
	 * 
	 * @param project_owner 如果为空，返回所有参数
	 * @return
	 */
	public List<FrameParam> listParam(String projectOwner) {
		return this.listBySql(LIST_PARAM_SQL, StringHelper.isNotEmpty(projectOwner) ? ("," + projectOwner + ",") : "");
	}
	
	public Map<String, String> mapParams(String appName) {
		Map<String, String> paramMap = new HashMap<String, String>();
		for (FrameParam t : this.listParam(appName))
			paramMap.put(t.getParam_name().trim(), t.getParam_value().trim());
		return paramMap;
	}
	
	public Pager pageParam(Integer start, Integer limit, String query, QueryOrder order) {
		String sql = "select * from frame_param ";
		if (StringHelper.isEmpty(query)) {
			return super.pageMapBySql(order, start, limit, sql);
		} 
		sql = sql + " where param_name like ? or param_value like ? or param_desc like ?";
		return this.pageMapBySql(order, start, limit, sql, "%" + query + "%", "%" + query + "%", "%" + query + "%");
	}
	
}
