You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
8.5 KiB

пре 3 година
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Text;
  7. /// <summary>
  8. /// 登录日志
  9. /// </summary>
  10. namespace Basic.DAL
  11. {
  12. public class manager_log
  13. {
  14. /// <summary>
  15. /// 增加一条数据
  16. /// </summary>
  17. public int Add(Model.manager_log model)
  18. {
  19. StringBuilder strSql = new StringBuilder();
  20. strSql.Append("insert into tb_manager_log(");
  21. strSql.Append("user_id,user_name,action_type,note,login_ip,login_time)");
  22. strSql.Append(" values (");
  23. strSql.Append("@user_id,@user_name,@action_type,@note,@login_ip,@login_time)");
  24. strSql.Append(";select @@IDENTITY");
  25. SqlParameter user_id = new SqlParameter("@user_id", model.user_id);
  26. SqlParameter user_name = new SqlParameter("@user_name", model.user_name);
  27. SqlParameter action_type = new SqlParameter("@action_type", model.action_type);
  28. SqlParameter note = new SqlParameter("@note", model.note);
  29. SqlParameter login_ip = new SqlParameter("@login_ip", model.login_ip);
  30. SqlParameter login_time = new SqlParameter("@login_time", model.login_time);
  31. SqlParameter[] parameters = new SqlParameter[6]
  32. {
  33. user_id,user_name,action_type,note,login_ip,login_time
  34. };
  35. object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
  36. if (obj == null)
  37. {
  38. return 0;
  39. }
  40. else
  41. {
  42. return Convert.ToInt32(obj);
  43. }
  44. }
  45. /// <summary>
  46. /// 获得查询分页数据
  47. /// </summary>
  48. public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
  49. {
  50. StringBuilder strSql = new StringBuilder();
  51. strSql.Append("select * FROM tb_manager_log");
  52. if (strWhere.Trim() != "")
  53. {
  54. strSql.Append(" where " + strWhere);
  55. }
  56. recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
  57. return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
  58. }
  59. /// <summary>
  60. /// 删除一条数据
  61. /// </summary>
  62. public bool Delete(int id)
  63. {
  64. StringBuilder strSql = new StringBuilder();
  65. strSql.Append("delete from tb_manager_log ");
  66. strSql.Append(" where id=@id");
  67. SqlParameter[] parameters = {
  68. new SqlParameter("@id", SqlDbType.Int,4)};
  69. parameters[0].Value = id;
  70. int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
  71. if (rows > 0)
  72. {
  73. return true;
  74. }
  75. else
  76. {
  77. return false;
  78. }
  79. }
  80. /// <summary>
  81. /// 根据用户名返回上一次登录记录
  82. /// </summary>
  83. public Model.manager_log GetModel(string user_name, int top_num, string action_type)
  84. {
  85. int rows = GetCount("user_name='" + user_name + "'");
  86. if (top_num == 1)
  87. {
  88. rows = 2;
  89. }
  90. if (rows > 1)
  91. {
  92. StringBuilder strSql = new StringBuilder();
  93. 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 ");
  94. strSql.Append(" order by id asc");
  95. SqlParameter[] parameters = {
  96. new SqlParameter("@user_name", SqlDbType.NVarChar,100),
  97. new SqlParameter("@action_type", SqlDbType.NVarChar,100)};
  98. parameters[0].Value = user_name;
  99. parameters[1].Value = action_type;
  100. object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
  101. if (obj != null)
  102. {
  103. return GetModel(Convert.ToInt32(obj));
  104. }
  105. }
  106. return null;
  107. }
  108. /// <summary>
  109. /// 返回数据数
  110. /// </summary>
  111. public int GetCount(string strWhere)
  112. {
  113. StringBuilder strSql = new StringBuilder();
  114. strSql.Append("select count(*) as H ");
  115. strSql.Append(" from tb_manager_log ");
  116. if (strWhere.Trim() != "")
  117. {
  118. strSql.Append(" where " + strWhere);
  119. }
  120. return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString()));
  121. }
  122. /// <summary>
  123. /// 得到一个对象实体
  124. /// </summary>
  125. public Model.manager_log GetModel(int id)
  126. {
  127. StringBuilder strSql = new StringBuilder();
  128. strSql.Append("select top 1 id,user_id,user_name,action_type,note,login_ip,login_time from tb_manager_log ");
  129. strSql.Append(" where id=@id");
  130. SqlParameter[] parameters = {
  131. new SqlParameter("@id", SqlDbType.Int,4)};
  132. parameters[0].Value = id;
  133. Model.manager_log model = new Model.manager_log();
  134. DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
  135. if (ds.Tables[0].Rows.Count > 0)
  136. {
  137. if (ds.Tables[0].Rows[0]["id"].ToString() != "")
  138. {
  139. model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
  140. }
  141. if (ds.Tables[0].Rows[0]["user_id"].ToString() != "")
  142. {
  143. model.user_id = int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString());
  144. }
  145. model.user_name = ds.Tables[0].Rows[0]["user_name"].ToString();
  146. model.action_type = ds.Tables[0].Rows[0]["action_type"].ToString();
  147. model.note = ds.Tables[0].Rows[0]["note"].ToString();
  148. model.login_ip = ds.Tables[0].Rows[0]["login_ip"].ToString();
  149. if (ds.Tables[0].Rows[0]["login_time"].ToString() != "")
  150. {
  151. model.login_time = DateTime.Parse(ds.Tables[0].Rows[0]["login_time"].ToString());
  152. }
  153. return model;
  154. }
  155. else
  156. {
  157. return null;
  158. }
  159. }
  160. /// <summary>
  161. /// 获得查询分页数据
  162. /// </summary>
  163. public DataSet GetList(string _strWhere, string _orderby, int _pageSize, int _page, out int recordCount)
  164. {
  165. BasicPage bp = new BasicPage();
  166. StringBuilder str_Sql = new StringBuilder();
  167. str_Sql.Append("select top " + _pageSize + " id,user_name,note,login_ip,login_time from tb_manager_log");
  168. str_Sql.Append(" where ");
  169. str_Sql.Append(_strWhere);
  170. str_Sql.Append(" and id not in ");
  171. str_Sql.Append(" ( ");
  172. str_Sql.Append(" select top " + (_page - 1) * _pageSize + " id from tb_manager_log ");
  173. str_Sql.Append(" where ");
  174. str_Sql.Append(_strWhere);
  175. str_Sql.Append(" order by ");
  176. str_Sql.Append(_orderby);
  177. str_Sql.Append(" ) ");
  178. str_Sql.Append(" order by ");
  179. str_Sql.Append(_orderby);
  180. DataSet dst = bp.SelectDataBase("tb_manager_log", str_Sql.ToString());
  181. //
  182. recordCount = totlePage(showTotal(_strWhere), _pageSize);
  183. return dst;
  184. }
  185. //总页数
  186. public int totlePage(int Total, int PageSize)
  187. {
  188. if (Total % PageSize == 0)
  189. {
  190. return Total / PageSize;
  191. }
  192. else
  193. {
  194. return Total / PageSize + 1;
  195. }
  196. }
  197. //总条数
  198. protected int showTotal(string strSql)
  199. {
  200. BasicPage bp = new BasicPage();
  201. int intTotal = 0;
  202. SqlDataReader myread = bp.getRead("select count(id) as CountId from tb_manager_log where " + strSql);
  203. if (myread.Read())
  204. {
  205. intTotal = Convert.ToInt32(myread["CountId"].ToString());
  206. }
  207. myread.Close();
  208. return intTotal;
  209. }
  210. }
  211. }