IndividualCustomer.razor.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using AI.Platform.Core;
  2. using AI.Platform.Core.Dto.MemberManagement;
  3. using AI.Platform.Core.Entity.System.VehicleTerminal.BusinessUnitInfo;
  4. using AI.Platform.Core.Entity.System.VehicleTerminal.Company;
  5. using Microsoft.AspNetCore.Components;
  6. using Microsoft.JSInterop;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text.Json;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.ElectronicAccount;
  14. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.UserInfo;
  15. namespace AI.Platform.Page.Pages.MemberManagement
  16. {
  17. public partial class IndividualCustomer : ComponentBase
  18. {
  19. #region 依赖注入
  20. /// <summary>
  21. /// 注入实例
  22. /// </summary>
  23. [Inject] private SqlSugarRepository<CompanyEntity> _repository { get; set; }
  24. [Inject] private SqlSugarRepository<ElectronicAccountEntity> _Accountrepository { get; set; }
  25. [Inject] private SqlSugarRepository<UserInfoEntity> _UserInforepository { get; set; }
  26. [Inject]
  27. protected IJSRuntime JSRuntime { get; set; } = default!;
  28. [Inject]
  29. protected NavigationManager NavigationManager { get; set; } = default!;
  30. #endregion
  31. #region 状态变量
  32. protected List<UserInfoDto> Customers { get; set; } = new();
  33. protected List<UserInfoDto> FilteredCustomers { get; set; } = new();
  34. protected Customer CurrentCustomer { get; set; } = new();
  35. protected Customer? CustomerToDelete { get; set; }
  36. protected bool ShowModal { get; set; } = false;
  37. protected bool ShowDeleteDialog { get; set; } = false;
  38. protected bool IsCreating { get; set; } = false;
  39. protected string ModalTitle { get; set; } = "";
  40. protected bool Loading { get; set; } = false;
  41. protected string SearchCustomerName { get; set; } = "";
  42. protected string SearchCustomerCode { get; set; } = "";
  43. protected string SearchCustomerMobile { get; set; } = "";
  44. protected string? ErrorMessage { get; set; }
  45. protected int TotalCount { get; set; }
  46. protected int CurrentPage { get; set; } = 1;
  47. protected int PageSize { get; set; } = 20;
  48. #endregion
  49. #region 生命周期方法
  50. protected override async Task OnInitializedAsync()
  51. {
  52. await LoadCustomers();
  53. }
  54. #endregion
  55. #region 数据操作方法
  56. protected async Task LoadCustomers()
  57. {
  58. Loading = true;
  59. // 初始化示例数据
  60. if (Customers.Count == 0)
  61. {
  62. // Customers = await GetSampleCustomers();
  63. }
  64. await ApplyFilter();
  65. Loading = false;
  66. }
  67. private async Task<List<UserInfoDto>> GetSampleCustomers()
  68. {
  69. return await _UserInforepository.AsQueryable()
  70. .LeftJoin<ElectronicAccountEntity>((a, b) => a.Id == b.UserId)
  71. .LeftJoin<BusinessUnitInfoEntity>((a, b, c) => a.SiteId == c.Id)
  72. .Select((a, b, c) => new UserInfoDto
  73. {
  74. Id = b.Id,
  75. AccountNo = b.AccountNo,
  76. AvatarUrl = a.AvatarUrl,
  77. Balance = b.Balance,
  78. LastActiveTime = a.LastActiveTime,
  79. Mobile = a.Mobile,
  80. Name = a.UserName,
  81. RecentBehavior = a.RecentBehavior,
  82. RegisterTime = a.RegisterTime,
  83. StationName = c.Name
  84. }).ToListAsync();
  85. }
  86. protected async Task ApplyFilter()
  87. {
  88. var query = _UserInforepository.AsQueryable()
  89. .LeftJoin<ElectronicAccountEntity>((a, b) => a.Id == b.UserId)
  90. .LeftJoin<BusinessUnitInfoEntity>((a, b, c) => a.SiteId == c.Id);
  91. if (!string.IsNullOrWhiteSpace(SearchCustomerName))
  92. {
  93. query = query.Where((a, b, c) => a.UserName.Contains(SearchCustomerName, StringComparison.OrdinalIgnoreCase));
  94. }
  95. if (!string.IsNullOrWhiteSpace(SearchCustomerCode))
  96. {
  97. query = query.Where((a, b, c) => b.AccountNo.Contains(SearchCustomerCode, StringComparison.OrdinalIgnoreCase));
  98. }
  99. if (!string.IsNullOrWhiteSpace(SearchCustomerMobile))
  100. {
  101. query = query.Where((a, b, c) => a.Mobile.Contains(SearchCustomerMobile, StringComparison.OrdinalIgnoreCase));
  102. }
  103. var baseQuery = query.Select((a, b, c) => new UserInfoDto
  104. {
  105. Id = b.Id,
  106. AccountNo = b.AccountNo,
  107. AvatarUrl = a.AvatarUrl,
  108. Balance = b.Balance,
  109. LastActiveTime = a.LastActiveTime,
  110. Mobile = a.Mobile,
  111. Name = a.UserName,
  112. RecentBehavior = a.RecentBehavior,
  113. RegisterTime = a.RegisterTime,
  114. StationName = c.Name
  115. });
  116. TotalCount = await baseQuery.CountAsync();
  117. FilteredCustomers = await baseQuery.Skip((CurrentPage - 1) * PageSize)
  118. .Take(PageSize)
  119. .ToListAsync();
  120. Customers = FilteredCustomers;
  121. }
  122. protected void HandleSearch()
  123. {
  124. ApplyFilter();
  125. }
  126. // 页码变化
  127. protected async Task HandlePageChange(PaginationEventArgs args)
  128. {
  129. CurrentPage = args.Page;
  130. await LoadCustomers();
  131. }
  132. // 每页大小变化 - 使用 PaginationEventArgs
  133. protected async Task HandlePageSizeChange(PaginationEventArgs args)
  134. {
  135. PageSize = args.PageSize;
  136. CurrentPage = 1;
  137. await LoadCustomers();
  138. }
  139. #endregion
  140. #region 模态框操作
  141. protected void ShowCreateModal()
  142. {
  143. CurrentCustomer = new Customer
  144. {
  145. RegisterTime = DateTime.Now,
  146. Status = "正常"
  147. };
  148. ModalTitle = "新建客户";
  149. ShowModal = true;
  150. IsCreating = true;
  151. ErrorMessage = null;
  152. }
  153. protected void ShowEditModal(UserInfoDto customer)
  154. {
  155. }
  156. protected void CloseModal()
  157. {
  158. ShowModal = false;
  159. CurrentCustomer = new Customer();
  160. ErrorMessage = null;
  161. }
  162. #endregion
  163. #region 删除操作
  164. protected void ShowDeleteConfirm(Customer customer)
  165. {
  166. CustomerToDelete = customer;
  167. ShowDeleteDialog = true;
  168. }
  169. protected void CancelDelete()
  170. {
  171. ShowDeleteDialog = false;
  172. CustomerToDelete = null;
  173. }
  174. #endregion
  175. #region 卡管理操作
  176. protected void ShowCardManagement(UserInfoDto customer)
  177. {
  178. NavigationManager.NavigateTo($"/cardManagement/personalCard?customerId={customer.Id}");
  179. }
  180. #endregion
  181. #region 辅助方法
  182. protected string GetStatusColor(string status)
  183. {
  184. return status switch
  185. {
  186. "正常" => "#52c41a", // 绿色
  187. "禁用" => "#f5222d", // 红色
  188. "注销" => "#faad14", // 橙色
  189. _ => "#d9d9d9" // 灰色
  190. };
  191. }
  192. protected async Task ShowAlert(string title, string message)
  193. {
  194. await JSRuntime.InvokeVoidAsync("alert", $"{title}: {message}");
  195. }
  196. // 验证邮箱格式
  197. private bool IsValidEmail(string email)
  198. {
  199. if (string.IsNullOrWhiteSpace(email))
  200. return false;
  201. try
  202. {
  203. var regex = new Regex(@"^[^@\s]+@[^@\s]+\.[^@\s]+$");
  204. return regex.IsMatch(email);
  205. }
  206. catch
  207. {
  208. return false;
  209. }
  210. }
  211. // 验证电话号码格式(简单的电话号码验证)
  212. private bool IsValidPhoneNumber(string phone)
  213. {
  214. if (string.IsNullOrWhiteSpace(phone))
  215. return false;
  216. // 移除所有空格和连字符
  217. var cleanedPhone = phone.Replace(" ", "").Replace("-", "");
  218. // 检查是否只包含数字和可能的加号
  219. var regex = new Regex(@"^\+?[0-9\s\-\(\)]+$");
  220. return regex.IsMatch(phone);
  221. }
  222. // 模拟保存数据到本地存储
  223. protected async Task SaveToLocalStorage()
  224. {
  225. var json = JsonSerializer.Serialize(Customers);
  226. await JSRuntime.InvokeVoidAsync("localStorage.setItem", "customers", json);
  227. }
  228. #endregion
  229. }
  230. }