App_PivotReport.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using AutoMapper;
  2. using Edge.Core.UniversalApi;
  3. using Gateway.POS.Models;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace Gateway.POS
  13. {
  14. public partial class App
  15. {
  16. #region PivotReport
  17. [UniversalApi]
  18. public async Task<object> PivotReportGetFuelItemsByFuelProductNameForTimeRange(DateTime start, DateTime end)
  19. {
  20. using (var db = new PosAppDbContext())
  21. {
  22. var results = await db.FuelItems.Include(f => f.Transaction)
  23. .Where(fi => fi.Transaction.ServerSideTimestamp >= start && fi.Transaction.ServerSideTimestamp <= end)
  24. .GroupBy(fi => fi.FuelProductName)
  25. .Select(g => new { FuelProductName = g.Key, SumQualtity = g.Sum(fis => fis.Qualtity), SumAmount = g.Sum(fis => fis.Amount) })
  26. .ToListAsync();
  27. return results;
  28. }
  29. }
  30. [UniversalApi]
  31. public async Task<object> PivotReportGetPaymentsByFuelProductNameForTimeRange(DateTime start, DateTime end)
  32. {
  33. using (var db = new PosAppDbContext())
  34. {
  35. var results = await db.Payments.Include(f => f.Transaction)
  36. .Where(fi => fi.Transaction.ServerSideTimestamp >= start && fi.Transaction.ServerSideTimestamp <= end)
  37. .GroupBy(fi => fi.Method)
  38. .Select(g => new { MethodName = g.Key, SumPaidAmount = g.Sum(fis => fis.PaidAmount), Count = g.Count() })
  39. .ToListAsync();
  40. return results;
  41. }
  42. }
  43. [UniversalApi]
  44. public async Task<object> PivotReportGetOperatorSalesByOperatorNameForTimeRange(DateTime start, DateTime end)
  45. {
  46. using (var db = new PosAppDbContext())
  47. {
  48. var results = await db.Transactions.Include(t => t.Operator).Include(t => t.FuelItems).Include(t => t.Payments)
  49. .Where(trx => trx.ServerSideTimestamp >= start && trx.ServerSideTimestamp <= end)
  50. .GroupBy(trx => new { trx.OperatorId, trx.Operator.Name, Mop = trx.Payments.Select(pa => pa.Method).FirstOrDefault() })
  51. .Select(g => new { Operator = g.Key, SumPaidAmount = g.Sum(trxs => trxs.NetAmount), TrxCount = g.Count() })
  52. .ToListAsync();
  53. var ops = results.GroupBy(r => new { r.Operator.OperatorId, r.Operator.Name })
  54. .Select(g => new { Operator = new { g.Key.OperatorId, g.Key.Name }, Sales = g.Select(gg => new { PaymentMethodName = gg.Operator.Mop, gg.SumPaidAmount, gg.TrxCount }) });
  55. //Dictionary<string, object> datas = new Dictionary<string, object>();
  56. //foreach (var op in ops)
  57. //{
  58. // //if (!datas.ContainsKey(op.Key.OperatorId + "|" + op.Key.Name))
  59. // // datas.Add(op.Key.OperatorId + "|" + op.Key.Name);
  60. // foreach (var r in results)
  61. // {
  62. // if (op.Key.OperatorId == r.Operator.OperatorId && op.Key.Name == r.Operator.Name)
  63. // {
  64. // }
  65. // }
  66. //}
  67. return ops;
  68. }
  69. }
  70. [UniversalApi]
  71. public async Task<object> PivotReportGetNozzlesBySiteLevelNozzleIdForTimeRange(DateTime start, DateTime end)
  72. {
  73. using (var db = new PosAppDbContext())
  74. {
  75. var results = await db.FuelItems.Include(f => f.Transaction)
  76. .Where(fi => fi.Transaction.ServerSideTimestamp >= start && fi.Transaction.ServerSideTimestamp <= end)
  77. .GroupBy(fi => fi.SiteLevelNozzleId)
  78. .Select(g => new
  79. {
  80. SiteLevelNozzleId = g.Key,
  81. SumQualtity = g.Sum(fis => fis.Qualtity),
  82. SumAmount = g.Sum(fis => fis.Amount),
  83. MaxTotalVolumeDiff = g.Max(fis => fis.TotalVolume ?? 0) - g.Min(fis => fis.TotalVolume ?? 0),
  84. FuelingCount = g.Count()
  85. })
  86. .ToListAsync();
  87. return results;
  88. }
  89. }
  90. #endregion
  91. }
  92. }