package cn.hmsoft.frame.service;

import java.util.List;

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

import cn.hmsoft.frame.data.dao.FrameTimerDao;
import cn.hmsoft.frame.data.model.FrameTimer;
import cn.hmsoft.frame.util.FrameAssertUtil;

@Service
public class FrameTimerService extends FrameService {

	@Autowired
	private FrameTimerDao frameTimerDao;

	public List<FrameTimer> listTimer(String project_name) {
		return frameTimerDao.listTimer(project_name);
	}
	
	public void addTimer(FrameTimer timer) {
		FrameAssertUtil.isNotEmpty(timer.getTimer_name(), "定时器名称为空");
		FrameAssertUtil.isNotEmpty(timer.getTimer_class(), "定时器实现类为空");
		timer.setTimer_name(timer.getTimer_name().trim());
		FrameTimer tr = this.frameTimerDao.find("timer_name", timer.getTimer_name());
		FrameAssertUtil.isNull(tr, "定时器名称 " + timer.getTimer_name() + " 已经存在，请重新输入");
		this.frameTimerDao.insert(timer);
	}

	public void editTimer(FrameTimer timer) {
		FrameAssertUtil.isNotEmpty(timer.getTimer_name(), "定时器名称为空");
		FrameAssertUtil.isNotEmpty(timer.getTimer_class(), "定时器实现类为空");
		FrameTimer tr = this.frameTimerDao.find("timer_name", timer.getTimer_name());
		if (tr != null)
			FrameAssertUtil.isEqual(tr.getTimer_id(), timer.getTimer_id(), "定时器 " + timer.getTimer_name() + " 已经存在，请重新输入");
		
		this.frameTimerDao.update(timer);
	}

	public void deleteTimer(int timer_id) {		
		this.frameTimerDao.delete(timer_id);
	}
}
