CardBlockUnblockRecords.razor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using EasyTemplate.Tool;
  2. using Microsoft.AspNetCore.Components;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.Device;
  8. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.Driver;
  9. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.Payment;
  10. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.Transaction;
  11. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.UserInfo;
  12. namespace EasyTemplate.Page.Pages.CardManagement
  13. {
  14. public partial class CardBlockUnblockRecords : ComponentBase
  15. {
  16. [Inject] private SqlSugarRepository<TransactionEntity> _TransactionPository { get; set; }
  17. // ========== 数据模型 ==========
  18. // 筛选条件模型
  19. public class FilterModel
  20. {
  21. public string DeviceId { get; set; } = "";
  22. public string PlateNumber { get; set; } = "";
  23. public string OilType { get; set; } = "";
  24. public string PaymentMethod { get; set; } = "";
  25. public string PaymentStatus { get; set; } = "";
  26. public string OrderStatus { get; set; } = "";
  27. public string Driver { get; set; } = "";
  28. public string OrderNo { get; set; } = "";
  29. public string Mobile { get; set; } = "";
  30. public DateTime? StartTime { get; set; }
  31. public DateTime? EndTime { get; set; }
  32. }
  33. // 交易数据模型
  34. public class TransactionItem
  35. {
  36. public string Id { get; set; } = "";
  37. public string DeviceId { get; set; } = "";
  38. public string PlateNumber { get; set; } = "";
  39. public DateTime TransactionTime { get; set; }
  40. public string OilProductName { get; set; } = "";
  41. public decimal OilPrice { get; set; }
  42. public decimal OilVolume { get; set; }
  43. public string PaymentMethod { get; set; } = "";
  44. public string DriverName { get; set; } = "";
  45. public string OrderNo { get; set; } = "";
  46. public decimal PayableAmount { get; set; }
  47. public string Mobile { get; set; } = "";
  48. public string UserName { get; set; } = "";
  49. public string WxName { get; set; } = "";
  50. public string PaymentStatus { get; set; }
  51. public string OrderStatus { get; set; } = "";
  52. }
  53. // ========== 公共属性 ==========
  54. protected FilterModel Filter { get; set; } = new();
  55. protected List<TransactionItem> TransactionData { get; set; } = new();
  56. protected bool Loading { get; set; }
  57. protected int TotalCount { get; set; }
  58. protected int CurrentPage { get; set; } = 1;
  59. protected int PageSize { get; set; } = 20;
  60. // ========== 选项数据 ==========
  61. protected List<string> DeviceOptions { get; set; } = new();
  62. protected List<string> PlateNumberOptions { get; set; } = new();
  63. protected List<string> OilTypeOptions { get; set; } = new();
  64. protected List<string> PaymentMethodOptions { get; set; } = new();
  65. protected List<string> PaymentStatusOptions { get; set; } = new();
  66. protected List<string> OrderStatusOptions { get; set; } = new();
  67. protected List<string> DriverOptions { get; set; } = new();
  68. public string DeviceId { get; set; } = "";
  69. public string PlateNumber { get; set; } = "";
  70. public string OilType { get; set; } = "";
  71. public string PaymentMethod { get; set; } = "";
  72. public string PaymentStatus { get; set; } = "";
  73. public string OrderStatus { get; set; } = "";
  74. public string Driver { get; set; } = "";
  75. public string OrderNo { get; set; } = "";
  76. public string Mobile { get; set; } = "";
  77. public DateTime? StartTime { get; set; }
  78. public DateTime? EndTime { get; set; }
  79. // ========== 生命周期方法 ==========
  80. protected override void OnInitialized()
  81. {
  82. base.OnInitialized();
  83. InitializeData();
  84. _ = LoadDataAsync();
  85. }
  86. // ========== 初始化方法 ==========
  87. private void InitializeData()
  88. {
  89. // 初始化选项数据
  90. DeviceOptions = new List<string> { "粤B67890" };
  91. PlateNumberOptions = new List<string> { "粤ACD0045", "粤ACD0046", "粤ACD0047", "粤ACD0048" };
  92. OilTypeOptions = new List<string> { "92#", "95#", "0#" };
  93. PaymentMethodOptions = new List<string> { "车队卡", "用户卡", "司机卡" };
  94. PaymentStatusOptions = new List<string> { "待支付", "支付成功", "支付失败" };
  95. OrderStatusOptions = new List<string> { "未支付", "已完成", "已取消" };
  96. DriverOptions = new List<string> { "张三", "李四", "王五" };
  97. // 设置默认时间范围(最近7天)
  98. Filter.StartTime = DateTime.Today.AddDays(-7);
  99. Filter.EndTime = DateTime.Today;
  100. }
  101. // ========== 数据操作 ==========
  102. // 重置筛选条件
  103. protected void ResetFilter()
  104. {
  105. Filter = new FilterModel
  106. {
  107. StartTime = DateTime.Today.AddDays(-7),
  108. EndTime = DateTime.Today
  109. };
  110. CurrentPage = 1;
  111. }
  112. // 加载数据
  113. protected async Task LoadDataAsync()
  114. {
  115. Loading = true;
  116. try
  117. {
  118. // 生成模拟数据
  119. TransactionData = await GenerateMockData();
  120. //TotalCount = TransactionData.Count(); // 模拟总记录数
  121. }
  122. finally
  123. {
  124. Loading = false;
  125. StateHasChanged();
  126. }
  127. }
  128. private async Task<List<TransactionItem>> GenerateMockData()
  129. {
  130. try
  131. {
  132. var payStatus = PaymentStatus == "待支付" ? 0 :
  133. PaymentStatus == "支付成功" ? 1 :
  134. PaymentStatus == "支付失败" ? 2 : 3;
  135. var OStatus = OrderStatus == "未支付" ? 0 :
  136. OrderStatus == "已完成" ? 1 :
  137. OrderStatus == "已取消" ? 2 : 3;
  138. var query = _TransactionPository.AsQueryable()
  139. .LeftJoin<DeviceEntity>((a, b) => a.DeviceId == b.DeviceId)
  140. .LeftJoin<PayTypeEntity>((a, b, c) => a.PaymentMethod == c.Id)
  141. .LeftJoin<DriverEntity>((a, b, c, e) => a.EmployeeId == e.Id)
  142. .LeftJoin<UserInfoEntity>((a, b, c, e, f) => a.UserId == f.Id);
  143. // 使用条件判断代替三元运算符
  144. if (!string.IsNullOrEmpty(DeviceId))
  145. query = query.Where((a, b, c, e, f) => b.DeviceId == DeviceId);
  146. if (!string.IsNullOrEmpty(PlateNumber))
  147. query = query.Where((a, b, c, e, f) => a.LicencePlate == PlateNumber);
  148. if (!string.IsNullOrEmpty(OilType))
  149. query = query.Where((a, b, c, e, f) => a.OilProduct == OilType);
  150. if (!string.IsNullOrEmpty(PaymentMethod))
  151. query = query.Where((a, b, c, e, f) => c.Name == PaymentMethod);
  152. if (payStatus != 3)
  153. query = query.Where((a, b, c, e, f) => a.PaymentStatus == payStatus);
  154. if (OStatus != 3)
  155. query = query.Where((a, b, c, e, f) => a.OrderStatus == OStatus);
  156. if (!string.IsNullOrEmpty(Driver))
  157. query = query.Where((a, b, c, e, f) => e.Name == Driver);
  158. if (!string.IsNullOrEmpty(OrderNo))
  159. query = query.Where((a, b, c, e, f) => a.BillNumber == OrderNo);
  160. if (!string.IsNullOrEmpty(Mobile))
  161. query = query.Where((a, b, c, e, f) => a.Mobile == Mobile);
  162. if (StartTime != null)
  163. query = query.Where((a, b, c, e, f) =>
  164. a.TransactionTime > StartTime && a.TransactionTime < EndTime);
  165. var baseQuery = query
  166. .OrderByDescending((a, b, c, e, f) => a.TransactionTime)
  167. .Select((a, b, c, e, f) => new TransactionItem
  168. {
  169. Id = a.Id.ToString(),
  170. DeviceId = b.DeviceId,
  171. PlateNumber = a.LicencePlate,
  172. TransactionTime = a.TransactionTime,
  173. OilProductName = a.OilProduct,
  174. OilPrice = a.OilPrice,
  175. OilVolume = a.OilVolume,
  176. PaymentMethod = c.Name,
  177. DriverName = e.Name,
  178. OrderNo = a.BillNumber,
  179. PayableAmount = a.PayableAmount,
  180. Mobile = a.Mobile,
  181. UserName = f.UserName,
  182. WxName = f.WxName,
  183. PaymentStatus = a.PaymentStatus == 0 ? "待支付" :
  184. a.PaymentStatus == 1 ? "支付成功" :
  185. a.PaymentStatus == 2 ? "支付失败" : "",
  186. OrderStatus = a.OrderStatus == 0 ? "未支付" :
  187. a.OrderStatus == 1 ? "已完成" : ""
  188. });
  189. TotalCount = await baseQuery.CountAsync();
  190. return await baseQuery
  191. .Skip((CurrentPage - 1) * PageSize)
  192. .Take(PageSize)
  193. .ToListAsync();
  194. }
  195. catch (Exception ex)
  196. {
  197. }
  198. return null;
  199. }
  200. // ========== 格式化方法 ==========
  201. // 格式化货币显示
  202. protected string FormatCurrency(decimal? value)
  203. {
  204. if (value == null) return "-";
  205. return value.Value.ToString("C", CultureInfo.CreateSpecificCulture("zh-CN"));
  206. }
  207. // 格式化数字显示
  208. protected string FormatNumber(decimal? value)
  209. {
  210. if (value == null) return "-";
  211. return value.Value.ToString("N2", CultureInfo.CreateSpecificCulture("zh-CN"));
  212. }
  213. // 格式化日期时间
  214. protected string FormatDateTime(DateTime? value)
  215. {
  216. if (value == null) return "-";
  217. return value.Value.ToString("yyyy-MM-dd HH:mm:ss");
  218. }
  219. // 格式化支付状态
  220. protected string FormatPaymentStatus(bool? status)
  221. {
  222. if (status == null) return "-";
  223. return status.Value ? "成功" : "失败";
  224. }
  225. // 格式化订单状态
  226. protected string FormatOrderStatus(string status)
  227. {
  228. return string.IsNullOrEmpty(status) ? "-" : status;
  229. }
  230. // ========== 事件处理方法 ==========
  231. // 查询处理
  232. protected async Task HandleQuery()
  233. {
  234. CurrentPage = 1;
  235. await LoadDataAsync();
  236. }
  237. // 重置处理
  238. protected async Task HandleReset()
  239. {
  240. ResetFilter();
  241. await LoadDataAsync();
  242. }
  243. // 页码变化
  244. protected async Task HandlePageChange(PaginationEventArgs args)
  245. {
  246. CurrentPage = args.Page;
  247. await LoadDataAsync();
  248. }
  249. // 每页大小变化 - 使用 PaginationEventArgs
  250. protected async Task HandlePageSizeChange(PaginationEventArgs args)
  251. {
  252. PageSize = args.PageSize;
  253. CurrentPage = 1;
  254. await LoadDataAsync();
  255. }
  256. }
  257. }