SmallStoreHouseService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 SmallStoreHouseService
  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("StoreNo"); //仓库编号
  32. fields.Add("StoreName"); //仓库名称
  33. fields.Add("UserId"); //仓库归属人
  34. fields.Add("BrandId"); //产品类型
  35. fields.Add("TotalNum"); //总库存数
  36. fields.Add("LaveNum"); //剩余库存数
  37. fields.Add("Mobile"); //仓库归属人手机号
  38. fields.Add("FromStoreId"); //出货仓库
  39. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("SmallStoreHouse", 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("StoreNo"); //仓库编号
  51. fields.Add("StoreName"); //仓库名称
  52. fields.Add("UserId"); //仓库归属人
  53. fields.Add("BrandId"); //产品类型
  54. fields.Add("TotalNum"); //总库存数
  55. fields.Add("LaveNum"); //剩余库存数
  56. fields.Add("Mobile"); //仓库归属人手机号
  57. fields.Add("FromStoreId"); //出货仓库
  58. Dictionary<string, object> obj = new DbService(AppConfig.Base.mainTables, _conn).IndexData("SmallStoreHouse", 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 SmallStoreHouse Query(int Id)
  68. {
  69. WebCMSEntities db = new WebCMSEntities();
  70. SmallStoreHouse editData = db.SmallStoreHouse.FirstOrDefault(m => m.Id == Id) ?? new SmallStoreHouse();
  71. db.Dispose();
  72. return editData;
  73. }
  74. public static SmallStoreHouse Query(string condition, string fields = "*")
  75. {
  76. DataTable dt = new DbService(AppConfig.Base.mainTables, _conn).QueryDetail(fields, "SmallStoreHouse", condition);
  77. if (dt.Rows.Count > 0)
  78. {
  79. Dictionary<string, object> row = new Dictionary<string, object>();
  80. foreach (DataColumn dc in dt.Columns)
  81. {
  82. row.Add(dc.ColumnName, dt.Rows[0][dc.ColumnName].ToString());
  83. }
  84. return Newtonsoft.Json.JsonConvert.DeserializeObject<SmallStoreHouse>(Newtonsoft.Json.JsonConvert.SerializeObject(row));
  85. }
  86. return new SmallStoreHouse();
  87. }
  88. public static decimal Sum(string condition, string field)
  89. {
  90. DataTable dt = new DbService(AppConfig.Base.mainTables, _conn).QueryDetail("Sum(" + field + ")", "SmallStoreHouse", condition);
  91. decimal amount = 0;
  92. if (dt.Rows.Count > 0)
  93. {
  94. amount = decimal.Parse(dt.Rows[0][0].ToString());
  95. }
  96. return amount;
  97. }
  98. /// <summary>
  99. /// 查询记录数
  100. /// </summary>
  101. /// <param name="Id">主键Id</param>
  102. /// <returns></returns>
  103. public static int Count(string condition = "")
  104. {
  105. int result = 0;
  106. DataTable dt = CustomerSqlConn.dtable("select count(Id) from SmallStoreHouse where 1=1" + condition, _conn);
  107. if (dt.Rows.Count > 0)
  108. {
  109. result = int.Parse(function.CheckInt(dt.Rows[0][0].ToString()));
  110. }
  111. return result;
  112. }
  113. /// <summary>
  114. /// 查询是否存在
  115. /// </summary>
  116. /// <param name="Id">主键Id</param>
  117. /// <returns></returns>
  118. public static bool Exist(int Id)
  119. {
  120. WebCMSEntities db = new WebCMSEntities();
  121. bool check = db.SmallStoreHouse.Any(m => m.Id == Id);
  122. db.Dispose();
  123. return check;
  124. }
  125. /// <summary>
  126. /// 添加数据
  127. /// </summary>
  128. /// <param name="Fields">要设置的字段</param>
  129. /// <returns></returns>
  130. public static AppResultJson Add(Dictionary<string, object> fields, bool check = true)
  131. {
  132. if (check)
  133. {
  134. if (string.IsNullOrEmpty(fields["StoreNo"].ToString()))
  135. {
  136. return new AppResultJson() { Status = "-1", Info = "请填写仓库编号" };
  137. }
  138. if (string.IsNullOrEmpty(fields["TotalNum"].ToString()))
  139. {
  140. return new AppResultJson() { Status = "-1", Info = "请填写总库存数" };
  141. }
  142. if (!function.IsInt(fields["TotalNum"].ToString()))
  143. {
  144. return new AppResultJson() { Status = "-1", Info = "请填写正确的总库存数" };
  145. }
  146. if (string.IsNullOrEmpty(fields["LaveNum"].ToString()))
  147. {
  148. return new AppResultJson() { Status = "-1", Info = "请填写剩余库存数" };
  149. }
  150. if (!function.IsInt(fields["LaveNum"].ToString()))
  151. {
  152. return new AppResultJson() { Status = "-1", Info = "请填写正确的剩余库存数" };
  153. }
  154. if (string.IsNullOrEmpty(fields["Mobile"].ToString()))
  155. {
  156. return new AppResultJson() { Status = "-1", Info = "请填写仓库归属人手机号" };
  157. }
  158. if (function.CheckMobile(fields["Mobile"].ToString()) == "")
  159. {
  160. return new AppResultJson() { Status = "-1", Info = "请填写正确的仓库归属人手机号" };
  161. }
  162. }
  163. int Id = new DbService(AppConfig.Base.mainTables, _conn).Add("SmallStoreHouse", fields, 0);
  164. return new AppResultJson() { Status = "1", Data = Id };
  165. }
  166. /// <summary>
  167. /// 修改数据
  168. /// </summary>
  169. /// <param name="Fields">要设置的字段</param>
  170. /// <param name="Id">主键Id</param>
  171. public static AppResultJson Edit(Dictionary<string, object> fields, int Id, bool check = true)
  172. {
  173. if (check)
  174. {
  175. if (string.IsNullOrEmpty(fields["StoreNo"].ToString()))
  176. {
  177. return new AppResultJson() { Status = "-1", Info = "请填写仓库编号" };
  178. }
  179. if (string.IsNullOrEmpty(fields["TotalNum"].ToString()))
  180. {
  181. return new AppResultJson() { Status = "-1", Info = "请填写总库存数" };
  182. }
  183. if (!function.IsInt(fields["TotalNum"].ToString()))
  184. {
  185. return new AppResultJson() { Status = "-1", Info = "请填写正确的总库存数" };
  186. }
  187. if (string.IsNullOrEmpty(fields["LaveNum"].ToString()))
  188. {
  189. return new AppResultJson() { Status = "-1", Info = "请填写剩余库存数" };
  190. }
  191. if (!function.IsInt(fields["LaveNum"].ToString()))
  192. {
  193. return new AppResultJson() { Status = "-1", Info = "请填写正确的剩余库存数" };
  194. }
  195. if (string.IsNullOrEmpty(fields["Mobile"].ToString()))
  196. {
  197. return new AppResultJson() { Status = "-1", Info = "请填写仓库归属人手机号" };
  198. }
  199. if (function.CheckMobile(fields["Mobile"].ToString()) == "")
  200. {
  201. return new AppResultJson() { Status = "-1", Info = "请填写正确的仓库归属人手机号" };
  202. }
  203. }
  204. new DbService(AppConfig.Base.mainTables, _conn).Edit("SmallStoreHouse", fields, Id);
  205. return new AppResultJson() { Status = "1", Data = Id };
  206. }
  207. /// <summary>
  208. /// 逻辑删除
  209. /// </summary>
  210. /// <param name="Id">主键Id</param>
  211. public static void Remove(int Id)
  212. {
  213. Dictionary<string, object> fields = new Dictionary<string, object>();
  214. fields.Add("Status", -1);
  215. new DbService(AppConfig.Base.mainTables, _conn).Edit("SmallStoreHouse", fields, Id);
  216. }
  217. /// <summary>
  218. /// 删除数据
  219. /// </summary>
  220. /// <param name="Id">主键Id</param>
  221. public static void Delete(int Id)
  222. {
  223. new DbService(AppConfig.Base.mainTables, _conn).Delete("SmallStoreHouse", Id);
  224. }
  225. /// <summary>
  226. /// 排序
  227. /// </summary>
  228. /// <param name="Id">主键Id</param>
  229. /// <param name="Sort">排序序号</param>
  230. public static void Sort(int Id, int Sort)
  231. {
  232. new DbService(AppConfig.Base.mainTables, _conn).Sort("SmallStoreHouse", Sort, Id);
  233. }
  234. /// <summary>
  235. /// 导入数据
  236. /// </summary>
  237. /// <param name="ExcelData">json数据</param>
  238. public static void Import(string ExcelData)
  239. {
  240. WebCMSEntities db = new WebCMSEntities();
  241. JsonData list = JsonMapper.ToObject(ExcelData);
  242. for (int i = 1; i < list.Count; i++)
  243. {
  244. JsonData dr = list[i];
  245. db.SmallStoreHouse.Add(new SmallStoreHouse()
  246. {
  247. CreateDate = DateTime.Now,
  248. UpdateDate = DateTime.Now,
  249. });
  250. db.SaveChanges();
  251. }
  252. db.Dispose();
  253. }
  254. /// <summary>
  255. /// 导出excel表格
  256. /// </summary>
  257. /// <param name="fields">查询条件(单个字段)</param>
  258. /// <param name="condition">查询条件(sql语句)</param>
  259. /// <returns></returns>
  260. // public static void ExportExcel(List<RelationData> relationData, string condition)
  261. // {
  262. // }
  263. }
  264. }