OrderProductService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 OrderProductService
  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("ProductName"); //商品名称
  32. fields.Add("ProductPrice"); //商品单价
  33. fields.Add("ProductCount"); //商品数量
  34. fields.Add("TotalPrice"); //小计
  35. fields.Add("TotalIntegral"); //积分
  36. fields.Add("ProductIntegral"); //商品抵扣积分
  37. fields.Add("ProductCode"); //商品编号
  38. fields.Add("StoreId"); //发货仓库
  39. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("OrderProduct", relationData, orderBy, page, limit, condition, fields);
  40. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  41. count = int.Parse(obj["count"].ToString());
  42. return diclist;
  43. }
  44. public static List<Dictionary<string, object>> List(List<RelationData> relationData, string condition, int page = 1, int limit = 30, string orderBy = "Sort desc,Id desc")
  45. {
  46. List<string> fields = new List<string>(); //要显示的列
  47. fields.Add("Id");
  48. fields.Add("CreateDate"); //添加时间
  49. fields.Add("Status"); //状态
  50. fields.Add("ProductName"); //商品名称
  51. fields.Add("ProductPrice"); //商品单价
  52. fields.Add("ProductCount"); //商品数量
  53. fields.Add("TotalPrice"); //小计
  54. fields.Add("TotalIntegral"); //积分
  55. fields.Add("ProductIntegral"); //商品抵扣积分
  56. fields.Add("ProductCode"); //商品编号
  57. fields.Add("StoreId"); //发货仓库
  58. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("OrderProduct", relationData, orderBy, page, limit, condition, fields);
  59. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  60. return diclist;
  61. }
  62. /// <summary>
  63. /// 查询一条记录
  64. /// </summary>
  65. /// <param name="Id">主键Id</param>
  66. /// <returns></returns>
  67. public static OrderProduct Query(int Id)
  68. {
  69. WebCMSEntities db = new WebCMSEntities();
  70. OrderProduct editData = db.OrderProduct.FirstOrDefault(m => m.Id == Id) ?? new OrderProduct();
  71. db.Dispose();
  72. return editData;
  73. }
  74. // /// <summary>
  75. // /// 通过订单Id查询一条记录
  76. // /// </summary>
  77. // /// <param name="OrderId">订单Id</param>
  78. // /// <returns></returns>
  79. // public static OrderProduct QueryByOrderId(int OrderId)
  80. // {
  81. // WebCMSEntities db = new WebCMSEntities();
  82. // OrderProduct editData = db.OrderProduct.FirstOrDefault(m => m.OrderId == OrderId) ?? new OrderProduct();
  83. // db.Dispose();
  84. // return editData;
  85. // }
  86. public static OrderProduct Query(string condition, string fields = "*")
  87. {
  88. var orderProduct = new DbService(AppConfig.Base.mainTables, _conn).Query(fields, "OrderProduct", condition);
  89. if (orderProduct.Count > 0)
  90. {
  91. return Newtonsoft.Json.JsonConvert.DeserializeObject<OrderProduct>(Newtonsoft.Json.JsonConvert.SerializeObject(orderProduct));
  92. }
  93. return new OrderProduct();
  94. }
  95. public static decimal Sum(string condition, string field)
  96. {
  97. // DataTable dt = new DbService(AppConfig.Base.mainTables, _conn).QueryDetail("Sum(" + field + ")", "OrderProduct", condition);
  98. // decimal amount = 0;
  99. // if (dt.Rows.Count > 0)
  100. // {
  101. // amount = decimal.Parse(dt.Rows[0][0].ToString());
  102. // }
  103. // return amount;
  104. var dt = new DbService(AppConfig.Base.mainTables, _conn).Query("Sum(" + field + ") " + field, "OrderProduct", condition);
  105. decimal amount = 0;
  106. if (dt.Count > 0)
  107. {
  108. amount = decimal.Parse(dt[field].ToString());
  109. }
  110. return amount;
  111. }
  112. /// <summary>
  113. /// 查询记录数
  114. /// </summary>
  115. /// <param name="Id">主键Id</param>
  116. /// <returns></returns>
  117. public static int Count(string condition = "", string field = "Id")
  118. {
  119. // int result = 0;
  120. // DataTable dt = new DbService(AppConfig.Base.mainTables, _conn).QueryDetail("count(" + field + ")", "OrderProduct", condition);
  121. // if (dt.Rows.Count > 0)
  122. // {
  123. // result = int.Parse(function.CheckInt(dt.Rows[0][0].ToString()));
  124. // }
  125. // return result;
  126. var dt = new DbService(AppConfig.Base.mainTables, _conn).Query("Count(" + field + ") " + field, "OrderProduct", condition);
  127. int result = 0;
  128. if (dt.Count > 0)
  129. {
  130. result = int.Parse(dt[field].ToString());
  131. }
  132. return result;
  133. }
  134. /// <summary>
  135. /// 查询是否存在
  136. /// </summary>
  137. /// <param name="Id">主键Id</param>
  138. /// <returns></returns>
  139. public static bool Exist(int Id)
  140. {
  141. WebCMSEntities db = new WebCMSEntities();
  142. bool check = db.OrderProduct.Any(m => m.Id == Id);
  143. db.Dispose();
  144. return check;
  145. }
  146. /// <summary>
  147. /// 添加数据
  148. /// </summary>
  149. /// <param name="Fields">要设置的字段</param>
  150. /// <returns></returns>
  151. public static AppResultJson Add(Dictionary<string, object> fields, bool check = true)
  152. {
  153. if (check)
  154. {
  155. if (string.IsNullOrEmpty(fields["ProductName"].ToString()))
  156. {
  157. return new AppResultJson() { Status = "-1", Info = "请填写商品名称" };
  158. }
  159. if (string.IsNullOrEmpty(fields["ProductPrice"].ToString()))
  160. {
  161. return new AppResultJson() { Status = "-1", Info = "请填写商品单价" };
  162. }
  163. if (string.IsNullOrEmpty(fields["ProductCount"].ToString()))
  164. {
  165. return new AppResultJson() { Status = "-1", Info = "请填写商品数量" };
  166. }
  167. if (!function.IsInt(fields["ProductCount"].ToString()))
  168. {
  169. return new AppResultJson() { Status = "-1", Info = "请填写正确的商品数量" };
  170. }
  171. if (string.IsNullOrEmpty(fields["TotalPrice"].ToString()))
  172. {
  173. return new AppResultJson() { Status = "-1", Info = "请填写小计" };
  174. }
  175. if (string.IsNullOrEmpty(fields["ProductCode"].ToString()))
  176. {
  177. return new AppResultJson() { Status = "-1", Info = "请填写商品编号" };
  178. }
  179. }
  180. int Id = new DbService(AppConfig.Base.mainTables, _conn).Add("OrderProduct", fields, 0);
  181. return new AppResultJson() { Status = "1", Data = Id };
  182. }
  183. /// <summary>
  184. /// 修改数据
  185. /// </summary>
  186. /// <param name="Fields">要设置的字段</param>
  187. /// <param name="Id">主键Id</param>
  188. public static AppResultJson Edit(Dictionary<string, object> fields, int Id, bool check = true)
  189. {
  190. if (check)
  191. {
  192. if (string.IsNullOrEmpty(fields["ProductName"].ToString()))
  193. {
  194. return new AppResultJson() { Status = "-1", Info = "请填写商品名称" };
  195. }
  196. if (string.IsNullOrEmpty(fields["ProductPrice"].ToString()))
  197. {
  198. return new AppResultJson() { Status = "-1", Info = "请填写商品单价" };
  199. }
  200. if (string.IsNullOrEmpty(fields["ProductCount"].ToString()))
  201. {
  202. return new AppResultJson() { Status = "-1", Info = "请填写商品数量" };
  203. }
  204. if (!function.IsInt(fields["ProductCount"].ToString()))
  205. {
  206. return new AppResultJson() { Status = "-1", Info = "请填写正确的商品数量" };
  207. }
  208. if (string.IsNullOrEmpty(fields["TotalPrice"].ToString()))
  209. {
  210. return new AppResultJson() { Status = "-1", Info = "请填写小计" };
  211. }
  212. if (string.IsNullOrEmpty(fields["ProductCode"].ToString()))
  213. {
  214. return new AppResultJson() { Status = "-1", Info = "请填写商品编号" };
  215. }
  216. }
  217. new DbService(AppConfig.Base.mainTables, _conn).Edit("OrderProduct", fields, Id);
  218. return new AppResultJson() { Status = "1", Data = Id };
  219. }
  220. /// <summary>
  221. /// 逻辑删除
  222. /// </summary>
  223. /// <param name="Id">主键Id</param>
  224. public static void Remove(int Id)
  225. {
  226. Dictionary<string, object> fields = new Dictionary<string, object>();
  227. fields.Add("Status", -1);
  228. new DbService(AppConfig.Base.mainTables, _conn).Edit("OrderProduct", fields, Id);
  229. }
  230. /// <summary>
  231. /// 删除数据
  232. /// </summary>
  233. /// <param name="Id">主键Id</param>
  234. public static void Delete(int Id)
  235. {
  236. new DbService(AppConfig.Base.mainTables, _conn).Delete("OrderProduct", Id);
  237. }
  238. /// <summary>
  239. /// 排序
  240. /// </summary>
  241. /// <param name="Id">主键Id</param>
  242. /// <param name="Sort">排序序号</param>
  243. public static void Sort(int Id, int Sort)
  244. {
  245. new DbService(AppConfig.Base.mainTables, _conn).Sort("OrderProduct", Sort, Id);
  246. }
  247. /// <summary>
  248. /// 导入数据
  249. /// </summary>
  250. /// <param name="ExcelData">json数据</param>
  251. public static void Import(string ExcelData)
  252. {
  253. WebCMSEntities db = new WebCMSEntities();
  254. JsonData list = JsonMapper.ToObject(ExcelData);
  255. for (int i = 1; i < list.Count; i++)
  256. {
  257. JsonData dr = list[i];
  258. db.OrderProduct.Add(new OrderProduct()
  259. {
  260. CreateDate = DateTime.Now,
  261. UpdateDate = DateTime.Now,
  262. });
  263. db.SaveChanges();
  264. }
  265. db.Dispose();
  266. }
  267. /// <summary>
  268. /// 导出excel表格
  269. /// </summary>
  270. /// <param name="fields">查询条件(单个字段)</param>
  271. /// <param name="condition">查询条件(sql语句)</param>
  272. /// <returns></returns>
  273. // public static void ExportExcel(List<RelationData> relationData, string condition)
  274. // {
  275. // }
  276. }
  277. }