/NorthwindDemo/step-2/aspnet-mvc/Northwind.Web.Dashboard/Controllers/RegionalSalesController.cs

https://github.com/Chinchilla-Software-Com/CQRS
C# | 207 lines | 192 code | 15 blank | 0 comment | 25 complexity | 05f30bbe23a3e56f94c713825103b90f MD5 | raw file
  1. using Northwind.Web.Dashboard.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. namespace Northwind.Web.Dashboard.Controllers
  9. {
  10. public class RegionalSalesController : Controller
  11. {
  12. public ActionResult TopSellingProducts(string Country, DateTime FromDate, DateTime ToDate)
  13. {
  14. var northwind = new NorthwindEntities();
  15. var topSellers = (from top in
  16. (from allSales in
  17. (from o in northwind.Orders
  18. join od in northwind.Order_Details on o.OrderID equals od.OrderID
  19. where o.OrderDate >= FromDate && o.OrderDate <= ToDate && o.ShipCountry == Country
  20. select new
  21. {
  22. ProductID = od.ProductID,
  23. Quantity = od.Quantity,
  24. Date = o.OrderDate,
  25. }).AsEnumerable()
  26. group allSales by new { allSales.ProductID, Date = new DateTime(allSales.Date.Value.Year, allSales.Date.Value.Month, 1) } into g
  27. select new
  28. {
  29. ProductID = g.Key.ProductID,
  30. Quantity = g.Sum(x => x.Quantity),
  31. Date = g.Key.Date
  32. })
  33. group top by top.ProductID into g
  34. orderby g.Sum(x => x.Quantity) descending
  35. select new
  36. {
  37. ProductID = g.FirstOrDefault().ProductID
  38. }).Take(5);
  39. var all = (from allSales in
  40. (from o in northwind.Orders
  41. join od in northwind.Order_Details on o.OrderID equals od.OrderID
  42. where o.OrderDate >= FromDate && o.OrderDate <= ToDate && o.ShipCountry == Country
  43. select new
  44. {
  45. ProductID = od.ProductID,
  46. Quantity = od.Quantity,
  47. Date = o.OrderDate,
  48. }).AsEnumerable()
  49. group allSales by new { allSales.ProductID, Date = new DateTime(allSales.Date.Value.Year, allSales.Date.Value.Month, 1) } into g
  50. where topSellers.Contains(new { ProductID = g.Key.ProductID })
  51. select new
  52. {
  53. ProductID = g.Key.ProductID,
  54. Quantity = g.Sum(x => x.Quantity),
  55. Date = g.Key.Date
  56. });
  57. var result = (from s in all
  58. join p in northwind.Products on s.ProductID equals p.ProductID
  59. select new
  60. {
  61. ProductName = p.ProductName,
  62. Date = s.Date,
  63. Quantity = s.Quantity
  64. });
  65. return Json(result, JsonRequestBehavior.AllowGet);
  66. }
  67. public ActionResult CountryCompanies(string Country)
  68. {
  69. var northwind = new NorthwindEntities();
  70. var companies = northwind.Customers.Select(customer => new CustomerViewModel
  71. {
  72. CompanyName = customer.CompanyName,
  73. Country = customer.Country
  74. }).Where(x => x.Country == Country);
  75. return Json(companies, JsonRequestBehavior.AllowGet);
  76. }
  77. public ActionResult MarketShareByCountry(string Country, DateTime FromDate, DateTime ToDate)
  78. {
  79. var northwind = new NorthwindEntities();
  80. var allSales = (from o in northwind.Orders
  81. join od in northwind.Order_Details on o.OrderID equals od.OrderID
  82. where o.OrderDate >= FromDate && o.OrderDate <= ToDate
  83. select new
  84. {
  85. Country = o.ShipCountry,
  86. Sales = od.Quantity * od.UnitPrice
  87. }).AsEnumerable();
  88. return Json(new [] {
  89. new { Country = "All", Sales = (decimal?)allSales.Sum(x => x.Sales) ?? 0},
  90. new { Country = Country, Sales = (decimal?)allSales.Where(w=>w.Country == Country).Sum(s => s.Sales) ?? 0}
  91. }, JsonRequestBehavior.AllowGet);
  92. }
  93. public ActionResult CountryRevenue(string Country, DateTime FromDate, DateTime ToDate)
  94. {
  95. var northwind = new NorthwindEntities();
  96. var q1 = (from o in northwind.Orders
  97. join od in northwind.Order_Details on o.OrderID equals od.OrderID
  98. where o.OrderDate >= FromDate && o.OrderDate <= ToDate && o.ShipCountry == Country
  99. select new
  100. {
  101. OrderID = o.OrderID,
  102. EmployeeID = o.EmployeeID,
  103. Date = o.OrderDate,
  104. Sales = od.Quantity * od.UnitPrice
  105. }).AsEnumerable();
  106. var q2 = (from allSales in q1
  107. group allSales by allSales.OrderID into g
  108. select new
  109. {
  110. OrderID = g.Key,
  111. EmployeeID = g.FirstOrDefault().EmployeeID,
  112. Sales = g.Sum(x => x.Sales),
  113. Date = new DateTime(g.FirstOrDefault().Date.Value.Year, g.FirstOrDefault().Date.Value.Month, 1),
  114. });
  115. var q3 = (from groupedSales in q2
  116. group groupedSales by new { groupedSales.EmployeeID, groupedSales.Date } into gs
  117. select new
  118. {
  119. EmployeeID = gs.FirstOrDefault().EmployeeID,
  120. Date = gs.Key.Date,
  121. Sales = gs.Sum(x => x.Sales)
  122. });
  123. var result = (from totalSales in q3
  124. group totalSales by totalSales.Date into gs
  125. select new
  126. {
  127. Date = gs.Key,
  128. Value = gs.Sum(x => x.Sales)
  129. }
  130. );
  131. return Json(result, JsonRequestBehavior.AllowGet);
  132. }
  133. public ActionResult CountryOrders(string Country, DateTime FromDate, DateTime ToDate)
  134. {
  135. var northwind = new NorthwindEntities();
  136. IQueryable<Order> data = northwind.Orders.Where(o => o.OrderDate >= FromDate && o.OrderDate <= ToDate && o.ShipCountry == Country);
  137. var result = from o in data
  138. group o by o.OrderDate into g
  139. select new { Date = g.Key, Value = g.Count() };
  140. return Json(result, JsonRequestBehavior.AllowGet);
  141. }
  142. public ActionResult CountryOrdersTotal(string Country, DateTime FromDate, DateTime ToDate)
  143. {
  144. var northwind = new NorthwindEntities();
  145. IQueryable<Order> data = northwind.Orders.Where(o => o.OrderDate >= FromDate && o.OrderDate <= ToDate && o.ShipCountry == Country);
  146. var result = from o in data
  147. group o by o.OrderDate into g
  148. select new { Date = g.Key, Value = g.Count() };
  149. int? total = 0;
  150. if (result.Count() > 0)
  151. {
  152. total = result.Sum(x => x.Value);
  153. }
  154. return Json(new { Orders = total}, JsonRequestBehavior.AllowGet);
  155. }
  156. public ActionResult CountryCustomers(string Country, DateTime FromDate, DateTime ToDate)
  157. {
  158. var northwind = new NorthwindEntities();
  159. var result = (from allCustomers in
  160. (from o in northwind.Orders
  161. join od in northwind.Order_Details on o.OrderID equals od.OrderID
  162. where o.OrderDate >= FromDate && o.OrderDate <= ToDate && o.ShipCountry == Country
  163. select new
  164. {
  165. CustomerID = o.CustomerID,
  166. Date = o.OrderDate
  167. }).AsEnumerable()
  168. group allCustomers by new { Date = new DateTime(allCustomers.Date.Value.Year, allCustomers.Date.Value.Month, 1) } into g
  169. select new
  170. {
  171. Date = g.Key.Date,
  172. Value = g.GroupBy(x => x.CustomerID).Count()
  173. }
  174. );
  175. return Json(result, JsonRequestBehavior.AllowGet);
  176. }
  177. public ActionResult CountryCustomersTotal(string Country, DateTime FromDate, DateTime ToDate)
  178. {
  179. var northwind = new NorthwindEntities();
  180. var result = (from allCustomers in
  181. (from o in northwind.Orders
  182. where o.OrderDate >= FromDate && o.OrderDate <= ToDate && o.ShipCountry == Country
  183. select new
  184. {
  185. CustomerID = o.CustomerID,
  186. })
  187. group allCustomers by allCustomers.CustomerID
  188. ).Count();
  189. return Json(new { Customers = result }, JsonRequestBehavior.AllowGet);
  190. }
  191. }
  192. }