ReportService.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using SqlSugar;
  2. using WMS.BZModels;
  3. using WMS.Info;
  4. using WMS.BZSqlSugar;
  5. using WMS.Util;
  6. using wms.sqlsugar.model.pt;
  7. using WMS.BZModels.Dto.PT.ReportDtos;
  8. using WMS.BZModels.Dto.PT.BillInvDtos;
  9. namespace WMS.BZServices.PT
  10. {
  11. public class ReportService
  12. {
  13. private readonly Repository<BillInvflow> _reportrepository;
  14. private readonly Repository<BaseWarecell> _warecellRepository;
  15. private readonly Repository<BillInvnow> _billInvnowrepository;
  16. public ReportService(Repository<BillInvflow> reportrepository, Repository<BaseWarecell> warecellRepository, Repository<BillInvnow> billInvnowrepository)
  17. {
  18. _reportrepository = reportrepository;
  19. _warecellRepository = warecellRepository;
  20. _billInvnowrepository = billInvnowrepository;
  21. }
  22. public PagedInfo<ReportDto> GetPageList(Pagination pagination, ReportQueryDto reportQueryDto)
  23. {
  24. if (pagination.sord.ToUpper() != "ASC")
  25. {
  26. pagination.sidx = pagination.sidx.IsEmpty() ? "AddTime DESC" : pagination.sidx + " DESC";
  27. }
  28. if (pagination.sidx.IsEmpty())
  29. {
  30. pagination.sidx = "AddTime DESC";
  31. }
  32. ISugarQueryable<ReportDto> sugarQueryable = GetQueryable(reportQueryDto);
  33. var list = sugarQueryable.ToPage(pagination);
  34. return list;
  35. }
  36. public IList<ReportDto> GetList(ReportQueryDto reportQueryDto)
  37. {
  38. ISugarQueryable<ReportDto> sugarQueryable = GetQueryable(reportQueryDto);
  39. var list = sugarQueryable.ToList();
  40. return list;
  41. }
  42. private ISugarQueryable<ReportDto> GetQueryable(ReportQueryDto reportQueryDto)
  43. {
  44. var predicate = Expressionable.Create<BillInvflow, BaseWarehouse, BaseWarecell>();
  45. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.KeyWord), (billInvflow, wareHouse, wareCell) => billInvflow.ContGrpBarCode.Contains(reportQueryDto.KeyWord) || billInvflow.MatCode.Contains(reportQueryDto.KeyWord) ||
  46. billInvflow.MatName.Contains(reportQueryDto.KeyWord) || billInvflow.RFIDBarCode.Contains(reportQueryDto.KeyWord) || wareCell.Code.Contains(reportQueryDto.KeyWord) || billInvflow.InvBarCode.Contains(reportQueryDto.KeyWord));
  47. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.ContGrpBarCode), (billInvflow, wareHouse, wareCell) => billInvflow.ContGrpBarCode.Contains(reportQueryDto.ContGrpBarCode));
  48. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.MatCode), (billInvflow, wareHouse, wareCell) => billInvflow.MatCode.Contains(reportQueryDto.MatCode));
  49. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.MatName), (billInvflow, wareHouse, wareCell) => billInvflow.MatName.Contains(reportQueryDto.MatName));
  50. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.RFIDBarCode), (billInvflow, wareHouse, wareCell) => billInvflow.RFIDBarCode.Contains(reportQueryDto.RFIDBarCode));
  51. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.CellCode), (billInvflow, wareHouse, wareCell) => wareCell.Code.Contains(reportQueryDto.CellCode));
  52. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.Tunnel), (billInvflow, wareHouse, wareCell) => wareCell.Tunnel == int.Parse(reportQueryDto.Tunnel));
  53. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.InvBarCode), (billInvflow, wareHouse, wareCell) => billInvflow.InvBarCode.Contains(reportQueryDto.InvBarCode));
  54. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.BoilerNo), (billInvflow, wareHouse, wareCell) => billInvflow.BoilerNo.Contains(reportQueryDto.BoilerNo));
  55. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.PackNo), (billInvflow, wareHouse, wareCell) => billInvflow.PackNo.Contains(reportQueryDto.PackNo));
  56. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.BrandNo), (billInvflow, wareHouse, wareCell) => billInvflow.BrandNo.Contains(reportQueryDto.BrandNo));
  57. predicate = predicate.AndIF(reportQueryDto != null && reportQueryDto.AddTimeFrom.HasValue, (billInvflow, wareHouse, wareCell) => billInvflow.AddTime >= reportQueryDto.AddTimeFrom);
  58. predicate = predicate.AndIF(reportQueryDto != null && reportQueryDto.AddTimeTo.HasValue, (billInvflow, wareHouse, wareCell) => billInvflow.AddTime <= reportQueryDto.AddTimeTo);
  59. predicate = predicate.AndIF(reportQueryDto != null && reportQueryDto.EndTimeBegin.HasValue, (billInvflow, wareHouse, wareCell) => billInvflow.EditTime >= reportQueryDto.EndTimeBegin);
  60. predicate = predicate.AndIF(reportQueryDto != null && reportQueryDto.EndTimeEnd.HasValue, (billInvflow, wareHouse, wareCell) => billInvflow.EditTime <= reportQueryDto.EndTimeEnd);
  61. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.IsSurplus), (billInvflow, wareHouse, wareCell) => billInvflow.IsSurplus.Equals(reportQueryDto.IsSurplus));
  62. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.IsRework), (billInvflow, wareHouse, wareCell) => billInvflow.IsRework.Equals(reportQueryDto.IsRework));
  63. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.IsFast), (billInvflow, wareHouse, wareCell) => billInvflow.IsFast.Equals(reportQueryDto.IsFast));
  64. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.IsFail), (billInvflow, wareHouse, wareCell) => billInvflow.IsFail.Equals(reportQueryDto.IsFail));
  65. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.IsBack), (billInvflow, wareHouse, wareCell) => billInvflow.IsBack.Equals(reportQueryDto.IsBack));
  66. if (!string.IsNullOrEmpty(reportQueryDto?.InvStateCode))
  67. {
  68. var enums = (InvState)Enum.ToObject(typeof(InvState), Convert.ToInt32(reportQueryDto?.InvStateCode));
  69. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.InvStateCode), (billInvflow, wareHouse, wareCell) => billInvflow.InvStateCode.Equals(enums.ToString()));
  70. }
  71. var sugarQueryable = _reportrepository.Context.Queryable<BillInvflow, BaseWarehouse, BaseWarecell>((billInvflow, wareHouse, wareCell) => new object[] {
  72. JoinType.Left, billInvflow.WarehouseId == wareHouse.Id,
  73. JoinType.Left, billInvflow.PutRow == wareCell.Row && billInvflow.PutCol == wareCell.Col && billInvflow.PutLayer == wareCell.Layer
  74. }).Where(predicate.ToExpression())
  75. .Select((billInvflow, wareHouse, wareCell) => new ReportDto
  76. {
  77. Id = billInvflow.Id.ToString(),
  78. WarehouseName = wareHouse.Name,
  79. Tunnel = wareCell.Tunnel,
  80. Memo = billInvflow.Memo,
  81. Code = wareCell.Code,
  82. ContGrpId = billInvflow.ContGrpId.ToString(),
  83. ContGrpBarCode = billInvflow.ContGrpBarCode,
  84. //ContGrpType = billInvflow.ContGrpType,
  85. InvStateCode = billInvflow.InvStateCode,
  86. BoxBarCode = billInvflow.BoxBarCode,
  87. BomDocsNo = billInvflow.BomDocsNo,
  88. BomMatId = billInvflow.BomMatId.ToString(),
  89. BomMatCode = billInvflow.BomMatCode,
  90. BomMatName = billInvflow.BomMatName,
  91. BomSetId = billInvflow.BomSetId.ToString(),
  92. ExecStateCode = billInvflow.ExecStateCode,
  93. ExecDocsNo = billInvflow.ExecDocsNo,
  94. ExecDocsRowNo = billInvflow.ExecDocsRowNo,
  95. ExecDocsTypeCode = billInvflow.ExecDocsTypeCode,
  96. InvBarCode = billInvflow.InvBarCode,
  97. InDocsNo = billInvflow.InDocsNo,
  98. InDocsRowNo = billInvflow.InDocsRowNo,
  99. SuppCode = billInvflow.SuppCode,
  100. SuppName = billInvflow.SuppName,
  101. CustCode = billInvflow.CustCode,
  102. CustName = billInvflow.CustName,
  103. IsFast = billInvflow.IsFast,
  104. IsFail = billInvflow.IsFail,
  105. FailReason = billInvflow.FailReason,
  106. PutRow = billInvflow.PutRow,
  107. PutCol = billInvflow.PutCol,
  108. PutLayer = billInvflow.PutLayer,
  109. MatId = billInvflow.MatId,
  110. MatCode = billInvflow.MatCode,
  111. MatName = billInvflow.MatName,
  112. TolWQty = billInvflow.TolWQty,
  113. NetWQty = billInvflow.NetWQty,
  114. TareWQty = billInvflow.TareWQty,
  115. LengthQty = billInvflow.LengthQty,
  116. CaQty = billInvflow.CaQty,
  117. SolderQty = billInvflow.SolderQty,
  118. ContUsageQty = billInvflow.ContUsageQty,
  119. BatchNo = billInvflow.BatchNo,
  120. ProductTime = billInvflow.ProductTime,
  121. OneInTime = billInvflow.OneInTime,
  122. RodBarCode = billInvflow.RodBarCode,
  123. HWBarCode = billInvflow.HWBarCode,
  124. RFIDBarCode = billInvflow.RFIDBarCode,
  125. CLBarCode = billInvflow.CLBarCode,
  126. HWTypeCode = billInvflow.HWTypeCode,
  127. BoilerNo = billInvflow.BoilerNo,
  128. PackNo = billInvflow.PackNo,
  129. BrandNo = billInvflow.BrandNo,
  130. ExecStd = billInvflow.ExecStd,
  131. LicenceCode = billInvflow.LicenceCode,
  132. SilkTypeCode = billInvflow.SilkTypeCode,
  133. Grade = billInvflow.Grade,
  134. IsBack = billInvflow.IsBack,
  135. BackReason = billInvflow.BackReason,
  136. ProcessDocsCode = billInvflow.ProcessDocsCode,
  137. ProductMachCode = billInvflow.ProductMachCode,
  138. ProductLineNo = billInvflow.ProductLineNo,
  139. AddTime = billInvflow.AddTime,
  140. EditTime = billInvflow.EditTime,
  141. }).MergeTable();
  142. return sugarQueryable;
  143. }
  144. /// <summary>
  145. /// 货位分析
  146. /// </summary>
  147. /// <returns></returns>
  148. public List<LocationUsageReportViewDto> GetLocationUsageReportList()
  149. {
  150. var list = _warecellRepository.Context.Queryable<BaseWarecell, BaseWarehouse>((warecell, warehouse) => new object[] {
  151. JoinType.Left,warecell.WarehouseId == warehouse.Id })
  152. .GroupBy((warecell, warehouse) => new
  153. {
  154. warecell.StateNum,
  155. warecell.IsStop,
  156. warecell.Tunnel,
  157. warehouse.Id,
  158. warehouse.Name
  159. }).Select((warecell, warehouse) => new
  160. {
  161. Status = warecell.StateNum,
  162. warecell.Tunnel,
  163. warecell.IsStop,
  164. HouseId = warehouse.Id,
  165. warehouse.Name,
  166. Total = SqlFunc.AggregateCount(warecell.Tunnel),
  167. })
  168. .MergeTable().ToList().OrderBy(o => o.Tunnel);
  169. int _sort = 1;
  170. //基表:巷道、货架标识和仓库名称为维度查找总货位
  171. 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();
  172. //关联表:巷道和仓库名称为维度,查找可用货位:未停用.已锁定也算有效货位
  173. 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();
  174. //关联表:巷道和仓库名称为维度,查找锁定货位
  175. var lockLocation = list.Where(s => s.Status == (int)LocationState.LocationState_StockIn || s.Status == (int)LocationState.LocationState_StockOut || s.Status == (int)LocationState.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();
  176. //关联表:巷道和仓库名称为维度,查找停用货位
  177. 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();
  178. //关联表:巷道和仓库名称为维度,查找物料货位
  179. var materialLocation = list.Where(s => s.Status == (int)LocationState.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();
  180. var locationUsageReportViewDtos = from basetemp in baseTemp
  181. join uselocation in useLocation on new { basetemp.Tunnel, basetemp.Name } equals new { uselocation.Tunnel, uselocation.Name } into useinfo
  182. join locklocation in lockLocation on new { basetemp.Tunnel, basetemp.Name } equals new { locklocation.Tunnel, locklocation.Name } into lockinfo
  183. from lockinfoif in lockinfo.DefaultIfEmpty()
  184. join stoplocation in stopLocation on new { basetemp.Tunnel, basetemp.Name } equals new { stoplocation.Tunnel, stoplocation.Name } into stopinfo
  185. from stopinfoif in stopinfo.DefaultIfEmpty()
  186. join materiallocation in materialLocation on new { basetemp.Tunnel, basetemp.Name } equals new { materiallocation.Tunnel, materiallocation.Name } into materialinfo
  187. from materialinfoif in materialinfo.DefaultIfEmpty()
  188. orderby basetemp.Name
  189. select new LocationUsageReportViewDto()
  190. {
  191. Sort = _sort++,
  192. Tunnel = basetemp.Tunnel.ToString(),
  193. WarehouseName = basetemp.Name,
  194. AllLocationTotal = basetemp.Total,
  195. CanUseLocation = useinfo.FirstOrDefault() == null ? 0 : useinfo.First().Total,
  196. LockLocation = lockinfoif == null ? 0 : lockinfoif.Total,
  197. StopLocation = stopinfoif == null ? 0 : stopinfoif.Total,
  198. MaterilLocation = materialinfoif == null ? 0 : materialinfoif.Total
  199. };
  200. return locationUsageReportViewDtos.OrderBy(p => p.WarehouseName).ToList();
  201. }
  202. public PagedInfo<StockKeepReportViewDto> GetStockKeepReportList(Pagination pagination, BillInvNowQueryDto billInvNowQueryDto)
  203. {
  204. ISugarQueryable<StockKeepReportViewDto> sugarQueryable = GetStockKeepQueryable(billInvNowQueryDto);
  205. var lists = sugarQueryable.ToPage(pagination);
  206. return lists;
  207. }
  208. public IList<StockKeepReportViewDto> GetStockKeepReports(BillInvNowQueryDto billInvNowQueryDto)
  209. {
  210. ISugarQueryable<StockKeepReportViewDto> sugarQueryable = GetStockKeepQueryable(billInvNowQueryDto);
  211. var list = sugarQueryable.ToList();
  212. return list;
  213. }
  214. private ISugarQueryable<StockKeepReportViewDto> GetStockKeepQueryable(BillInvNowQueryDto billInvNowQueryDto)
  215. {
  216. var predicate = Expressionable.Create<BillInvnow>();
  217. predicate = predicate.AndIF(!string.IsNullOrEmpty(billInvNowQueryDto?.KeyWord), billInvnow => billInvnow.MatCode.Contains(billInvNowQueryDto.KeyWord) || billInvnow.MatName.Contains(billInvNowQueryDto.KeyWord));
  218. var sugarQueryable = _billInvnowrepository.Context.Queryable<BillInvnow>().Where(predicate.ToExpression())
  219. .GroupBy(it => new { it.MatCode, it.MatName })
  220. .Select(it => new StockKeepReportViewDto
  221. {
  222. StockKeepTime7 = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.DateDiff(DateType.Day, it.ProductTime, DateTime.Now) >= 0 && SqlFunc.DateDiff(DateType.Day, it.ProductTime, DateTime.Now) <= 7, 1, 0)),
  223. StockKeepTime15 = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.DateDiff(DateType.Day, it.ProductTime, DateTime.Now) > 7 && SqlFunc.DateDiff(DateType.Day, it.ProductTime, DateTime.Now) <= 15, 1, 0)),
  224. StockKeepTime30 = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.DateDiff(DateType.Day, it.ProductTime, DateTime.Now) > 15 && SqlFunc.DateDiff(DateType.Day, it.ProductTime, DateTime.Now) <= 30, 1, 0)),
  225. StockKeepTime31 = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.DateDiff(DateType.Day, it.ProductTime, DateTime.Now) > 30, 1, 0)),
  226. MatName = it.MatName,
  227. MatCode = it.MatCode
  228. });
  229. return sugarQueryable;
  230. }
  231. public PagedInfo<MatNameNetWeightCategory> GetMatNameNetWeightCategory(Pagination pagination, BillInvNowQueryDto billInvNowQueryDto)
  232. {
  233. ISugarQueryable<MatNameNetWeightCategory> sugarQueryable = GetMatNameNetWeightCategoryQueryable(billInvNowQueryDto);
  234. var lists = sugarQueryable.ToPage(pagination);
  235. return lists;
  236. }
  237. public IList<MatNameNetWeightCategory> GetMatNameNetWeightCategorys(BillInvNowQueryDto billInvNowQueryDto)
  238. {
  239. ISugarQueryable<MatNameNetWeightCategory> sugarQueryable = GetMatNameNetWeightCategoryQueryable(billInvNowQueryDto);
  240. var list = sugarQueryable.ToList();
  241. return list;
  242. }
  243. private ISugarQueryable<MatNameNetWeightCategory> GetMatNameNetWeightCategoryQueryable(BillInvNowQueryDto billInvNowQueryDto)
  244. {
  245. var predicate = Expressionable.Create<BillInvnow>();
  246. predicate = predicate.AndIF(!string.IsNullOrEmpty(billInvNowQueryDto?.KeyWord), billInvnow => billInvnow.MatCode.Contains(billInvNowQueryDto.KeyWord) || billInvnow.MatName.Contains(billInvNowQueryDto.KeyWord));
  247. var sugarQueryable = _billInvnowrepository.Queryable().Where(o => o.InvStateCode == "InvEcecState_In")
  248. .Where(predicate.ToExpression())
  249. .Select(it => new MatNameNetWeightCategory
  250. {
  251. MatName = it.MatName,
  252. MatCode = it.MatCode,
  253. NetWQty = SqlFunc.AggregateSum(it.TolWQty),
  254. Qty = SqlFunc.AggregateDistinctCount(it.Id)
  255. })
  256. .GroupBy(o => new { o.MatName, o.MatCode });
  257. return sugarQueryable;
  258. }
  259. }
  260. }