package com.palmnest.dao.estateserve.impl;

import com.palmnest.application.core.util.DateUtils;
import com.palmnest.dao.estateserve.MethodExecuteLogDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.MethodLog;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @Author HIAPAD
 * @Description TODO
 * @Date 2018/12/27 17:43
 **/
@Repository("methodExecuteLogDao")
public class MethodExecuteLogDaoImpl extends GenericDaoHibernate<MethodLog, Long> implements MethodExecuteLogDao {


	public MethodExecuteLogDaoImpl() {
		super(MethodLog.class);
	}

	@Override
	public MethodLog getMethodLog(Integer orgId, String methodName, int minute) {
		StringBuffer strSQL = new StringBuffer(" SELECT * FROM tb_method_execute_log WHERE orgId=").append(orgId)
				.append(" AND method_name=").append("'").append(methodName).append("'");
		//无时间限制
		if (minute != 0) {
			strSQL.append(" AND date_add(create_time, INTERVAL " + minute + " MINUTE) >= '" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss") + "'");
		}
		strSQL.append(" ORDER BY create_time DESC");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(strSQL.toString());
		if (!CollectionUtils.isEmpty(list)) {
			Long codeId = (Long) list.get(0).get("id");
			return get(codeId);
		}
		return null;
	}
}
