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.

387 lines
16 KiB

преди 3 години
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. /// <summary>
  7. /// 管理员
  8. /// </summary>
  9. namespace Basic.DAL
  10. {
  11. public class manager
  12. {
  13. /// <summary>
  14. /// 获得查询分页数据
  15. /// </summary>
  16. public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
  17. {
  18. StringBuilder strSql = new StringBuilder();
  19. strSql.Append("select * from tb_manager");
  20. if (strWhere.Trim() != "")
  21. {
  22. strSql.Append(" where " + strWhere);
  23. }
  24. recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
  25. return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
  26. }
  27. /// <summary>
  28. /// 增加一条数据
  29. /// </summary>
  30. public int Add(Model.manager model)
  31. {
  32. StringBuilder strSql = new StringBuilder();
  33. strSql.Append("insert into tb_manager(");
  34. strSql.Append("role_id,role_type,shop_id,user_name,user_pwd,real_name,telephone,email,is_lock,add_time)");
  35. strSql.Append(" values (");
  36. strSql.Append("@role_id,@role_type,@shop_id,@user_name,@user_pwd,@real_name,@telephone,@email,@is_lock,@add_time)");
  37. strSql.Append(";select @@IDENTITY");
  38. SqlParameter[] parameters = {
  39. new SqlParameter("@role_id", SqlDbType.Int,10),
  40. new SqlParameter("@role_type", SqlDbType.Int,10),
  41. new SqlParameter("@shop_id", SqlDbType.Int,10),
  42. new SqlParameter("@user_name", SqlDbType.NVarChar,100),
  43. new SqlParameter("@user_pwd", SqlDbType.NVarChar,100),
  44. new SqlParameter("@real_name", SqlDbType.NVarChar,50),
  45. new SqlParameter("@telephone", SqlDbType.NVarChar,30),
  46. new SqlParameter("@email", SqlDbType.NVarChar,30),
  47. new SqlParameter("@is_lock", SqlDbType.Int,10),
  48. new SqlParameter("@add_time", SqlDbType.DateTime,23)};
  49. parameters[0].Value = model.role_id;
  50. parameters[1].Value = model.role_type;
  51. parameters[2].Value = model.shop_id;
  52. parameters[3].Value = model.user_name;
  53. parameters[4].Value = model.user_pwd;
  54. parameters[5].Value = model.real_name;
  55. parameters[6].Value = model.telephone;
  56. parameters[7].Value = model.email;
  57. parameters[8].Value = model.is_lock;
  58. parameters[9].Value = model.add_time;
  59. object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
  60. if (obj == null)
  61. {
  62. return 0;
  63. }
  64. else
  65. {
  66. return Convert.ToInt32(obj);
  67. }
  68. }
  69. /// <summary>
  70. /// 得到一个对象实体
  71. /// </summary>
  72. public Model.manager GetModel(int id)
  73. {
  74. StringBuilder strSql = new StringBuilder();
  75. strSql.Append("select top 1 id,role_id,role_type,shop_id,user_name,user_pwd,real_name,telephone,email,is_lock,add_time from tb_manager");
  76. strSql.Append(" where id=@id");
  77. SqlParameter[] parameters = {
  78. new SqlParameter("@id", SqlDbType.Int,4)};
  79. parameters[0].Value = id;
  80. Model.manager model = new Model.manager();
  81. DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
  82. if (ds.Tables[0].Rows.Count > 0)
  83. {
  84. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["id"].ToString()))
  85. {
  86. model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
  87. }
  88. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["role_id"].ToString()))
  89. {
  90. model.role_id = int.Parse(ds.Tables[0].Rows[0]["role_id"].ToString());
  91. }
  92. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["role_type"].ToString()))
  93. {
  94. model.role_type = int.Parse(ds.Tables[0].Rows[0]["role_type"].ToString());
  95. }
  96. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["shop_id"].ToString()))
  97. {
  98. model.shop_id = int.Parse(ds.Tables[0].Rows[0]["shop_id"].ToString());
  99. }
  100. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_name"].ToString()))
  101. {
  102. model.user_name = ds.Tables[0].Rows[0]["user_name"].ToString();
  103. }
  104. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_pwd"].ToString()))
  105. {
  106. model.user_pwd = ds.Tables[0].Rows[0]["user_pwd"].ToString();
  107. }
  108. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["real_name"].ToString()))
  109. {
  110. model.real_name = ds.Tables[0].Rows[0]["real_name"].ToString();
  111. }
  112. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["telephone"].ToString()))
  113. {
  114. model.telephone = ds.Tables[0].Rows[0]["telephone"].ToString();
  115. }
  116. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["email"].ToString()))
  117. {
  118. model.email = ds.Tables[0].Rows[0]["email"].ToString();
  119. }
  120. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["is_lock"].ToString()))
  121. {
  122. model.is_lock = int.Parse(ds.Tables[0].Rows[0]["is_lock"].ToString());
  123. }
  124. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["add_time"].ToString()))
  125. {
  126. model.add_time = DateTime.Parse(ds.Tables[0].Rows[0]["add_time"].ToString());
  127. }
  128. return model;
  129. }
  130. else
  131. {
  132. return null;
  133. }
  134. }
  135. /// <summary>
  136. /// 是否存在该记录
  137. /// </summary>
  138. public bool Exists(int id)
  139. {
  140. StringBuilder strSql = new StringBuilder();
  141. strSql.Append("select count(1) from tb_manager");
  142. strSql.Append(" where id=@id ");
  143. SqlParameter[] parameters = {
  144. new SqlParameter("@id", SqlDbType.Int,4)};
  145. parameters[0].Value = id;
  146. return DbHelperSQL.Exists(strSql.ToString(), parameters);
  147. }
  148. /// <summary>
  149. /// 更新一条数据
  150. /// </summary>
  151. public bool Update(Model.manager model)
  152. {
  153. StringBuilder strSql = new StringBuilder();
  154. strSql.Append("update tb_manager set ");
  155. strSql.Append("role_id=@role_id,");
  156. strSql.Append("role_type=@role_type,");
  157. strSql.Append("shop_id=@shop_id,");
  158. strSql.Append("user_name=@user_name,");
  159. strSql.Append("user_pwd=@user_pwd,");
  160. strSql.Append("real_name=@real_name,");
  161. strSql.Append("telephone=@telephone,");
  162. strSql.Append("email=@email,");
  163. strSql.Append("is_lock=@is_lock,");
  164. strSql.Append("add_time=@add_time");
  165. strSql.Append(" where id=@id");
  166. SqlParameter[] parameters = {
  167. new SqlParameter("@role_id", SqlDbType.Int,10),
  168. new SqlParameter("@role_type", SqlDbType.Int,10),
  169. new SqlParameter("@shop_id", SqlDbType.Int,10),
  170. new SqlParameter("@user_name", SqlDbType.NVarChar,100),
  171. new SqlParameter("@user_pwd", SqlDbType.NVarChar,100),
  172. new SqlParameter("@real_name", SqlDbType.NVarChar,50),
  173. new SqlParameter("@telephone", SqlDbType.NVarChar,30),
  174. new SqlParameter("@email", SqlDbType.NVarChar,30),
  175. new SqlParameter("@is_lock", SqlDbType.Int,10),
  176. new SqlParameter("@add_time", SqlDbType.DateTime,23),
  177. new SqlParameter("@id", SqlDbType.Int,4)};
  178. parameters[0].Value = model.role_id;
  179. parameters[1].Value = model.role_type;
  180. parameters[2].Value = model.shop_id;
  181. parameters[3].Value = model.user_name;
  182. parameters[4].Value = model.user_pwd;
  183. parameters[5].Value = model.real_name;
  184. parameters[6].Value = model.telephone;
  185. parameters[7].Value = model.email;
  186. parameters[8].Value = model.is_lock;
  187. parameters[9].Value = model.add_time;
  188. parameters[10].Value = model.id;
  189. int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
  190. if (rows > 0)
  191. {
  192. return true;
  193. }
  194. else
  195. {
  196. return false;
  197. }
  198. }
  199. /// <summary>
  200. /// 删除一条数据
  201. /// </summary>
  202. public bool Delete(int id)
  203. {
  204. List<CommandInfo> sqllist = new List<CommandInfo>();
  205. StringBuilder strSql = new StringBuilder();
  206. strSql.Append("delete from tb_manager_log ");
  207. strSql.Append(" where user_id=@id");
  208. SqlParameter[] parameters = {
  209. new SqlParameter("@id", SqlDbType.Int,4)};
  210. parameters[0].Value = id;
  211. CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
  212. sqllist.Add(cmd);
  213. StringBuilder strSql1 = new StringBuilder();
  214. strSql1.Append("delete from tb_manager ");
  215. strSql1.Append(" where id=@id");
  216. SqlParameter[] parameters1 = {
  217. new SqlParameter("@id", SqlDbType.Int,4)};
  218. parameters1[0].Value = id;
  219. cmd = new CommandInfo(strSql1.ToString(), parameters1);
  220. sqllist.Add(cmd);
  221. int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
  222. if (rowsAffected > 0)
  223. {
  224. return true;
  225. }
  226. else
  227. {
  228. return false;
  229. }
  230. }
  231. /// <summary>
  232. /// 根据用户名密码返回一个实体
  233. /// </summary>
  234. public Model.manager GetModel(string user_name, string user_pwd)
  235. {
  236. StringBuilder strSql = new StringBuilder();
  237. strSql.Append("select id from tb_manager");
  238. strSql.Append(" where user_name=@user_name and user_pwd=@user_pwd and is_lock=0");
  239. SqlParameter[] parameters = {
  240. new SqlParameter("@user_name", SqlDbType.NVarChar,100),
  241. new SqlParameter("@user_pwd", SqlDbType.NVarChar,100)};
  242. parameters[0].Value = user_name;
  243. parameters[1].Value = user_pwd;
  244. object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
  245. if (obj != null)
  246. {
  247. return GetModel(Convert.ToInt32(obj));
  248. }
  249. return null;
  250. }
  251. /// <summary>
  252. /// 得到一个对象实体
  253. /// </summary>
  254. public Model.manager GetModel(string username)
  255. {
  256. StringBuilder strSql = new StringBuilder();
  257. strSql.Append("select top 1 id,role_id,role_type,shop_id,user_name,user_pwd,real_name,telephone,email,is_lock,add_time from tb_manager");
  258. strSql.Append(" where user_name=@user_name");
  259. SqlParameter[] parameters = {
  260. new SqlParameter("@user_name", SqlDbType.NVarChar,255)};
  261. parameters[0].Value = username;
  262. Model.manager model = new Model.manager();
  263. DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
  264. if (ds.Tables[0].Rows.Count > 0)
  265. {
  266. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["id"].ToString()))
  267. {
  268. model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
  269. }
  270. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["role_id"].ToString()))
  271. {
  272. model.role_id = int.Parse(ds.Tables[0].Rows[0]["role_id"].ToString());
  273. }
  274. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["role_type"].ToString()))
  275. {
  276. model.role_type = int.Parse(ds.Tables[0].Rows[0]["role_type"].ToString());
  277. }
  278. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["shop_id"].ToString()))
  279. {
  280. model.shop_id = int.Parse(ds.Tables[0].Rows[0]["shop_id"].ToString());
  281. }
  282. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_name"].ToString()))
  283. {
  284. model.user_name = ds.Tables[0].Rows[0]["user_name"].ToString();
  285. }
  286. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_pwd"].ToString()))
  287. {
  288. model.user_pwd = ds.Tables[0].Rows[0]["user_pwd"].ToString();
  289. }
  290. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["real_name"].ToString()))
  291. {
  292. model.real_name = ds.Tables[0].Rows[0]["real_name"].ToString();
  293. }
  294. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["telephone"].ToString()))
  295. {
  296. model.telephone = ds.Tables[0].Rows[0]["telephone"].ToString();
  297. }
  298. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["email"].ToString()))
  299. {
  300. model.email = ds.Tables[0].Rows[0]["email"].ToString();
  301. }
  302. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["is_lock"].ToString()))
  303. {
  304. model.is_lock = int.Parse(ds.Tables[0].Rows[0]["is_lock"].ToString());
  305. }
  306. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["add_time"].ToString()))
  307. {
  308. model.add_time = DateTime.Parse(ds.Tables[0].Rows[0]["add_time"].ToString());
  309. }
  310. return model;
  311. }
  312. else
  313. {
  314. return null;
  315. }
  316. }
  317. #region
  318. /// <summary>
  319. /// 获得查询分页数据
  320. /// </summary>
  321. public DataSet GetList(string _strWhere, string _orderby, int _pageSize, int _page, out int recordCount)
  322. {
  323. BasicPage bp = new BasicPage();
  324. StringBuilder str_Sql = new StringBuilder();
  325. str_Sql.Append("select top " + _pageSize + " id,user_name,role_id,real_name,shop_id,telephone,email,add_time,is_lock from tb_manager");
  326. str_Sql.Append(" where ");
  327. str_Sql.Append(_strWhere);
  328. str_Sql.Append(" and id not in ");
  329. str_Sql.Append(" ( ");
  330. str_Sql.Append(" select top " + (_page - 1) * _pageSize + " id from tb_manager ");
  331. str_Sql.Append(" where ");
  332. str_Sql.Append(_strWhere);
  333. str_Sql.Append(" order by ");
  334. str_Sql.Append(_orderby);
  335. str_Sql.Append(" ) ");
  336. str_Sql.Append(" order by ");
  337. str_Sql.Append(_orderby);
  338. DataSet dst = bp.SelectDataBase("tb_manager", str_Sql.ToString());
  339. //
  340. recordCount = totlePage(showTotal(_strWhere), _pageSize);
  341. return dst;
  342. }
  343. //总页数
  344. public int totlePage(int Total, int PageSize)
  345. {
  346. if (Total % PageSize == 0)
  347. {
  348. return Total / PageSize;
  349. }
  350. else
  351. {
  352. return Total / PageSize + 1;
  353. }
  354. }
  355. //总条数
  356. protected int showTotal(string strSql)
  357. {
  358. BasicPage bp = new BasicPage();
  359. int intTotal = 0;
  360. SqlDataReader myread = bp.getRead("select count(id) as CountId from tb_manager where " + strSql);
  361. if (myread.Read())
  362. {
  363. intTotal = Convert.ToInt32(myread["CountId"].ToString());
  364. }
  365. myread.Close();
  366. return intTotal;
  367. }
  368. #endregion
  369. }
  370. }