ZhongTianFjDataService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using SqlSugar;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using wms.sqlsugar.model.fj;
  8. using Wms.Screen.DataService.Interface;
  9. using Wms.Screen.Dto;
  10. using Wms.Screen.Dto.ZhongTian;
  11. using Wms.Screen.Dto.ZhongTian.Request;
  12. using Wms.Screen.Dto.ZhongTian.Response;
  13. using Wms.Screen.SqlSugar.ZhongTian;
  14. using BaseWarehouse = wms.sqlsugar.model.fj.BaseWarehouse;
  15. using BillBomsetgrp = wms.sqlsugar.model.fj.BillBomsetgrp;
  16. using BillBomsetinfo = wms.sqlsugar.model.fj.BillBomsetinfo;
  17. using BillInvnow = wms.sqlsugar.model.fj.BillInvnow;
  18. using WCS_TaskOld = wms.sqlsugar.model.fj.WCS_TaskOld;
  19. namespace Wms.Screen.DataService.Impl
  20. {
  21. public class ZhongTianFjDataService : IZhongTianFjDataService
  22. {
  23. private SqlSugarClient _db;
  24. public ZhongTianFjDataService(SqlSugarClient db)
  25. {
  26. _db = db;
  27. }
  28. public List<WCS_TaskOld> GetFjEquips(GetEquipsRequest reqEntity)
  29. {
  30. var list = _db.Queryable<wms.sqlsugar.model.fj.WCS_TaskOld>().With(SqlWith.NoLock).Where(p => p.Status != (int)TaskStatus.Finish && p.Status != (int)TaskStatus.Cancel && !string.IsNullOrEmpty(p.Device))
  31. .SplitTable(tabs => tabs.Take(2))
  32. .ToList();
  33. return list;
  34. }
  35. /// <summary>
  36. /// 跺型当前来料比
  37. /// </summary>
  38. /// <returns></returns>
  39. public List<BomSetGrpCurrentMaterialRateDto> GetBomSetGrpCurrentMaterialRateList(string warehouse)
  40. {
  41. var WarehouseCodes = _db.Queryable<BaseWarehouse>().Where(o => o.Code.Contains(warehouse)).Select(o => o.Code).ToList();
  42. var bomsetquery = _db.Queryable<BillBomsetgrp, BillBomsetinfo>((billBomsetgrp, billBomsetinfo) => new object[] {
  43. JoinType.Left, billBomsetgrp.Id == billBomsetinfo.BomSetHdrId,
  44. })
  45. //.Where((billBomsetgrp, billBomsetinfo) => billBomsetgrp.IsStop == 0 && billBomsetgrp.IsDelete == 0)
  46. .GroupBy((billBomsetgrp, billBomsetinfo) => new { billBomsetgrp.BomCode, billBomsetgrp.Name, billBomsetinfo.MatCode })
  47. .Select((billBomsetgrp, billBomsetinfo) => new
  48. {
  49. SetGrpBomCode = billBomsetgrp.BomCode,
  50. SetGrpMatCode = billBomsetinfo.MatCode,
  51. SetGrpName = billBomsetgrp.Name,
  52. SetGrpQty = SqlFunc.AggregateCount(billBomsetinfo.MatCode)
  53. }).Distinct();
  54. var now = DateTime.Now;
  55. var tasksquery = _db.Queryable<wms.sqlsugar.model.fj.WCS_TaskOld>().With(SqlWith.NoLock).SplitTable(p => p.Take(2))
  56. .Where(o => WarehouseCodes.Contains(o.WarehouseCode))
  57. .Where(o => o.Status > 0 && o.Status <= 10)
  58. .Where(o => o.EditTime >= now.AddHours(-3) && o.EditTime <= now)
  59. //.WhereIF(!string.IsNullOrWhiteSpace(statisticsQueryDto.KeyWord),o=>o.MatCode.Contains(statisticsQueryDto.KeyWord))
  60. .GroupBy(taskold => taskold.MatCode)
  61. .Select(taskold => new
  62. {
  63. MatCode = taskold.MatCode,
  64. Qty = SqlFunc.AggregateCount(taskold.ID)
  65. });//.ToPage<BomSetGrpCurrentMaterialRateDto>(pagination);
  66. var tasks = tasksquery.LeftJoin(bomsetquery, (taskold, bomset) => taskold.MatCode == bomset.SetGrpMatCode)
  67. .With(SqlWith.NoLock).Where((taskold, bomset) => !SqlFunc.IsNullOrEmpty(bomset.SetGrpBomCode))
  68. .GroupBy((taskold, bomset) => new { bomset.SetGrpBomCode, taskold.MatCode })
  69. .Select((taskold, bomset) =>
  70. new BomSetGrpCurrentMaterialRateDto
  71. {
  72. BomCode = bomset.SetGrpBomCode,
  73. MatCode = taskold.MatCode,
  74. Qty = SqlFunc.AggregateCount(taskold.MatCode)
  75. }).ToList();
  76. var BomSetGrps = bomsetquery.ToList();// _billBomsetgrprepository.Queryable().Where(o => o.IsStop == 0 && o.IsDelete == 0).ToList();
  77. foreach (var item in tasks)
  78. {
  79. var bom = BomSetGrps.FirstOrDefault(o => o.SetGrpBomCode.Split('|').Any(v => v.Equals(item.MatCode)));
  80. if (bom != null)
  81. {
  82. var boms = bom.SetGrpBomCode.Split('|');
  83. if (boms.Length > 1)
  84. {
  85. var total = tasks.Where(o => boms.Contains(o.MatCode)).Sum(o => o.Qty);
  86. if (total == 0)
  87. {
  88. item.Rate = 0;// "0%";
  89. }
  90. else
  91. {
  92. item.Rate = (decimal)item.Qty / total;// rate.ToString("P2");
  93. }
  94. }
  95. else
  96. {
  97. item.Rate = 1;// "100%";
  98. }
  99. item.BomSetName = bom.SetGrpName;
  100. var bomtotalqty = BomSetGrps.Where(o => o.SetGrpBomCode == bom.SetGrpBomCode).Sum(o => o.SetGrpQty);
  101. var matQty = BomSetGrps.Where(o => o.SetGrpMatCode == item.MatCode).Sum(o => o.SetGrpQty);
  102. if (bomtotalqty == 0)
  103. {
  104. item.NormalRate = 0;
  105. }
  106. else
  107. {
  108. item.NormalRate = (decimal)matQty / bomtotalqty;
  109. }
  110. }
  111. }
  112. var groups = tasks.Select(o => o.BomSetName).Distinct().ToList();
  113. foreach (var item in groups)
  114. {
  115. foreach (var child in BomSetGrps.Where(o => o.SetGrpName == item && o.SetGrpQty > 0).ToList())
  116. {
  117. var bommat = tasks.FirstOrDefault(o => o.MatCode == child.SetGrpMatCode);
  118. if (bommat == null)
  119. {
  120. var bomtotalqty = BomSetGrps.Where(o => o.SetGrpName == item).Sum(o => o.SetGrpQty);
  121. var matQty = BomSetGrps.Where(o => o.SetGrpMatCode == child.SetGrpMatCode).Sum(o => o.SetGrpQty);
  122. tasks.Add(new BomSetGrpCurrentMaterialRateDto
  123. {
  124. BomSetName = child.SetGrpName,
  125. MatCode = child.SetGrpMatCode,
  126. Qty = 0,
  127. Rate = 0,
  128. NormalRate = bomtotalqty == 0 ? 0 : (decimal)matQty / bomtotalqty,
  129. });
  130. }
  131. }
  132. }
  133. foreach (var item in tasks)
  134. {
  135. var totalqty = tasks.Where(o => o.BomSetName == item.BomSetName).Sum(o => o.Qty);
  136. item.DiffQty = item.Qty - (int)Math.Floor((totalqty * item.NormalRate));
  137. }
  138. return tasks;
  139. }
  140. /// <summary>
  141. /// 库内满托占比
  142. /// </summary>
  143. /// <returns></returns>
  144. public List<MantuoBarCodeRateDto> GetMantuoBarCodeRate(string warehouse)
  145. {
  146. //var WarehouseIds = _db.Queryable<BaseWarehouse>().Where(o => o.Code.Contains(warehouse)).Select(o => o.Id).ToList();
  147. var predicate = Expressionable.Create<BillInvnow, wms.sqlsugar.model.fj.BaseWarehouse, BaseMatinfo, BillBomsetgrp>();
  148. predicate = predicate.And((billInvnow, wareHouse, baseMatinfo, billBomsetgrp) => wareHouse.TypeNum == 1);
  149. predicate = predicate.And((billInvnow, wareHouse, baseMatinfo, billBomsetgrp) => billInvnow.Secondary == false);
  150. predicate = predicate.And((billInvnow, wareHouse, baseMatinfo, billBomsetgrp) => !SqlFunc.IsNullOrEmpty(billInvnow.BomMatCode));
  151. predicate = predicate.And((billInvnow, wareHouse, baseMatinfo, billBomsetgrp) => billInvnow.InvStateCode.Equals("InvEcecState_In"));
  152. //predicate = predicate.And((billInvnow, wareHouse, baseMatinfo, billBomsetgrp) => WarehouseIds.Contains(billInvnow.WarehouseId));
  153. var list = _db.Queryable<BillInvnow, BaseWarehouse, BaseMatinfo, BillBomsetgrp>((billInvnow, wareHouse, baseMatinfo, billBomsetgrp) => new object[] {
  154. JoinType.Left, billInvnow.WarehouseId == wareHouse.Id,
  155. JoinType.Left,billInvnow.BomMatCode== baseMatinfo.Code,
  156. JoinType.Left,billInvnow.SetGrpCode== billBomsetgrp.Code
  157. }).Where(predicate.ToExpression())
  158. .GroupBy((billInvnow, wareHouse, baseMatinfo, billBomsetgrp) => new { SetGrpCode = billBomsetgrp.Name, billInvnow.BomMatCode, BomMatName = baseMatinfo.Name })
  159. .Select((billInvnow, wareHouse, baseMatinfo, billBomsetgrp) => new MantuoBarCodeRateDto
  160. {
  161. SetGrpCode = billBomsetgrp.Name,
  162. BomMatCode = billInvnow.BomMatCode,
  163. Qty = SqlFunc.AggregateDistinctCount(billInvnow.ContGrpBarCode),
  164. BomMatName = baseMatinfo.Name,
  165. }).ToList();
  166. return list;
  167. }
  168. public List<InvSetGrpDto> GetInvSetGrpList()
  169. {
  170. var predicate = Expressionable.Create<BillInvnow, BillBomsetgrp>();
  171. predicate = predicate.And((billInvnow, billBomsetgrp) => billInvnow.InvStateCode == "InvEcecState_In");
  172. predicate = predicate.And((billInvnow, billBomsetgrp) => billInvnow.ContGrpType == FJContGrpType.Material);
  173. var list = _db.Queryable<BillInvnow , BillBomsetgrp>((billInvnow, billBomsetgrp) => new object[] {
  174. JoinType.Left,billInvnow.SetGrpCode== billBomsetgrp.Code
  175. }).Where(predicate.ToExpression())
  176. .GroupBy((billInvnow, billBomsetgrp) => new { SetGrpCode = billBomsetgrp.Name })
  177. .Select((billInvnow, billBomsetgrp) => new InvSetGrpDto
  178. {
  179. SetGrpCode = billBomsetgrp.Name,
  180. Qty = SqlFunc.AggregateDistinctCount(billInvnow.ContGrpBarCode),
  181. }).ToList();
  182. return list;
  183. }
  184. public List<BaseWarecell> GetLocationList(string warehouse)
  185. {
  186. //var WarehouseIds = _db.Queryable<BaseWarehouse>().Where(o => o.Code.Contains(warehouse)).Select(o => o.Id).ToList();
  187. return _db.Queryable<BaseWarecell>()
  188. //.Where(o=>SqlFunc.Subqueryable<BaseWarehouse>().Where(s => s.Code.Contains(warehouse) ).Where(s=> s.Id == o.WarehouseId).Any())
  189. .ToList();
  190. }
  191. public List<TaskInOutDto> GeHistoryTaskInfo(GeFJHistoryTaskInfoRequest request)
  192. {
  193. var predicate = Expressionable.Create<WCS_TaskOld>();
  194. predicate = predicate.And(m => m.Status == 99);
  195. //predicate = predicate.And(m => SqlFunc.Subqueryable<BaseWarehouse>().Where(s => s.Code.Contains(request.WareHouse)).Where(s => s.Code == m.WarehouseCode).Any());
  196. predicate = predicate.AndIF(request.StartTime != null && request.StartTime != DateTime.MinValue, m => m.EndTime >= request.StartTime);
  197. predicate = predicate.AndIF(request.EndTime != null && request.EndTime != DateTime.MinValue, m => m.EndTime <= request.EndTime);
  198. var sugarQueryable = _db.Queryable<WCS_TaskOld>().With(SqlWith.NoLock).SplitTable(tabs => tabs.Take(3)).Where(predicate.ToExpression())
  199. .Select(it => new
  200. {
  201. Type = it.Type,
  202. BusType = it.BusType,
  203. CreateTime = it.EndTime.Value.Date
  204. })
  205. .MergeTable()//将查询结果转成一个表
  206. .GroupBy(o => new { o.Type, o.BusType, o.CreateTime })
  207. .Select(it => new TaskInOutDto { Count = SqlFunc.AggregateCount(it.Type), Type = it.Type, BusType = it.BusType, CreateTime = it.CreateTime });
  208. return sugarQueryable.ToList();
  209. }
  210. public List<TaskInfoDto> GetFjRunWcsTaskInfo(GetWcsTaskInfoRequest request)
  211. {
  212. var list = _db.Queryable<WCS_TaskInfo, BillInvnow>((wcstask, code) => new object[] {
  213. JoinType.Left, wcstask.BarCode == code.ContGrpBarCode
  214. }).With(SqlWith.NoLock)
  215. .Where(wcstask => wcstask.Status != TaskStatus.Finish && wcstask.Status != TaskStatus.Cancel)
  216. .WhereIF(request.EquipList != null && request.EquipList.Any(), (wcstask, code) => request.EquipList.Contains(wcstask.AddrFrom) || request.EquipList.Contains(wcstask.AddrTo))
  217. .WhereIF(request.TaskType > 0, (wcstask, code) => wcstask.Type == request.TaskType)
  218. .WhereIF(request.TaskTypelist != null && request.TaskTypelist.Any(), (wcstask, code) => request.TaskTypelist.Contains(wcstask.Type))
  219. .OrderBy((wcstask, code) => wcstask.AddTime, OrderByType.Desc)
  220. .Select((wcstask, code) => new TaskInfoDto
  221. {
  222. TaskCode = wcstask.ID.ToString(),
  223. TaskState = wcstask.Status == TaskStatus.NewBuild ? "新建" : wcstask.Status == TaskStatus.WaitingToExecute ? "待执行" : wcstask.Status == TaskStatus.AGVExecution ? "AGV执行中" : wcstask.Status == TaskStatus.StackerExecution ? "堆垛机执行" : wcstask.Status == TaskStatus.ConveyorExecution ? "输送机执行中" : wcstask.Status == TaskStatus.StackerCompleted ? "堆垛机完成" : "状态类型错误",
  224. TaskType = wcstask.Type == TaskType.EnterDepot ? "入库" : wcstask.Type == TaskType.OutDepot ? "出库" : wcstask.Type == TaskType.TransferDepot ? "移库" : "移动",
  225. HWBarCode = wcstask.BarCode,
  226. HWSpec = code.HWTypeCode,
  227. BeginPosition = wcstask.AddrFrom,
  228. EndPosition = wcstask.AddrTo,
  229. CreatedTime = wcstask.AddTime
  230. }).Distinct().ToList();
  231. return list;
  232. }
  233. public List<StackDto> GetFjStackInfo(GetWorkPlanBillListRequest reqEntity)
  234. {
  235. var list = _db.Queryable<StackDto>().ToList();
  236. return list;
  237. }
  238. public List<ZtLocationUsageReportViewDto> GetFjLocationUsageReportList(string warehouse)
  239. {
  240. var list = _db.Queryable<BaseWarecell, BaseWarehouse>((warecell, warehouse) => new object[] {
  241. JoinType.Left,warecell.WarehouseId == warehouse.Id })
  242. .GroupBy((warecell, warehouse) => new
  243. {
  244. warecell.StateNum,
  245. warecell.IsStop,
  246. warecell.Tunnel,
  247. warehouse.Id,
  248. warehouse.Name
  249. }).Select((warecell, warehouse) => new
  250. {
  251. Status = warecell.StateNum,
  252. warecell.Tunnel,
  253. warecell.IsStop,
  254. HouseId = warehouse.Id,
  255. warehouse.Name,
  256. Total = SqlFunc.AggregateCount(warecell.Tunnel),
  257. })
  258. .MergeTable().ToList().OrderBy(o => o.Tunnel);
  259. int _sort = 1;
  260. //基表:巷道、货架标识和仓库名称为维度查找总货位
  261. var baseTemp = list.GroupBy(s => new { s.Tunnel, s.HouseId, s.Name }).Select(m => new { m.Key.Name, m.Key.Tunnel, Total = m.Sum(n => n.Total) }).ToList();
  262. //关联表:巷道和仓库名称为维度,查找可用货位:未停用.已锁定也算有效货位
  263. var useLocation = list.Where(s => s.IsStop == 0).GroupBy(s => new { s.Tunnel, s.HouseId, s.Name }).Select(m => new { m.Key.Name, m.Key.Tunnel, Total = m.Sum(n => n.Total) }).ToList();
  264. //关联表:巷道和仓库名称为维度,查找锁定货位
  265. var lockLocation = list.Where(s => s.Status == (int)FJLocationState.LocationState_StockIn || s.Status == (int)FJLocationState.LocationState_StockOut || s.Status == (int)FJLocationState.LocationState_StockMove).GroupBy(s => new { s.Tunnel, s.HouseId, s.Name }).Select(m => new { m.Key.Name, m.Key.Tunnel, Total = m.Sum(n => n.Total) }).ToList();
  266. //关联表:巷道和仓库名称为维度,查找停用货位
  267. var stopLocation = list.Where(s => s.IsStop == 1).GroupBy(s => new { s.Tunnel, s.HouseId, s.Name }).Select(m => new { m.Key.Name, m.Key.Tunnel, Total = m.Sum(n => n.Total) }).ToList();
  268. //关联表:巷道和仓库名称为维度,查找物料货位
  269. var materialLocation = list.Where(s => s.Status == (int)FJLocationState.LocationState_Full).GroupBy(s => new { s.Tunnel, s.HouseId, s.Name }).Select(m => new { m.Key.Name, m.Key.Tunnel, Total = m.Sum(n => n.Total) }).ToList();
  270. var locationUsageReportViewDtos = from basetemp in baseTemp
  271. join uselocation in useLocation on new { basetemp.Tunnel, basetemp.Name } equals new { uselocation.Tunnel, uselocation.Name } into useinfo
  272. join locklocation in lockLocation on new { basetemp.Tunnel, basetemp.Name } equals new { locklocation.Tunnel, locklocation.Name } into lockinfo
  273. from lockinfoif in lockinfo.DefaultIfEmpty()
  274. join stoplocation in stopLocation on new { basetemp.Tunnel, basetemp.Name } equals new { stoplocation.Tunnel, stoplocation.Name } into stopinfo
  275. from stopinfoif in stopinfo.DefaultIfEmpty()
  276. join materiallocation in materialLocation on new { basetemp.Tunnel, basetemp.Name } equals new { materiallocation.Tunnel, materiallocation.Name } into materialinfo
  277. from materialinfoif in materialinfo.DefaultIfEmpty()
  278. orderby basetemp.Name
  279. select new ZtLocationUsageReportViewDto()
  280. {
  281. Sort = _sort++,
  282. Tunnel = basetemp.Tunnel.ToString(),
  283. WarehouseName = basetemp.Name,
  284. AllLocationTotal = basetemp.Total,
  285. CanUseLocation = useinfo.FirstOrDefault() == null ? 0 : useinfo.First().Total,
  286. LockLocation = lockinfoif == null ? 0 : lockinfoif.Total,
  287. StopLocation = stopinfoif == null ? 0 : stopinfoif.Total,
  288. MaterilLocation = materialinfoif == null ? 0 : materialinfoif.Total
  289. };
  290. return locationUsageReportViewDtos.OrderBy(p => p.WarehouseName).ToList();
  291. }
  292. }
  293. public class IntRange
  294. {
  295. public int Start { get; }
  296. public int End { get; }
  297. public IntRange(int start, int end)
  298. {
  299. Start = start;
  300. End = end;
  301. }
  302. public bool Contains(int number)
  303. {
  304. return number >= Start && number <= End;
  305. }
  306. }
  307. public class RangeChecker
  308. {
  309. private List<IntRange> ranges;
  310. public RangeChecker()
  311. {
  312. ranges = new List<IntRange>();
  313. }
  314. public RangeChecker(List<IntRange> _ranges)
  315. {
  316. ranges = _ranges;
  317. }
  318. public void AddRange(IntRange range)
  319. {
  320. ranges.Add(range);
  321. }
  322. public bool IsCodeInRanges(string number)
  323. {
  324. if (int.TryParse(number, out var code))
  325. {
  326. foreach (var range in ranges)
  327. {
  328. if (range.Contains(code))
  329. {
  330. return true; // 若整数在任何一个区间内,返回 true
  331. }
  332. }
  333. }
  334. return false; // 若整数不在任何一个区间内,返回 false
  335. }
  336. public bool IsInRanges(int number)
  337. {
  338. foreach (var range in ranges)
  339. {
  340. if (range.Contains(number))
  341. {
  342. return true; // 若整数在任何一个区间内,返回 true
  343. }
  344. }
  345. return false; // 若整数不在任何一个区间内,返回 false
  346. }
  347. }
  348. }