ProductCommentService.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 ProductCommentService
  14. {
  15. string _conn = "";
  16. public ProductCommentService()
  17. {
  18. _conn = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  19. }
  20. /// <summary>
  21. /// 查询列表
  22. /// </summary>
  23. /// <param name="relationData">关联表</param>
  24. /// <param name="condition">查询条件(sql语句)</param>
  25. /// <param name="count">总数(输出)</param>
  26. /// <param name="page">页码</param>
  27. /// <param name="limit">每页条数</param>
  28. /// <returns></returns>
  29. public 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")
  30. {
  31. List<string> fields = new List<string>(); //要显示的列
  32. fields.Add("Id");
  33. fields.Add("CreateDate"); //添加时间
  34. fields.Add("Status"); //状态
  35. fields.Add("UserId"); //创客Id
  36. fields.Add("ProductId"); //商品Id
  37. fields.Add("OrderId"); //订单Id
  38. fields.Add("Contents"); //评论内容
  39. fields.Add("StarNum"); //商品评分
  40. fields.Add("AgreeCount"); //点赞数
  41. fields.Add("BackCount"); //回复数
  42. Dictionary<string, object> obj = new DbService(AppConfig.Base.dbTables, _conn).IndexData("ProductComment", relationData, orderBy, page, limit, condition, fields);
  43. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  44. count = int.Parse(obj["count"].ToString());
  45. return diclist;
  46. }
  47. public List<Dictionary<string, object>> List(List<RelationData> relationData, string condition, int page = 1, int limit = 30, string orderBy = "Sort desc,Id desc")
  48. {
  49. List<string> fields = new List<string>(); //要显示的列
  50. fields.Add("Id");
  51. fields.Add("CreateDate"); //添加时间
  52. fields.Add("Status"); //状态
  53. fields.Add("UserId"); //创客Id
  54. fields.Add("ProductId"); //商品Id
  55. fields.Add("OrderId"); //订单Id
  56. fields.Add("Contents"); //评论内容
  57. fields.Add("StarNum"); //商品评分
  58. fields.Add("AgreeCount"); //点赞数
  59. fields.Add("BackCount"); //回复数
  60. Dictionary<string, object> obj = new DbService(AppConfig.Base.dbTables, _conn).IndexData("ProductComment", relationData, orderBy, page, limit, condition, fields);
  61. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  62. return diclist;
  63. }
  64. /// <summary>
  65. /// 查询一条记录
  66. /// </summary>
  67. /// <param name="Id">主键Id</param>
  68. /// <returns></returns>
  69. public ProductComment Query(int Id)
  70. {
  71. WebCMSEntities db = new WebCMSEntities();
  72. ProductComment editData = db.ProductComment.FirstOrDefault(m => m.Id == Id) ?? new ProductComment();
  73. db.Dispose();
  74. return editData;
  75. }
  76. /// <summary>
  77. /// 查询记录数
  78. /// </summary>
  79. /// <param name="Id">主键Id</param>
  80. /// <returns></returns>
  81. public int Count(string condition = "")
  82. {
  83. int result = 0;
  84. DataTable dt = CustomerSqlConn.dtable("select count(Id) from ProductComment where 1=1" + condition, _conn);
  85. if(dt.Rows.Count > 0)
  86. {
  87. result = int.Parse(function.CheckInt(dt.Rows[0][0].ToString()));
  88. }
  89. return result;
  90. }
  91. /// <summary>
  92. /// 查询是否存在
  93. /// </summary>
  94. /// <param name="Id">主键Id</param>
  95. /// <returns></returns>
  96. public bool Exist(int Id)
  97. {
  98. WebCMSEntities db = new WebCMSEntities();
  99. bool check = db.ProductComment.Any(m => m.Id == Id);
  100. db.Dispose();
  101. return check;
  102. }
  103. /// <summary>
  104. /// 添加数据
  105. /// </summary>
  106. /// <param name="Fields">要设置的字段</param>
  107. /// <returns></returns>
  108. public AppResultJson Add(Dictionary<string, object> fields, bool check = true)
  109. {
  110. if(check)
  111. {
  112. if (string.IsNullOrEmpty(fields["Contents"].ToString()))
  113. {
  114. return new AppResultJson() { Status = "-1", Info = "请填写评论内容" };
  115. }
  116. if (string.IsNullOrEmpty(fields["StarNum"].ToString()))
  117. {
  118. return new AppResultJson() { Status = "-1", Info = "请填写商品评分" };
  119. }
  120. if (!function.IsInt(fields["StarNum"].ToString()))
  121. {
  122. return new AppResultJson() { Status = "-1", Info = "请填写正确的商品评分" };
  123. }
  124. if (string.IsNullOrEmpty(fields["AgreeCount"].ToString()))
  125. {
  126. return new AppResultJson() { Status = "-1", Info = "请填写点赞数" };
  127. }
  128. if (!function.IsInt(fields["AgreeCount"].ToString()))
  129. {
  130. return new AppResultJson() { Status = "-1", Info = "请填写正确的点赞数" };
  131. }
  132. if (string.IsNullOrEmpty(fields["BackCount"].ToString()))
  133. {
  134. return new AppResultJson() { Status = "-1", Info = "请填写回复数" };
  135. }
  136. if (!function.IsInt(fields["BackCount"].ToString()))
  137. {
  138. return new AppResultJson() { Status = "-1", Info = "请填写正确的回复数" };
  139. }
  140. }
  141. int Id = new DbService(AppConfig.Base.dbTables, _conn).Add("ProductComment", fields, 0);
  142. return new AppResultJson(){ Status = "1", Data = Id };
  143. }
  144. /// <summary>
  145. /// 修改数据
  146. /// </summary>
  147. /// <param name="Fields">要设置的字段</param>
  148. /// <param name="Id">主键Id</param>
  149. public AppResultJson Edit(Dictionary<string, object> fields, int Id, bool check = true)
  150. {
  151. if(check)
  152. {
  153. if (string.IsNullOrEmpty(fields["Contents"].ToString()))
  154. {
  155. return new AppResultJson() { Status = "-1", Info = "请填写评论内容" };
  156. }
  157. if (string.IsNullOrEmpty(fields["StarNum"].ToString()))
  158. {
  159. return new AppResultJson() { Status = "-1", Info = "请填写商品评分" };
  160. }
  161. if (!function.IsInt(fields["StarNum"].ToString()))
  162. {
  163. return new AppResultJson() { Status = "-1", Info = "请填写正确的商品评分" };
  164. }
  165. if (string.IsNullOrEmpty(fields["AgreeCount"].ToString()))
  166. {
  167. return new AppResultJson() { Status = "-1", Info = "请填写点赞数" };
  168. }
  169. if (!function.IsInt(fields["AgreeCount"].ToString()))
  170. {
  171. return new AppResultJson() { Status = "-1", Info = "请填写正确的点赞数" };
  172. }
  173. if (string.IsNullOrEmpty(fields["BackCount"].ToString()))
  174. {
  175. return new AppResultJson() { Status = "-1", Info = "请填写回复数" };
  176. }
  177. if (!function.IsInt(fields["BackCount"].ToString()))
  178. {
  179. return new AppResultJson() { Status = "-1", Info = "请填写正确的回复数" };
  180. }
  181. }
  182. new DbService(AppConfig.Base.dbTables, _conn).Edit("ProductComment", fields, Id);
  183. return new AppResultJson(){ Status = "1", Data = Id };
  184. }
  185. /// <summary>
  186. /// 逻辑删除
  187. /// </summary>
  188. /// <param name="Id">主键Id</param>
  189. public void Remove(int Id)
  190. {
  191. Dictionary<string, object> fields = new Dictionary<string, object>();
  192. fields.Add("Status", -1);
  193. new DbService(AppConfig.Base.dbTables, _conn).Edit("ProductComment", fields, Id);
  194. }
  195. /// <summary>
  196. /// 删除数据
  197. /// </summary>
  198. /// <param name="Id">主键Id</param>
  199. public void Delete(int Id)
  200. {
  201. new DbService(AppConfig.Base.dbTables, _conn).Delete("ProductComment", Id);
  202. }
  203. /// <summary>
  204. /// 排序
  205. /// </summary>
  206. /// <param name="Id">主键Id</param>
  207. /// <param name="Sort">排序序号</param>
  208. public void Sort(int Id, int Sort)
  209. {
  210. new DbService(AppConfig.Base.dbTables, _conn).Sort("ProductComment", Sort, Id);
  211. }
  212. /// <summary>
  213. /// 导入数据
  214. /// </summary>
  215. /// <param name="ExcelData">json数据</param>
  216. public void Import(string ExcelData)
  217. {
  218. WebCMSEntities db = new WebCMSEntities();
  219. JsonData list = JsonMapper.ToObject(ExcelData);
  220. for (int i = 1; i < list.Count;i++ )
  221. {
  222. JsonData dr = list[i];
  223. db.ProductComment.Add(new ProductComment()
  224. {
  225. CreateDate = DateTime.Now,
  226. UpdateDate = DateTime.Now,
  227. });
  228. db.SaveChanges();
  229. }
  230. db.Dispose();
  231. }
  232. /// <summary>
  233. /// 导出excel表格
  234. /// </summary>
  235. /// <param name="fields">查询条件(单个字段)</param>
  236. /// <param name="condition">查询条件(sql语句)</param>
  237. /// <returns></returns>
  238. // public void ExportExcel(List<RelationData> relationData, string condition)
  239. // {
  240. // }
  241. }
  242. }