MerchantsService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 MerchantsService
  14. {
  15. string _conn = "";
  16. public MerchantsService()
  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("Name"); //名称
  36. fields.Add("Logo"); //Logo图片
  37. fields.Add("Areas"); //所在地区
  38. fields.Add("Principal"); //负责人
  39. fields.Add("Phone"); //联系电话
  40. fields.Add("BusinessLicense"); //营业执照
  41. fields.Add("StarNum1"); //快递包装评分
  42. fields.Add("FollowCount"); //关注数
  43. fields.Add("IsAuth"); //是否认证
  44. fields.Add("CommentCount"); //评论数
  45. fields.Add("Address"); //详细地址
  46. fields.Add("StarNum2"); //送货速度评分
  47. fields.Add("StarNum3"); //服务态度评分
  48. Dictionary<string, object> obj = new DbService(AppConfig.Base.dbTables, _conn).IndexData("Merchants", relationData, orderBy, page, limit, condition, fields);
  49. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  50. count = int.Parse(obj["count"].ToString());
  51. return diclist;
  52. }
  53. public List<Dictionary<string, object>> List(List<RelationData> relationData, string condition, int page = 1, int limit = 30, string orderBy = "Sort desc,Id desc")
  54. {
  55. List<string> fields = new List<string>(); //要显示的列
  56. fields.Add("Id");
  57. fields.Add("CreateDate"); //添加时间
  58. fields.Add("Status"); //状态
  59. fields.Add("Name"); //名称
  60. fields.Add("Logo"); //Logo图片
  61. fields.Add("Areas"); //所在地区
  62. fields.Add("Principal"); //负责人
  63. fields.Add("Phone"); //联系电话
  64. fields.Add("BusinessLicense"); //营业执照
  65. fields.Add("StarNum1"); //快递包装评分
  66. fields.Add("FollowCount"); //关注数
  67. fields.Add("IsAuth"); //是否认证
  68. fields.Add("CommentCount"); //评论数
  69. fields.Add("Address"); //详细地址
  70. fields.Add("StarNum2"); //送货速度评分
  71. fields.Add("StarNum3"); //服务态度评分
  72. Dictionary<string, object> obj = new DbService(AppConfig.Base.dbTables, _conn).IndexData("Merchants", relationData, orderBy, page, limit, condition, fields);
  73. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  74. return diclist;
  75. }
  76. /// <summary>
  77. /// 查询一条记录
  78. /// </summary>
  79. /// <param name="Id">主键Id</param>
  80. /// <returns></returns>
  81. public Merchants Query(int Id)
  82. {
  83. WebCMSEntities db = new WebCMSEntities();
  84. Merchants editData = db.Merchants.FirstOrDefault(m => m.Id == Id) ?? new Merchants();
  85. db.Dispose();
  86. return editData;
  87. }
  88. /// <summary>
  89. /// 查询记录数
  90. /// </summary>
  91. /// <param name="Id">主键Id</param>
  92. /// <returns></returns>
  93. public int Count(string condition = "")
  94. {
  95. int result = 0;
  96. DataTable dt = CustomerSqlConn.dtable("select count(Id) from Merchants where 1=1" + condition, _conn);
  97. if(dt.Rows.Count > 0)
  98. {
  99. result = int.Parse(function.CheckInt(dt.Rows[0][0].ToString()));
  100. }
  101. return result;
  102. }
  103. /// <summary>
  104. /// 查询是否存在
  105. /// </summary>
  106. /// <param name="Id">主键Id</param>
  107. /// <returns></returns>
  108. public bool Exist(int Id)
  109. {
  110. WebCMSEntities db = new WebCMSEntities();
  111. bool check = db.Merchants.Any(m => m.Id == Id);
  112. db.Dispose();
  113. return check;
  114. }
  115. /// <summary>
  116. /// 添加数据
  117. /// </summary>
  118. /// <param name="Fields">要设置的字段</param>
  119. /// <returns></returns>
  120. public AppResultJson Add(Dictionary<string, object> fields, bool check = true)
  121. {
  122. if(check)
  123. {
  124. if (string.IsNullOrEmpty(fields["Name"].ToString()))
  125. {
  126. return new AppResultJson() { Status = "-1", Info = "请填写名称" };
  127. }
  128. if (string.IsNullOrEmpty(fields["Areas"].ToString()))
  129. {
  130. return new AppResultJson() { Status = "-1", Info = "请填写所在地区" };
  131. }
  132. if (string.IsNullOrEmpty(fields["Principal"].ToString()))
  133. {
  134. return new AppResultJson() { Status = "-1", Info = "请填写负责人" };
  135. }
  136. if (string.IsNullOrEmpty(fields["Phone"].ToString()))
  137. {
  138. return new AppResultJson() { Status = "-1", Info = "请填写联系电话" };
  139. }
  140. if (string.IsNullOrEmpty(fields["StarNum1"].ToString()))
  141. {
  142. return new AppResultJson() { Status = "-1", Info = "请填写快递包装评分" };
  143. }
  144. if (!function.IsNum(fields["StarNum1"].ToString()))
  145. {
  146. return new AppResultJson() { Status = "-1", Info = "请填写正确的快递包装评分" };
  147. }
  148. if (string.IsNullOrEmpty(fields["FollowCount"].ToString()))
  149. {
  150. return new AppResultJson() { Status = "-1", Info = "请填写关注数" };
  151. }
  152. if (!function.IsInt(fields["FollowCount"].ToString()))
  153. {
  154. return new AppResultJson() { Status = "-1", Info = "请填写正确的关注数" };
  155. }
  156. if (string.IsNullOrEmpty(fields["CommentCount"].ToString()))
  157. {
  158. return new AppResultJson() { Status = "-1", Info = "请填写评论数" };
  159. }
  160. if (!function.IsInt(fields["CommentCount"].ToString()))
  161. {
  162. return new AppResultJson() { Status = "-1", Info = "请填写正确的评论数" };
  163. }
  164. if (string.IsNullOrEmpty(fields["StarNum2"].ToString()))
  165. {
  166. return new AppResultJson() { Status = "-1", Info = "请填写送货速度评分" };
  167. }
  168. if (!function.IsNum(fields["StarNum2"].ToString()))
  169. {
  170. return new AppResultJson() { Status = "-1", Info = "请填写正确的送货速度评分" };
  171. }
  172. if (string.IsNullOrEmpty(fields["StarNum3"].ToString()))
  173. {
  174. return new AppResultJson() { Status = "-1", Info = "请填写服务态度评分" };
  175. }
  176. if (!function.IsNum(fields["StarNum3"].ToString()))
  177. {
  178. return new AppResultJson() { Status = "-1", Info = "请填写正确的服务态度评分" };
  179. }
  180. }
  181. int Id = new DbService(AppConfig.Base.dbTables, _conn).Add("Merchants", fields, 0);
  182. return new AppResultJson(){ Status = "1", Data = Id };
  183. }
  184. /// <summary>
  185. /// 修改数据
  186. /// </summary>
  187. /// <param name="Fields">要设置的字段</param>
  188. /// <param name="Id">主键Id</param>
  189. public AppResultJson Edit(Dictionary<string, object> fields, int Id, bool check = true)
  190. {
  191. if(check)
  192. {
  193. if (string.IsNullOrEmpty(fields["Name"].ToString()))
  194. {
  195. return new AppResultJson() { Status = "-1", Info = "请填写名称" };
  196. }
  197. if (string.IsNullOrEmpty(fields["Areas"].ToString()))
  198. {
  199. return new AppResultJson() { Status = "-1", Info = "请填写所在地区" };
  200. }
  201. if (string.IsNullOrEmpty(fields["Principal"].ToString()))
  202. {
  203. return new AppResultJson() { Status = "-1", Info = "请填写负责人" };
  204. }
  205. if (string.IsNullOrEmpty(fields["Phone"].ToString()))
  206. {
  207. return new AppResultJson() { Status = "-1", Info = "请填写联系电话" };
  208. }
  209. if (string.IsNullOrEmpty(fields["StarNum1"].ToString()))
  210. {
  211. return new AppResultJson() { Status = "-1", Info = "请填写快递包装评分" };
  212. }
  213. if (!function.IsNum(fields["StarNum1"].ToString()))
  214. {
  215. return new AppResultJson() { Status = "-1", Info = "请填写正确的快递包装评分" };
  216. }
  217. if (string.IsNullOrEmpty(fields["FollowCount"].ToString()))
  218. {
  219. return new AppResultJson() { Status = "-1", Info = "请填写关注数" };
  220. }
  221. if (!function.IsInt(fields["FollowCount"].ToString()))
  222. {
  223. return new AppResultJson() { Status = "-1", Info = "请填写正确的关注数" };
  224. }
  225. if (string.IsNullOrEmpty(fields["CommentCount"].ToString()))
  226. {
  227. return new AppResultJson() { Status = "-1", Info = "请填写评论数" };
  228. }
  229. if (!function.IsInt(fields["CommentCount"].ToString()))
  230. {
  231. return new AppResultJson() { Status = "-1", Info = "请填写正确的评论数" };
  232. }
  233. if (string.IsNullOrEmpty(fields["StarNum2"].ToString()))
  234. {
  235. return new AppResultJson() { Status = "-1", Info = "请填写送货速度评分" };
  236. }
  237. if (!function.IsNum(fields["StarNum2"].ToString()))
  238. {
  239. return new AppResultJson() { Status = "-1", Info = "请填写正确的送货速度评分" };
  240. }
  241. if (string.IsNullOrEmpty(fields["StarNum3"].ToString()))
  242. {
  243. return new AppResultJson() { Status = "-1", Info = "请填写服务态度评分" };
  244. }
  245. if (!function.IsNum(fields["StarNum3"].ToString()))
  246. {
  247. return new AppResultJson() { Status = "-1", Info = "请填写正确的服务态度评分" };
  248. }
  249. }
  250. new DbService(AppConfig.Base.dbTables, _conn).Edit("Merchants", fields, Id);
  251. return new AppResultJson(){ Status = "1", Data = Id };
  252. }
  253. /// <summary>
  254. /// 逻辑删除
  255. /// </summary>
  256. /// <param name="Id">主键Id</param>
  257. public void Remove(int Id)
  258. {
  259. Dictionary<string, object> fields = new Dictionary<string, object>();
  260. fields.Add("Status", -1);
  261. new DbService(AppConfig.Base.dbTables, _conn).Edit("Merchants", fields, Id);
  262. }
  263. /// <summary>
  264. /// 删除数据
  265. /// </summary>
  266. /// <param name="Id">主键Id</param>
  267. public void Delete(int Id)
  268. {
  269. new DbService(AppConfig.Base.dbTables, _conn).Delete("Merchants", Id);
  270. }
  271. /// <summary>
  272. /// 排序
  273. /// </summary>
  274. /// <param name="Id">主键Id</param>
  275. /// <param name="Sort">排序序号</param>
  276. public void Sort(int Id, int Sort)
  277. {
  278. new DbService(AppConfig.Base.dbTables, _conn).Sort("Merchants", Sort, Id);
  279. }
  280. /// <summary>
  281. /// 导入数据
  282. /// </summary>
  283. /// <param name="ExcelData">json数据</param>
  284. public void Import(string ExcelData)
  285. {
  286. WebCMSEntities db = new WebCMSEntities();
  287. JsonData list = JsonMapper.ToObject(ExcelData);
  288. for (int i = 1; i < list.Count;i++ )
  289. {
  290. JsonData dr = list[i];
  291. db.Merchants.Add(new Merchants()
  292. {
  293. CreateDate = DateTime.Now,
  294. UpdateDate = DateTime.Now,
  295. });
  296. db.SaveChanges();
  297. }
  298. db.Dispose();
  299. }
  300. /// <summary>
  301. /// 导出excel表格
  302. /// </summary>
  303. /// <param name="fields">查询条件(单个字段)</param>
  304. /// <param name="condition">查询条件(sql语句)</param>
  305. /// <returns></returns>
  306. // public void ExportExcel(List<RelationData> relationData, string condition)
  307. // {
  308. // }
  309. }
  310. }