Site.razor.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using AI.Platform.Core;
  2. using AI.Platform.Core.Entity.Site;
  3. using AI.Platform.Page.Pages.Site.Model;
  4. using AntDesign;
  5. using AntDesign.TableModels;
  6. using Dm.util;
  7. using Microsoft.AspNetCore.Components;
  8. using Microsoft.JSInterop;
  9. using SqlSugar;
  10. using System.Data.Common;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. namespace AI.Platform.Page.Pages.Site;
  14. public partial class Site
  15. {
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. [Inject] NavigationManager NavigationManager { get; set; }
  20. /// <summary>
  21. /// 数据库仓储
  22. /// </summary>
  23. [Inject] SqlSugarRepository<SiteEntity> _Repository { get; set; }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. [Inject] IJSRuntime IJSRuntime { get; set; }
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. private ITable _Table;
  32. /// <summary>
  33. /// 站点列表信息
  34. /// </summary>
  35. private List<SiteOutput> _DataSource;
  36. /// <summary>
  37. /// 总条数
  38. /// </summary>
  39. private int Total = 0;
  40. /// <summary>
  41. /// 加载
  42. /// </summary>
  43. private bool Loading = false;
  44. /// <summary>
  45. /// 查询过滤条件
  46. /// </summary>
  47. private FilterData filterData { set; get; } = new();
  48. /// <summary>
  49. /// 编辑弹窗
  50. /// </summary>
  51. private UpdateSiteDialog updateMediaDialog { set; get; } = new();
  52. /// <summary>
  53. /// 标识弹窗是否显示
  54. /// </summary>
  55. private bool isOpen { set; get; } = false;
  56. private StateDialogModel model = new StateDialogModel();
  57. // <summary>
  58. /// 可供选择的父站列表
  59. /// </summary>
  60. private List<SiteInfo> Sites;
  61. /// <summary>
  62. /// 打开编辑弹窗
  63. /// </summary>
  64. /// <param name="type">类型:1:新增;2:编辑;3:删除</param>
  65. /// <param name="media">广告信息</param>
  66. /// <returns></returns>
  67. private async Task ShowDialog(int type, SiteOutput? media)
  68. {
  69. await searchSitesAsync();
  70. model = new StateDialogModel();
  71. model.Type = type;
  72. model.Sites = Sites;
  73. if(type == 2 || type == 3)
  74. {
  75. model.Address = media?.Address ?? "";
  76. model.Name = media?.Name ?? "";
  77. model.Contact = media?.Contact ?? "";
  78. model.Id = media?.Id;
  79. model.ParentId = media?.ParentID ?? 0;
  80. }
  81. isOpen = true;
  82. }
  83. /// <summary>
  84. /// 编辑弹窗信息回调
  85. /// </summary>
  86. /// <param name="model"></param>
  87. private async Task OnDialogCallback(StateDialogModel model)
  88. {
  89. switch (model.Type)
  90. {
  91. case 1:
  92. await _Repository.InsertAsync(model.ToCompany());
  93. break;
  94. case 2:
  95. SiteEntity siteEntity = model.ToCompany();
  96. await _Repository.Context.Updateable(siteEntity).
  97. UpdateColumns(it => new { it.Name,it.Address, it.Contact,it.ParentId})
  98. .ExecuteCommandAsync();
  99. break;
  100. case 3:
  101. await _Repository.DeleteByIdAsync(model.Id);
  102. break;
  103. }
  104. await Query();
  105. }
  106. /// <summary>
  107. /// 编辑弹窗是否显示回调
  108. /// </summary>
  109. /// <param name="isOpen"></param>
  110. private void OnDialogVisibleCallback(bool isOpen)
  111. {
  112. this.isOpen = isOpen;
  113. }
  114. /// <summary>
  115. /// 表格查询
  116. /// </summary>
  117. /// <param name="query"></param>
  118. /// <returns></returns>
  119. private async Task OnChange(QueryModel<SiteOutput> query)
  120. => await Query();
  121. /// <summary>
  122. /// 查
  123. /// </summary>
  124. /// <returns></returns>
  125. private async Task Query()
  126. {
  127. Loading = true;
  128. string? searchName = filterData.siteName;
  129. int pageSize = filterData.pageSize;
  130. int offset = (filterData.currentPage - 1) * pageSize;
  131. string whereClause = string.IsNullOrEmpty(searchName)
  132. ? ""
  133. : "WHERE st.name LIKE CONCAT('%', @searchName, '%')";
  134. if (Global.CurrentUser.Account != "admin")
  135. {
  136. long siteID = Global.CurrentUser.SiteId;
  137. //string countSql = $@"
  138. // WITH RECURSIVE site_tree AS(
  139. // SELECT id,parent_id
  140. // FROM site_entity
  141. // WHERE id = @siteID
  142. // UNION ALL
  143. // SELECT s.id,s.parent_id
  144. // FROM site_entity s
  145. // INNER JOIN site_tree t ON s.parent_id = t.id
  146. // )
  147. // SELECT
  148. // st.id AS Id,st.parent_id AS ParentID,
  149. // p.name AS ParentName
  150. // FROM site_tree st
  151. // LEFT JOIN site_entity p ON st.parent_id = p.id
  152. // {whereClause};
  153. //";
  154. //Total = await _Repository.Context.Ado.GetIntAsync(countSql, new { siteID, searchName });
  155. Total = await _Repository.AsQueryable()
  156. .Where(it => it.Id == siteID || it.ParentId == siteID)
  157. .WhereIF(filterData.siteName.IsNotNullOrEmpty(), it => it.Name.Contains(filterData.siteName))
  158. .CountAsync();
  159. string dataSql = $@"
  160. WITH RECURSIVE site_tree AS(
  161. SELECT id,parent_id,name,address,contact,create_time
  162. FROM site_entity
  163. WHERE id = @siteID
  164. UNION ALL
  165. SELECT s.id,s.parent_id,s.name,s.address,s.contact,s.create_time
  166. FROM site_entity s
  167. INNER JOIN site_tree t ON s.parent_id = t.id
  168. )
  169. SELECT
  170. st.id AS Id,st.parent_id AS ParentID,st.name AS Name ,st.address AS Address,st.contact AS Contact,st.create_time AS CreateTime,
  171. p.name AS ParentName
  172. FROM site_tree st
  173. LEFT JOIN site_entity p ON st.parent_id = p.id
  174. {whereClause}
  175. ORDER BY st.create_time DESC
  176. LIMIT @pageSize OFFSET @offset;
  177. ";
  178. _DataSource = await _Repository.Context.Ado.SqlQueryAsync<SiteOutput>(dataSql, new { siteID, searchName, pageSize, offset });
  179. }
  180. else
  181. {
  182. //,st.name,st.address,st.contact,st.create_time
  183. //string countSql = $@"
  184. // SELECT
  185. // st.id,st.parent_id,
  186. // p.name AS ParentName
  187. // FROM site_entity st
  188. // LEFT JOIN site_entity p ON st.parent_id = p.id
  189. // {whereClause};
  190. //";
  191. //Total = await _Repository.Context.Ado.GetIntAsync(countSql, new { searchName });
  192. Total = await _Repository.AsQueryable()
  193. .WhereIF(filterData.siteName.IsNotNullOrEmpty(), it => it.Name.Contains(filterData.siteName))
  194. .CountAsync();
  195. string dataSql = $@"
  196. SELECT
  197. st.id AS Id,st.parent_id AS ParentID,st.name AS Name ,st.address AS Address,st.contact AS Contact,st.create_time AS CreateTime,
  198. p.name AS ParentName
  199. FROM site_entity st
  200. LEFT JOIN site_entity p ON st.parent_id = p.id
  201. {whereClause}
  202. ORDER BY st.create_time DESC
  203. LIMIT @pageSize OFFSET @offset;
  204. ";
  205. _DataSource = await _Repository.Context.Ado.SqlQueryAsync<SiteOutput>(dataSql, new { searchName, pageSize, offset });
  206. }
  207. await searchSitesAsync();
  208. Loading = false;
  209. }
  210. Func<PaginationTotalContext, string> showTotal = ctx => $"总数 {ctx.Total} ";
  211. private async Task OnPageChange(PaginationEventArgs args)
  212. {
  213. filterData.pageSize = args.PageSize;
  214. filterData.currentPage = args.Page;
  215. await Query();
  216. }
  217. /// <summary>
  218. /// 清空查询条件
  219. /// </summary>
  220. private void HandleReset()
  221. {
  222. filterData = new();
  223. }
  224. private async Task searchSitesAsync()
  225. {
  226. if (Global.CurrentUser.Account == "admin")
  227. {
  228. Sites = await _Repository.AsQueryable()
  229. .Where(it => it.ParentId == 0)
  230. .Select<SiteInfo>()
  231. .ToListAsync();
  232. }
  233. else
  234. {
  235. Sites = await _Repository.AsQueryable()
  236. .Where(it => it.Id == Global.CurrentUser.SiteId)
  237. .Select<SiteInfo>()
  238. .ToListAsync();
  239. }
  240. }
  241. protected override async void OnInitialized()
  242. {
  243. }
  244. protected override async Task OnAfterRenderAsync(bool firstRender)
  245. {
  246. if (firstRender)
  247. {
  248. await NavigationManager.RedirectLogin(IJSRuntime);
  249. await Query();
  250. }
  251. }
  252. private async Task OnChange(QueryModel<SystemMenu> query)
  253. => await Query();
  254. /// <summary>
  255. /// 查找条件
  256. /// </summary>
  257. public class FilterData
  258. {
  259. /// <summary>
  260. /// 页码
  261. /// </summary>
  262. public int currentPage { set; get; } = 1;
  263. /// <summary>
  264. /// 页数
  265. /// </summary>
  266. public int pageSize { set; get; } = 10;
  267. /// <summary>
  268. /// 要查找的站名
  269. /// </summary>
  270. public string? siteName { set; get; }
  271. }
  272. }