package cn.hmsoft.skill.data.dao.std;

import java.time.LocalDateTime;

import org.springframework.stereotype.Repository;

import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.jdbc.core.PlatformDaoSupport;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.skill.data.model.std.StdLog;

@Repository
public class StdLogDao extends PlatformDaoSupport<StdLog> {
	/***********************
	 * 分页查询
	 */
	public Pager pageLog(Integer std_id, Integer start, Integer limit) {
		String sql = "select * from std_log where std_id=? order by log_time desc";
		return this.pageMapBySql(start, limit, sql, std_id);
	}

	public StdLog log(Integer school_id, Integer std_id, String log_type, FrameOptr optr) {
		return log(school_id, std_id, log_type, LocalDateTime.now(), optr);
	}

	public StdLog log(Integer school_id, Integer std_id, String log_type, FrameOptr optr, Object... args) {
		return log(school_id, std_id, log_type, LocalDateTime.now(), optr, args);
	}

	public StdLog log(Integer school_id, Integer std_id, String log_type, LocalDateTime log_time, FrameOptr optr, Object... args) {
		StdLog log = new StdLog();
		log.setSchool_id(school_id);
		if (log_time == null) {
			log.setLog_time(LocalDateTime.now());
		} else {
			log.setLog_time(log_time);
		}
		log.setLog_type(log_type);
		log.setStd_id(std_id);
		if (optr != null) {
			log.setOptr_id(optr.getOptr_id());
			log.setOptr_name(optr.getOptr_name());
		}
		String log_value = "";
		for (int i = 0; i < args.length; i++) {
			if (args[i] != null) {
				log_value = log_value + (i > 0 ? "->" : "") + args[i].toString();
			}
		}
		log.setLog_value(log_value);
		this.insert(log);
		return log;
	}
	
	public StdLog log(int std_id, String log_type, LocalDateTime log_time, FrameOptr optr, Object... args) {
		StdLog log = new StdLog();
		if (log_time == null) {
			log.setLog_time(LocalDateTime.now());
		} else {
			log.setLog_time(log_time);
		}
		log.setLog_type(log_type);
		log.setStd_id(std_id);
		if (optr != null) {
			log.setOptr_id(optr.getOptr_id());
			log.setOptr_name(optr.getOptr_name());
		}

		String log_value = "";
		for (int i = 0; i < args.length; i++) {
			if (args[i] != null) {
				log_value = log_value + (i > 0 ? "~" : "") + args[i].toString();
			}
		}
		log.setLog_value(log_value);

		this.insert(log);
		return log;
	}

}
