StatisticsreportService.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using WMS.BZModels;
  7. using WMS.Info;
  8. using wms.sqlsugar.model.sx;
  9. using WMS.BZSqlSugar;
  10. using WMS.BZModels.Dto.SX.ReportDtos;
  11. using System.Reflection;
  12. using WMS.BZModels.Dto;
  13. using WMS.Util;
  14. using NPOI.Util;
  15. namespace WMS.BZServices.SX
  16. {
  17. public class StatisticsreportService
  18. {
  19. private readonly Repository<BillInvflow> _flowrepository;
  20. private readonly Repository<WCS_TaskOld> _taskoldrepository;
  21. private readonly Repository<WCSTaskDtl> _taskdtlrepository;
  22. private readonly Repository<Palletizing> _palletizingrepository;
  23. private readonly Repository<BillInvnow> _billInvnowRepository;
  24. private readonly Repository<BillPboxrule> _billPboxruleRepository;
  25. public StatisticsreportService(Repository<BillInvflow> flowrepository, Repository<WCS_TaskOld> taskoldrepository, Repository<WCSTaskDtl> taskdtlrepository, Repository<Palletizing> palletizingrepository, Repository<BillInvnow> billInvnowRepository, Repository<BillPboxrule> billPboxruleRepository)
  26. {
  27. _flowrepository = flowrepository;
  28. _taskoldrepository = taskoldrepository;
  29. _taskdtlrepository = taskdtlrepository;
  30. _palletizingrepository = palletizingrepository;
  31. _billInvnowRepository = billInvnowRepository;
  32. _billPboxruleRepository = billPboxruleRepository;
  33. }
  34. public PagedInfo<StatisticsDto> GetPageList(Pagination pagination, StatisticsQueryDto statisticsQueryDto)
  35. {
  36. //if (pagination.sord.ToUpper() != "ASC")
  37. //{
  38. // pagination.sidx = pagination.sidx + " DESC";
  39. //}
  40. //if (pagination.sidx.IsEmpty())
  41. //{
  42. // pagination.sidx = "AddTime DESC";
  43. //}
  44. //var predicate = Expressionable.Create<BillInvflow, WCS_TaskOld>();
  45. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.KeyWord), (flow, taskold) => flow.MatCode.Contains(statisticsQueryDto.KeyWord) || taskold.Floor.ToString() == statisticsQueryDto.KeyWord
  46. //|| taskold.Tunnel.Contains(statisticsQueryDto.KeyWord) || taskold.Device.Contains(statisticsQueryDto.KeyWord) || taskold.AddrTo.Contains(statisticsQueryDto.KeyWord) || taskold.AddrFrom.Contains(statisticsQueryDto.KeyWord));
  47. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.MatCode), (flow, taskold) => flow.MatCode.Contains(statisticsQueryDto.MatCode));
  48. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.Tunnel), (flow, taskold) => taskold.Tunnel.Contains(statisticsQueryDto.Tunnel));
  49. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.Floor), (flow, taskold) => taskold.Floor.ToString() == statisticsQueryDto.Floor);
  50. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.Device), (flow, taskold) => taskold.Device.Contains(statisticsQueryDto.Device));
  51. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.AddrFrom), (flow, taskold) => taskold.AddrFrom.Contains(statisticsQueryDto.AddrFrom));
  52. //predicate = predicate.AndIF(!string.IsNullOrEmpty(statisticsQueryDto?.AddrTo), (flow, taskold) => taskold.AddrTo.Contains(statisticsQueryDto.AddrTo));
  53. var list = GetQueryable(statisticsQueryDto).ToPage(pagination);
  54. return list;
  55. }
  56. public PagedInfo<StatisticsDto> GetBGradeStatisticesPageList(Pagination pagination, StatisticsQueryDto statisticsQueryDto)
  57. {
  58. var list = GetBGradeStatisticesQueryable(statisticsQueryDto).ToPage(pagination);
  59. return list;
  60. }
  61. public IList<StatisticsDto> GetList(StatisticsQueryDto statisticsQueryDto)
  62. {
  63. ISugarQueryable<StatisticsDto> sugarQueryable = GetQueryable(statisticsQueryDto);
  64. var list = sugarQueryable.ToList();
  65. return list;
  66. }
  67. private ISugarQueryable<StatisticsDto> GetQueryable(StatisticsQueryDto statisticsQueryDto)
  68. {
  69. var sugarQueryable = _taskoldrepository.Context.Queryable<WCS_TaskOld>().With(SqlWith.NoLock).SplitTable(p => p.Take(10))
  70. .LeftJoin<BaseMatinfo>((taskold, mater) => taskold.MaterialCode == mater.Code)
  71. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.Status), (taskold, mater) => taskold.Status.Equals(statisticsQueryDto.Status))
  72. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.KeyWord), (taskold, mater) => mater.Code.Contains(statisticsQueryDto.KeyWord) || mater.Name.Contains(statisticsQueryDto.KeyWord))
  73. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.MatCode), (taskold, mater) => mater.Code.Contains(statisticsQueryDto.MatCode))
  74. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.MatName), (taskold, mater) => mater.Name.Contains(statisticsQueryDto.MatName))
  75. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.AddTimeFrom != null && statisticsQueryDto.AddTimeFrom.HasValue, (taskold, mater) => taskold.AddTime >= statisticsQueryDto.AddTimeFrom)
  76. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.AddTimeTo != null && statisticsQueryDto.AddTimeTo.HasValue, (taskold, mater) => taskold.AddTime <= statisticsQueryDto.AddTimeTo)
  77. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.StartTimeBegin.HasValue, (taskold, mater) => taskold.StartTime >= statisticsQueryDto.StartTimeBegin)
  78. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.StartTimeEnd.HasValue, (taskold, mater) => taskold.StartTime <= statisticsQueryDto.StartTimeEnd)
  79. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.EndTimeBegin.HasValue, (taskold, mater) => taskold.EndTime >= statisticsQueryDto.EndTimeBegin)
  80. .WhereIF(statisticsQueryDto != null && statisticsQueryDto.EndTimeEnd.HasValue, (taskold, mater) => taskold.EndTime <= statisticsQueryDto.EndTimeEnd)
  81. .GroupBy((taskold, mater) => new { mater.Code, mater.Name, taskold.Floor, taskold.Type })
  82. .Select((taskold, mater) => new StatisticsDto
  83. {
  84. MatCode = mater.Code,
  85. MatName = mater.Name,
  86. Floor = taskold.Floor,
  87. Type = taskold.Type,
  88. Count = SqlFunc.AggregateDistinctCount(taskold.ID)
  89. }).MergeTable();
  90. return sugarQueryable;
  91. }
  92. private ISugarQueryable<StatisticsDto> GetBGradeStatisticesQueryable(StatisticsQueryDto statisticsQueryDto)
  93. {
  94. var sugarQueryable = _billInvnowRepository.Context.Queryable<BillInvnow>().With(SqlWith.NoLock)
  95. .Where(p=> p.InvStateCode == "InvEcecState_In" && p.Grade == "B" && string.IsNullOrEmpty(p.PreStock) && (DateTime.Now - p.ProductTime).TotalHours >= 72 && (DateTime.Now - p.ProductTime).TotalHours < 120)
  96. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.KeyWord), (p) => p.MatCode.Contains(statisticsQueryDto.KeyWord) || p.MatName.Contains(statisticsQueryDto.KeyWord))
  97. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.MatCode), (p) => p.MatCode.Contains(statisticsQueryDto.MatCode))
  98. .WhereIF(statisticsQueryDto != null && !string.IsNullOrWhiteSpace(statisticsQueryDto.MatName), (p) => p.MatName.Contains(statisticsQueryDto.MatName))
  99. //.WhereIF(statisticsQueryDto != null && statisticsQueryDto.AddTimeFrom != null && statisticsQueryDto.AddTimeFrom.HasValue, (p) => taskold.AddTime >= statisticsQueryDto.AddTimeFrom)
  100. //.WhereIF(statisticsQueryDto != null && statisticsQueryDto.AddTimeTo != null && statisticsQueryDto.AddTimeTo.HasValue, (p) => taskold.AddTime <= statisticsQueryDto.AddTimeTo)
  101. //.WhereIF(statisticsQueryDto != null && statisticsQueryDto.StartTimeBegin.HasValue, (p) => taskold.StartTime >= statisticsQueryDto.StartTimeBegin)
  102. //.WhereIF(statisticsQueryDto != null && statisticsQueryDto.StartTimeEnd.HasValue, (p) => taskold.StartTime <= statisticsQueryDto.StartTimeEnd)
  103. //.WhereIF(statisticsQueryDto != null && statisticsQueryDto.EndTimeBegin.HasValue, (p) => taskold.EndTime >= statisticsQueryDto.EndTimeBegin)
  104. //.WhereIF(statisticsQueryDto != null && statisticsQueryDto.EndTimeEnd.HasValue, (p) => taskold.EndTime <= statisticsQueryDto.EndTimeEnd)
  105. .GroupBy((p) => new { p.MatCode, p.MatName, p.HWTypeCode , p.Wind })
  106. .Select((p) => new StatisticsDto
  107. {
  108. MatCode = p.MatCode,
  109. MatName = p.MatName,
  110. HwType = p.HWTypeCode,
  111. Wind = p.Wind,
  112. Count = SqlFunc.AggregateDistinctCount(p.Id)
  113. }).MergeTable();
  114. return sugarQueryable;
  115. }
  116. /// <summary>
  117. /// 24小时内任务动态
  118. /// </summary>
  119. /// <param name="start"></param>
  120. /// <param name="end"></param>
  121. /// <returns></returns>
  122. public PagedInfo<HourTaskDto> GetHourTask(Pagination pagination, HourTaskQueryDto hourTaskQueryDto)
  123. {
  124. if (!hourTaskQueryDto.EndTimeBegin.HasValue)
  125. {
  126. hourTaskQueryDto.EndTimeBegin = DateTime.Now.Date;
  127. }
  128. if (!hourTaskQueryDto.EndTimeEnd.HasValue)
  129. {
  130. hourTaskQueryDto.EndTimeEnd = DateTime.Now.AddHours(1);
  131. }
  132. if ((hourTaskQueryDto.EndTimeEnd.Value - hourTaskQueryDto.EndTimeBegin.Value).Days > 60)
  133. {
  134. throw BZSysExCore.ThrowFailException("查询日期范围不能超过60天!");
  135. }
  136. var predicate = Expressionable.Create<WCS_TaskOld>();
  137. predicate = predicate.And(m => m.Status == (int)BZModels.TaskStatus.Finish);
  138. predicate = predicate.AndIF(!string.IsNullOrEmpty(hourTaskQueryDto.WarehouseCode), m => hourTaskQueryDto.WarehouseCode.Equals(m.WarehouseCode));
  139. predicate = predicate.AndIF(hourTaskQueryDto.EndTimeBegin.HasValue, m => m.EditTime >= hourTaskQueryDto.EndTimeBegin.Value);
  140. predicate = predicate.AndIF(hourTaskQueryDto.EndTimeEnd.HasValue, m => m.EditTime <= hourTaskQueryDto.EndTimeEnd.Value);
  141. predicate = predicate.AndIF(!string.IsNullOrEmpty(hourTaskQueryDto.TaskType) && (new string[] { "1", "2", "3" }.Any(o => o.Equals(hourTaskQueryDto.TaskType))), m => m.Type.Equals(hourTaskQueryDto.TaskType));
  142. predicate = predicate.AndIF(!string.IsNullOrEmpty(hourTaskQueryDto.BusType), m => m.BusType.Equals(hourTaskQueryDto.BusType));
  143. var sugarQueryable = _taskoldrepository.Queryable().With(SqlWith.NoLock).Select(it => new TaskInOutDto { });
  144. if (!string.IsNullOrEmpty(hourTaskQueryDto.TaskType) && (hourTaskQueryDto.TaskType == "100" || hourTaskQueryDto.TaskType == "101"))
  145. {
  146. sugarQueryable = _taskoldrepository.Queryable().With(SqlWith.NoLock).Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(3))
  147. .Select(it => new
  148. {
  149. EditTime = it.EditTime.Date,
  150. Hour = it.EditTime.Hour
  151. })
  152. .MergeTable()//将查询结果转成一个表
  153. .GroupBy(o => new { o.Hour, o.EditTime })
  154. .Select(it => new TaskInOutDto { Count = SqlFunc.AggregateCount(it.Hour), Hour = it.Hour, CreateTime = it.EditTime })
  155. .OrderBy(o => o.CreateTime);
  156. }
  157. else
  158. {
  159. sugarQueryable = _taskoldrepository.Queryable().With(SqlWith.NoLock).Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(3))
  160. .Select(it => new
  161. {
  162. Type = it.Type,
  163. EditTime = it.EditTime.Date,
  164. Hour = it.EditTime.Hour
  165. })
  166. .MergeTable()//将查询结果转成一个表
  167. .GroupBy(o => new { o.Type, o.Hour, o.EditTime })
  168. .Select(it => new TaskInOutDto { Count = SqlFunc.AggregateCount(it.Type), Type = it.Type, Hour = it.Hour, CreateTime = it.EditTime })
  169. .OrderBy(o => o.CreateTime);
  170. }
  171. var lists = sugarQueryable.ToList();
  172. PagedInfo<HourTaskDto> HourTasks = new PagedInfo<HourTaskDto>()
  173. {
  174. Result = new List<HourTaskDto>()
  175. };
  176. //var plist = new List<PropertyInfo>(typeof(HourTaskDto).GetProperties());
  177. var plist = typeof(HourTaskDto);
  178. var types = lists.Select(o => o.Type).Distinct().OrderBy(o => o).ToList();
  179. var dates = lists.Select(o => o.CreateTime).Distinct().OrderBy(o => o).ToList();
  180. foreach (var p in dates)
  181. {
  182. if (!string.IsNullOrEmpty(hourTaskQueryDto.TaskType) && (hourTaskQueryDto.TaskType == "100" || hourTaskQueryDto.TaskType == "101"))
  183. {
  184. if (!lists.Any(o => o.CreateTime == p))
  185. {
  186. continue;
  187. }
  188. HourTaskDto s = Activator.CreateInstance<HourTaskDto>();
  189. var TaskDate = plist.GetProperty("TaskDate");
  190. //var TaskDate = plist.Find(p => p.Name == "TaskDate");
  191. if (TaskDate != null)
  192. TaskDate.SetValue(s, p.ToString("yyyy-MM-dd"), null);
  193. var TaskType = plist.GetProperty("TaskType");
  194. if (TaskType != null)
  195. TaskType.SetValue(s, -1, null);
  196. for (int i = 0; i < 24; i++)
  197. {
  198. PropertyInfo info = plist.GetProperty("A" + i.ToString());
  199. if (info != null)
  200. {
  201. var first = lists.FirstOrDefault(o => o.CreateTime == p && o.Hour == i);
  202. if (first != null)
  203. {
  204. info.SetValue(s, first.Count, null);
  205. }
  206. }
  207. }
  208. HourTasks.Result.Add(s);
  209. }
  210. else
  211. {
  212. foreach (var t in types)
  213. {
  214. if (!lists.Any(o => o.CreateTime == p && o.Type == t))
  215. {
  216. continue;
  217. }
  218. HourTaskDto s = Activator.CreateInstance<HourTaskDto>();
  219. var TaskType = plist.GetProperty("TaskType");
  220. if (TaskType != null)
  221. TaskType.SetValue(s, (BZModels.TaskType)t, null);
  222. var TaskDate = plist.GetProperty("TaskDate");
  223. if (TaskDate != null)
  224. TaskDate.SetValue(s, p.ToString("yyyy-MM-dd"), null);
  225. for (int i = 0; i < 24; i++)
  226. {
  227. PropertyInfo info = plist.GetProperty("A" + i.ToString());
  228. if (info != null)
  229. {
  230. var first = lists.FirstOrDefault(o => o.Type == t && o.CreateTime == p && o.Hour == i);
  231. if (first != null)
  232. {
  233. info.SetValue(s, first.Count, null);
  234. }
  235. }
  236. }
  237. HourTasks.Result.Add(s);
  238. }
  239. }
  240. }
  241. if (!pagination.sidx.IsEmpty())
  242. {
  243. pagination.sidx = pagination.sidx.Replace("DESC", "").Replace("ASC", "");
  244. HourTasks.Result.Sort(
  245. delegate (HourTaskDto info1, HourTaskDto info2)
  246. {
  247. Type t = typeof(HourTaskDto);
  248. PropertyInfo pro = t.GetProperty(pagination.sidx);
  249. if (pagination.sidx == "TaskDate" || pagination.sidx == "TypeName")
  250. {
  251. return pagination.sord.ToLower().Contains("asc") ?
  252. pro.GetValue(info1, null).ToString().CompareTo(pro.GetValue(info2, null).ToString()) :
  253. pro.GetValue(info2, null).ToString().CompareTo(pro.GetValue(info1, null).ToString());
  254. }
  255. else
  256. return pagination.sord.ToLower().Contains("asc") ?
  257. pro.GetValue(info1, null).ToInt().CompareTo(pro.GetValue(info2, null).ToInt()) :
  258. pro.GetValue(info2, null).ToInt().CompareTo(pro.GetValue(info1, null).ToInt());
  259. });
  260. //typeof(HourTaskDto).GetProperty( pagination.sidx.Replace("DESC", "").Replace("ASC", ""))).ToList();
  261. }
  262. HourTasks.PageIndex = 1;
  263. HourTasks.PageSize = HourTasks.Result.Count;
  264. HourTasks.TotalNum = HourTasks.Result.Count;
  265. return HourTasks;
  266. }
  267. /// <summary>
  268. /// 码垛数
  269. /// </summary>
  270. /// <param name="pagination"></param>
  271. /// <param name="QueryDto"></param>
  272. /// <returns></returns>
  273. public PagedInfo<PalletizingReportDto> GetPalletizingReport(Pagination pagination, PalletizingReportQueryDto QueryDto)
  274. {
  275. if (!QueryDto.EndTimeBegin.HasValue)
  276. {
  277. QueryDto.EndTimeBegin = DateTime.Now.Date;
  278. }
  279. if (!QueryDto.EndTimeEnd.HasValue)
  280. {
  281. QueryDto.EndTimeEnd = DateTime.Now.AddHours(1);
  282. }
  283. var predicate = Expressionable.Create<Palletizing>();
  284. predicate = predicate.And(m => m.Finish != 1 && !new string[] { "Special", "OnePack" }.Contains(m.BoxRule));
  285. predicate = predicate.And(m => m.PalletizState == 1);
  286. predicate = predicate.AndIF(QueryDto.EndTimeBegin.HasValue, m => m.AddTime >= QueryDto.EndTimeBegin.Value);
  287. predicate = predicate.AndIF(QueryDto.EndTimeEnd.HasValue, m => m.AddTime <= QueryDto.EndTimeEnd.Value);
  288. ISugarQueryable<PalletizingReportDto> sugarQueryable;
  289. if (QueryDto.GroupName == "Hour")
  290. {
  291. sugarQueryable = _palletizingrepository.Queryable().With(SqlWith.NoLock).Where(predicate.ToExpression())
  292. .GroupBy(it => new { it.EditTime.Year, it.EditTime.Month, it.EditTime.Day, it.EditTime.Hour })
  293. .Select(it => new PalletizingReportDto
  294. {
  295. Year = it.EditTime.Year.ToString(),
  296. Month = it.EditTime.Month.ToString(),
  297. Day = it.EditTime.Day.ToString(),
  298. Hour = it.EditTime.Hour.ToString(),
  299. BigQty = SqlFunc.AggregateCount(SqlFunc.IF(it.GoodsType == 1).Return(1).End<int>()),
  300. SmallQty = SqlFunc.AggregateCount(SqlFunc.IF(it.GoodsType == 2).Return(1).End<int>()),
  301. TotalQty = SqlFunc.AggregateCount(1),
  302. }).MergeTable();
  303. }
  304. else
  305. {
  306. sugarQueryable = _palletizingrepository.Queryable().With(SqlWith.NoLock).With(SqlWith.NoLock).Where(predicate.ToExpression())
  307. .GroupBy(it => new { it.EditTime.Year, it.EditTime.Month, it.EditTime.Day })
  308. .Select(it => new PalletizingReportDto
  309. {
  310. Year = it.EditTime.Year.ToString(),
  311. Month = it.EditTime.Month.ToString(),
  312. Day = it.EditTime.Day.ToString(),
  313. BigQty = SqlFunc.AggregateCount(SqlFunc.IF(it.GoodsType == 1).Return(1).End<int>()),
  314. SmallQty = SqlFunc.AggregateCount(SqlFunc.IF(it.GoodsType == 2).Return(1).End<int>()),
  315. TotalQty = SqlFunc.AggregateCount(1),
  316. }).MergeTable();
  317. if (pagination.sidx == "Hour")
  318. pagination.sidx = "Day";
  319. }
  320. var lists = sugarQueryable.ToPage(pagination);
  321. lists.Result.Add(new PalletizingReportDto
  322. {
  323. Year = "合计:",
  324. BigQty = lists.Result.Sum(o => o.BigQty),
  325. SmallQty = lists.Result.Sum(o => o.SmallQty),
  326. TotalQty = lists.Result.Sum(o => o.TotalQty)
  327. });
  328. return lists;
  329. }
  330. public PagedInfo<InOutReportDto> GetInOutReport(Pagination pagination, InOutReportQueryDto queryDto)
  331. {
  332. if (!queryDto.EndTimeBegin.HasValue)
  333. {
  334. queryDto.EndTimeBegin = DateTime.Now.Date;
  335. }
  336. if (!queryDto.EndTimeEnd.HasValue)
  337. {
  338. queryDto.EndTimeEnd = DateTime.Now.AddHours(1);
  339. }
  340. var predicate = Expressionable.Create<WCS_TaskOld, WCSTaskDtl>();
  341. predicate = predicate.And((taskold, dtl) => taskold.Status >= (int)BZModels.TaskStatus.Finish);
  342. predicate = predicate.And((taskold, dtl) => new string[] { "1", "2" }.Contains(taskold.Type.ToString()));
  343. predicate = predicate.AndIF(queryDto.EndTimeBegin.HasValue, (taskold, dtl) => dtl.AddTime >= queryDto.EndTimeBegin.Value);
  344. predicate = predicate.AndIF(queryDto.EndTimeEnd.HasValue, (taskold, dtl) => dtl.AddTime <= queryDto.EndTimeEnd.Value);
  345. ISugarQueryable<InOutReportDto> sugarQueryable;
  346. var joinq = _taskdtlrepository.Queryable().With(SqlWith.NoLock).SplitTable(tabs => tabs.Take(3));
  347. if (queryDto.GroupName == "Hour")
  348. {
  349. sugarQueryable = _taskoldrepository.Queryable().With(SqlWith.NoLock).SplitTable(tabs => tabs.Take(3))
  350. .LeftJoin(joinq, (taskold, dtl) => taskold.ID == dtl.ParentTaskCode).Where(predicate.ToExpression())
  351. .GroupBy((taskold, dtl) => new { dtl.AddTime.Year, dtl.AddTime.Month, dtl.AddTime.Day, dtl.AddTime.Hour })
  352. .Select((taskold, dtl) => new InOutReportDto
  353. {
  354. Year = dtl.AddTime.Year.ToString(),
  355. Month = dtl.AddTime.Month.ToString(),
  356. Day = dtl.AddTime.Day.ToString(),
  357. Hour = dtl.AddTime.Hour.ToString(),
  358. CordInQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 1 && dtl.Desc == "帘线扫码" && taskold.Status >= 99).Return(taskold.ID).End<int>()),
  359. TwistInQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 1 && dtl.Desc == "扭转回库").Return(taskold.ID).End<int>()),
  360. StackingTaskQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(dtl.Desc == "码垛抓取完成").Return(taskold.ID).End<int>()),
  361. TorsionTaskQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 2 && dtl.Desc == "扭转检测").Return(taskold.ID).End<int>()),
  362. TotalInQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 1 && dtl.Desc == "帘线扫码" && taskold.Status >= 99).Return(taskold.ID).End<int>()) + SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 1 && dtl.Desc == "扭转回库").Return(taskold.ID).End<int>()),
  363. TotalOutQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(dtl.Desc == "码垛抓取完成").Return(taskold.ID).End<int>()) + SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 2 && dtl.Desc == "扭转检测").Return(taskold.ID).End<int>()),
  364. }).MergeTable();//.OrderBy(o => new { o.Year, o.Month, o.Day, o.Hour })
  365. }
  366. else
  367. {
  368. sugarQueryable = _taskoldrepository.Queryable().With(SqlWith.NoLock).SplitTable(tabs => tabs.Take(3))
  369. .LeftJoin(joinq, (taskold, dtl) => taskold.ID == dtl.ParentTaskCode).Where(predicate.ToExpression())
  370. .GroupBy((taskold, dtl) => new { dtl.AddTime.Year, dtl.AddTime.Month, dtl.AddTime.Day })
  371. .Select((taskold, dtl) => new InOutReportDto
  372. {
  373. Year = dtl.AddTime.Year.ToString(),
  374. Month = dtl.AddTime.Month.ToString(),
  375. Day = dtl.AddTime.Day.ToString(),
  376. CordInQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 1 && dtl.Desc == "帘线扫码" && taskold.Status >= 99).Return(taskold.ID).End<int>()),
  377. TwistInQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 1 && dtl.Desc == "扭转回库").Return(taskold.ID).End<int>()),
  378. StackingTaskQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(dtl.Desc == "码垛抓取完成").Return(taskold.ID).End<int>()),
  379. TorsionTaskQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 2 && dtl.Desc == "扭转检测").Return(taskold.ID).End<int>()),
  380. TotalInQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 1 && dtl.Desc == "帘线扫码" && taskold.Status >= 99).Return(taskold.ID).End<int>()) + SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 1 && dtl.Desc == "扭转回库").Return(taskold.ID).End<int>()),
  381. TotalOutQty = SqlFunc.AggregateDistinctCount(SqlFunc.IF(dtl.Desc == "码垛抓取完成").Return(taskold.ID).End<int>()) + SqlFunc.AggregateDistinctCount(SqlFunc.IF(taskold.Type == 2 && dtl.Desc == "扭转检测").Return(taskold.ID).End<int>()),
  382. }).MergeTable();//.OrderBy(o => new { o.Year, o.Month, o.Day, o.Hour })
  383. if (pagination.sidx == "Hour")
  384. pagination.sidx = "Day";
  385. }
  386. var lists = sugarQueryable.ToPage(pagination);
  387. lists.Result.Add(new InOutReportDto
  388. {
  389. Year = "合计:",
  390. CordInQty = lists.Result.Sum(o => o.CordInQty),
  391. TwistInQty = lists.Result.Sum(o => o.TwistInQty),
  392. StackingTaskQty = lists.Result.Sum(o => o.StackingTaskQty),
  393. TorsionTaskQty = lists.Result.Sum(o => o.TorsionTaskQty),
  394. TotalInQty = lists.Result.Sum(o => o.TotalInQty),
  395. TotalOutQty = lists.Result.Sum(o => o.TotalOutQty),
  396. });
  397. return lists;
  398. }
  399. public PagedInfo<CurStockInfo> GetCurStockInfo(Pagination pagination, CurStockInfoQueryDto queryDto)
  400. {
  401. var predicate = Expressionable.Create<BillInvnow>();
  402. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.KeyWord), (billInvnow) => billInvnow.MatCode.Contains(queryDto.KeyWord)
  403. || billInvnow.MatName.Contains(queryDto.KeyWord) || billInvnow.InDocsNo.Contains(queryDto.KeyWord) || billInvnow.ContGrpBarCode.Contains(queryDto.KeyWord));
  404. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.MatName), (billInvnow) => billInvnow.MatName.Contains(queryDto.MatName));
  405. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.MatCode), (billInvnow) => billInvnow.MatCode.Contains(queryDto.MatCode));
  406. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.DocNo), (billInvnow) => billInvnow.InDocsNo.Contains(queryDto.DocNo));
  407. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.WbGroupCode), (billInvnow) => billInvnow.WbGroupCode.Contains(queryDto.WbGroupCode));
  408. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.Wind), (billInvnow) => billInvnow.Wind.Contains(queryDto.Wind));
  409. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.HWType), (billInvnow) => billInvnow.HWTypeCode.Contains(queryDto.HWType));
  410. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.IsBlack), (billInvnow) => billInvnow.IsBlack.Equals(queryDto.IsBlack));
  411. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.Grade), (billInvnow) => billInvnow.Grade.Contains(queryDto.Grade));
  412. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.ContGrpBarCode), (billInvnow) => billInvnow.ContGrpBarCode.Contains(queryDto.ContGrpBarCode));
  413. predicate = predicate.AndIF(queryDto != null && queryDto.ProductTimeFrom.HasValue, (billInvnow) => billInvnow.ProductTime >= queryDto.ProductTimeFrom);
  414. predicate = predicate.AndIF(queryDto != null && queryDto.ProductTimeTo.HasValue, (billInvnow) => billInvnow.ProductTime <= queryDto.ProductTimeTo);
  415. var CurStockInfoLists = _billInvnowRepository.Context.Queryable<BillInvnow>()
  416. .With(SqlWith.NoLock)
  417. .Where((billInvnow) => billInvnow.InvStateCode == SXInvState.InvEcecState_In.ToString() && string.IsNullOrEmpty(billInvnow.PreStock) && ((billInvnow.IsControlpanel == false) || (billInvnow.IsControlpanel == true && billInvnow.InDocsNo.StartsWith("CHA"))))
  418. .Where(predicate.ToExpression())
  419. .Select((billInvnow) => new CurStockInfo
  420. {
  421. MatName = billInvnow.MatName,
  422. MatCode = billInvnow.MatCode,
  423. DocNo = billInvnow.InDocsNo,
  424. WbGroupCode = billInvnow.WbGroupCode,
  425. Wind = billInvnow.Wind,
  426. HWType = billInvnow.HWTypeCode,
  427. //IsTorsChk = billInvnow.IsTorsChk.ToString(),
  428. IsBlack = billInvnow.IsBlack.ToString(),
  429. //Rule = boxrule.PackRule,
  430. Grade = billInvnow.Grade,
  431. ContGrpBarCode = billInvnow.ContGrpBarCode,
  432. BatchNo = billInvnow.BatchNo
  433. }).MergeTable().ToList();
  434. //var boxlistqueryable = _billPboxruleRepository.Queryable().With(SqlWith.NoLock).LeftJoin(queryable, (billPboxrule, list) => billPboxrule.DocsNo == list.DocNo)
  435. // .Select((billPboxrule, list) => billPboxrule);
  436. var doclist = _billPboxruleRepository.Context.Queryable<BillPboxrule>().With(SqlWith.NoLock).Select(p => new CurStockInfo() { DocNo = p.DocsNo, SKU = p.SkuCode }).Distinct().ToList();
  437. var newCurStockInfoLists = from st in CurStockInfoLists
  438. join doc in doclist on st.DocNo equals doc.DocNo into tempstock
  439. from nst in tempstock.DefaultIfEmpty()
  440. select new CurStockInfo
  441. {
  442. MatName = st.MatName,
  443. MatCode = st.MatCode,
  444. DocNo = nst == null ? "" : nst.DocNo,
  445. WbGroupCode = st.WbGroupCode,
  446. Wind = st.Wind,
  447. HWType = st.HWType,
  448. //IsTorsChk = billInvnow.IsTorsChk.ToString(),
  449. IsBlack = st.IsBlack.ToString(),
  450. //Rule = boxrule.PackRule,
  451. Grade = st.Grade,
  452. ContGrpBarCode = st.ContGrpBarCode,
  453. SKU = nst == null ? "" : nst.DocNo,
  454. BatchNo = st.BatchNo
  455. };
  456. // var pagelists = queryable.ToPage(pagination);
  457. var lists = newCurStockInfoLists.Distinct(new CompareStock());
  458. var list = new List<CurStockInfo>();
  459. // var boxlist = _billPboxruleRepository.Queryable().With(SqlWith.NoLock).ToList();
  460. var DocNos= lists.Select(o => o.DocNo).ToList();
  461. var boxlist = _billPboxruleRepository.Queryable().With(SqlWith.NoLock).Where(o=> DocNos.Contains(o.DocsNo)) .ToList();
  462. foreach (var item in lists.GroupBy(p => new { p.SKU, p.MatCode, p.DocNo, p.WbGroupCode, p.Grade,p.BatchNo }).ToList())
  463. {
  464. list.Add(new CurStockInfo()
  465. {
  466. SKU = item.Key.SKU ?? "",
  467. MatName = item.First().MatName,
  468. DocNo = item.Key.DocNo,
  469. WbGroupCode = item.Key.WbGroupCode ?? "",
  470. BatchNo = item.Key.BatchNo,
  471. NoBCount = new List<string>() { item.Count(p => p.IsBlack == "0" && p.Wind == "R") + "R", item.Count(p => p.IsBlack == "0" && p.Wind == "L") + "L" },
  472. BCount = new List<string>() { item.Count(p => p.IsBlack == "1" && p.Wind == "R") + "R", item.Count(p => p.IsBlack == "1" && p.Wind == "L") + "L" },
  473. Grade = item.Key.Grade ?? "",
  474. //Rule = item.Key.Rule ?? "",
  475. FinCount = boxlist.Where(p => p.DocsNo == item.Key.DocNo && p.ZXStateCode == 2).Count(),
  476. NoFinCount = boxlist.Where(p => p.DocsNo == item.Key.DocNo && p.ZXStateCode == 0 && !string.IsNullOrEmpty(p.BatchNo) && !string.IsNullOrEmpty(p.Wind) && p.PackRule == "SPC").Count(),
  477. Count = item.Count(p => !string.IsNullOrEmpty(p.ContGrpBarCode))
  478. });
  479. }
  480. PagedInfo<CurStockInfo> pagelists = new PagedInfo<CurStockInfo>()
  481. {
  482. Result = new List<CurStockInfo>()
  483. };
  484. pagelists.Result = list.OrderBy(o => o.SKU).ThenBy(o => o.WbGroupCode).ThenBy(o => o.Wind).ThenBy(o => o.HWType).ThenBy(o => o.Grade).ToList();
  485. pagelists.PageIndex = 1;
  486. pagelists.PageSize = list.Count;
  487. pagelists.TotalNum = list.Count;
  488. // pagelists.Result = list;
  489. // return list.OrderBy(o => o.SKU).ThenBy(o => o.WbGroupCode).ThenBy(o => o.Wind).ThenBy(o => o.HWType).ThenBy(o => o.Grade).ToList();
  490. return pagelists;
  491. }
  492. /// <summary>
  493. /// 时效当前库存分布(控制盘)
  494. /// </summary>
  495. /// <returns></returns>
  496. public PagedInfo<CurStockInfo> GetCurStockInfoControl(Pagination pagination, CurStockInfoQueryDto queryDto)
  497. {
  498. var predicate = Expressionable.Create<BillInvnow, BillPboxrule>();
  499. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.KeyWord), (billInvnow, boxrule) => billInvnow.MatCode.Contains(queryDto.KeyWord)
  500. || billInvnow.MatName.Contains(queryDto.KeyWord) || billInvnow.InDocsNo.Contains(queryDto.KeyWord) || billInvnow.ContGrpBarCode.Contains(queryDto.KeyWord) || boxrule.SkuCode.Contains(queryDto.KeyWord));
  501. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.SKU), (billInvnow, boxrule) => boxrule.SkuCode.Contains(queryDto.SKU));
  502. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.MatName), (billInvnow, boxrule) => billInvnow.MatName.Contains(queryDto.MatName));
  503. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.MatCode), (billInvnow, boxrule) => billInvnow.MatCode.Contains(queryDto.MatCode));
  504. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.DocNo), (billInvnow, boxrule) => billInvnow.InDocsNo.Contains(queryDto.DocNo));
  505. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.WbGroupCode), (billInvnow, boxrule) => billInvnow.WbGroupCode.Contains(queryDto.WbGroupCode));
  506. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.Wind), (billInvnow, boxrule) => billInvnow.Wind.Contains(queryDto.Wind));
  507. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.HWType), (billInvnow, boxrule) => billInvnow.HWTypeCode.Contains(queryDto.HWType));
  508. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.IsBlack), (billInvnow, boxrule) => billInvnow.IsBlack.Equals(queryDto.IsBlack));
  509. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.Grade), (billInvnow, boxrule) => billInvnow.Grade.Contains(queryDto.Grade));
  510. predicate = predicate.AndIF(!string.IsNullOrEmpty(queryDto?.ContGrpBarCode), (billInvnow, boxrule) => billInvnow.ContGrpBarCode.Contains(queryDto.ContGrpBarCode));
  511. predicate = predicate.AndIF(queryDto != null && queryDto.ProductTimeFrom.HasValue, (billInvnow, boxrule) => billInvnow.ProductTime >= queryDto.ProductTimeFrom);
  512. predicate = predicate.AndIF(queryDto != null && queryDto.ProductTimeTo.HasValue, (billInvnow, boxrule) => billInvnow.ProductTime <= queryDto.ProductTimeTo);
  513. var CurStockInfoLists = _billInvnowRepository.Context.Queryable<BillInvnow, BillPboxrule>((billInvnow, boxrule) => new object[] {
  514. JoinType.Left,billInvnow.InDocsNo == boxrule.DocsNo,JoinType.Left})
  515. .With(SqlWith.NoLock)
  516. .Where((billInvnow, boxrule) => billInvnow.InvStateCode == SXInvState.InvEcecState_In.ToString() && string.IsNullOrEmpty(billInvnow.PreStock) && billInvnow.IsControlpanel == true && billInvnow.Grade == "A")
  517. .Where(predicate.ToExpression())
  518. .Select((billInvnow, boxrule) => new CurStockInfo
  519. {
  520. SKU = boxrule.SkuCode,
  521. MatName = billInvnow.MatName,
  522. MatCode = billInvnow.MatCode,
  523. DocNo = billInvnow.InDocsNo,
  524. WbGroupCode = billInvnow.WbGroupCode,
  525. Wind = billInvnow.Wind,
  526. HWType = billInvnow.HWTypeCode,
  527. //IsTorsChk = billInvnow.IsTorsChk.ToString(),
  528. IsBlack = billInvnow.IsBlack.ToString(),
  529. //Rule = boxrule.PackRule,
  530. Grade = billInvnow.Grade,
  531. ContGrpBarCode = billInvnow.ContGrpBarCode,
  532. ProductTime = billInvnow.ProductTime
  533. }).MergeTable().ToList();
  534. //var pagelists = queryable.ToPage(pagination);
  535. var lists = CurStockInfoLists.Distinct(new CompareStock());
  536. //var lists = pagelists.Result ?? new List<CurStockInfo>().Distinct(new CompareStock());
  537. //var lists = queryable.ToList().Distinct(new CompareStock());
  538. var list = new List<CurStockInfo>();
  539. //var boxlist = _billPboxruleRepository.Queryable().With(SqlWith.NoLock).ToList();
  540. //var boxlist = _billPboxruleRepository.Queryable().With(SqlWith.NoLock).LeftJoin(queryable, (billPboxrule, list) => billPboxrule.DocsNo == list.DocNo)
  541. // .Select((billPboxrule, list) => billPboxrule).ToList();
  542. var DocNos = lists.Select(o => o.DocNo).ToList();
  543. var boxlist = _billPboxruleRepository.Queryable().With(SqlWith.NoLock).Where(o => DocNos.Contains(o.DocsNo)).ToList();
  544. foreach (var item in lists.GroupBy(p => new { p.SKU, p.MatCode, p.WbGroupCode, p.Grade }).ToList())
  545. {
  546. list.Add(new CurStockInfo()
  547. {
  548. SKU = item.Key.SKU ?? "",
  549. MatName = item.First().MatName,
  550. WbGroupCode = item.Key.WbGroupCode ?? "",
  551. HaveBlack = new List<string>() {
  552. "("+ item.Count(p => p.IsBlack == "1" && p.Wind == "R" && p.ProductTime >= DateTime.Now.Date.AddDays(-2).AddHours(13) && p.ProductTime <= DateTime.Now.Date.AddDays(-1).AddHours(8)) + "R"+"/"+ item.Count(p => p.IsBlack == "1" && p.Wind == "L" && p.ProductTime >= DateTime.Now.Date.AddDays(-2).AddHours(13) && p.ProductTime <= DateTime.Now.Date.AddDays(-1).AddHours(8)) + "L" +")",
  553. "("+ item.Count(p => p.IsBlack == "1" && p.Wind == "R" && p.ProductTime >= DateTime.Now.Date.AddDays(-1).AddHours(13) && p.ProductTime <= DateTime.Now.Date.AddHours(8)) + "R"+"/"+ item.Count(p => p.IsBlack == "1" && p.Wind == "L" && p.ProductTime >= DateTime.Now.Date.AddDays(-1).AddHours(13) && p.ProductTime <= DateTime.Now.Date.AddHours(8)) + "L" +")",
  554. "("+ item.Count(p => p.IsBlack == "1" && p.Wind == "R" && p.ProductTime >= DateTime.Now.Date.AddHours(13)) + "R"+"/"+ item.Count(p => p.IsBlack == "1" && p.Wind == "L" && p.ProductTime >= DateTime.Now.Date.AddHours(13)) + "L" +")",
  555. },
  556. Grade = item.Key.Grade ?? "",
  557. //Rule = item.Key.Rule ?? "",
  558. Count = item.Count(p => !string.IsNullOrEmpty(p.ContGrpBarCode)),
  559. TodayQty = new List<string>() { item.Count(p => p.IsBlack == "0" && p.Wind == "R" && p.ProductTime >= DateTime.Now.Date.AddHours(13)) + "R", item.Count(p => p.IsBlack == "0" && p.Wind == "L" && p.ProductTime >= DateTime.Now.Date.AddHours(13)) + "L" },
  560. YesterdayQty = new List<string>() { item.Count(p => p.IsBlack == "0" && p.Wind == "R" && p.ProductTime >= DateTime.Now.Date.AddDays(-1).AddHours(13) && p.ProductTime <= DateTime.Now.Date.AddHours(8)) + "R", item.Count(p => p.IsBlack == "0" && p.Wind == "L" && p.ProductTime >= DateTime.Now.Date.AddDays(-1).AddHours(13) && p.ProductTime <= DateTime.Now.Date.AddHours(8)) + "L" },
  561. BeforeYesterdayQty = new List<string>() { item.Count(p => p.IsBlack == "0" && p.Wind == "R" && p.ProductTime >= DateTime.Now.Date.AddDays(-2).AddHours(13) && p.ProductTime <= DateTime.Now.Date.AddDays(-1).AddHours(8)) + "R", item.Count(p => p.IsBlack == "0" && p.Wind == "L" && p.ProductTime >= DateTime.Now.Date.AddDays(-2).AddHours(13) && p.ProductTime <= DateTime.Now.Date.AddDays(-1).AddHours(8)) + "L" },
  562. });
  563. }
  564. // return list.OrderBy(o => o.SKU).ThenBy(o => o.WbGroupCode).ThenBy(o => o.Wind).ThenBy(o => o.HWType).ThenBy(o => o.Grade).ToList();
  565. //pagelists.Result = list;
  566. PagedInfo<CurStockInfo> pagelists = new PagedInfo<CurStockInfo>()
  567. {
  568. Result = new List<CurStockInfo>()
  569. };
  570. pagelists.Result = list.OrderBy(o => o.SKU).ThenBy(o => o.WbGroupCode).ThenBy(o => o.Wind).ThenBy(o => o.HWType).ThenBy(o => o.Grade).ToList();
  571. pagelists.PageIndex = 1;
  572. pagelists.PageSize = list.Count;
  573. pagelists.TotalNum = list.Count;
  574. return pagelists;
  575. }
  576. }
  577. }