BusinessPartnerService.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 BusinessPartnerService
  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("Mobile"); //手机号
  32. fields.Add("Name"); //姓名
  33. fields.Add("ThisMonthTrade"); //本月交易额
  34. fields.Add("UserId"); //企业创客
  35. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("BusinessPartner", relationData, orderBy, page, limit, condition, fields);
  36. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  37. count = int.Parse(obj["count"].ToString());
  38. return diclist;
  39. }
  40. public static List<Dictionary<string, object>> List(List<RelationData> relationData, string condition, int page = 1, int limit = 30, string orderBy = "Sort desc,Id desc")
  41. {
  42. List<string> fields = new List<string>(); //要显示的列
  43. fields.Add("Id");
  44. fields.Add("CreateDate"); //添加时间
  45. fields.Add("Status"); //状态
  46. fields.Add("Mobile"); //手机号
  47. fields.Add("Name"); //姓名
  48. fields.Add("ThisMonthTrade"); //本月交易额
  49. fields.Add("UserId"); //企业创客
  50. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("BusinessPartner", relationData, orderBy, page, limit, condition, fields);
  51. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  52. return diclist;
  53. }
  54. /// <summary>
  55. /// 查询一条记录
  56. /// </summary>
  57. /// <param name="Id">主键Id</param>
  58. /// <returns></returns>
  59. public static BusinessPartner Query(int Id)
  60. {
  61. WebCMSEntities db = new WebCMSEntities();
  62. BusinessPartner editData = db.BusinessPartner.FirstOrDefault(m => m.Id == Id) ?? new BusinessPartner();
  63. db.Dispose();
  64. return editData;
  65. }
  66. public static BusinessPartner Query(string condition, string fields = "*")
  67. {
  68. DataTable dt = new DbService(AppConfig.Base.mainTables, _conn).QueryDetail(fields, "BusinessPartner", condition);
  69. if (dt.Rows.Count > 0)
  70. {
  71. Dictionary<string, object> row = new Dictionary<string, object>();
  72. foreach (DataColumn dc in dt.Columns)
  73. {
  74. row.Add(dc.ColumnName, dt.Rows[0][dc.ColumnName].ToString());
  75. }
  76. return Newtonsoft.Json.JsonConvert.DeserializeObject<BusinessPartner>(Newtonsoft.Json.JsonConvert.SerializeObject(row));
  77. }
  78. return new BusinessPartner();
  79. }
  80. public static decimal Sum(string condition, string field)
  81. {
  82. DataTable dt = new DbService(AppConfig.Base.mainTables, _conn).QueryDetail("Sum(" + field + ")", "BusinessPartner", condition);
  83. decimal amount = 0;
  84. if (dt.Rows.Count > 0)
  85. {
  86. amount = decimal.Parse(dt.Rows[0][0].ToString());
  87. }
  88. return amount;
  89. }
  90. /// <summary>
  91. /// 查询记录数
  92. /// </summary>
  93. /// <param name="Id">主键Id</param>
  94. /// <returns></returns>
  95. public static int Count(string condition = "", string field = "Id")
  96. {
  97. int result = 0;
  98. DataTable dt = new DbService(AppConfig.Base.mainTables, _conn).QueryDetail("count(" + field + ")", "BusinessPartner", condition);
  99. if (dt.Rows.Count > 0)
  100. {
  101. result = int.Parse(function.CheckInt(dt.Rows[0][0].ToString()));
  102. }
  103. return result;
  104. }
  105. /// <summary>
  106. /// 查询是否存在
  107. /// </summary>
  108. /// <param name="Id">主键Id</param>
  109. /// <returns></returns>
  110. public static bool Exist(int Id)
  111. {
  112. WebCMSEntities db = new WebCMSEntities();
  113. bool check = db.BusinessPartner.Any(m => m.Id == Id);
  114. db.Dispose();
  115. return check;
  116. }
  117. /// <summary>
  118. /// 添加数据
  119. /// </summary>
  120. /// <param name="Fields">要设置的字段</param>
  121. /// <returns></returns>
  122. public static AppResultJson Add(Dictionary<string, object> fields, bool check = true)
  123. {
  124. if (check)
  125. {
  126. if (string.IsNullOrEmpty(fields["Mobile"].ToString()))
  127. {
  128. return new AppResultJson() { Status = "-1", Info = "请填写手机号" };
  129. }
  130. if (function.CheckMobile(fields["Mobile"].ToString()) == "")
  131. {
  132. return new AppResultJson() { Status = "-1", Info = "请填写正确的手机号" };
  133. }
  134. if (string.IsNullOrEmpty(fields["Name"].ToString()))
  135. {
  136. return new AppResultJson() { Status = "-1", Info = "请填写姓名" };
  137. }
  138. }
  139. int Id = new DbService(AppConfig.Base.mainTables, _conn).Add("BusinessPartner", fields, 0);
  140. return new AppResultJson() { Status = "1", Data = Id };
  141. }
  142. /// <summary>
  143. /// 修改数据
  144. /// </summary>
  145. /// <param name="Fields">要设置的字段</param>
  146. /// <param name="Id">主键Id</param>
  147. public static AppResultJson Edit(Dictionary<string, object> fields, int Id, bool check = true)
  148. {
  149. if (check)
  150. {
  151. if (string.IsNullOrEmpty(fields["Mobile"].ToString()))
  152. {
  153. return new AppResultJson() { Status = "-1", Info = "请填写手机号" };
  154. }
  155. if (function.CheckMobile(fields["Mobile"].ToString()) == "")
  156. {
  157. return new AppResultJson() { Status = "-1", Info = "请填写正确的手机号" };
  158. }
  159. if (string.IsNullOrEmpty(fields["Name"].ToString()))
  160. {
  161. return new AppResultJson() { Status = "-1", Info = "请填写姓名" };
  162. }
  163. }
  164. new DbService(AppConfig.Base.mainTables, _conn).Edit("BusinessPartner", fields, Id);
  165. return new AppResultJson() { Status = "1", Data = Id };
  166. }
  167. /// <summary>
  168. /// 逻辑删除
  169. /// </summary>
  170. /// <param name="Id">主键Id</param>
  171. public static void Remove(int Id)
  172. {
  173. Dictionary<string, object> fields = new Dictionary<string, object>();
  174. fields.Add("Status", -1);
  175. new DbService(AppConfig.Base.mainTables, _conn).Edit("BusinessPartner", fields, Id);
  176. }
  177. /// <summary>
  178. /// 删除数据
  179. /// </summary>
  180. /// <param name="Id">主键Id</param>
  181. public static void Delete(int Id)
  182. {
  183. new DbService(AppConfig.Base.mainTables, _conn).Delete("BusinessPartner", Id);
  184. }
  185. /// <summary>
  186. /// 排序
  187. /// </summary>
  188. /// <param name="Id">主键Id</param>
  189. /// <param name="Sort">排序序号</param>
  190. public static void Sort(int Id, int Sort)
  191. {
  192. new DbService(AppConfig.Base.mainTables, _conn).Sort("BusinessPartner", Sort, Id);
  193. }
  194. /// <summary>
  195. /// 导入数据
  196. /// </summary>
  197. /// <param name="ExcelData">json数据</param>
  198. public static void Import(string ExcelData)
  199. {
  200. WebCMSEntities db = new WebCMSEntities();
  201. JsonData list = JsonMapper.ToObject(ExcelData);
  202. for (int i = 1; i < list.Count; i++)
  203. {
  204. JsonData dr = list[i];
  205. db.BusinessPartner.Add(new BusinessPartner()
  206. {
  207. CreateDate = DateTime.Now,
  208. UpdateDate = DateTime.Now,
  209. });
  210. db.SaveChanges();
  211. }
  212. db.Dispose();
  213. }
  214. /// <summary>
  215. /// 导出excel表格
  216. /// </summary>
  217. /// <param name="fields">查询条件(单个字段)</param>
  218. /// <param name="condition">查询条件(sql语句)</param>
  219. /// <returns></returns>
  220. // public static void ExportExcel(List<RelationData> relationData, string condition)
  221. // {
  222. // }
  223. }
  224. }