package com.wt.dms.dao.impl;

import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import com.hirisun.components.dao.hibernate.AbstractHibernateDaoSupport;
import com.wt.dms.dao.BackLogRecordDao;
import com.wt.dms.model.BackLogRecord;
import com.wt.dms.util.StatisticVariable;

/**
 * <pre>
 * 业务名:
 * 功能说明: 
 * 编写日期:
 * 作者:	HEA IDE
 * 
 * 历史记录
 * 1、修改日期：
 *    修改人：
 *    修改内容：
 * </pre>
 */
@Repository
public class BackLogRecordDaoImpl extends AbstractHibernateDaoSupport<BackLogRecord> implements
		BackLogRecordDao
{
	/**
	 * 根据指标uuid、步骤，从操作记录表中获得一条记录
	 * 
	 * @param idxUUID
	 *            指标id
	 * @param step
	 *            步骤
	 * @return 日志对象
	 */
	public BackLogRecord getBackLogRecordByIdxAndStep(String idxUUID, int step)
	{
		BackLogRecord blr = new BackLogRecord();
		String hql = "From BackLogRecord t where t.operateType != '"
				+ StatisticVariable.OPERATE_TYPE_BACK + "' and t.indexManager.idxUuid = '"
				+ idxUUID + "' and t.processStep = " + step + " order by t.operateTime desc";
		List<BackLogRecord> rackLogRecords = getSession().createQuery(hql).list();
		if (rackLogRecords != null && rackLogRecords.size() > 0) {
			blr = rackLogRecords.get(0);
		}
		return blr;
	}

	/**
	 * 根据指标uuid、步骤，从操作记录表中获得一条记录,流程中消除门户待办使用
	 * 
	 * @param idxUUID
	 *            指标id
	 * @param step
	 *            步骤
	 * @return 日志对象
	 */
	public String getBackLogRecordByIdxAndStepBKP(String idxUUID)
	{
		String sql = "select o.TASK_IDS from(select t.TASK_IDS,row_number() over(order by t.OPERATE_TIME desc)as rn From H_BACKLOG_RECORDS t where t.IDX_UUID = '"
				+ idxUUID + "' order by t.OPERATE_TIME desc) o where o.rn = 2";
		SQLQuery query = super.getSession().createSQLQuery(sql);
		return query.uniqueResult() == null ? "" : String.valueOf(query.uniqueResult());
	}
}
