ConsumerOpenIdsService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * 消费者
  3. */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Data;
  8. using MySystem.Models.Main;
  9. using Library;
  10. using LitJson;
  11. namespace MySystem.Service.Main
  12. {
  13. public class ConsumerOpenIdsService
  14. {
  15. static string _conn = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  16. /// <summary>
  17. /// 查询列表
  18. /// </summary>
  19. /// <param name="relationData">关联表</param>
  20. /// <param name="condition">查询条件(sql语句)</param>
  21. /// <param name="count">总数(输出)</param>
  22. /// <param name="page">页码</param>
  23. /// <param name="limit">每页条数</param>
  24. /// <returns></returns>
  25. 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")
  26. {
  27. List<string> fields = new List<string>(); //要显示的列
  28. fields.Add("Id");
  29. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("ConsumerOpenIds", relationData, orderBy, page, limit, condition, fields);
  30. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  31. count = int.Parse(obj["count"].ToString());
  32. return diclist;
  33. }
  34. public static List<Dictionary<string, object>> List(List<RelationData> relationData, string condition, int page = 1, int limit = 30, string orderBy = "Sort desc,Id desc")
  35. {
  36. List<string> fields = new List<string>(); //要显示的列
  37. fields.Add("Id");
  38. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("ConsumerOpenIds", relationData, orderBy, page, limit, condition, fields);
  39. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  40. return diclist;
  41. }
  42. /// <summary>
  43. /// 查询一条记录
  44. /// </summary>
  45. /// <param name="OpenId">OpenId</param>
  46. /// <returns></returns>
  47. public static ConsumerOpenIds Query(string OpenId)
  48. {
  49. WebCMSEntities db = new WebCMSEntities();
  50. ConsumerOpenIds editData = db.ConsumerOpenIds.FirstOrDefault(m => m.OpenId == OpenId) ?? new ConsumerOpenIds();
  51. db.Dispose();
  52. return editData;
  53. }
  54. public static ConsumerOpenIds Query(string condition, string fields = "*")
  55. {
  56. var consumerOpenIds = new DbService(AppConfig.Base.mainTables, _conn).Query(fields, "ConsumerOpenIds", condition);
  57. if (consumerOpenIds.Count > 0)
  58. {
  59. return Newtonsoft.Json.JsonConvert.DeserializeObject<ConsumerOpenIds>(Newtonsoft.Json.JsonConvert.SerializeObject(consumerOpenIds));
  60. }
  61. return new ConsumerOpenIds();
  62. }
  63. public static decimal Sum(string condition, string field)
  64. {
  65. var dt = new DbService(AppConfig.Base.mainTables, _conn).Query("Sum(" + field + ") " + field, "ConsumerOpenIds", condition);
  66. decimal amount = 0;
  67. if (dt.Count > 0)
  68. {
  69. amount = decimal.Parse(dt[field].ToString());
  70. }
  71. return amount;
  72. }
  73. /// <summary>
  74. /// 查询记录数
  75. /// </summary>
  76. /// <param name="Id">主键Id</param>
  77. /// <returns></returns>
  78. public static int Count(string condition = "", string field = "Id")
  79. {
  80. var dt = new DbService(AppConfig.Base.mainTables, _conn).Query("Count(" + field + ") " + field, "ConsumerOpenIds", condition);
  81. int result = 0;
  82. if (dt.Count > 0)
  83. {
  84. result = int.Parse(dt[field].ToString());
  85. }
  86. return result;
  87. }
  88. /// <summary>
  89. /// 查询是否存在
  90. /// </summary>
  91. /// <param name="OpenId">OpenId</param>
  92. /// <returns></returns>
  93. public static bool Exist(string OpenId)
  94. {
  95. WebCMSEntities db = new WebCMSEntities();
  96. bool check = db.ConsumerOpenIds.Any(m => m.OpenId == OpenId);
  97. db.Dispose();
  98. return check;
  99. }
  100. /// <summary>
  101. /// 添加数据
  102. /// </summary>
  103. /// <param name="Fields">要设置的字段</param>
  104. /// <returns></returns>
  105. public static AppResultJson Add(Dictionary<string, object> fields, bool check = true)
  106. {
  107. if (check)
  108. {
  109. if (string.IsNullOrEmpty(fields["NickName"].ToString()))
  110. {
  111. return new AppResultJson() { Status = "-1", Info = "请填写昵称" };
  112. }
  113. }
  114. int Id = new DbService(AppConfig.Base.mainTables, _conn).Add("ConsumerOpenIds", 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 static AppResultJson Edit(Dictionary<string, object> fields, int Id, bool check = true)
  123. {
  124. if (check)
  125. {
  126. if (string.IsNullOrEmpty(fields["NickName"].ToString()))
  127. {
  128. return new AppResultJson() { Status = "-1", Info = "请填写昵称" };
  129. }
  130. }
  131. new DbService(AppConfig.Base.mainTables, _conn).Edit("ConsumerOpenIds", fields, Id);
  132. return new AppResultJson() { Status = "1", Data = Id };
  133. }
  134. /// <summary>
  135. /// 逻辑删除
  136. /// </summary>
  137. /// <param name="Id">主键Id</param>
  138. public static void Remove(int Id)
  139. {
  140. Dictionary<string, object> fields = new Dictionary<string, object>();
  141. fields.Add("Status", -1);
  142. new DbService(AppConfig.Base.mainTables, _conn).Edit("ConsumerOpenIds", fields, Id);
  143. }
  144. /// <summary>
  145. /// 删除数据
  146. /// </summary>
  147. /// <param name="Id">主键Id</param>
  148. public static void Delete(int Id)
  149. {
  150. new DbService(AppConfig.Base.mainTables, _conn).Delete("ConsumerOpenIds", Id);
  151. }
  152. /// <summary>
  153. /// 排序
  154. /// </summary>
  155. /// <param name="Id">主键Id</param>
  156. /// <param name="Sort">排序序号</param>
  157. public static void Sort(int Id, int Sort)
  158. {
  159. new DbService(AppConfig.Base.mainTables, _conn).Sort("ConsumerOpenIds", Sort, Id);
  160. }
  161. /// <summary>
  162. /// 导入数据
  163. /// </summary>
  164. /// <param name="ExcelData">json数据</param>
  165. public static void Import(string ExcelData)
  166. {
  167. WebCMSEntities db = new WebCMSEntities();
  168. JsonData list = JsonMapper.ToObject(ExcelData);
  169. for (int i = 1; i < list.Count; i++)
  170. {
  171. JsonData dr = list[i];
  172. db.ConsumerOpenIds.Add(new ConsumerOpenIds()
  173. {
  174. });
  175. db.SaveChanges();
  176. }
  177. db.Dispose();
  178. }
  179. /// <summary>
  180. /// 导出excel表格
  181. /// </summary>
  182. /// <param name="fields">查询条件(单个字段)</param>
  183. /// <param name="condition">查询条件(sql语句)</param>
  184. /// <returns></returns>
  185. // public static void ExportExcel(List<RelationData> relationData, string condition)
  186. // {
  187. // }
  188. }
  189. }