ReportService.cs 18 KB

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