ProductsService.cs 13 KB

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