革博士程序V1仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
11 KiB

  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.DefectInfoList)
  53. .First(m => m.Id == id);
  54. }
  55. public List<Records> GetListNav(int pageNum, int pageSize, ref int totalCount, Expression<Func<Records, bool>> exp)
  56. {
  57. return base.AsSugarClient().Queryable<Records>()
  58. //.Includes(m => m.ProductInfo.ToList(x => new Product() {Code=x.Code, Name = x.Name }))//,n=>n.ClassesInfo)
  59. .WhereIF(exp!=null,exp)
  60. .OrderByDescending(m=>m.CreateTime)
  61. .ToPageList(pageNum, pageSize, ref totalCount);
  62. }
  63. public bool InsertNav(Models.Records model)
  64. {
  65. return base.AsSugarClient().InsertNav(model)
  66. .Include(m => m.DefectInfoList)
  67. .ExecuteCommand();
  68. }
  69. public bool DelNav(Models.Records model)
  70. {
  71. return base.AsSugarClient().DeleteNav(model)
  72. .Include(a => a.DefectInfoList)//.ThenInclude(z1 => z1.RoomList) //插入2层 Root->ShoolA->RoomList
  73. .ExecuteCommand();
  74. }
  75. //获取产品
  76. public List<Product> GetProductList()
  77. {
  78. var db = base.Change<Product>();//切换仓储(新功能)
  79. return db.AsSugarClient().Queryable<Product>()
  80. .OrderBy(x => x.Code)
  81. .ToList();
  82. }
  83. //分页
  84. //public List<Models.User> GetOrderPage(Expression<Func<Models.User, bool>> where, int pagesize, int pageindex)
  85. //{
  86. // return base.GetPageList(where, new SqlSugar.PageModel() { PageIndex = pageindex, PageSize = pagesize }); //使用自已的仓储方法
  87. //}
  88. //调用仓储扩展方法
  89. //public List<Models.User> GetOrderByJson(string Json)
  90. //{
  91. // return base.CommQuery(Json);
  92. //}
  93. /// <summary>
  94. /// 按日期统计合格率(日期段<=30,因只显示天)
  95. /// </summary>
  96. /// <returns></returns>
  97. public string GetReport_Qualified_Date(Expression<Func<Records, bool>> exp)
  98. {
  99. var sql = base.AsSugarClient().Queryable<Records>()
  100. .WhereIF(exp != null, exp);
  101. var sql2 = sql.GroupBy((a) => new { CreateTime = a.CreateTime.Date }) //DATE_FORMAT(NOW(), '%Y-%m-%d')
  102. .Select((a) => new { a.CreateTime.Date, Qualified = SqlFunc.AggregateSum(a.Qualified ? 1 : 0), Total = SqlFunc.AggregateSum(1) });
  103. //if (!string.IsNullOrWhiteSpace(orderby))
  104. // sql2 = sql2.OrderBy(orderby);
  105. return sql2.ToJson();
  106. }
  107. /// <summary>
  108. /// 合格率[[当日总量,当日合格量],[当月总量,当月合格量],[当季总量,当季合格量],[当年总量,当年合格量]]
  109. /// </summary>
  110. /// <param name="domain"></param>
  111. /// <returns></returns>
  112. public List<List<int>> GetReport_Qualified_Total(string barCode)
  113. {
  114. List<List<int>> result = new List<List<int>>();
  115. int liQualifiedCount, liTotal;
  116. DateTime now = DateTime.Now.AddDays(-1);
  117. //DateTime now = DateTime.Parse("2023-6-13");
  118. //当天
  119. //bool qualifiedGrade = 5;
  120. var sql = base.AsSugarClient().Queryable<Records>();//只能执行一次,不能复用
  121. liQualifiedCount = liTotal = 0;
  122. var list = sql.Where(m => m.CreateTime >= now.Date && !SqlFunc.EqualsNull(m.Qualified, null))
  123. .WhereIF(string.IsNullOrWhiteSpace(barCode), m => m.BarCode == barCode)
  124. .GroupBy(m => m.Qualified)
  125. .Select((a) => new { a.Qualified, Count = SqlFunc.AggregateCount(a.Id) })
  126. .ToList();
  127. foreach (var item in list)
  128. {
  129. if (item.Qualified)
  130. liQualifiedCount = item.Count;
  131. liTotal += item.Count;
  132. }
  133. //result.Add(liTotal == 0 ? 0 : (liQualifiedCount * 100 / liTotal));
  134. result.Add(new List<int> { liTotal, liQualifiedCount });
  135. //最近一月
  136. sql = base.AsSugarClient().Queryable<Records>();
  137. now = DateTime.Now.AddDays(-30);
  138. liQualifiedCount = liTotal = 0;
  139. list = sql.Where(m => m.CreateTime >= now.Date && !SqlFunc.EqualsNull(m.Qualified, null))
  140. .WhereIF(string.IsNullOrWhiteSpace(barCode), m => m.BarCode == barCode)
  141. .GroupBy(m => m.Qualified)
  142. .Select((a) => new { a.Qualified, Count = SqlFunc.AggregateCount(a.Id) })
  143. .ToList();
  144. foreach (var item in list)
  145. {
  146. if (item.Qualified)
  147. liQualifiedCount = item.Count;
  148. liTotal += item.Count;
  149. }
  150. //result.Add(liTotal == 0 ? 0 : (liQualifiedCount * 100 / liTotal));
  151. result.Add(new List<int> { liTotal, liQualifiedCount });
  152. //最近一季
  153. sql = base.AsSugarClient().Queryable<Records>();
  154. now = DateTime.Now.AddDays(-90);
  155. liQualifiedCount = liTotal = 0;
  156. list = sql.Where(m => m.CreateTime >= now.Date && !SqlFunc.EqualsNull(m.Qualified, null))
  157. .WhereIF(string.IsNullOrWhiteSpace(barCode), m => m.BarCode == barCode)
  158. .GroupBy(m => m.Qualified)
  159. .Select((a) => new { a.Qualified, Count = SqlFunc.AggregateCount(a.Id) })
  160. .ToList();
  161. foreach (var item in list)
  162. {
  163. if (item.Qualified)
  164. liQualifiedCount = item.Count;
  165. liTotal += item.Count;
  166. }
  167. //result.Add(liTotal == 0 ? 0 : (liQualifiedCount * 100 / liTotal));
  168. result.Add(new List<int> { liTotal, liQualifiedCount });
  169. //最近一年
  170. sql = base.AsSugarClient().Queryable<Records>();
  171. now = DateTime.Now.AddDays(-365);
  172. liQualifiedCount = liTotal = 0;
  173. list = sql.Where(m => m.CreateTime >= now.Date && !SqlFunc.EqualsNull(m.Qualified, null))
  174. .WhereIF(string.IsNullOrWhiteSpace(barCode), m => m.BarCode == barCode)
  175. .GroupBy(m => m.Qualified)
  176. .Select((a) => new { a.Qualified, Count = SqlFunc.AggregateCount(a.Id) })
  177. .ToList();
  178. foreach (var item in list)
  179. {
  180. if (item.Qualified)
  181. liQualifiedCount = item.Count;
  182. liTotal += item.Count;
  183. }
  184. //result.Add(liTotal == 0 ? 0 : (liQualifiedCount * 100 / liTotal));
  185. result.Add(new List<int> { liTotal, liQualifiedCount });
  186. return result;
  187. }
  188. /// <summary>
  189. /// 按日期统计各缺陷数
  190. /// </summary>
  191. /// <returns></returns>
  192. public string GetReport_Defects_Date(Expression<Func<Records, bool>> exp)
  193. {
  194. var sql = base.AsSugarClient().Queryable<Records>()
  195. .WhereIF(exp != null, exp);
  196. var sql2 = sql.GroupBy((a) => new { CreateTime = a.CreateTime.Date })
  197. .Select((a) => new {
  198. a.CreateTime.Date,
  199. Total = SqlFunc.AggregateSum(1),
  200. 堵孔 = SqlFunc.AggregateSum(a.DKCount),
  201. 脏污 = SqlFunc.AggregateSum(a.ZWCount),
  202. 钢丝异常 = SqlFunc.AggregateSum(a.GSYCCount),
  203. 纤维丝 = SqlFunc.AggregateSum(a.XWSCount),
  204. 缺口 = SqlFunc.AggregateSum(a.QKCount),
  205. 针孔 = SqlFunc.AggregateSum(a.ZKCount),
  206. 泡泡 = SqlFunc.AggregateSum(a.PPCount),
  207. 划伤 = SqlFunc.AggregateSum(a.HSCount),
  208. 压线 = SqlFunc.AggregateSum(a.YXCount),
  209. 斜边 = SqlFunc.AggregateSum(a.XBCount),
  210. 栅线 = SqlFunc.AggregateSum(a.SXCount),
  211. });
  212. return sql2.ToJson();
  213. }
  214. /// <summary>
  215. /// 缺陷总数
  216. /// </summary>
  217. /// <param name="domain"></param>
  218. /// <returns></returns>
  219. public string GetReport_Defects_Total(Expression<Func<Records, bool>> exp)
  220. {
  221. var sql = base.AsSugarClient().Queryable<Records>()
  222. .WhereIF(exp != null, exp);
  223. return sql.Select((a) => new
  224. {
  225. 堵孔 = SqlFunc.AggregateSum(a.DKCount),
  226. 脏污 = SqlFunc.AggregateSum(a.ZWCount),
  227. 钢丝异常 = SqlFunc.AggregateSum(a.GSYCCount),
  228. 纤维丝 = SqlFunc.AggregateSum(a.XWSCount),
  229. 缺口 = SqlFunc.AggregateSum(a.QKCount),
  230. 针孔 = SqlFunc.AggregateSum(a.ZKCount),
  231. 泡泡 = SqlFunc.AggregateSum(a.PPCount),
  232. 划伤 = SqlFunc.AggregateSum(a.HSCount),
  233. 压线 = SqlFunc.AggregateSum(a.YXCount),
  234. 斜边 = SqlFunc.AggregateSum(a.XBCount),
  235. 栅线 = SqlFunc.AggregateSum(a.SXCount),
  236. }).ToJson();
  237. }
  238. }
  239. }