AdminDesktopHomeService.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using SqlSugar;
  2. using WMS.BZModels;
  3. using WMS.BZModels.Dto;
  4. using WMS.BZModels.Models.KLHC;
  5. using WMS.BZSqlSugar;
  6. using WMS.Info;
  7. namespace WMS.BZServices.KLHC
  8. {
  9. public class AdminDesktopHomeService
  10. {
  11. private readonly Repository<WCS_TaskOld> _wcstaskoldrepository;
  12. private readonly Repository<BaseWarehouse> _baseWarehouseRepository;
  13. private readonly Repository<BillInvnow> _billInvnowrepository;
  14. public AdminDesktopHomeService(Repository<WCS_TaskOld> wcstaskoldrepository, Repository<BillInvnow> billInvnowrepository,
  15. Repository<BaseWarehouse> baseWarehouseRepository)
  16. {
  17. _wcstaskoldrepository = wcstaskoldrepository;
  18. _billInvnowrepository = billInvnowrepository;
  19. _baseWarehouseRepository = baseWarehouseRepository;
  20. }
  21. /// <summary>
  22. /// 今日出入库统计
  23. /// </summary>
  24. public TodayTaskInOutPieDto GetTodayTaskInOutPie(DateTime todaystart, DateTime todayend, string warehouse)
  25. {
  26. TodayTaskInOutPieDto todayTaskInOutPieDto = new TodayTaskInOutPieDto();
  27. var valuenames = new List<ValueName<int>>();
  28. var lists = TaskInOutQueryable(todaystart, todayend, warehouse).ToList();
  29. //出库 入库
  30. var outs = lists.Where(o => o.Type == 2).Sum(o => o.Count * o.FullQty);
  31. valuenames.Add(new ValueName<int>() { Value = outs, Name = "出库" });
  32. var ins = lists.Where(o => o.Type == 1).Sum(o => o.Count);
  33. valuenames.Add(new ValueName<int>() { Value = ins, Name = "入库" });
  34. todayTaskInOutPieDto.Total += valuenames.Sum(o => o.Value);
  35. todayTaskInOutPieDto.Tasks = valuenames;
  36. return todayTaskInOutPieDto;
  37. }
  38. /// <summary>
  39. /// 近七天任务动态
  40. /// </summary>
  41. /// <returns></returns>
  42. public Models Get7daysTaskBar(DateTime start, DateTime end, string warehouse)
  43. {
  44. Models models = new Models() { Series = new List<Series>() };
  45. var lists = TaskInOutQueryable(start.Date, DateTime.Parse(end.Date.ToString("yyyy-MM-dd") + " 23:59:59"), warehouse).ToList();
  46. models.XAxis = new string[7]; //lists.OrderBy(o => o.CreateTime).Select(o => o.CreateTime.ToString("M-dd")).Distinct().ToArray();
  47. int[] Outints = new int[7];
  48. int[] EnterDepots = new int[7];
  49. int[] TransferDepots = new int[7];
  50. int[] SetPlate = new int[7];
  51. for (int i = 0; i < (end - start).TotalDays - 1; i++)
  52. {
  53. var date = start.AddDays(i);
  54. models.XAxis[i] = date.ToString("M-d");
  55. //if (DateTime.Now.Hour <= 8)
  56. //{
  57. // continue;
  58. //}
  59. var starttime = new DateTime(date.Year, date.Month, date.Day, 8, 0, 0);
  60. var endtime = starttime.AddDays(1);
  61. Outints[i] = lists.Where(o => o.Type == 2 && o.CreateTime >= starttime && o.CreateTime <= endtime).Sum(o => o.Count * o.FullQty);
  62. EnterDepots[i] = lists.Where(o => o.Type == 1 && o.CreateTime >= starttime && o.CreateTime <= endtime).Sum(o => o.Count);
  63. }
  64. models.Series.Add(new Series { Name = "出库", Data = Outints });
  65. models.Series.Add(new Series { Name = "入库", Data = EnterDepots });
  66. return models;
  67. }
  68. public ISugarQueryable<TaskInOutDto> TaskInOutQueryable(DateTime? start, DateTime? end, string warehouse)
  69. {
  70. string[] warehousecodes = GetWearhouseCodes(warehouse);
  71. var predicate = Expressionable.Create<WCS_TaskOld>();
  72. predicate = predicate.And(m => m.Status == (int)BZModels.TaskStatus.Finish);
  73. predicate = predicate.AndIF(start != null && start.HasValue && start.Value != DateTime.MinValue, m => m.EndTime >= start.Value);
  74. predicate = predicate.AndIF(end != null && end.HasValue && end.Value != DateTime.MinValue, m => m.EndTime <= end.Value);
  75. var sugarQueryable = _wcstaskoldrepository.Queryable().With(SqlWith.NoLock).Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(3))
  76. .Select(it => new
  77. {
  78. Type = it.Type,
  79. CreateTime = it.EndTime.Value,
  80. FullQty = it.FullQty
  81. })
  82. .MergeTable()//将查询结果转成一个表
  83. .GroupBy(o => new { o.Type, o.CreateTime, o.FullQty })
  84. .Select(it => new TaskInOutDto { Count = SqlFunc.AggregateCount(it.Type), Type = it.Type, CreateTime = it.CreateTime, FullQty = it.FullQty });
  85. var a = sugarQueryable.ToList();
  86. return sugarQueryable;
  87. }
  88. private long[] GetWearhouseIds(string warehouse)
  89. {
  90. var list = _baseWarehouseRepository.Queryable().Where(o => o.IsStop == 0).ToArray();
  91. if (warehouse == "fjall")
  92. {
  93. return list.Select(o => o.Id).ToArray();
  94. }
  95. else
  96. {
  97. warehouse = warehouse.Replace("fj", "");
  98. long[] warehouseIds = list.Where(o => o.Code.Contains(warehouse)).Select(o => o.Id).ToArray();
  99. return warehouseIds;
  100. }
  101. }
  102. private string[] GetWearhouseCodes(string warehouse)
  103. {
  104. var list = _baseWarehouseRepository.Queryable().Where(o => o.IsStop == 0).Select(o => o.Code).ToArray();
  105. if (warehouse == "fjall")
  106. {
  107. return list.ToArray();
  108. }
  109. else
  110. {
  111. warehouse = warehouse.Replace("fj", "");
  112. string[] warehousecodes = list.Where(o => o.Contains(warehouse)).ToArray();
  113. return warehousecodes;
  114. }
  115. }
  116. /// <summary>
  117. /// 24小时内任务动态
  118. /// </summary>
  119. /// <param name="start"></param>
  120. /// <param name="end"></param>
  121. /// <returns></returns>
  122. public Models GettodaysHourTask(DateTime start, DateTime end, string warehouse)
  123. {
  124. Models models = new Models() { Series = new List<Series>() };
  125. string[] warehousecodes = GetWearhouseCodes(warehouse);
  126. var predicate = Expressionable.Create<WCS_TaskOld>();
  127. predicate = predicate.And(m => m.Status == (int)BZModels.TaskStatus.Finish);
  128. predicate = predicate.AndIF(start != DateTime.MinValue, m => m.EndTime >= start.Date);
  129. predicate = predicate.AndIF(end != DateTime.MinValue, m => m.EndTime <= DateTime.Parse(end.Date.ToString("yyyy-MM-dd") + " 23:59:59"));
  130. var sugarQueryable = _wcstaskoldrepository.Queryable().With(SqlWith.NoLock).Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(3))
  131. .Select(it => new
  132. {
  133. Type = it.Type,
  134. FullQty = it.FullQty,
  135. Hour = it.EndTime.Value.Hour
  136. })
  137. .MergeTable()//将查询结果转成一个表
  138. .GroupBy(o => new { o.Type, o.FullQty, o.Hour })
  139. .Select(it => new TaskInOutDto { Count = SqlFunc.AggregateCount(it.Type), FullQty = it.FullQty, Type = it.Type, Hour = it.Hour });
  140. var lists = sugarQueryable.ToList();
  141. models.XAxis = new string[24]; //lists.OrderBy(o => o.Hour).Select(o => o.Hour.ToString()).Distinct().ToArray();
  142. int[] Outints = new int[24];
  143. int[] EnterDepots = new int[24];
  144. int[] TransferDepots = new int[24];
  145. int[] SetPlate = new int[24];
  146. //for (int i = 0; i < models.XAxis.Length; i++)
  147. //{
  148. // var Outqty = lists.Where(o => o.Type == (int)TaskType.OutDepot && o.Hour.ToString() == models.XAxis[i]).Select(o => o.Count).FirstOrDefault(0);
  149. // Outints[i] = Outqty;
  150. // var EnterDepotqty = lists.Where(o => o.Type == (int)TaskType.EnterDepot && o.Hour.ToString() == models.XAxis[i]).Select(o => o.Count).FirstOrDefault(0);
  151. // EnterDepots[i] = EnterDepotqty;
  152. // var TransferDepotqty = lists.Where(o => o.Type == (int)TaskType.TransferDepot && o.Hour.ToString() == models.XAxis[i]).Select(o => o.Count).FirstOrDefault(0);
  153. // TransferDepots[i] = TransferDepotqty;
  154. //}
  155. for (int i = 0; i < 24; i++)
  156. {
  157. models.XAxis[i] = i.ToString();
  158. var Outqty = lists.Where(o => o.Type == 2 && o.Hour == i).Sum(o => o.Count * o.FullQty);
  159. Outints[i] = Outqty;
  160. var EnterDepotqty = lists.Where(o => o.Type == 1 && o.Hour == i).Sum(o => o.Count);
  161. EnterDepots[i] = EnterDepotqty;
  162. }
  163. models.Series.Add(new Series { Name = "出库", Data = Outints });
  164. models.Series.Add(new Series { Name = "入库", Data = EnterDepots });
  165. return models;
  166. }
  167. public Models GetHourTask(DateTime start, DateTime end, string warehouse)
  168. {
  169. Models models = new Models() { Series = new List<Series>() };
  170. string[] warehousecodes = GetWearhouseCodes(warehouse);
  171. var predicate = Expressionable.Create<WCS_TaskOld>();
  172. predicate = predicate.And(m => m.Status == (int)BZModels.TaskStatus.Finish);
  173. predicate = predicate.AndIF(start != DateTime.MinValue, m => m.EndTime >= start.Date);
  174. predicate = predicate.AndIF(end != DateTime.MinValue, m => m.EndTime <= DateTime.Parse(end.Date.ToString("yyyy-MM-dd") + " 23:59:59"));
  175. var sugarQueryable = _wcstaskoldrepository.Queryable().With(SqlWith.NoLock).Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(3))
  176. .Select(it => new
  177. {
  178. Type = it.Type,
  179. FullQty = it.FullQty,
  180. Hour = it.EndTime.Value.Hour,
  181. Day = it.EndTime.Value.Day,
  182. })
  183. .MergeTable()//将查询结果转成一个表
  184. .GroupBy(o => new { o.Type, o.FullQty, o.Hour, o.Day })
  185. .Select(it => new TaskInOutDto { Count = SqlFunc.AggregateCount(it.Type), FullQty = it.FullQty, Type = it.Type, Hour = it.Hour, Day = it.Day });
  186. var lists = sugarQueryable.ToList();
  187. //models.XAxis = new string[24]; //lists.OrderBy(o => o.Hour).Select(o => o.Hour.ToString()).Distinct().ToArray();
  188. var XAxis = new List<string>();
  189. var Outints = new List<int>();
  190. var EnterDepots = new List<int>();
  191. var TransferDepots = new List<int>();
  192. var SetPlate = new List<int>();
  193. foreach (var item in new int[] { start.Day, end.Day })
  194. {
  195. for (int i = 0; i < 24; i++)
  196. {
  197. //XAxis.Add(item+"-"+i.ToString());
  198. XAxis.Add(i.ToString());
  199. //models.XAxis[i] = i.ToString();
  200. var Outqty = lists.Where(o => o.Type == 2 && o.Hour == i && o.Day == item).Sum(o => o.Count * o.FullQty);
  201. Outints.Add(Outqty);
  202. var EnterDepotqty = lists.Where(o => o.Type == 1 && o.Hour == i && o.Day == item).Sum(o => o.Count);
  203. EnterDepots.Add(EnterDepotqty);
  204. }
  205. }
  206. models.XAxis = XAxis.ToArray();
  207. models.Series.Add(new Series { Name = "出库", Data = Outints.ToArray() });
  208. models.Series.Add(new Series { Name = "入库", Data = EnterDepots.ToArray() });
  209. return models;
  210. }
  211. public ISugarQueryable<TaskInOutDto> TaskGoodsTypeQueryable(DateTime? start, DateTime? end, string warehouse)
  212. {
  213. string[] warehousecodes = GetWearhouseCodes(warehouse);
  214. var predicate = Expressionable.Create<WCS_TaskOld>();
  215. predicate = predicate.And(m => m.Type == 1 || m.Type == 2);
  216. predicate = predicate.And(m => m.Status == (int)BZModels.TaskStatus.Finish);
  217. predicate = predicate.AndIF(start != null && start.HasValue && start.Value != DateTime.MinValue, m => m.EndTime >= start.Value);
  218. predicate = predicate.AndIF(end != null && end.HasValue && end.Value != DateTime.MinValue, m => m.EndTime <= end.Value);
  219. var sugarQueryable = _wcstaskoldrepository.Queryable().With(SqlWith.NoLock).Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(2))
  220. .Select(x => new TaskInOutDto { Type = x.Type, Count = x.Height, FullQty = x.FullQty });
  221. return sugarQueryable;
  222. }
  223. public List<ValueName<decimal>> GetTaskGoodsTypes(DateTime todaystart, DateTime todayend, string warehouse)
  224. {
  225. var valuenames = new List<ValueName<decimal>>();
  226. long[] warehouseIds = GetWearhouseIds(warehouse);
  227. var a = TaskGoodsTypeQueryable(todaystart, todayend, warehouse).ToList();
  228. var lists = TaskGoodsTypeQueryable(todaystart, todayend, warehouse).ToList().GroupBy(x => x.Count).Select(x =>
  229. {
  230. var count = 0;
  231. count += x.Where(x => x.Type == 1).Count();
  232. count += x.Where(x => x.Type == 2).Sum(s => s.FullQty);
  233. return new TaskInOutDto { Type = x.Key, Count = count };
  234. }).ToList();
  235. decimal total = 0;
  236. for (int i = 0; i < lists.Count; i++)
  237. {
  238. if (i < 5)
  239. {
  240. valuenames.Add(new ValueName<decimal> { Value = lists[i].Count, Name = lists[i].Type.ToString() });
  241. }
  242. else
  243. {
  244. total += lists[i].Count;
  245. }
  246. }
  247. valuenames.Add(new ValueName<decimal> { Value = total, Name = "其他" });
  248. return valuenames;
  249. }
  250. public List<ValueName<decimal>> GetMatWeightCategory(string warehouse)
  251. {
  252. var valuenames = new List<ValueName<decimal>>();
  253. long[] warehouseIds = GetWearhouseIds(warehouse);
  254. var lists = _billInvnowrepository.Queryable().Where(o => warehouseIds.Contains(o.WarehouseId))
  255. .Select(it => new
  256. {
  257. matname = it.MatName,
  258. tolwqty = SqlFunc.AggregateSum(it.TolWQty)
  259. })
  260. .GroupBy(o => o.matname).OrderByDescending(o => o.tolwqty).ToList();
  261. decimal total = 0;
  262. for (int i = 0; i < lists.Count; i++)
  263. {
  264. if (i < 5)
  265. {
  266. valuenames.Add(new ValueName<decimal> { Value = lists[i].tolwqty, Name = lists[i].matname });
  267. }
  268. else
  269. {
  270. total += lists[i].tolwqty;
  271. }
  272. }
  273. valuenames.Add(new ValueName<decimal> { Value = total, Name = "其他" });
  274. return valuenames;
  275. }
  276. private string[] WarehouseNames = new string[] { "分拣一", "分拣二", "分拣三" };
  277. public PagedInfo<DailyStatisticsDto> GetDailyStatistics(Pagination pagination, DateTime? start, DateTime? end)
  278. {
  279. var predicate = Expressionable.Create<WCS_TaskOld>();
  280. predicate = predicate.And(m => m.Status == (int)BZModels.TaskStatus.Finish);
  281. predicate = predicate.And(m => m.EndTime >= start);
  282. predicate = predicate.And(m => m.EndTime <= end);
  283. var TaskStatisticsLists = _wcstaskoldrepository.Queryable().With(SqlWith.NoLock).Where(predicate.ToExpression()).SplitTable(tabs => tabs.Take(3))
  284. .GroupBy(o => new { o.WarehouseCode, o.BusType, o.Type }).Select(it => new TaskStatisticsDto
  285. {
  286. WarehouseCode = it.WarehouseCode,
  287. BusType = it.BusType,
  288. Type = it.Type,
  289. Count = SqlFunc.AggregateCount(it.WarehouseCode),
  290. }).ToList();
  291. //满托异常
  292. var taskErrorLists = _wcstaskoldrepository.Queryable().With(SqlWith.NoLock).Where(m => m.Status == (int)BZModels.TaskStatus.Finish)
  293. .Where(m => m.EditTime >= start).Where(m => m.EditTime <= end).Where(m => m.MaterialCode.StartsWith("Error"))
  294. .SplitTable(tabs => tabs.Take(3))
  295. .GroupBy(o => new { o.WarehouseCode })
  296. .Select(it => new TaskStatisticsDto
  297. {
  298. WarehouseCode = it.WarehouseCode,
  299. Count = SqlFunc.AggregateCount(it.WarehouseCode),
  300. }).ToList();
  301. var task = _wcstaskoldrepository.Queryable().With(SqlWith.NoLock).SplitTable(tabs => tabs.Take(3));
  302. PagedInfo<DailyStatisticsDto> pagedInfos = new PagedInfo<DailyStatisticsDto>()
  303. {
  304. Result = new List<DailyStatisticsDto>()
  305. };
  306. for (int i = 0; i < WarehouseNames.Length; i++)
  307. {
  308. var code = i + 1;
  309. var mod = new DailyStatisticsDto
  310. {
  311. WarehouseName = WarehouseNames[i],
  312. TrussNorthQty = TaskStatisticsLists.Where(o => !string.IsNullOrEmpty(o.WarehouseCode) && o.WarehouseCode.Contains(code.ToString()) && o.WarehouseCode == code.ToString() + "N" && o.Type == 0).Sum(o => o.Count),
  313. TrussSouthQty = TaskStatisticsLists.Where(o => !string.IsNullOrEmpty(o.WarehouseCode) && o.WarehouseCode.Contains(code.ToString()) && o.WarehouseCode == code.ToString() + "S" && o.Type == 0).Sum(o => o.Count),
  314. CircularSouthQty = TaskStatisticsLists.Where(o => !string.IsNullOrEmpty(o.WarehouseCode) && o.WarehouseCode.Contains(code.ToString()) && o.WarehouseCode == code.ToString() + "SR" && o.Type == 0).Sum(o => o.Count),
  315. CircularNorthQty = TaskStatisticsLists.Where(o => !string.IsNullOrEmpty(o.WarehouseCode) && o.WarehouseCode.Contains(code.ToString()) && o.WarehouseCode == code.ToString() + "NR" && o.Type == 0).Sum(o => o.Count),
  316. DailySummary = TaskStatisticsLists.Where(o => !string.IsNullOrEmpty(o.WarehouseCode) && o.WarehouseCode.Contains(code.ToString()) && o.Type == 0).Sum(o => o.Count),
  317. FullNormalQty = TaskStatisticsLists.Where(o => !string.IsNullOrEmpty(o.WarehouseCode) && o.WarehouseCode.Contains(code.ToString()) && o.BusType == "车间叫料").Sum(o => o.Count),
  318. EmptyAbnormalQty = taskErrorLists.Where(o => !string.IsNullOrEmpty(o.WarehouseCode) && o.WarehouseCode.Contains(code.ToString())).Sum(o => o.Count),
  319. EmptyWarehouseQty = TaskStatisticsLists.Where(o => !string.IsNullOrEmpty(o.WarehouseCode) && o.WarehouseCode.Contains(code.ToString()) && o.BusType == "皮盘入库").Sum(o => o.Count),
  320. };
  321. pagedInfos.Result.Add(mod);
  322. }
  323. return pagedInfos;
  324. }
  325. }
  326. }