You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

215 lines
8.0 KiB

  1. using DataVApi.Order.Dto;
  2. using DataVApi.Order.RequestModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. using XExten.Advance.HttpFramework.MultiFactory;
  10. namespace DataVApi.Order
  11. {
  12. public class OrderProvider : SugarDb
  13. {
  14. /// <summary>
  15. /// 全部数据
  16. /// </summary>
  17. /// <param name="input"></param>
  18. /// <returns></returns>
  19. public object GetAllOrderDataState(FullScreenInput input)
  20. {
  21. var db = DbContext();
  22. StringBuilder sb = new StringBuilder();
  23. sb.Append(@"SELECT
  24. a.Id,
  25. b.CreatedAt as Time,
  26. b.OrderRealMoney as Money
  27. FROM
  28. BPA_Organize AS a
  29. JOIN BPA_Order AS b ON a.Id= b.OrgId
  30. WHERE
  31. ( b.PayMode= 1 OR b.PayMode= 4 )
  32. AND ( b.TransactionId IS NOT NULL OR b.TransactionId != '' )
  33. AND ( b.TradeNo IS NOT NULL OR b.TradeNo != '' )");
  34. if (input.OrgId.HasValue)
  35. {
  36. sb.Append(@$" AND a.AutoKey = {input.OrgId.Value} ");
  37. if (input.DeviceId.HasValue)
  38. sb.Append($@" AND b.Id IN(SELECT c.OrderId FROM BPA_SubOrder as c JOIN BPA_DeviceInfo as d ON c.StoreId = d.Id WHERE d.AutoKey={input.DeviceId.Value} AND d.OrgId=a.Id)");
  39. }
  40. if (input.Star.HasValue)
  41. sb.Append(@$" AND b.CreatedAt>='{input.Star.Value}'");
  42. if (input.End.HasValue)
  43. sb.Append(@$" AND b.CreatedAt<'{input.End.Value}'");
  44. var sql = sb.ToString();
  45. var data = db.SqlQueryable<OrderDataStateDto>(sql).ToList();
  46. return new
  47. {
  48. Total = data.Sum(t => t.Money).ToString(),
  49. OrderCount = data.Count().ToString(),
  50. MoneyM = data.Where(t => t.Time.Month == DateTime.Now.Month).Sum(t => t.Money).ToString(),
  51. MoneyD = data.Where(t => t.Time >= DateTime.Parse(DateTime.Now.ToShortDateString()) && t.Time < DateTime.Parse(DateTime.Now.ToShortDateString()).AddDays(1)).Sum(t => t.Money).ToString(),
  52. StoreCount = data.GroupBy(t => t.Id).Count()
  53. };
  54. }
  55. /// <summary>
  56. /// 折线图
  57. /// </summary>
  58. /// <returns></returns>
  59. public object OrderLine(FullScreenBasic input)
  60. {
  61. var db = DbContext();
  62. StringBuilder sb = new StringBuilder();
  63. sb.Append(@"SELECT
  64. b.OrderStatus,
  65. b.CreatedAt,
  66. b.AfterStatus,
  67. b.PayMode
  68. FROM
  69. BPA_Organize AS a
  70. JOIN BPA_Order AS b ON a.Id= b.OrgId
  71. WHERE
  72. ( b.TransactionId IS NOT NULL OR b.TransactionId != '' )
  73. AND ( b.TradeNo IS NOT NULL OR b.TradeNo != '' )");
  74. if (input.OrgId.HasValue)
  75. {
  76. sb.Append(@$" AND a.AutoKey = {input.OrgId.Value} ");
  77. if (input.DeviceId.HasValue)
  78. sb.Append($@" AND b.Id IN(SELECT c.OrderId FROM BPA_SubOrder as c JOIN BPA_DeviceInfo as d ON c.StoreId = d.Id WHERE d.AutoKey={input.DeviceId.Value} AND d.OrgId=a.Id)");
  79. }
  80. var sql = sb.ToString();
  81. var data = db.SqlQueryable<OrderLineDto>(sql).ToList();
  82. List<object> res = new List<object>();
  83. int indexcount = input.Count.HasValue ? input.Count.Value : 10;
  84. for (int index = indexcount; index >= 0; index--)
  85. {
  86. var temp = DateTime.Now.AddDays(-index);
  87. var end = DateTime.Parse(temp.ToString("yyyy-MM-dd 23:59:59"));
  88. var start = DateTime.Parse(temp.ToString("yyyy-MM-dd 00:00:00"));
  89. var NO = data.Where(t => t.CreatedAt < end && t.CreatedAt >= start).Where(t => t.AfterStatus != 0 && (t.OrderStatus == 1 || t.PayMode == 4)).Count();
  90. var OK = data.Where(t => t.CreatedAt < end && t.CreatedAt >= start).Where(t => t.AfterStatus == 0 && (t.OrderStatus == 1 || t.PayMode == 4)).Count();
  91. var Date = temp.ToString("MM/dd");
  92. res.Add(new
  93. {
  94. NO,
  95. OK,
  96. Date
  97. });
  98. }
  99. return new { data= res };
  100. }
  101. /// <summary>
  102. /// 区域销售
  103. /// </summary>
  104. /// <returns></returns>
  105. public object GetLocSale()
  106. {
  107. var db = DbContext();
  108. var baiduMap = "http://api.map.baidu.com/geocoder/v2/?ak=3ce7ddfda60046708248ea55e10b1435&callback=renderReverse&location={0}&output=xml";
  109. List<LocSaleDto> loc = new List<LocSaleDto>();
  110. var data = db.SqlQueryable<dynamic>(@"SELECT
  111. a.Store_Loc AS Loc,
  112. (SELECT Count(b.Id) FROM BPA_Order as b WHERE b.OrgId=a.Id) as Count
  113. FROM
  114. BPA_Organize AS a
  115. WHERE
  116. a.IsDeleted = 0
  117. AND a.Store_Loc LIKE '%,%';
  118. ").ToList();
  119. IHttpMultiClient http = null;
  120. data.ForEach(item =>
  121. {
  122. if (http == null)
  123. http = IHttpMultiClient.HttpMulti.AddNode(opt =>
  124. {
  125. opt.NodePath = string.Format(baiduMap, item.Loc);
  126. });
  127. else
  128. http = http.AddNode(opt =>
  129. {
  130. opt.NodePath = string.Format(baiduMap, item.Loc);
  131. });
  132. });
  133. XmlDocument doc = new XmlDocument();
  134. var Temps = http.Build().RunString();
  135. for (int i = 0; i < Temps.Count; i++)
  136. {
  137. doc.LoadXml(Temps[i]);
  138. loc.Add(new LocSaleDto
  139. {
  140. Count = data[i].Count,
  141. City = doc.SelectSingleNode("//province")?.InnerText + doc.SelectSingleNode("//city")?.InnerText
  142. });
  143. }
  144. return new
  145. {
  146. data = loc.GroupBy(t => t.City).Select(t => new LocSaleDto
  147. {
  148. City = string.IsNullOrEmpty(t.Key)?"未知": t.Key,
  149. Count = t.Sum(t => t.Count)
  150. })
  151. };
  152. }
  153. /// <summary>
  154. /// 下单数量
  155. /// </summary>
  156. /// <returns></returns>
  157. public object OrderNumber(FullScreenBasic input)
  158. {
  159. var db = DbContext();
  160. StringBuilder sb = new StringBuilder();
  161. sb.Append(@"SELECT
  162. b.CreatedAt
  163. FROM
  164. BPA_Organize AS a
  165. JOIN BPA_Order AS b ON a.Id= b.OrgId
  166. WHERE
  167. ( b.PayMode= 1 OR b.PayMode= 4 )
  168. AND ( b.TransactionId IS NOT NULL OR b.TransactionId != '' )
  169. AND ( b.TradeNo IS NOT NULL OR b.TradeNo != '' )");
  170. if (input.OrgId.HasValue)
  171. {
  172. sb.Append(@$" AND a.AutoKey = {input.OrgId.Value} ");
  173. if (input.DeviceId.HasValue)
  174. sb.Append($@" AND b.Id IN(SELECT c.OrderId FROM BPA_SubOrder as c JOIN BPA_DeviceInfo as d ON c.StoreId = d.Id WHERE d.AutoKey={input.DeviceId.Value} AND d.OrgId=a.Id)");
  175. }
  176. var sql = sb.ToString();
  177. var data = db.SqlQueryable<OrderNumberDto>(sql).ToList();
  178. int Year = data.Where(t => t.CreatedAt.Year == DateTime.Now.Year).Count();
  179. int Month = data.Where(t => t.CreatedAt.Month == DateTime.Now.Month).Count();
  180. int Day = data.Where(t => t.CreatedAt.Day == DateTime.Now.Day).Count();
  181. int L_Year = data.Where(t => t.CreatedAt.Year == DateTime.Now.AddYears(-1).Year).Count();
  182. int L_Month = data.Where(t => t.CreatedAt.Month == DateTime.Now.AddMonths(-1).Month).Count();
  183. int L_Day = data.Where(t => t.CreatedAt.Day == DateTime.Now.AddDays(-1).Day).Count();
  184. return new
  185. {
  186. Year = Year, Month = Month, Day = Day,
  187. L_Year = L_Year, L_Month = L_Month, L_Day = L_Day,
  188. B_Year = ((Year + L_Year) == 0) ? 0 : (((Year - L_Year) * 100) / (Year + L_Year)),
  189. B_Month = ((Month + L_Month) == 0) ? 0 : (((Month - L_Month) * 100) / (Month + L_Month)),
  190. B_Day = ((Day + L_Day) == 0) ? 0 : (((Day - L_Day) * 100) / (Day + L_Day))
  191. };
  192. }
  193. }
  194. }