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.
 
 

236 lines
9.1 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.TransactionId IS NOT NULL OR b.TransactionId != '' )
  32. AND ( b.TradeNo IS NOT NULL OR b.TradeNo != '' )");
  33. if (input.OrgId.HasValue)
  34. {
  35. sb.Append(@$" AND a.AutoKey = {input.OrgId.Value} ");
  36. if (input.DeviceId.HasValue)
  37. 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)");
  38. }
  39. if (input.Star.HasValue)
  40. sb.Append(@$" AND b.CreatedAt>='{input.Star.Value}'");
  41. if (input.End.HasValue)
  42. sb.Append(@$" AND b.CreatedAt<'{input.End.Value}'");
  43. var sql = sb.ToString();
  44. var data = db.SqlQueryable<OrderDataStateDto>(sql).ToList();
  45. return new
  46. {
  47. Total = data.Sum(t => t.Money).ToString(),
  48. OrderCount = data.Count().ToString(),
  49. MoneyM = data.Where(t => t.Time.Year == DateTime.Now.Year && t.Time.Month == DateTime.Now.Month).Sum(t => t.Money).ToString(),
  50. 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(),
  51. StoreCount = data.GroupBy(t => t.Id).Count()
  52. };
  53. }
  54. /// <summary>
  55. /// 折线图
  56. /// </summary>
  57. /// <returns></returns>
  58. public object OrderLine(FullScreenBasic input)
  59. {
  60. var db = DbContext();
  61. StringBuilder sb = new StringBuilder();
  62. sb.Append(@"SELECT
  63. b.OrderStatus,
  64. b.CreatedAt,
  65. b.AfterStatus,
  66. b.PayMode
  67. FROM
  68. BPA_Organize AS a
  69. JOIN BPA_Order AS b ON a.Id= b.OrgId
  70. WHERE
  71. ( b.TransactionId IS NOT NULL OR b.TransactionId != '' )
  72. AND ( b.TradeNo IS NOT NULL OR b.TradeNo != '' )");
  73. if (input.OrgId.HasValue)
  74. {
  75. sb.Append(@$" AND a.AutoKey = {input.OrgId.Value} ");
  76. if (input.DeviceId.HasValue)
  77. 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)");
  78. }
  79. var sql = sb.ToString();
  80. var data = db.SqlQueryable<OrderLineDto>(sql).ToList();
  81. List<object> res = new List<object>();
  82. int indexcount = input.Count.HasValue ? input.Count.Value : 10;
  83. for (int index = indexcount; index >= 0; index--)
  84. {
  85. var temp = DateTime.Now.AddDays(-index);
  86. var end = DateTime.Parse(temp.ToString("yyyy-MM-dd 23:59:59"));
  87. var start = DateTime.Parse(temp.ToString("yyyy-MM-dd 00:00:00"));
  88. var NO = data.Where(t => t.CreatedAt < end && t.CreatedAt >= start).Where(t => t.AfterStatus != 0 && (t.OrderStatus == 1 || t.PayMode == 4)).Count();
  89. var OK = data.Where(t => t.CreatedAt < end && t.CreatedAt >= start).Where(t => t.AfterStatus == 0 && (t.OrderStatus == 1 || t.PayMode == 4)).Count();
  90. var Date = temp.ToString("MM/dd");
  91. res.Add(new
  92. {
  93. NO,
  94. OK,
  95. Date
  96. });
  97. }
  98. return new { data= res };
  99. }
  100. /// <summary>
  101. /// 区域销售
  102. /// </summary>
  103. /// <returns></returns>
  104. public object GetLocSale()
  105. {
  106. var db = DbContext();
  107. var baiduMap = "http://api.map.baidu.com/geocoder/v2/?ak=3ce7ddfda60046708248ea55e10b1435&callback=renderReverse&location={0}&output=xml";
  108. List<LocSaleDto> loc = new List<LocSaleDto>();
  109. var data = db.SqlQueryable<dynamic>(@"SELECT
  110. a.Store_Loc AS Loc,
  111. (SELECT Count(b.Id) FROM BPA_Order as b WHERE b.OrgId=a.Id) as Count
  112. FROM
  113. BPA_Organize AS a
  114. WHERE
  115. a.IsDeleted = 0
  116. AND a.Store_Loc LIKE '%,%';
  117. ").ToList();
  118. IHttpMultiClient http = null;
  119. data.ForEach(item =>
  120. {
  121. if (http == null)
  122. http = IHttpMultiClient.HttpMulti.AddNode(opt =>
  123. {
  124. opt.NodePath = string.Format(baiduMap, item.Loc);
  125. });
  126. else
  127. http = http.AddNode(opt =>
  128. {
  129. opt.NodePath = string.Format(baiduMap, item.Loc);
  130. });
  131. });
  132. XmlDocument doc = new XmlDocument();
  133. var Temps = http.Build().RunString();
  134. for (int i = 0; i < Temps.Count; i++)
  135. {
  136. doc.LoadXml(Temps[i]);
  137. loc.Add(new LocSaleDto
  138. {
  139. Count = data[i].Count,
  140. City = doc.SelectSingleNode("//province")?.InnerText + doc.SelectSingleNode("//city")?.InnerText
  141. });
  142. }
  143. return new
  144. {
  145. data = loc.GroupBy(t => t.City).Select(t => new LocSaleDto
  146. {
  147. City = string.IsNullOrEmpty(t.Key)?"未知": t.Key,
  148. Count = t.Sum(t => t.Count)
  149. })
  150. };
  151. }
  152. /// <summary>
  153. /// 下单数量
  154. /// </summary>
  155. /// <returns></returns>
  156. public object OrderNumber(FullScreenBasic input)
  157. {
  158. var db = DbContext();
  159. StringBuilder sb = new StringBuilder();
  160. sb.Append(@"SELECT
  161. b.CreatedAt
  162. FROM
  163. BPA_Organize AS a
  164. JOIN BPA_Order AS b ON a.Id= b.OrgId
  165. WHERE
  166. ( b.TransactionId IS NOT NULL OR b.TransactionId != '' )
  167. AND ( b.TradeNo IS NOT NULL OR b.TradeNo != '' )");
  168. if (input.OrgId.HasValue)
  169. {
  170. sb.Append(@$" AND a.AutoKey = {input.OrgId.Value} ");
  171. if (input.DeviceId.HasValue)
  172. 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)");
  173. }
  174. var sql = sb.ToString();
  175. var data = db.SqlQueryable<OrderNumberDto>(sql).ToList();
  176. int Year = data.Where(t => t.CreatedAt.Year == DateTime.Now.Year).Count();
  177. int Month = data.Where(t => t.CreatedAt.Year == DateTime.Now.Year && t.CreatedAt.Month == DateTime.Now.Month).Count();
  178. int Day = data.Where(t => t.CreatedAt.Year == DateTime.Now.Year && t.CreatedAt.Month == DateTime.Now.Month && t.CreatedAt.Day == DateTime.Now.Day).Count();
  179. int L_Year = data.Where(t => t.CreatedAt.Year == DateTime.Now.AddYears(-1).Year).Count();
  180. int L_Month = data.Where(t => t.CreatedAt.Year == DateTime.Now.Year && t.CreatedAt.Month == DateTime.Now.AddMonths(-1).Month).Count();
  181. int L_Day = data.Where(t => t.CreatedAt.Year == DateTime.Now.Year && t.CreatedAt.Month == DateTime.Now.Month && t.CreatedAt.Day == DateTime.Now.AddDays(-1).Day).Count();
  182. return new
  183. {
  184. Year = Year, Month = Month, Day = Day,
  185. L_Year = L_Year, L_Month = L_Month, L_Day = L_Day,
  186. B_Year = ((Year + L_Year) == 0) ? 0 : (((Year - L_Year) * 100) / (Year + L_Year)),
  187. B_Month = ((Month + L_Month) == 0) ? 0 : (((Month - L_Month) * 100) / (Month + L_Month)),
  188. B_Day = ((Day + L_Day) == 0) ? 0 : (((Day - L_Day) * 100) / (Day + L_Day))
  189. };
  190. }
  191. /// <summary>
  192. /// 获取店铺集合
  193. /// </summary>
  194. /// <returns></returns>
  195. public Dictionary<int, List<StoreListDto>> StoreList()
  196. {
  197. var db = DbContext();
  198. StringBuilder sb = new StringBuilder();
  199. sb.Append(@"SELECT a.AutoKey as ClientId,a.Name as ClientName, b.AutoKey as DeviceID,b.DeviceName,b.DeviceTypeKey FROM BPA_Organize as a JOIN BPA_DeviceInfo as b ON a.Id= b.OrgId");
  200. var sql = sb.ToString();
  201. var data = db.SqlQueryable<StoreListDto>(sql).ToList();
  202. Dictionary<int, List<StoreListDto>> dic = new Dictionary<int, List<StoreListDto>>();
  203. IEnumerable<IGrouping<int, StoreListDto>> re = data.GroupBy(s => s.ClientId);
  204. foreach (IGrouping<int, StoreListDto> item in re)
  205. {
  206. dic[item.Key] = item?.ToList();
  207. }
  208. return dic;
  209. }
  210. }
  211. }