StatisticsreportService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using wms.sqlsugar.model.pt;
  7. using WMS.BZModels;
  8. using WMS.BZModels.Dto.PT.ReportDtos;
  9. using WMS.Info;
  10. using WMS.BZSqlSugar;
  11. using System.Reflection;
  12. using WMS.BZModels.Dto;
  13. using WMS.Util;
  14. namespace WMS.BZServices.PT
  15. {
  16. public class StatisticsreportService
  17. {
  18. private readonly Repository<BillInvflow> _flowrepository;
  19. private readonly Repository<WCS_TaskOld> _taskoldrepository;
  20. public StatisticsreportService(Repository<BillInvflow> flowrepository, Repository<WCS_TaskOld> taskoldrepository)
  21. {
  22. _flowrepository = flowrepository;
  23. _taskoldrepository = taskoldrepository;
  24. }
  25. public PagedInfo<StatisticsDto> GetPageList(Pagination pagination, StatisticsQueryDto statisticsQueryDto)
  26. {
  27. //if (pagination.sord.ToUpper() != "ASC")
  28. //{
  29. // pagination.sidx = pagination.sidx + " DESC";
  30. //}
  31. //if (pagination.sidx.IsEmpty())
  32. //{
  33. // pagination.sidx = "AddTime DESC";
  34. //}
  35. //var predicate = Expressionable.Create<BillInvflow, WCS_TaskOld>();
  36. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.KeyWord), (flow, taskold) => flow.MatCode.Contains(statisticsQueryDto.KeyWord) || taskold.Floor.ToString() == statisticsQueryDto.KeyWord
  37. //|| taskold.Tunnel.Contains(statisticsQueryDto.KeyWord) || taskold.Device.Contains(statisticsQueryDto.KeyWord) || taskold.AddrTo.Contains(statisticsQueryDto.KeyWord) || taskold.AddrFrom.Contains(statisticsQueryDto.KeyWord));
  38. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.MatCode), (flow, taskold) => flow.MatCode.Contains(statisticsQueryDto.MatCode));
  39. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.Tunnel), (flow, taskold) => taskold.Tunnel.Contains(statisticsQueryDto.Tunnel));
  40. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.Floor), (flow, taskold) => taskold.Floor.ToString() == statisticsQueryDto.Floor);
  41. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.Device), (flow, taskold) => taskold.Device.Contains(statisticsQueryDto.Device));
  42. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.AddrFrom), (flow, taskold) => taskold.AddrFrom.Contains(statisticsQueryDto.AddrFrom));
  43. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.AddrTo), (flow, taskold) => taskold.AddrTo.Contains(statisticsQueryDto.AddrTo));
  44. var list = GetQueryable(statisticsQueryDto).ToPage(pagination);
  45. list.Result.ForEach(o =>
  46. {
  47. o.NetWQty = Math.Round(Convert.ToDecimal(o.NetWQty), 2, MidpointRounding.AwayFromZero);
  48. });
  49. return list;
  50. }
  51. public IList<StatisticsDto> GetList(StatisticsQueryDto statisticsQueryDto)
  52. {
  53. ISugarQueryable<StatisticsDto> sugarQueryable = GetQueryable(statisticsQueryDto);
  54. var list = sugarQueryable.ToList();
  55. list.ForEach(o =>
  56. {
  57. o.NetWQty = Math.Round(Convert.ToDecimal(o.NetWQty), 2, MidpointRounding.AwayFromZero);
  58. });
  59. return list;
  60. }
  61. private ISugarQueryable<StatisticsDto> GetQueryable(StatisticsQueryDto statisticsQueryDto)
  62. {
  63. var sugarQueryable = _taskoldrepository.Context.Queryable<WCS_TaskOld>().With(SqlWith.NoLock).SplitTable(p => p.Take(10))
  64. .LeftJoin<BaseMatinfo>((taskold, mater) => taskold.MaterialCode == mater.Code)
  65. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.Status), (taskold, mater) => taskold.Status. Equals(statisticsQueryDto.Status))
  66. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.KeyWord), (taskold, mater) => mater.Code.Contains(statisticsQueryDto.KeyWord) || mater.Name.Contains(statisticsQueryDto.KeyWord))
  67. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.MatCode), (taskold, mater) => mater.Code.Contains(statisticsQueryDto.MatCode))
  68. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.MatName), (taskold, mater) => mater.Name.Contains(statisticsQueryDto.MatName))
  69. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.AddTimeFrom != null && statisticsQueryDto.AddTimeFrom.HasValue, (taskold, mater) => taskold.AddTime >= statisticsQueryDto.AddTimeFrom)
  70. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.AddTimeTo != null && statisticsQueryDto.AddTimeTo.HasValue, (taskold, mater) => taskold.AddTime <= statisticsQueryDto.AddTimeTo)
  71. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.StartTimeBegin.HasValue, (taskold, mater) => taskold.StartTime >= statisticsQueryDto.StartTimeBegin)
  72. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.StartTimeEnd.HasValue, (taskold, mater) => taskold.StartTime <= statisticsQueryDto.StartTimeEnd)
  73. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.EndTimeBegin.HasValue, (taskold, mater) => taskold.EndTime >= statisticsQueryDto.EndTimeBegin)
  74. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.EndTimeEnd.HasValue, (taskold, mater) => taskold.EndTime <= statisticsQueryDto.EndTimeEnd)
  75. .GroupBy((taskold, mater) => new { mater.Code, mater.Name, taskold.Floor, taskold.Type })
  76. .Select((taskold, mater) => new StatisticsDto
  77. {
  78. MatCode = mater.Code,
  79. MatName = mater.Name,
  80. Floor = taskold.Floor,
  81. Type = taskold.Type,
  82. NetWQty = SqlFunc.AggregateSum(Convert.ToDecimal(taskold.Weight ?? 0.0)),
  83. Count = SqlFunc.AggregateDistinctCount(taskold.ID)
  84. }).MergeTable();
  85. return sugarQueryable;
  86. }
  87. /// <summary>
  88. /// 24小时内任务动态
  89. /// </summary>
  90. /// <param name="start"></param>
  91. /// <param name="end"></param>
  92. /// <returns></returns>
  93. public PagedInfo<HourTaskDto> GetHourTask(Pagination pagination, HourTaskQueryDto hourTaskQueryDto)
  94. {
  95. if (!hourTaskQueryDto.EndTimeBegin.HasValue)
  96. {
  97. hourTaskQueryDto.EndTimeBegin = DateTime.Now.Date;
  98. }
  99. if (!hourTaskQueryDto.EndTimeEnd.HasValue)
  100. {
  101. hourTaskQueryDto.EndTimeEnd = DateTime.Now.AddHours(1);
  102. }
  103. if ((hourTaskQueryDto.EndTimeEnd.Value - hourTaskQueryDto.EndTimeBegin.Value).Days > 60)
  104. {
  105. throw BZSysExCore.ThrowFailException("查询日期范围不能超过60天!");
  106. }
  107. var predicate = Expressionable.Create<WCS_TaskOld>();
  108. predicate = predicate.And(m => m.Status == (int)BZModels.TaskStatus.Finish);
  109. predicate = predicate.AndIF(!string.IsNullOrEmpty(hourTaskQueryDto.WarehouseCode), m => hourTaskQueryDto.WarehouseCode.Equals(m.WarehouseCode));
  110. predicate = predicate.AndIF(hourTaskQueryDto.EndTimeBegin.HasValue, m => m.EndTime >= hourTaskQueryDto.EndTimeBegin.Value);
  111. predicate = predicate.AndIF(hourTaskQueryDto.EndTimeEnd.HasValue, m => m.EndTime <= hourTaskQueryDto.EndTimeEnd.Value);
  112. predicate = predicate.AndIF(!string.IsNullOrEmpty(hourTaskQueryDto.TaskType) && (new string[] { "1", "2", "3", "5" }.Any(o => o.Equals(hourTaskQueryDto.TaskType))), m => m.Type.Equals(hourTaskQueryDto.TaskType));
  113. predicate = predicate.AndIF(!string.IsNullOrEmpty(hourTaskQueryDto.BusType), m => m.BusType.Equals(hourTaskQueryDto.BusType));
  114. var sugarQueryable = _taskoldrepository.Queryable().Select(it => new TaskInOutDto { });
  115. if (!string.IsNullOrEmpty(hourTaskQueryDto.TaskType) && (hourTaskQueryDto.TaskType == "100" || hourTaskQueryDto.TaskType == "101"))
  116. {
  117. sugarQueryable = _taskoldrepository.Queryable().Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(3))
  118. .Select(it => new
  119. {
  120. EditTime = it.EndTime.Value.Date,
  121. Hour = it.EndTime.Value.Hour
  122. })
  123. .MergeTable()//将查询结果转成一个表
  124. .GroupBy(o => new { o.Hour, o.EditTime })
  125. .Select(it => new TaskInOutDto { Count = SqlFunc.AggregateCount(it.Hour), Hour = it.Hour, CreateTime = it.EditTime })
  126. .OrderBy(o => o.CreateTime);
  127. }
  128. else
  129. {
  130. sugarQueryable = _taskoldrepository.Queryable().Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(3))
  131. .Select(it => new
  132. {
  133. Type = it.Type,
  134. EditTime = it.EndTime.Value.Date,
  135. Hour = it.EndTime.Value.Hour
  136. })
  137. .MergeTable()//将查询结果转成一个表
  138. .GroupBy(o => new { o.Type, o.Hour, o.EditTime })
  139. .Select(it => new TaskInOutDto { Count = SqlFunc.AggregateCount(it.Type), Type = it.Type, Hour = it.Hour, CreateTime = it.EditTime })
  140. .OrderBy(o => o.CreateTime);
  141. }
  142. var lists = sugarQueryable.ToList();
  143. PagedInfo<HourTaskDto> HourTasks = new PagedInfo<HourTaskDto>()
  144. {
  145. Result = new List<HourTaskDto>()
  146. };
  147. var plist = typeof(HourTaskDto);
  148. var types = lists.Select(o => o.Type).Distinct().OrderBy(o => o).ToList();
  149. var dates = lists.Select(o => o.CreateTime).Distinct().OrderBy(o => o).ToList();
  150. foreach (var p in dates)
  151. {
  152. if (!string.IsNullOrEmpty(hourTaskQueryDto.TaskType) && (hourTaskQueryDto.TaskType == "100" || hourTaskQueryDto.TaskType == "101"))
  153. {
  154. if (!lists.Any(o => o.CreateTime == p))
  155. {
  156. continue;
  157. }
  158. HourTaskDto s = Activator.CreateInstance<HourTaskDto>();
  159. var TaskDate = plist.GetProperty("TaskDate");
  160. if (TaskDate != null)
  161. TaskDate.SetValue(s, p.ToString("yyyy-MM-dd"), null);
  162. var TaskType = plist.GetProperty("TaskType");
  163. if (TaskType != null)
  164. TaskType.SetValue(s, -1, null);
  165. for (int i = 0; i < 24; i++)
  166. {
  167. PropertyInfo info = plist.GetProperty( "A" + i.ToString());
  168. if (info != null)
  169. {
  170. var first = lists.FirstOrDefault(o => o.CreateTime == p && o.Hour == i);
  171. if (first != null)
  172. {
  173. info.SetValue(s, first.Count, null);
  174. }
  175. }
  176. }
  177. HourTasks.Result.Add(s);
  178. }
  179. else
  180. {
  181. foreach (var t in types)
  182. {
  183. if (!lists.Any(o => o.CreateTime == p && o.Type == t))
  184. {
  185. continue;
  186. }
  187. HourTaskDto s = Activator.CreateInstance<HourTaskDto>();
  188. var TaskType = plist.GetProperty("TaskType");
  189. if (TaskType != null)
  190. TaskType.SetValue(s, (TaskType)t, null);
  191. var TaskDate = plist.GetProperty("TaskDate");
  192. if (TaskDate != null)
  193. TaskDate.SetValue(s, p.ToString("yyyy-MM-dd"), null);
  194. for (int i = 0; i < 24; i++)
  195. {
  196. PropertyInfo info = plist.GetProperty( "A" + i.ToString());
  197. if (info != null)
  198. {
  199. var first = lists.FirstOrDefault(o => o.Type == t && o.CreateTime == p && o.Hour == i);
  200. if (first != null)
  201. {
  202. info.SetValue(s, first.Count, null);
  203. }
  204. }
  205. }
  206. HourTasks.Result.Add(s);
  207. }
  208. }
  209. }
  210. if (!pagination.sidx.IsEmpty())
  211. {
  212. pagination.sidx = pagination.sidx.Replace("DESC", "").Replace("ASC", "");
  213. HourTasks.Result.Sort(
  214. delegate (HourTaskDto info1, HourTaskDto info2)
  215. {
  216. Type t = typeof(HourTaskDto);
  217. PropertyInfo pro = t.GetProperty(pagination.sidx);
  218. if (pagination.sidx == "TaskDate" || pagination.sidx == "TypeName")
  219. {
  220. return pagination.sord.ToLower().Contains("asc") ?
  221. pro.GetValue(info1, null).ToString().CompareTo(pro.GetValue(info2, null).ToString()) :
  222. pro.GetValue(info2, null).ToString().CompareTo(pro.GetValue(info1, null).ToString());
  223. }
  224. else
  225. return pagination.sord.ToLower().Contains("asc") ?
  226. pro.GetValue(info1, null).ToInt().CompareTo(pro.GetValue(info2, null).ToInt()) :
  227. pro.GetValue(info2, null).ToInt().CompareTo(pro.GetValue(info1, null).ToInt());
  228. });
  229. //typeof(HourTaskDto).GetProperty( pagination.sidx.Replace("DESC", "").Replace("ASC", ""))).ToList();
  230. }
  231. HourTasks.PageIndex = 1;
  232. HourTasks.PageSize = HourTasks.Result.Count;
  233. HourTasks.TotalNum = HourTasks.Result.Count;
  234. return HourTasks;
  235. }
  236. public PagedInfo<InOutReportDto> GetInOutReport(Pagination pagination, InOutReportQueryDto queryDto)
  237. {
  238. if (!queryDto.EndTimeBegin.HasValue)
  239. {
  240. queryDto.EndTimeBegin = DateTime.Now.Date;
  241. }
  242. if (!queryDto.EndTimeEnd.HasValue)
  243. {
  244. queryDto.EndTimeEnd = DateTime.Now.AddHours(1);
  245. }
  246. var predicate = Expressionable.Create<WCS_TaskOld>();
  247. predicate = predicate.And(m => m.Status == (int)BZModels.TaskStatus.Finish);
  248. predicate = predicate.And(m => new string[] { "1", "2", "5" }.Contains(m.Type.ToString()));
  249. predicate = predicate.AndIF(queryDto.EndTimeBegin.HasValue, m => m.EndTime >= queryDto.EndTimeBegin.Value);
  250. predicate = predicate.AndIF(queryDto.EndTimeEnd.HasValue, m => m.EndTime <= queryDto.EndTimeEnd.Value);
  251. ISugarQueryable<InOutReportDto> sugarQueryable;
  252. if (queryDto.GroupName == "Hour")
  253. {
  254. sugarQueryable = _taskoldrepository.Queryable().With(SqlWith.NoLock).Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(3))
  255. .GroupBy(it => new { it.EndTime.Value.Year, it.EndTime.Value.Month, it.EndTime.Value.Day, it.EndTime.Value.Hour })
  256. .Select(it => new InOutReportDto
  257. {
  258. Year = it.EndTime.Value.Year.ToString(),
  259. Month = it.EndTime.Value.Month.ToString(),
  260. Day = it.EndTime.Value.Day.ToString(),
  261. Hour = it.EndTime.Value.Hour.ToString(),
  262. InQty = SqlFunc.AggregateCount(SqlFunc.IF(it.Type == 1).Return(1).End<int>()),
  263. OutQty = SqlFunc.AggregateCount(SqlFunc.IF(it.Type == 2).Return(1).End<int>()),
  264. }).MergeTable();//.OrderBy(o => new { o.Year, o.Month, o.Day, o.Hour })
  265. }
  266. else
  267. {
  268. sugarQueryable = _taskoldrepository.Queryable().With(SqlWith.NoLock).Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(3))
  269. .GroupBy(it => new { it.EndTime.Value.Year, it.EndTime.Value.Month, it.EndTime.Value.Day })
  270. .Select(it => new InOutReportDto
  271. {
  272. Year = it.EndTime.Value.Year.ToString(),
  273. Month = it.EndTime.Value.Month.ToString(),
  274. Day = it.EndTime.Value.Day.ToString(),
  275. InQty = SqlFunc.AggregateCount(SqlFunc.IF(it.Type == 1).Return(1).End<int>()),
  276. OutQty = SqlFunc.AggregateCount(SqlFunc.IF(it.Type == 2).Return(1).End<int>()),
  277. }).MergeTable(); //.OrderBy(o => new { o.Year, o.Month, o.Day })
  278. if (pagination.sidx == "Hour")
  279. pagination.sidx = "Day";
  280. }
  281. var lists = sugarQueryable.ToPage(pagination);
  282. lists.Result.Add(new InOutReportDto
  283. {
  284. Year = "合计:",
  285. InQty = lists.Result.Sum(o => o.InQty),
  286. OutQty = lists.Result.Sum(o => o.OutQty),
  287. });
  288. return lists;
  289. }
  290. }
  291. }