package cn.hmsoft.frame.data.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.hmsoft.frame.data.model.FrameDict;
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 FrameDictDao extends PlatformDaoSupport<FrameDict>{

	public List<FrameDict> getAllConfig() {
		String sql = "select * from frame_dict order by dict_name,dict_order";
		return this.listBySql(sql);
	}

	public Pager pageDict(Integer start, Integer limit, String type_name, String query, QueryOrder order) {
		String sql = "select * from frame_dict where dict_name=?";
		if (StringHelper.isEmpty(query))
			return this.pageBySql(FrameDict.class, order, start, limit, sql, type_name);
		sql = sql + " and (dict_value like ? or dict_text like ?) ";
		return this.pageMapBySql(order, start, limit, sql, type_name, "%" + query + "%", "%" + query + "%");
	}

	public FrameDict getFrameDict(String dict_name, String dict_value) {
		String sql = "select * from frame_dict where dict_name=? and dict_value=?";
		return this.findBySql(sql, dict_name, dict_value);
	}

	public void updateFrameDict(FrameDict config) {
		String sql = "update frame_dict set dict_text = ?,dict_order=? where dict_name=? and dict_value=?";
		this.updateBySql(sql, config.getDict_text(), config.getDict_order(),  config.getDict_name(), config.getDict_value());
	}

	public void deleteFrameDict(FrameDict dict) {
		String sql = "delete from frame_dict where dict_name=? and dict_value=?";
		this.updateBySql(sql, dict.getDict_name(), dict.getDict_value());
	}
	
	public void addFrameDict(String dict_name,String dict_text){
		FrameDict fc = new FrameDict();
		fc.setDict_name(dict_name);
		fc.setDict_text(dict_text);
		fc.setDict_value(dict_text);
		fc.setDict_order(this.count("dict_name", dict_name)+1);
		this.insert(fc);
	}

	public FrameDict getFrameDictBysql(String dict_name, String dict_value, String dict_text) {
		String sql = "select * from frame_dict where dict_name=? and (dict_value=? or dict_text=?)";
		return this.findBySql(FrameDict.class, sql, dict_name,dict_value,dict_text);
	}
}
