Site.razor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. model = new StateDialogModel();
  70. model.Type = type;
  71. model.Sites = Sites;
  72. if(type == 2 || type == 3)
  73. {
  74. model.Address = media?.Address ?? "";
  75. model.Name = media?.Name ?? "";
  76. model.Contact = media?.Contact ?? "";
  77. model.Id = media?.Id;
  78. }
  79. isOpen = true;
  80. }
  81. /// <summary>
  82. /// 编辑弹窗信息回调
  83. /// </summary>
  84. /// <param name="model"></param>
  85. private async Task OnDialogCallback(StateDialogModel model)
  86. {
  87. switch (model.Type)
  88. {
  89. case 1:
  90. await _Repository.InsertAsync(model.ToCompany());
  91. break;
  92. case 2:
  93. SiteEntity siteEntity = model.ToCompany();
  94. await _Repository.Context.Updateable(siteEntity).
  95. UpdateColumns(it => new { it.Name,it.Address, it.Contact})
  96. .ExecuteCommandAsync();
  97. break;
  98. case 3:
  99. await _Repository.DeleteByIdAsync(model.Id);
  100. break;
  101. }
  102. await Query();
  103. }
  104. /// <summary>
  105. /// 编辑弹窗是否显示回调
  106. /// </summary>
  107. /// <param name="isOpen"></param>
  108. private void OnDialogVisibleCallback(bool isOpen)
  109. {
  110. this.isOpen = isOpen;
  111. }
  112. /// <summary>
  113. /// 表格查询
  114. /// </summary>
  115. /// <param name="query"></param>
  116. /// <returns></returns>
  117. private async Task OnChange(QueryModel<SiteOutput> query)
  118. => await Query();
  119. /// <summary>
  120. /// 查
  121. /// </summary>
  122. /// <returns></returns>
  123. private async Task Query()
  124. {
  125. Loading = true;
  126. string? searchName = filterData.siteName;
  127. int pageSize = filterData.pageSize;
  128. int offset = (filterData.currentPage - 1) * pageSize;
  129. string whereClause = string.IsNullOrEmpty(searchName)
  130. ? ""
  131. : "WHERE st.name LIKE CONCAT('%', @searchName, '%')";
  132. if (Global.CurrentUser.Account != "admin")
  133. {
  134. long siteID = Global.CurrentUser.SiteId;
  135. string countSql = $@"
  136. WITH RECURSIVE site_tree AS(
  137. SELECT id,parent_id
  138. FROM site_entity
  139. WHERE id = @siteID
  140. UNION ALL
  141. SELECT s.id,s.parent_id
  142. FROM site_entity s
  143. INNER JOIN site_tree t ON s.parent_id = t.id
  144. )
  145. SELECT
  146. st.id AS Id,st.parent_id AS ParentID,
  147. p.name AS ParentName
  148. FROM site_tree st
  149. LEFT JOIN site_entity p ON st.parent_id = p.id
  150. {whereClause};
  151. ";
  152. Total = await _Repository.Context.Ado.GetIntAsync(countSql, new { siteID, searchName });
  153. string dataSql = $@"
  154. WITH RECURSIVE site_tree AS(
  155. SELECT id,parent_id,name,address,contact,create_time
  156. FROM site_entity
  157. WHERE id = @siteID
  158. UNION ALL
  159. SELECT s.id,s.parent_id,s.name,s.address,s.contact,s.create_time
  160. FROM site_entity s
  161. INNER JOIN site_tree t ON s.parent_id = t.id
  162. )
  163. SELECT
  164. 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,
  165. p.name AS ParentName
  166. FROM site_tree st
  167. LEFT JOIN site_entity p ON st.parent_id = p.id
  168. {whereClause}
  169. ORDER BY st.create_time DESC
  170. LIMIT @pageSize OFFSET @offset;
  171. ";
  172. _DataSource = await _Repository.Context.Ado.SqlQueryAsync<SiteOutput>(dataSql, new { siteID, searchName, pageSize, offset });
  173. }
  174. else
  175. {
  176. //,st.name,st.address,st.contact,st.create_time
  177. string countSql = $@"
  178. SELECT
  179. st.id,st.parent_id,
  180. p.name AS ParentName
  181. FROM site_entity st
  182. LEFT JOIN site_entity p ON st.parent_id = p.id
  183. {whereClause};
  184. ";
  185. Total = await _Repository.Context.Ado.GetIntAsync(countSql, new { searchName });
  186. string dataSql = $@"
  187. SELECT
  188. 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,
  189. p.name AS ParentName
  190. FROM site_entity st
  191. LEFT JOIN site_entity p ON st.parent_id = p.id
  192. {whereClause}
  193. ORDER BY st.create_time DESC
  194. LIMIT @pageSize OFFSET @offset;
  195. ";
  196. _DataSource = await _Repository.Context.Ado.SqlQueryAsync<SiteOutput>(dataSql, new { searchName, pageSize, offset });
  197. }
  198. Loading = false;
  199. }
  200. Func<PaginationTotalContext, string> showTotal = ctx => $"总数 {ctx.Total} ";
  201. private async Task OnPageChange(PaginationEventArgs args)
  202. {
  203. filterData.pageSize = args.PageSize;
  204. filterData.currentPage = args.Page;
  205. await Query();
  206. }
  207. /// <summary>
  208. /// 清空查询条件
  209. /// </summary>
  210. private void HandleReset()
  211. {
  212. }
  213. private async Task searchSitesAsync()
  214. {
  215. if (Global.CurrentUser.Account == "admin")
  216. {
  217. //string dataSql = $@"
  218. // SELECT
  219. // st.id AS Id,st.name AS Name
  220. // p.name AS ParentName
  221. // FROM site_entity st
  222. // LEFT JOIN site_entity p ON st.parent_id = p.id;
  223. //";
  224. //Sites = await _Repository.Context.Ado.SqlQueryAsync<SiteInfo>(dataSql);
  225. Sites = await _Repository.AsQueryable()
  226. .Where(it => it.ParentId == 0)
  227. .Select<SiteInfo>()
  228. .ToListAsync();
  229. }
  230. else
  231. {
  232. //long siteID = Global.CurrentUser.SiteId;
  233. //string dataSql = $@"
  234. // WITH RECURSIVE site_tree AS(
  235. // SELECT id,parent_id,name,address,contact,create_time
  236. // FROM site_entity
  237. // WHERE id = @siteID
  238. // UNION ALL
  239. // SELECT s.id,s.parent_id,s.name,s.address,s.address,s.address
  240. // FROM site_entity s
  241. // INNER JOIN site_tree t ON s.parent_id = t.id
  242. // )
  243. // SELECT
  244. // 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,
  245. // p.name AS ParentName
  246. // FROM site_tree st
  247. // LEFT JOIN site_entity p ON st.parent_id = p.id;
  248. //";
  249. //Sites = await _Repository.Context.Ado.SqlQueryAsync<SiteInfo>(dataSql, new { siteID });
  250. Sites = await _Repository.AsQueryable()
  251. .Where(it => it.Id == Global.CurrentUser.SiteId)
  252. .Select<SiteInfo>()
  253. .ToListAsync();
  254. }
  255. }
  256. protected override async void OnInitialized()
  257. {
  258. }
  259. protected override async Task OnAfterRenderAsync(bool firstRender)
  260. {
  261. if (firstRender)
  262. {
  263. await NavigationManager.RedirectLogin(IJSRuntime);
  264. await Query();
  265. await searchSitesAsync();
  266. }
  267. }
  268. private async Task OnChange(QueryModel<SystemMenu> query)
  269. => await Query();
  270. /// <summary>
  271. /// 查找条件
  272. /// </summary>
  273. public class FilterData
  274. {
  275. /// <summary>
  276. /// 页码
  277. /// </summary>
  278. public int currentPage { set; get; } = 1;
  279. /// <summary>
  280. /// 页数
  281. /// </summary>
  282. public int pageSize { set; get; } = 10;
  283. /// <summary>
  284. /// 要查找的站名
  285. /// </summary>
  286. public string? siteName { set; get; }
  287. }
  288. }