ConsumerProfitService.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.MainModels;
  5. using Library;
  6. using LitJson;
  7. using System.Data;
  8. namespace MySystem
  9. {
  10. public class ConsumerProfitService
  11. {
  12. string _conn = "";
  13. public ConsumerProfitService()
  14. {
  15. _conn = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  16. }
  17. /// <summary>
  18. /// 查询列表
  19. /// </summary>
  20. /// <param name="fields">查询条件(单个字段)</param>
  21. /// <param name="condition">查询条件(sql语句)</param>
  22. /// <param name="page">页码</param>
  23. /// <param name="limit">每页条数</param>
  24. /// <returns></returns>
  25. public List<Dictionary<string, object>> List(List<FieldItem> fields, string condition, int page = 1, int limit = 30, string orderby = "Id desc")
  26. {
  27. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("ConsumerProfit", fields, orderby, "0", page, limit, condition);
  28. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  29. return diclist;
  30. }
  31. /// <summary>
  32. /// 查询一条记录
  33. /// </summary>
  34. /// <param name="Id">主键Id</param>
  35. /// <returns></returns>
  36. public ConsumerProfit Query(int Id)
  37. {
  38. WebCMSEntities db = new WebCMSEntities();
  39. ConsumerProfit editData = db.ConsumerProfit.FirstOrDefault(m => m.Id == Id) ?? new ConsumerProfit();
  40. db.Dispose();
  41. return editData;
  42. }
  43. public Dictionary<string, string> Sum(string field, string condition)
  44. {
  45. Dictionary<string, string> result = new Dictionary<string, string>();
  46. string sumString = "";
  47. string[] fieldlist = field.Split(',');
  48. foreach(string f in fieldlist)
  49. {
  50. sumString += "sum(" + f + ") " + f + ",";
  51. }
  52. sumString = sumString.TrimEnd(',');
  53. DataTable dt = CustomerSqlConn.dtable("select " + sumString + " from ConsumerProfit where 1=1" + condition, _conn);
  54. if(dt.Rows.Count > 0)
  55. {
  56. foreach(string f in fieldlist)
  57. {
  58. result.Add(f, function.CheckNum(dt.Rows[0][f].ToString()));
  59. }
  60. }
  61. return result;
  62. }
  63. /// <summary>
  64. /// 添加数据
  65. /// </summary>
  66. /// <param name="Fields">要设置的字段</param>
  67. /// <returns></returns>
  68. public int Add(Dictionary<string, object> Fields)
  69. {
  70. return new DbService(AppConfig.Base.mainTables, _conn).Add("ConsumerProfit", Fields, 0);
  71. }
  72. /// <summary>
  73. /// 修改数据
  74. /// </summary>
  75. /// <param name="Fields">要设置的字段</param>
  76. /// <param name="Id">主键Id</param>
  77. public void Edit(Dictionary<string, object> Fields, int Id)
  78. {
  79. new DbService(AppConfig.Base.mainTables, _conn).Edit("ConsumerProfit", Fields, Id);
  80. }
  81. /// <summary>
  82. /// 删除数据
  83. /// </summary>
  84. /// <param name="Id">主键Id</param>
  85. public void Delete(int Id)
  86. {
  87. new DbService(AppConfig.Base.mainTables, _conn).Delete("ConsumerProfit", Id);
  88. }
  89. /// <summary>
  90. /// 排序
  91. /// </summary>
  92. /// <param name="Id">主键Id</param>
  93. /// <param name="Sort">排序序号</param>
  94. public void Sort(int Id, int Sort)
  95. {
  96. new DbService(AppConfig.Base.mainTables, _conn).Sort("ConsumerProfit", Sort, Id);
  97. }
  98. /// <summary>
  99. /// 导入数据
  100. /// </summary>
  101. /// <param name="ExcelData">json数据</param>
  102. public void Import(string ExcelData)
  103. {
  104. WebCMSEntities db = new WebCMSEntities();
  105. JsonData list = JsonMapper.ToObject(ExcelData);
  106. for (int i = 1; i < list.Count;i++ )
  107. {
  108. JsonData dr = list[i];
  109. db.ConsumerProfit.Add(new ConsumerProfit()
  110. {
  111. CreateDate = DateTime.Now,
  112. UpdateDate = DateTime.Now,
  113. });
  114. db.SaveChanges();
  115. }
  116. db.Dispose();
  117. }
  118. /// <summary>
  119. /// 导出excel表格
  120. /// </summary>
  121. /// <param name="fields">查询条件(单个字段)</param>
  122. /// <param name="condition">查询条件(sql语句)</param>
  123. /// <returns></returns>
  124. public Dictionary<string, object> ExportExcel(List<FieldItem> fields, string condition, string orderby = "Id desc")
  125. {
  126. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("ConsumerProfit", fields, orderby, "0", 1, 20000, condition, "Id", false);
  127. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  128. foreach (Dictionary<string, object> dic in diclist)
  129. {
  130. }
  131. Dictionary<string, object> result = new Dictionary<string, object>();
  132. result.Add("Status", "1");
  133. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  134. result.Add("Obj", diclist);
  135. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  136. result.Add("Fields", ReturnFields);
  137. return result;
  138. }
  139. }
  140. }