package cn.hmsoft.frame.data.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.hmsoft.frame.constants.FrameStatus;
import cn.hmsoft.frame.data.model.FrameTimer;
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 FrameTimerDao extends PlatformDaoSupport<FrameTimer> {

	public Pager pageTimer(Integer start, Integer limit, String query, QueryOrder order) {
		String sql = "select * from frame_timer ";
		if (StringHelper.isEmpty(query))
			return this.pageMapBySql(order, start, limit, sql);
		sql += " where timer_name like ?";
		return this.pageMapBySql(order, start, limit, sql,this.generateLikeParamter(query));
	}
	public Pager pageTimerLog(int timer_id,Integer start, Integer limit) {
		String sql = "select * from frame_timer_log where timer_id =? order by log_id desc";
			return this.pageMapBySql( start, limit, sql,timer_id);
	}

	private final static String LIST_TIMER_SQL = "select * from frame_timer where project_name = ? or 1=? order by timer_order";

	/***********************
	 * 查询所有定时器
	 * 
	 * @param project_name
	 *            如果为空，返回所有定时器
	 * @return
	 */
	public List<FrameTimer> listTimer(String project_name) {
		return this.listBySql(LIST_TIMER_SQL, StringHelper.isNotEmpty(project_name) ? project_name : "",
				StringHelper.isNotEmpty(project_name) ? 2 : 1);
	}

	private final static String UPDATE_RUNNING_STATUS_SQL = "update frame_timer set running_status=? where timer_id=?";

	/******************
	 * 更新运行状态
	 */
	public void updateRunningStatus(int timer_id, FrameStatus status) {
		this.updateBySql(UPDATE_RUNNING_STATUS_SQL, status.toString(), timer_id);
	}
}
