StatisticsreportService.cs 15 KB

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