ReportService.cs 19 KB

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