using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Text;
///
/// 登录日志
///
namespace Basic.DAL
{
public class manager_log
{
///
/// 增加一条数据
///
public int Add(Model.manager_log model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into tb_manager_log(");
strSql.Append("user_id,user_name,action_type,note,login_ip,login_time)");
strSql.Append(" values (");
strSql.Append("@user_id,@user_name,@action_type,@note,@login_ip,@login_time)");
strSql.Append(";select @@IDENTITY");
SqlParameter user_id = new SqlParameter("@user_id", model.user_id);
SqlParameter user_name = new SqlParameter("@user_name", model.user_name);
SqlParameter action_type = new SqlParameter("@action_type", model.action_type);
SqlParameter note = new SqlParameter("@note", model.note);
SqlParameter login_ip = new SqlParameter("@login_ip", model.login_ip);
SqlParameter login_time = new SqlParameter("@login_time", model.login_time);
SqlParameter[] parameters = new SqlParameter[6]
{
user_id,user_name,action_type,note,login_ip,login_time
};
object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
if (obj == null)
{
return 0;
}
else
{
return Convert.ToInt32(obj);
}
}
///
/// 获得查询分页数据
///
public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * FROM tb_manager_log");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
}
///
/// 删除一条数据
///
public bool Delete(int id)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("delete from tb_manager_log ");
strSql.Append(" where id=@id");
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Int,4)};
parameters[0].Value = id;
int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
///
/// 根据用户名返回上一次登录记录
///
public Model.manager_log GetModel(string user_name, int top_num, string action_type)
{
int rows = GetCount("user_name='" + user_name + "'");
if (top_num == 1)
{
rows = 2;
}
if (rows > 1)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select top 1 id from (select top " + top_num + " id from tb_manager_log where user_name=@user_name and action_type=@action_type order by id desc) as T ");
strSql.Append(" order by id asc");
SqlParameter[] parameters = {
new SqlParameter("@user_name", SqlDbType.NVarChar,100),
new SqlParameter("@action_type", SqlDbType.NVarChar,100)};
parameters[0].Value = user_name;
parameters[1].Value = action_type;
object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
if (obj != null)
{
return GetModel(Convert.ToInt32(obj));
}
}
return null;
}
///
/// 返回数据数
///
public int GetCount(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select count(*) as H ");
strSql.Append(" from tb_manager_log ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString()));
}
///
/// 得到一个对象实体
///
public Model.manager_log GetModel(int id)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select top 1 id,user_id,user_name,action_type,note,login_ip,login_time from tb_manager_log ");
strSql.Append(" where id=@id");
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Int,4)};
parameters[0].Value = id;
Model.manager_log model = new Model.manager_log();
DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
if (ds.Tables[0].Rows.Count > 0)
{
if (ds.Tables[0].Rows[0]["id"].ToString() != "")
{
model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
}
if (ds.Tables[0].Rows[0]["user_id"].ToString() != "")
{
model.user_id = int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString());
}
model.user_name = ds.Tables[0].Rows[0]["user_name"].ToString();
model.action_type = ds.Tables[0].Rows[0]["action_type"].ToString();
model.note = ds.Tables[0].Rows[0]["note"].ToString();
model.login_ip = ds.Tables[0].Rows[0]["login_ip"].ToString();
if (ds.Tables[0].Rows[0]["login_time"].ToString() != "")
{
model.login_time = DateTime.Parse(ds.Tables[0].Rows[0]["login_time"].ToString());
}
return model;
}
else
{
return null;
}
}
///
/// 获得查询分页数据
///
public DataSet GetList(string _strWhere, string _orderby, int _pageSize, int _page, out int recordCount)
{
BasicPage bp = new BasicPage();
StringBuilder str_Sql = new StringBuilder();
str_Sql.Append("select top " + _pageSize + " id,user_name,note,login_ip,login_time from tb_manager_log");
str_Sql.Append(" where ");
str_Sql.Append(_strWhere);
str_Sql.Append(" and id not in ");
str_Sql.Append(" ( ");
str_Sql.Append(" select top " + (_page - 1) * _pageSize + " id from tb_manager_log ");
str_Sql.Append(" where ");
str_Sql.Append(_strWhere);
str_Sql.Append(" order by ");
str_Sql.Append(_orderby);
str_Sql.Append(" ) ");
str_Sql.Append(" order by ");
str_Sql.Append(_orderby);
DataSet dst = bp.SelectDataBase("tb_manager_log", str_Sql.ToString());
//
recordCount = totlePage(showTotal(_strWhere), _pageSize);
return dst;
}
//总页数
public int totlePage(int Total, int PageSize)
{
if (Total % PageSize == 0)
{
return Total / PageSize;
}
else
{
return Total / PageSize + 1;
}
}
//总条数
protected int showTotal(string strSql)
{
BasicPage bp = new BasicPage();
int intTotal = 0;
SqlDataReader myread = bp.getRead("select count(id) as CountId from tb_manager_log where " + strSql);
if (myread.Read())
{
intTotal = Convert.ToInt32(myread["CountId"].ToString());
}
myread.Close();
return intTotal;
}
}
}