SysAdminService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * 运营中心管理
  3. */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Data;
  8. using MySystem.Models.Operate;
  9. using Library;
  10. using LitJson;
  11. namespace MySystem.Service.Operate
  12. {
  13. public class SysAdminService
  14. {
  15. static string _conn = ConfigurationManager.AppSettings["OpSqlConnStr"].ToString();
  16. // public SysAdminService()
  17. // {
  18. // _conn = ConfigurationManager.AppSettings["OpSqlConnStr"].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 static 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. fields.Add("MakerCode"); //所属人编号
  40. fields.Add("MakerName"); //所属人姓名
  41. fields.Add("MakerMobile"); //所属人手机号
  42. fields.Add("StoreCount"); //分仓数量
  43. fields.Add("OpCode"); //运营中心编号
  44. fields.Add("TotalMachineCount"); //机具总数
  45. Dictionary<string, object> obj = new DbService(AppConfig.Base.opTables, _conn).IndexData("SysAdmin", relationData, orderBy, page, limit, condition, fields);
  46. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  47. count = int.Parse(obj["count"].ToString());
  48. return diclist;
  49. }
  50. public static List<Dictionary<string, object>> List(List<RelationData> relationData, string condition, int page = 1, int limit = 30, string orderBy = "Sort desc,Id desc")
  51. {
  52. List<string> fields = new List<string>(); //要显示的列
  53. fields.Add("Id");
  54. fields.Add("CreateDate"); //添加时间
  55. fields.Add("Status"); //状态
  56. fields.Add("AdminName"); //用户名
  57. fields.Add("RealName"); //名称
  58. fields.Add("Role"); //角色
  59. fields.Add("LastLoginDate"); //最后登录时间
  60. fields.Add("MakerCode"); //所属人编号
  61. fields.Add("MakerName"); //所属人姓名
  62. fields.Add("MakerMobile"); //所属人手机号
  63. fields.Add("StoreCount"); //分仓数量
  64. fields.Add("OpCode"); //运营中心编号
  65. fields.Add("TotalMachineCount"); //机具总数
  66. Dictionary<string, object> obj = new DbService(AppConfig.Base.opTables, _conn).IndexData("SysAdmin", relationData, orderBy, page, limit, condition, fields);
  67. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  68. return diclist;
  69. }
  70. /// <summary>
  71. /// 查询一条记录
  72. /// </summary>
  73. /// <param name="Id">主键Id</param>
  74. /// <returns></returns>
  75. public static SysAdmin Query(int Id)
  76. {
  77. WebCMSEntities db = new WebCMSEntities();
  78. SysAdmin editData = db.SysAdmin.FirstOrDefault(m => m.Id == Id) ?? new SysAdmin();
  79. db.Dispose();
  80. return editData;
  81. }
  82. /// <summary>
  83. /// 根据指定条件查询数据
  84. /// </summary>
  85. /// <param name="condition"></param>
  86. /// <returns></returns>
  87. // public static SysAdmin Query(string condition)
  88. // {
  89. // DataTable dt = new DbService(AppConfig.Base.opTables, _conn).QueryDetail("*", "SysAdmin", condition);
  90. // if (dt.Rows.Count > 0)
  91. // {
  92. // Dictionary<string, object> row = new Dictionary<string, object>();
  93. // foreach (DataColumn dc in dt.Columns)
  94. // {
  95. // row.Add(dc.ColumnName, dt.Rows[0][dc.ColumnName].ToString());
  96. // }
  97. // return Newtonsoft.Json.JsonConvert.DeserializeObject<SysAdmin>(Newtonsoft.Json.JsonConvert.SerializeObject(row));
  98. // }
  99. // return new SysAdmin();
  100. // }
  101. public static SysAdmin Query(string condition, string fields = "*")
  102. {
  103. var sysAdmin = new DbService(AppConfig.Base.opTables, _conn).Query(fields, "SysAdmin", condition);
  104. if (sysAdmin.Count > 0)
  105. {
  106. return Newtonsoft.Json.JsonConvert.DeserializeObject<SysAdmin>(Newtonsoft.Json.JsonConvert.SerializeObject(sysAdmin));
  107. }
  108. return new SysAdmin();
  109. }
  110. /// <summary>
  111. /// 通过创客Id查询一条记录
  112. /// </summary>
  113. /// <param name="UserId">创客Id</param>
  114. /// <returns></returns>
  115. public static SysAdmin QueryByUserId(int UserId)
  116. {
  117. WebCMSEntities db = new WebCMSEntities();
  118. SysAdmin editData = db.SysAdmin.FirstOrDefault(m => m.Sort == m.Id && m.UserId == UserId) ?? new SysAdmin();
  119. db.Dispose();
  120. return editData;
  121. }
  122. /// <summary>
  123. /// 查询记录数
  124. /// </summary>
  125. /// <param name="Id">主键Id</param>
  126. /// <returns></returns>
  127. public static int Count(string condition = "")
  128. {
  129. int result = 0;
  130. DataTable dt = CustomerSqlConn.dtable("select count(Id) from SysAdmin where 1=1" + condition, _conn);
  131. if (dt.Rows.Count > 0)
  132. {
  133. result = int.Parse(function.CheckInt(dt.Rows[0][0].ToString()));
  134. }
  135. return result;
  136. }
  137. /// <summary>
  138. /// 查询是否存在
  139. /// </summary>
  140. /// <param name="Id">主键Id</param>
  141. /// <returns></returns>
  142. public static bool Exist(int Id)
  143. {
  144. WebCMSEntities db = new WebCMSEntities();
  145. bool check = db.SysAdmin.Any(m => m.Id == Id);
  146. db.Dispose();
  147. return check;
  148. }
  149. /// <summary>
  150. /// 添加数据
  151. /// </summary>
  152. /// <param name="Fields">要设置的字段</param>
  153. /// <returns></returns>
  154. public static AppResultJson Add(Dictionary<string, object> fields, bool check = true)
  155. {
  156. if (check)
  157. {
  158. if (string.IsNullOrEmpty(fields["MakerCode"].ToString()))
  159. {
  160. return new AppResultJson() { Status = "-1", Info = "请填写所属人编号" };
  161. }
  162. if (string.IsNullOrEmpty(fields["MakerMobile"].ToString()))
  163. {
  164. return new AppResultJson() { Status = "-1", Info = "请填写所属人手机号" };
  165. }
  166. if (function.CheckMobile(fields["MakerMobile"].ToString()) == "")
  167. {
  168. return new AppResultJson() { Status = "-1", Info = "请填写正确的所属人手机号" };
  169. }
  170. }
  171. int Id = new DbService(AppConfig.Base.opTables, _conn).Add("SysAdmin", fields, 0);
  172. return new AppResultJson() { Status = "1", Data = Id };
  173. }
  174. /// <summary>
  175. /// 修改数据
  176. /// </summary>
  177. /// <param name="Fields">要设置的字段</param>
  178. /// <param name="Id">主键Id</param>
  179. public static AppResultJson Edit(Dictionary<string, object> fields, int Id, bool check = true)
  180. {
  181. new DbService(AppConfig.Base.opTables, _conn).Edit("SysAdmin", fields, Id);
  182. return new AppResultJson() { Status = "1", Data = Id };
  183. }
  184. /// <summary>
  185. /// 逻辑删除
  186. /// </summary>
  187. /// <param name="Id">主键Id</param>
  188. public static void Remove(int Id)
  189. {
  190. Dictionary<string, object> fields = new Dictionary<string, object>();
  191. fields.Add("Status", -1);
  192. new DbService(AppConfig.Base.opTables, _conn).Edit("SysAdmin", fields, Id);
  193. }
  194. /// <summary>
  195. /// 删除数据
  196. /// </summary>
  197. /// <param name="Id">主键Id</param>
  198. public static void Delete(int Id)
  199. {
  200. new DbService(AppConfig.Base.opTables, _conn).Delete("SysAdmin", Id);
  201. }
  202. /// <summary>
  203. /// 排序
  204. /// </summary>
  205. /// <param name="Id">主键Id</param>
  206. /// <param name="Sort">排序序号</param>
  207. public static void Sort(int Id, int Sort)
  208. {
  209. new DbService(AppConfig.Base.opTables, _conn).Sort("SysAdmin", Sort, Id);
  210. }
  211. /// <summary>
  212. /// 导入数据
  213. /// </summary>
  214. /// <param name="ExcelData">json数据</param>
  215. public static void Import(string ExcelData)
  216. {
  217. WebCMSEntities db = new WebCMSEntities();
  218. JsonData list = JsonMapper.ToObject(ExcelData);
  219. for (int i = 1; i < list.Count; i++)
  220. {
  221. JsonData dr = list[i];
  222. db.SysAdmin.Add(new SysAdmin()
  223. {
  224. CreateDate = DateTime.Now,
  225. UpdateDate = DateTime.Now,
  226. });
  227. db.SaveChanges();
  228. }
  229. db.Dispose();
  230. }
  231. /// <summary>
  232. /// 导出excel表格
  233. /// </summary>
  234. /// <param name="fields">查询条件(单个字段)</param>
  235. /// <param name="condition">查询条件(sql语句)</param>
  236. /// <returns></returns>
  237. // public void ExportExcel(List<RelationData> relationData, string condition)
  238. // {
  239. // }
  240. }
  241. }