ConsumersService.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 ConsumersService
  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. fields.Add("CreateDate"); //添加时间
  30. fields.Add("Status"); //状态
  31. fields.Add("NickName"); //昵称
  32. fields.Add("Mobile"); //手机号
  33. fields.Add("TotalDividend"); //累计分红
  34. fields.Add("TotalIntegral"); //累计积分
  35. fields.Add("CurIntgegral"); //当前积分
  36. fields.Add("WechatUnionid"); //微信Unionid
  37. fields.Add("TotalAmount"); //累计消费金额
  38. fields.Add("TotalConsumCount"); //累计消费次数
  39. fields.Add("LastConsumDate"); //最近消费时间
  40. fields.Add("WeChatPayFlag"); //微信付款标记
  41. fields.Add("AlipayFlag"); //支付宝付款标记
  42. fields.Add("CardFlag"); //是否领取会员卡
  43. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("Consumers", relationData, orderBy, page, limit, condition, fields);
  44. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  45. count = int.Parse(obj["count"].ToString());
  46. return diclist;
  47. }
  48. public static List<Dictionary<string, object>> List(List<RelationData> relationData, string condition, int page = 1, int limit = 30, string orderBy = "Sort desc,Id desc")
  49. {
  50. List<string> fields = new List<string>(); //要显示的列
  51. fields.Add("Id");
  52. fields.Add("CreateDate"); //添加时间
  53. fields.Add("Status"); //状态
  54. fields.Add("NickName"); //昵称
  55. fields.Add("Mobile"); //手机号
  56. fields.Add("TotalDividend"); //累计分红
  57. fields.Add("TotalIntegral"); //累计积分
  58. fields.Add("CurIntgegral"); //当前积分
  59. fields.Add("WechatUnionid"); //微信Unionid
  60. fields.Add("TotalAmount"); //累计消费金额
  61. fields.Add("TotalConsumCount"); //累计消费次数
  62. fields.Add("LastConsumDate"); //最近消费时间
  63. fields.Add("WeChatPayFlag"); //微信付款标记
  64. fields.Add("AlipayFlag"); //支付宝付款标记
  65. fields.Add("CardFlag"); //是否领取会员卡
  66. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("Consumers", 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 Consumers Query(int Id)
  76. {
  77. WebCMSEntities db = new WebCMSEntities();
  78. Consumers editData = db.Consumers.FirstOrDefault(m => m.Id == Id) ?? new Consumers();
  79. db.Dispose();
  80. return editData;
  81. }
  82. public static Consumers Query(string condition, string fields = "*")
  83. {
  84. var consumers = new DbService(AppConfig.Base.mainTables, _conn).Query(fields, "Consumers", condition);
  85. if (consumers.Count > 0)
  86. {
  87. return Newtonsoft.Json.JsonConvert.DeserializeObject<Consumers>(Newtonsoft.Json.JsonConvert.SerializeObject(consumers));
  88. }
  89. return new Consumers();
  90. }
  91. public static decimal Sum(string condition, string field)
  92. {
  93. var dt = new DbService(AppConfig.Base.mainTables, _conn).Query("Sum(" + field + ") " + field, "Consumers", condition);
  94. decimal amount = 0;
  95. if (dt.Count > 0)
  96. {
  97. amount = decimal.Parse(dt[field].ToString());
  98. }
  99. return amount;
  100. }
  101. /// <summary>
  102. /// 查询记录数
  103. /// </summary>
  104. /// <param name="Id">主键Id</param>
  105. /// <returns></returns>
  106. public static int Count(string condition = "", string field = "Id")
  107. {
  108. var dt = new DbService(AppConfig.Base.mainTables, _conn).Query("Count(" + field + ") " + field, "Consumers", condition);
  109. int result = 0;
  110. if (dt.Count > 0)
  111. {
  112. result = int.Parse(dt[field].ToString());
  113. }
  114. return result;
  115. }
  116. /// <summary>
  117. /// 查询是否存在
  118. /// </summary>
  119. /// <param name="WechatOpenId">WechatOpenId</param>
  120. /// <returns></returns>
  121. public static bool Exist(string WechatOpenId)
  122. {
  123. WebCMSEntities db = new WebCMSEntities();
  124. bool check = db.Consumers.Any(m => m.WechatOpenId == WechatOpenId);
  125. db.Dispose();
  126. return check;
  127. }
  128. /// <summary>
  129. /// 添加数据
  130. /// </summary>
  131. /// <param name="Fields">要设置的字段</param>
  132. /// <returns></returns>
  133. public static AppResultJson Add(Dictionary<string, object> fields, bool check = true)
  134. {
  135. if (check)
  136. {
  137. }
  138. int Id = new DbService(AppConfig.Base.mainTables, _conn).Add("Consumers", fields, 0);
  139. return new AppResultJson() { Status = "1", Data = Id };
  140. }
  141. /// <summary>
  142. /// 修改数据
  143. /// </summary>
  144. /// <param name="Fields">要设置的字段</param>
  145. /// <param name="Id">主键Id</param>
  146. public static AppResultJson Edit(Dictionary<string, object> fields, int Id, bool check = true)
  147. {
  148. if (check)
  149. {
  150. if (string.IsNullOrEmpty(fields["NickName"].ToString()))
  151. {
  152. return new AppResultJson() { Status = "-1", Info = "请填写昵称" };
  153. }
  154. }
  155. new DbService(AppConfig.Base.mainTables, _conn).Edit("Consumers", fields, Id);
  156. return new AppResultJson() { Status = "1", Data = Id };
  157. }
  158. /// <summary>
  159. /// 逻辑删除
  160. /// </summary>
  161. /// <param name="Id">主键Id</param>
  162. public static void Remove(int Id)
  163. {
  164. Dictionary<string, object> fields = new Dictionary<string, object>();
  165. fields.Add("Status", -1);
  166. new DbService(AppConfig.Base.mainTables, _conn).Edit("Consumers", fields, Id);
  167. }
  168. /// <summary>
  169. /// 删除数据
  170. /// </summary>
  171. /// <param name="Id">主键Id</param>
  172. public static void Delete(int Id)
  173. {
  174. new DbService(AppConfig.Base.mainTables, _conn).Delete("Consumers", Id);
  175. }
  176. /// <summary>
  177. /// 排序
  178. /// </summary>
  179. /// <param name="Id">主键Id</param>
  180. /// <param name="Sort">排序序号</param>
  181. public static void Sort(int Id, int Sort)
  182. {
  183. new DbService(AppConfig.Base.mainTables, _conn).Sort("Consumers", Sort, Id);
  184. }
  185. /// <summary>
  186. /// 导入数据
  187. /// </summary>
  188. /// <param name="ExcelData">json数据</param>
  189. public static void Import(string ExcelData)
  190. {
  191. WebCMSEntities db = new WebCMSEntities();
  192. JsonData list = JsonMapper.ToObject(ExcelData);
  193. for (int i = 1; i < list.Count; i++)
  194. {
  195. JsonData dr = list[i];
  196. db.Consumers.Add(new Consumers()
  197. {
  198. CreateDate = DateTime.Now,
  199. UpdateDate = DateTime.Now,
  200. });
  201. db.SaveChanges();
  202. }
  203. db.Dispose();
  204. }
  205. /// <summary>
  206. /// 导出excel表格
  207. /// </summary>
  208. /// <param name="fields">查询条件(单个字段)</param>
  209. /// <param name="condition">查询条件(sql语句)</param>
  210. /// <returns></returns>
  211. // public static void ExportExcel(List<RelationData> relationData, string condition)
  212. // {
  213. // }
  214. }
  215. }