SysAdminService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * 后台管理员
  3. */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Data;
  8. using MySystem.Models.Bs;
  9. using Library;
  10. using LitJson;
  11. namespace MySystem.Service.Bs
  12. {
  13. public class SysAdminService
  14. {
  15. string _conn = "";
  16. public SysAdminService()
  17. {
  18. _conn = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  19. }
  20. /// <summary>
  21. /// 查询列表
  22. /// </summary>
  23. /// <param name="relationData">关联表</param>
  24. /// <param name="condition">查询条件(sql语句)</param>
  25. /// <param name="count">总数(输出)</param>
  26. /// <param name="page">页码</param>
  27. /// <param name="limit">每页条数</param>
  28. /// <returns></returns>
  29. public List<Dictionary<string, object>> List(List<RelationData> relationData, string condition, out int count, int page = 1, int limit = 30, string orderBy = "Sort desc,Id desc")
  30. {
  31. List<string> fields = new List<string>(); //要显示的列
  32. fields.Add("Id");
  33. fields.Add("CreateDate"); //添加时间
  34. fields.Add("Status"); //状态
  35. fields.Add("AdminName"); //用户名
  36. fields.Add("RealName"); //名称
  37. fields.Add("Role"); //角色
  38. fields.Add("LastLoginDate"); //最后登录时间
  39. Dictionary<string, object> obj = new DbService(AppConfig.Base.dbTables, _conn).IndexData("SysAdmin", relationData, orderBy, page, limit, condition, fields);
  40. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  41. count = int.Parse(obj["count"].ToString());
  42. return diclist;
  43. }
  44. public List<Dictionary<string, object>> List(List<RelationData> relationData, string condition, int page = 1, int limit = 30, string orderBy = "Sort desc,Id desc")
  45. {
  46. List<string> fields = new List<string>(); //要显示的列
  47. fields.Add("Id");
  48. fields.Add("CreateDate"); //添加时间
  49. fields.Add("Status"); //状态
  50. fields.Add("AdminName"); //用户名
  51. fields.Add("RealName"); //名称
  52. fields.Add("Role"); //角色
  53. fields.Add("LastLoginDate"); //最后登录时间
  54. Dictionary<string, object> obj = new DbService(AppConfig.Base.dbTables, _conn).IndexData("SysAdmin", relationData, orderBy, page, limit, condition, fields);
  55. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  56. return diclist;
  57. }
  58. /// <summary>
  59. /// 查询一条记录
  60. /// </summary>
  61. /// <param name="Id">主键Id</param>
  62. /// <returns></returns>
  63. public SysAdmin Query(int Id)
  64. {
  65. WebCMSEntities db = new WebCMSEntities();
  66. SysAdmin editData = db.SysAdmin.FirstOrDefault(m => m.Id == Id) ?? new SysAdmin();
  67. db.Dispose();
  68. return editData;
  69. }
  70. public SysAdmin Query(string adminName, string pwd)
  71. {
  72. WebCMSEntities db = new WebCMSEntities();
  73. SysAdmin editData = db.SysAdmin.FirstOrDefault(m => m.AdminName == adminName && m.Password == pwd) ?? new SysAdmin();
  74. db.Dispose();
  75. return editData;
  76. }
  77. /// <summary>
  78. /// 查询记录数
  79. /// </summary>
  80. /// <param name="Id">主键Id</param>
  81. /// <returns></returns>
  82. public int Count(string condition = "")
  83. {
  84. int result = 0;
  85. DataTable dt = CustomerSqlConn.dtable("select count(Id) from SysAdmin where 1=1" + condition, _conn);
  86. if(dt.Rows.Count > 0)
  87. {
  88. result = int.Parse(function.CheckInt(dt.Rows[0][0].ToString()));
  89. }
  90. return result;
  91. }
  92. /// <summary>
  93. /// 查询是否存在
  94. /// </summary>
  95. /// <param name="Id">主键Id</param>
  96. /// <returns></returns>
  97. public bool Exist(int Id)
  98. {
  99. WebCMSEntities db = new WebCMSEntities();
  100. bool check = db.SysAdmin.Any(m => m.Id == Id);
  101. db.Dispose();
  102. return check;
  103. }
  104. /// <summary>
  105. /// 添加数据
  106. /// </summary>
  107. /// <param name="Fields">要设置的字段</param>
  108. /// <returns></returns>
  109. public AppResultJson Add(Dictionary<string, object> fields, bool check = true)
  110. {
  111. if(check)
  112. {
  113. }
  114. int Id = new DbService(AppConfig.Base.dbTables, _conn).Add("SysAdmin", fields, 0);
  115. return new AppResultJson(){ Status = "1", Data = Id };
  116. }
  117. /// <summary>
  118. /// 修改数据
  119. /// </summary>
  120. /// <param name="Fields">要设置的字段</param>
  121. /// <param name="Id">主键Id</param>
  122. public AppResultJson Edit(Dictionary<string, object> fields, int Id, bool check = true)
  123. {
  124. if(check)
  125. {
  126. }
  127. new DbService(AppConfig.Base.dbTables, _conn).Edit("SysAdmin", fields, Id);
  128. return new AppResultJson(){ Status = "1", Data = Id };
  129. }
  130. /// <summary>
  131. /// 逻辑删除
  132. /// </summary>
  133. /// <param name="Id">主键Id</param>
  134. public void Remove(int Id)
  135. {
  136. Dictionary<string, object> fields = new Dictionary<string, object>();
  137. fields.Add("Status", -1);
  138. new DbService(AppConfig.Base.dbTables, _conn).Edit("SysAdmin", fields, Id);
  139. }
  140. /// <summary>
  141. /// 删除数据
  142. /// </summary>
  143. /// <param name="Id">主键Id</param>
  144. public void Delete(int Id)
  145. {
  146. new DbService(AppConfig.Base.dbTables, _conn).Delete("SysAdmin", Id);
  147. }
  148. /// <summary>
  149. /// 排序
  150. /// </summary>
  151. /// <param name="Id">主键Id</param>
  152. /// <param name="Sort">排序序号</param>
  153. public void Sort(int Id, int Sort)
  154. {
  155. new DbService(AppConfig.Base.dbTables, _conn).Sort("SysAdmin", Sort, Id);
  156. }
  157. /// <summary>
  158. /// 导入数据
  159. /// </summary>
  160. /// <param name="ExcelData">json数据</param>
  161. public void Import(string ExcelData)
  162. {
  163. WebCMSEntities db = new WebCMSEntities();
  164. JsonData list = JsonMapper.ToObject(ExcelData);
  165. for (int i = 1; i < list.Count;i++ )
  166. {
  167. JsonData dr = list[i];
  168. db.SysAdmin.Add(new SysAdmin()
  169. {
  170. CreateDate = DateTime.Now,
  171. UpdateDate = DateTime.Now,
  172. });
  173. db.SaveChanges();
  174. }
  175. db.Dispose();
  176. }
  177. /// <summary>
  178. /// 导出excel表格
  179. /// </summary>
  180. /// <param name="fields">查询条件(单个字段)</param>
  181. /// <param name="condition">查询条件(sql语句)</param>
  182. /// <returns></returns>
  183. // public void ExportExcel(List<RelationData> relationData, string condition)
  184. // {
  185. // }
  186. }
  187. }