ReportService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.cp;
  10. using WMS.BZSqlSugar;
  11. using WMS.Util;
  12. using WMS.BZModels.Dto.CP.TaskDtos;
  13. using NPOI.SS.Formula.Functions;
  14. using WMS.BZModels.Dto.CP.RfidDtos;
  15. using WMS.BZModels.Dto.CP.BaseWareareaDtos;
  16. using WMS.BZModels.Dto.CP.BillInvDtos;
  17. using WMS.BZModels.Dto.CP.ReportDtos;
  18. namespace WMS.BZServices.CP
  19. {
  20. public class ReportService
  21. {
  22. private readonly Repository<BillInvflow> _reportrepository;
  23. private readonly Repository<BaseWarecell> _warecellRepository;
  24. private readonly Repository<BillInvnow> _billInvnowrepository;
  25. public ReportService(Repository<BillInvflow> reportrepository, Repository<BaseWarecell> warecellRepository, Repository<BillInvnow> billInvnowrepository)
  26. {
  27. _reportrepository = reportrepository;
  28. _warecellRepository = warecellRepository;
  29. _billInvnowrepository = billInvnowrepository;
  30. }
  31. public PagedInfo<ReportDto> GetPageList(Pagination pagination, ReportQueryDto reportQueryDto)
  32. {
  33. if (pagination.sord.ToUpper() != "ASC")
  34. {
  35. pagination.sidx = pagination.sidx.IsEmpty() ? "AddTime DESC" : pagination.sidx + " DESC";
  36. }
  37. if (pagination.sidx.IsEmpty())
  38. {
  39. pagination.sidx = "AddTime DESC";
  40. }
  41. var list = GetQueryable(reportQueryDto).ToPage(pagination);
  42. return list;
  43. }
  44. private ISugarQueryable<ReportDto> GetQueryable(ReportQueryDto reportQueryDto)
  45. {
  46. var predicate = Expressionable.Create<BillInvflow, BaseWarehouse, BaseWarecell>();
  47. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.KeyWord), (billInvflow, wareHouse, wareCell) => billInvflow.ContGrpBarCode.Contains(reportQueryDto.KeyWord) || billInvflow.MatCode.Contains(reportQueryDto.KeyWord) ||
  48. billInvflow.MatName.Contains(reportQueryDto.KeyWord) || billInvflow.BoxBarCode.Contains(reportQueryDto.KeyWord) || wareCell.Code.Contains(reportQueryDto.KeyWord));
  49. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.ContGrpBarCode), (billInvflow, wareHouse, wareCell) => billInvflow.ContGrpBarCode.Contains(reportQueryDto.ContGrpBarCode));
  50. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.MatCode), (billInvflow, wareHouse, wareCell) => billInvflow.MatCode.Contains(reportQueryDto.MatCode));
  51. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.MatName), (billInvflow, wareHouse, wareCell) => billInvflow.MatName.Contains(reportQueryDto.MatName));
  52. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.RFIDBarCode), (billInvflow, wareHouse, wareCell) => billInvflow.BoxBarCode.Contains(reportQueryDto.RFIDBarCode));
  53. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.CellCode), (billInvflow, wareHouse, wareCell) => wareCell.Code.Contains(reportQueryDto.CellCode));
  54. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.Tunnel), (billInvflow, wareHouse, wareCell) => wareCell.Tunnel == int.Parse(reportQueryDto.Tunnel));
  55. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.InvBarCode), (billInvflow, wareHouse, wareCell) => billInvflow.InvBarCode.Contains(reportQueryDto.InvBarCode));
  56. predicate = predicate.AndIF(reportQueryDto != null && reportQueryDto.AddTimeFrom.HasValue, (billInvflow, wareHouse, wareCell) => billInvflow.AddTime >= reportQueryDto.AddTimeFrom);
  57. predicate = predicate.AndIF(reportQueryDto != null && reportQueryDto.AddTimeTo.HasValue, (billInvflow, wareHouse, wareCell) => billInvflow.AddTime <= reportQueryDto.AddTimeTo);
  58. predicate = predicate.AndIF(reportQueryDto != null && reportQueryDto.EndTimeBegin.HasValue, (billInvflow, wareHouse, wareCell) => billInvflow.EditTime >= reportQueryDto.EndTimeBegin);
  59. predicate = predicate.AndIF(reportQueryDto != null && reportQueryDto.EndTimeEnd.HasValue, (billInvflow, wareHouse, wareCell) => billInvflow.EditTime <= reportQueryDto.EndTimeEnd);
  60. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.IsSurplus), (billInvflow, wareHouse, wareCell) => billInvflow.IsSurplus.Equals(reportQueryDto.IsSurplus));
  61. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.IsRework), (billInvflow, wareHouse, wareCell) => billInvflow.IsRework.Equals(reportQueryDto.IsRework));
  62. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.IsFast), (billInvflow, wareHouse, wareCell) => billInvflow.IsFast.Equals(reportQueryDto.IsFast));
  63. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.IsFail), (billInvflow, wareHouse, wareCell) => billInvflow.IsFail.Equals(reportQueryDto.IsFail));
  64. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.IsBack), (billInvflow, wareHouse, wareCell) => billInvflow.IsBack.Equals(reportQueryDto.IsBack));
  65. if (!string.IsNullOrEmpty(reportQueryDto?.InvStateCode))
  66. {
  67. var enums = (InvState)Enum.ToObject(typeof(InvState), Convert.ToInt32(reportQueryDto?.InvStateCode));
  68. predicate = predicate.AndIF(!string.IsNullOrEmpty(reportQueryDto?.InvStateCode), (billInvflow, wareHouse, wareCell) => billInvflow.InvStateCode.Equals(enums.ToString()));
  69. }
  70. var queryable = _reportrepository.Context.Queryable<BillInvflow, BaseWarehouse, BaseWarecell>((billInvflow, wareHouse, wareCell) => new object[] {
  71. JoinType.Left, billInvflow.WarehouseId == wareHouse.Id,
  72. JoinType.Left, billInvflow.PutRow == wareCell.Row && billInvflow.PutCol == wareCell.Col && billInvflow.PutLayer == wareCell.Layer,
  73. JoinType.Left,wareCell.WarehouseId==wareHouse.Id
  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. HuTotalWt = billInvflow.HuTotalWt,
  142. }).MergeTable();
  143. return queryable;
  144. }
  145. public IList<ReportDto> GetList(ReportQueryDto reportQueryDto)
  146. {
  147. ISugarQueryable<ReportDto> sugarQueryable = GetQueryable(reportQueryDto);
  148. var list = sugarQueryable.ToList();
  149. return list;
  150. }
  151. /// <summary>
  152. /// 货位分析
  153. /// </summary>
  154. /// <returns></returns>
  155. public List<LocationUsageReportViewDto> GetLocationUsageReportList()
  156. {
  157. var list = _warecellRepository.Context.Queryable<BaseWarecell, BaseWarehouse>((warecell, warehouse) => new object[] {
  158. JoinType.Left,warecell.WarehouseId == warehouse.Id })
  159. .GroupBy((warecell, warehouse) => new
  160. {
  161. warecell.StateNum,
  162. warecell.IsStop,
  163. warecell.Tunnel,
  164. warehouse.Id,
  165. warehouse.Name
  166. }).Select((warecell, warehouse) => new
  167. {
  168. Status = warecell.StateNum,
  169. warecell.Tunnel,
  170. warecell.IsStop,
  171. HouseId = warehouse.Id,
  172. warehouse.Name,
  173. Total = SqlFunc.AggregateCount(warecell.Tunnel),
  174. })
  175. .MergeTable().ToList().OrderBy(o => o.Tunnel);
  176. int _sort = 1;
  177. //基表:巷道、货架标识和仓库名称为维度查找总货位
  178. 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();
  179. //关联表:巷道和仓库名称为维度,查找可用货位:未停用.已锁定也算有效货位
  180. 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();
  181. //关联表:巷道和仓库名称为维度,查找锁定货位
  182. 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();
  183. //关联表:巷道和仓库名称为维度,查找停用货位
  184. 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();
  185. //关联表:巷道和仓库名称为维度,查找物料货位
  186. 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();
  187. var locationUsageReportViewDtos = from basetemp in baseTemp
  188. join uselocation in useLocation on new { basetemp.Tunnel, basetemp.Name } equals new { uselocation.Tunnel, uselocation.Name } into useinfo
  189. join locklocation in lockLocation on new { basetemp.Tunnel, basetemp.Name } equals new { locklocation.Tunnel, locklocation.Name } into lockinfo
  190. from lockinfoif in lockinfo.DefaultIfEmpty()
  191. join stoplocation in stopLocation on new { basetemp.Tunnel, basetemp.Name } equals new { stoplocation.Tunnel, stoplocation.Name } into stopinfo
  192. from stopinfoif in stopinfo.DefaultIfEmpty()
  193. join materiallocation in materialLocation on new { basetemp.Tunnel, basetemp.Name } equals new { materiallocation.Tunnel, materiallocation.Name } into materialinfo
  194. from materialinfoif in materialinfo.DefaultIfEmpty()
  195. orderby basetemp.Name
  196. select new LocationUsageReportViewDto()
  197. {
  198. Sort = _sort++,
  199. Tunnel = basetemp.Tunnel.ToString(),
  200. WarehouseName = basetemp.Name,
  201. AllLocationTotal = basetemp.Total,
  202. CanUseLocation = useinfo.FirstOrDefault() == null ? 0 : useinfo.First().Total,
  203. LockLocation = lockinfoif == null ? 0 : lockinfoif.Total,
  204. StopLocation = stopinfoif == null ? 0 : stopinfoif.Total,
  205. MaterilLocation = materialinfoif == null ? 0 : materialinfoif.Total
  206. };
  207. return locationUsageReportViewDtos.OrderBy(p => p.WarehouseName).ToList();
  208. }
  209. public PagedInfo<StockKeepReportViewDto> GetStockKeepReportList(Pagination pagination, BillInvNowQueryDto billInvNowQueryDto)
  210. {
  211. ISugarQueryable<StockKeepReportViewDto> queryable = GetStockKeepQueryable(billInvNowQueryDto);
  212. var lists = queryable.ToPage(pagination);
  213. return lists;
  214. }
  215. public IList<StockKeepReportViewDto> GetStockKeepReports(BillInvNowQueryDto billInvNowQueryDto)
  216. {
  217. ISugarQueryable<StockKeepReportViewDto> sugarQueryable = GetStockKeepQueryable(billInvNowQueryDto);
  218. var list = sugarQueryable.ToList();
  219. return list;
  220. }
  221. private ISugarQueryable<StockKeepReportViewDto> GetStockKeepQueryable(BillInvNowQueryDto billInvNowQueryDto)
  222. {
  223. var predicate = Expressionable.Create<BillInvnow>();
  224. predicate = predicate.AndIF(!string.IsNullOrEmpty(billInvNowQueryDto?.KeyWord), billInvnow => billInvnow.MatCode.Contains(billInvNowQueryDto.KeyWord) || billInvnow.MatName.Contains(billInvNowQueryDto.KeyWord));
  225. var queryable = _billInvnowrepository.Context.Queryable<BillInvnow>().Where(predicate.ToExpression())
  226. .GroupBy(it => new { it.MatCode, it.MatName })
  227. .Select(it => new StockKeepReportViewDto
  228. {
  229. 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)),
  230. 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)),
  231. 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)),
  232. StockKeepTime31 = SqlFunc.AggregateSum(SqlFunc.IIF(SqlFunc.DateDiff(DateType.Day, it.ProductTime, DateTime.Now) > 30, 1, 0)),
  233. MatName = it.MatName,
  234. MatCode = it.MatCode
  235. });
  236. return queryable;
  237. }
  238. public PagedInfo<MatNameNetWeightCategory> GetMatNameNetWeightCategory(Pagination pagination, BillInvNowQueryDto billInvNowQueryDto)
  239. {
  240. ISugarQueryable<MatNameNetWeightCategory> queryable = GetMatNameNetWeightCategoryQueryable(billInvNowQueryDto);
  241. var lists = queryable.ToPage(pagination); ;
  242. return lists;
  243. }
  244. public IList<MatNameNetWeightCategory> GetMatNameNetWeightCategorys(BillInvNowQueryDto billInvNowQueryDto)
  245. {
  246. ISugarQueryable<MatNameNetWeightCategory> sugarQueryable = GetMatNameNetWeightCategoryQueryable(billInvNowQueryDto);
  247. var list = sugarQueryable.ToList();
  248. return list;
  249. }
  250. private ISugarQueryable<MatNameNetWeightCategory> GetMatNameNetWeightCategoryQueryable(BillInvNowQueryDto billInvNowQueryDto)
  251. {
  252. var predicate = Expressionable.Create<BillInvnow>();
  253. predicate = predicate.AndIF(!string.IsNullOrEmpty(billInvNowQueryDto?.KeyWord), billInvnow => billInvnow.MatCode.Contains(billInvNowQueryDto.KeyWord) || billInvnow.MatName.Contains(billInvNowQueryDto.KeyWord));
  254. var queryable = _billInvnowrepository.Queryable().Where(o => o.InvStateCode == "InvEcecState_In" && o.ContGrpType == 1)
  255. .Where(predicate.ToExpression())
  256. .Select(it => new MatNameNetWeightCategory
  257. {
  258. MatName = it.MatName,
  259. MatCode = it.MatCode,
  260. NetWQty = SqlFunc.AggregateSum(it.TolWQty),
  261. Qty = SqlFunc.AggregateDistinctCount(it.Id)
  262. })
  263. .GroupBy(o => new { o.MatName, o.MatCode });
  264. return queryable;
  265. }
  266. }
  267. }