package org.fouram.service;

import java.util.List;

import org.fouram.core.base.model.PageData;
import org.fouram.core.base.service.BaseService;
import org.fouram.entity.SysDictionary;
import org.springframework.stereotype.Service;

@Service
public class SysDictionaryService extends BaseService {

	public PageData findOneById(PageData pd) throws Exception {
		return (PageData) findObject("SysDictionaryMapper.findOneById", pd);
	}

	@SuppressWarnings("unchecked")
	public List<SysDictionary> findListByType(String type) throws Exception {
		return (List<SysDictionary>) findList("SysDictionaryMapper.findListByType", type);
	}

	public SysDictionary findOneByTypeAndKey(String type, String key) throws Exception {
		PageData pd = new PageData();
		pd.put("type", type);
		pd.put("key", key);
		return (SysDictionary) findObject("SysDictionaryMapper.findOneByTypeAndKey", pd);
	}

	public String findValueByTypeAndKey(String type, String key) throws Exception {
		return findOneByTypeAndKey(type, key).getValue();
	}

	public PageData edit(PageData pd) throws Exception {
		return (PageData) findObject("SysDictionaryMapper.edit", pd);
	}

	public void updateValue(String type, String key, String newValue) throws Exception {
		PageData pd = new PageData();
		pd.put("type", type);
		pd.put("key", key);
		pd.put("value", newValue);
		update("SysDictionaryMapper.updateValue", pd);
	}
	
	public Long getNextId(String type, String key) throws Exception{
		String value = findOneByTypeAndKey(type, key).getValue();
		Long nextId = Long.valueOf(value) + 1;
		updateValue(type, key, nextId.toString());
		return nextId;
	}
	
	public Long getCurrentId(String type, String key) throws Exception{
		String value = findOneByTypeAndKey(type, key).getValue();
		return Long.valueOf(value);
	}
}
