革博士程序V1仓库
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 2 gadiem
pirms 2 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using Models;
  2. using Newtonsoft.Json;
  3. using SqlSugar;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Dynamic;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Service
  12. {
  13. public class RecordsService : Repository<Models.Records>
  14. {
  15. //通用查询 ,所有表
  16. public List<ExpandoObject> GetList(string tablename, string fields, string domain, string orderby)
  17. {
  18. int totalCount=0;
  19. return GetList(tablename,fields, domain, orderby, 0, 0, ref totalCount);
  20. }
  21. //通用分页查询 ,所有表
  22. public List<ExpandoObject> GetList(string tablename,string fields, string domain, string orderby, int pageNum, int pageSize, ref int totalCount)
  23. {
  24. var sql = base.AsSugarClient().Queryable(tablename,"");
  25. if (!string.IsNullOrEmpty(domain))
  26. sql = sql.Where(base.Context.Utilities.JsonToConditionalModels(domain));
  27. if (!string.IsNullOrEmpty(fields) && fields!="*")
  28. sql = sql.Select(fields);
  29. if (!string.IsNullOrEmpty(orderby))
  30. sql = sql.OrderBy(orderby);
  31. else
  32. sql = sql.OrderBy("id desc");
  33. if(pageNum==0 && pageSize == 0)
  34. return sql.ToList();
  35. else
  36. return sql.ToPageList(pageNum, pageSize, ref totalCount);
  37. }
  38. public int Insert(string tablename, string jsonStr)
  39. {
  40. //使用JsonConvert.DeserializeObject方法将json字符串转换为Dictionary
  41. Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonStr);
  42. return base.AsSugarClient().Insertable(dic).AS(tablename).ExecuteCommand();
  43. }
  44. public int DeleteIdList(string tablename,int[] ids)
  45. {
  46. return base.AsSugarClient().Deleteable<object>().AS(tablename).Where("Id in (@id) ", new { id = ids }).ExecuteCommand();//批量
  47. }
  48. public Records GetModelNav(int id)
  49. {
  50. return base.AsSugarClient().Queryable<Records>()
  51. //.Includes(m => m.ProductInfo.ToList(x => new Product() { Code = x.Code, Name = x.Name }))//,n=>n.ClassesInfo)
  52. //.Includes(m => m.ProductInfo)
  53. .Includes(m => m.DefectInfoList)
  54. .First(m => m.Id == id);
  55. }
  56. public List<Records> GetListNav(int pageNum, int pageSize, ref int totalCount, Expression<Func<Records, bool>> exp)
  57. {
  58. return base.AsSugarClient().Queryable<Records>()
  59. //.Includes(m => m.ProductInfo.ToList(x => new Product() {Code=x.Code, Name = x.Name }))//,n=>n.ClassesInfo)
  60. .WhereIF(exp!=null,exp)
  61. .OrderByDescending(m=>m.CreateTime)
  62. .ToPageList(pageNum, pageSize, ref totalCount);
  63. }
  64. public bool InsertNav(Models.Records model)
  65. {
  66. return base.AsSugarClient().InsertNav(model)
  67. .Include(m => m.DefectInfoList)
  68. .ExecuteCommand();
  69. }
  70. public bool DelNav(Models.Records model)
  71. {
  72. return base.AsSugarClient().DeleteNav(model)
  73. .Include(a => a.DefectInfoList)//.ThenInclude(z1 => z1.RoomList) //插入2层 Root->ShoolA->RoomList
  74. .ExecuteCommand();
  75. }
  76. //获取产品
  77. public List<Product> GetProductList()
  78. {
  79. var db = base.Change<Product>();//切换仓储(新功能)
  80. return db.AsSugarClient().Queryable<Product>()
  81. .OrderBy(x => x.Code)
  82. .ToList();
  83. }
  84. //分页
  85. //public List<Models.User> GetOrderPage(Expression<Func<Models.User, bool>> where, int pagesize, int pageindex)
  86. //{
  87. // return base.GetPageList(where, new SqlSugar.PageModel() { PageIndex = pageindex, PageSize = pagesize }); //使用自已的仓储方法
  88. //}
  89. //调用仓储扩展方法
  90. //public List<Models.User> GetOrderByJson(string Json)
  91. //{
  92. // return base.CommQuery(Json);
  93. //}
  94. /// <summary>
  95. /// 按日期统计合格率(日期段<=30,因只显示天)
  96. /// </summary>
  97. /// <returns></returns>
  98. public string GetReport_Qualified_Date(Expression<Func<Records, bool>> exp)
  99. {
  100. var sql = base.AsSugarClient().Queryable<Records>()
  101. .WhereIF(exp != null, exp);
  102. var sql2 = sql.GroupBy((a) => new { CreateTime = a.CreateTime.Date }) //DATE_FORMAT(NOW(), '%Y-%m-%d')
  103. .Select((a) => new { a.CreateTime.Date, Qualified = SqlFunc.AggregateSum(a.Qualified ? 1 : 0), Total = SqlFunc.AggregateSum(1) });
  104. //if (!string.IsNullOrWhiteSpace(orderby))
  105. // sql2 = sql2.OrderBy(orderby);
  106. return sql2.ToJson();
  107. }
  108. /// <summary>
  109. /// 合格率[[当日总量,当日合格量],[当月总量,当月合格量],[当季总量,当季合格量],[当年总量,当年合格量]]
  110. /// </summary>
  111. /// <param name="domain"></param>
  112. /// <returns></returns>
  113. public List<List<int>> GetReport_Qualified_Total(string barCode)
  114. {
  115. List<List<int>> result = new List<List<int>>();
  116. int liQualifiedCount, liTotal;
  117. DateTime now = DateTime.Now.AddDays(-1);
  118. //DateTime now = DateTime.Parse("2023-6-13");
  119. //当天
  120. //bool qualifiedGrade = 5;
  121. var sql = base.AsSugarClient().Queryable<Records>();//只能执行一次,不能复用
  122. liQualifiedCount = liTotal = 0;
  123. var list = sql.Where(m => m.CreateTime >= now.Date && !SqlFunc.EqualsNull(m.Qualified, null))
  124. .WhereIF(string.IsNullOrWhiteSpace(barCode), m => m.BarCode == barCode)
  125. .GroupBy(m => m.Qualified)
  126. .Select((a) => new { a.Qualified, Count = SqlFunc.AggregateCount(a.Id) })
  127. .ToList();
  128. foreach (var item in list)
  129. {
  130. if (item.Qualified)
  131. liQualifiedCount = item.Count;
  132. liTotal += item.Count;
  133. }
  134. //result.Add(liTotal == 0 ? 0 : (liQualifiedCount * 100 / liTotal));
  135. result.Add(new List<int> { liTotal, liQualifiedCount });
  136. //最近一月
  137. sql = base.AsSugarClient().Queryable<Records>();
  138. now = DateTime.Now.AddDays(-30);
  139. liQualifiedCount = liTotal = 0;
  140. list = sql.Where(m => m.CreateTime >= now.Date && !SqlFunc.EqualsNull(m.Qualified, null))
  141. .WhereIF(string.IsNullOrWhiteSpace(barCode), m => m.BarCode == barCode)
  142. .GroupBy(m => m.Qualified)
  143. .Select((a) => new { a.Qualified, Count = SqlFunc.AggregateCount(a.Id) })
  144. .ToList();
  145. foreach (var item in list)
  146. {
  147. if (item.Qualified)
  148. liQualifiedCount = item.Count;
  149. liTotal += item.Count;
  150. }
  151. //result.Add(liTotal == 0 ? 0 : (liQualifiedCount * 100 / liTotal));
  152. result.Add(new List<int> { liTotal, liQualifiedCount });
  153. //最近一季
  154. sql = base.AsSugarClient().Queryable<Records>();
  155. now = DateTime.Now.AddDays(-90);
  156. liQualifiedCount = liTotal = 0;
  157. list = sql.Where(m => m.CreateTime >= now.Date && !SqlFunc.EqualsNull(m.Qualified, null))
  158. .WhereIF(string.IsNullOrWhiteSpace(barCode), m => m.BarCode == barCode)
  159. .GroupBy(m => m.Qualified)
  160. .Select((a) => new { a.Qualified, Count = SqlFunc.AggregateCount(a.Id) })
  161. .ToList();
  162. foreach (var item in list)
  163. {
  164. if (item.Qualified)
  165. liQualifiedCount = item.Count;
  166. liTotal += item.Count;
  167. }
  168. //result.Add(liTotal == 0 ? 0 : (liQualifiedCount * 100 / liTotal));
  169. result.Add(new List<int> { liTotal, liQualifiedCount });
  170. //最近一年
  171. sql = base.AsSugarClient().Queryable<Records>();
  172. now = DateTime.Now.AddDays(-365);
  173. liQualifiedCount = liTotal = 0;
  174. list = sql.Where(m => m.CreateTime >= now.Date && !SqlFunc.EqualsNull(m.Qualified, null))
  175. .WhereIF(string.IsNullOrWhiteSpace(barCode), m => m.BarCode == barCode)
  176. .GroupBy(m => m.Qualified)
  177. .Select((a) => new { a.Qualified, Count = SqlFunc.AggregateCount(a.Id) })
  178. .ToList();
  179. foreach (var item in list)
  180. {
  181. if (item.Qualified)
  182. liQualifiedCount = item.Count;
  183. liTotal += item.Count;
  184. }
  185. //result.Add(liTotal == 0 ? 0 : (liQualifiedCount * 100 / liTotal));
  186. result.Add(new List<int> { liTotal, liQualifiedCount });
  187. return result;
  188. }
  189. /// <summary>
  190. /// 按日期统计各缺陷数
  191. /// </summary>
  192. /// <returns></returns>
  193. public string GetReport_Defects_Date(Expression<Func<Records, bool>> exp)
  194. {
  195. var sql = base.AsSugarClient().Queryable<Records>()
  196. .WhereIF(exp != null, exp);
  197. var sql2 = sql.GroupBy((a) => new { CreateTime = a.CreateTime.Date })
  198. .Select((a) => new {
  199. a.CreateTime.Date,
  200. Total = SqlFunc.AggregateSum(1),
  201. 堵孔 = SqlFunc.AggregateSum(a.DKCount),
  202. 脏污 = SqlFunc.AggregateSum(a.ZWCount),
  203. 钢丝异常 = SqlFunc.AggregateSum(a.GSYCCount),
  204. 纤维丝 = SqlFunc.AggregateSum(a.XWSCount),
  205. 缺口 = SqlFunc.AggregateSum(a.QKCount),
  206. 针孔 = SqlFunc.AggregateSum(a.ZKCount),
  207. 泡泡 = SqlFunc.AggregateSum(a.PPCount),
  208. 划伤 = SqlFunc.AggregateSum(a.HSCount),
  209. 压线 = SqlFunc.AggregateSum(a.YXCount),
  210. 斜边 = SqlFunc.AggregateSum(a.XBCount),
  211. 栅线 = SqlFunc.AggregateSum(a.SXCount),
  212. });
  213. return sql2.ToJson();
  214. }
  215. /// <summary>
  216. /// 缺陷总数
  217. /// </summary>
  218. /// <param name="domain"></param>
  219. /// <returns></returns>
  220. public string GetReport_Defects_Total(Expression<Func<Records, bool>> exp)
  221. {
  222. var sql = base.AsSugarClient().Queryable<Records>()
  223. .WhereIF(exp != null, exp);
  224. return sql.Select((a) => new
  225. {
  226. 堵孔 = SqlFunc.AggregateSum(a.DKCount),
  227. 脏污 = SqlFunc.AggregateSum(a.ZWCount),
  228. 钢丝异常 = SqlFunc.AggregateSum(a.GSYCCount),
  229. 纤维丝 = SqlFunc.AggregateSum(a.XWSCount),
  230. 缺口 = SqlFunc.AggregateSum(a.QKCount),
  231. 针孔 = SqlFunc.AggregateSum(a.ZKCount),
  232. 泡泡 = SqlFunc.AggregateSum(a.PPCount),
  233. 划伤 = SqlFunc.AggregateSum(a.HSCount),
  234. 压线 = SqlFunc.AggregateSum(a.YXCount),
  235. 斜边 = SqlFunc.AggregateSum(a.XBCount),
  236. 栅线 = SqlFunc.AggregateSum(a.SXCount),
  237. }).ToJson();
  238. }
  239. }
  240. }