package cn.hmsoft.frame.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hmsoft.frame.data.dao.FrameDictDao;
import cn.hmsoft.frame.data.dao.FrameDictNameDao;
import cn.hmsoft.frame.data.model.FrameDict;
import cn.hmsoft.frame.data.model.FrameDictName;
import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.util.FrameAssertUtil;

@Service
public class FrameDictService extends FrameService {

	@Autowired
	private FrameDictDao frameDictDao;
	@Autowired
	private FrameDictNameDao dictNameDao;



	public void addFrameDict(FrameDict config, FrameOptr optr) {
		FrameAssertUtil.adminOptr(optr);
		FrameDictName type = this.dictNameDao.find(FrameDictName.class, "dict_name", config.getDict_name());
		if (type == null)
			return;
		FrameDict dict = this.frameDictDao.getFrameDictBysql(config.getDict_name(), config.getDict_value(),
				config.getDict_text());
		FrameAssertUtil.isNull(dict, "字典  " + type.getDict_name() + " 已经存在字典 " + config.getDict_value());
		this.frameDictDao.insert(config);
	}

	public void editFrameDict(FrameDict dict, FrameOptr frameOptr) {
		FrameAssertUtil.adminOptr(frameOptr);
		FrameDictName type = this.frameDictDao.find(FrameDictName.class, "dict_name", dict.getDict_name());
		if (type == null)
			return;
		FrameDict cg = this.frameDictDao.getFrameDict(dict.getDict_name(), dict.getDict_value());
		FrameAssertUtil.isNotNull(cg, "字典  " + type.getDict_name() + " 不存在字典 " + dict.getDict_value());
		this.frameDictDao.updateFrameDict(dict);
	}

	public void deleteFrameDict(FrameDict config, FrameOptr optr) {
		FrameAssertUtil.adminOptr(optr);
		FrameDictName type = this.dictNameDao.find(FrameDictName.class, "dict_name", config.getDict_name());
		if (type == null)
			return;
		FrameDict cg = this.frameDictDao.getFrameDict(config.getDict_name(), config.getDict_value());
		FrameAssertUtil.isNotNull(cg, "字典  " + type.getDict_name() + " 不存在字典 " + config.getDict_value());
		this.frameDictDao.deleteFrameDict(config);
	}

}
