using AI.Platform.Core;
using AI.Platform.Core.Entity.Device;
using AI.Platform.Core.Entity.Site;
using AI.Platform.Page.Pages.Site.Model;
using AntDesign;
using AntDesign.TableModels;
using Dm.util;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using SqlSugar;
using System.Data.Common;
using System.Text.Json;
using System.Threading.Tasks;
namespace AI.Platform.Page.Pages.Site;
public partial class Screen
{
///
///
///
[Inject] NavigationManager NavigationManager { get; set; }
///
/// 数据库仓储
///
[Inject] SqlSugarRepository _Repository { get; set; }
///
/// 站点
///
[Inject] SqlSugarRepository _SiteRepository { get; set; }
///
///
///
[Inject] IJSRuntime IJSRuntime { get; set; }
[Inject] IMessageService MessageService { get; set; }
///
///
///
private ITable _Table;
///
/// 站点列表信息
///
private List _DataSource;
///
/// 总条数
///
private int Total = 0;
///
/// 加载
///
private bool Loading = false;
///
/// 查询过滤条件
///
private FilterData filterData { set; get; } = new();
///
/// 编辑弹窗
///
private UpdateScreenDialog updateMediaDialog { set; get; } = new();
///
/// 标识弹窗是否显示
///
private bool isOpen { set; get; } = false;
private ScrrenDialogModel model = new ScrrenDialogModel();
//
/// 站点
///
private List Sites;
///
/// 打开编辑弹窗
///
/// 类型:1:新增;2:编辑;3:删除
/// 大屏信息
///
private async Task ShowDialog(int type, SnInfo? snInfo)
{
await searchSitesAsync();
model = new ScrrenDialogModel();
model.Type = type;
model.Sites = Sites;
if(type == 2 || type == 3)
{
model.sn = snInfo?.sn?? "";
model.SiteId = snInfo?.SiteId ?? -1;
model.siteName = snInfo?.siteName ?? "";
model.Id = snInfo?.Id;
model.Remark = snInfo?.Remark ?? "";
}
isOpen = true;
}
///
/// 编辑弹窗信息回调
///
///
private async Task OnDialogCallback(ScrrenDialogModel model)
{
switch (model.Type)
{
case 1:
ScreentEntity screentEntity = await _Repository.AsQueryable().Where(it => it.sn == model.sn).FirstAsync();
if(screentEntity == null)
{
await _Repository.InsertAsync(model.ToCompany());
} else
{
MessageService.Error("该设备已存在");
}
break;
case 2:
ScreentEntity siteEntity = model.ToCompany();
await _Repository.Context.Updateable(siteEntity).
UpdateColumns(it => new { it.sn,it.SiteId, it.Remark})
.ExecuteCommandAsync();
break;
case 3:
await _Repository.DeleteByIdAsync(model.Id);
break;
}
await Query();
}
///
/// 编辑弹窗是否显示回调
///
///
private void OnDialogVisibleCallback(bool isOpen)
{
this.isOpen = isOpen;
}
///
/// 表格查询
///
///
///
private async Task OnChange(QueryModel query)
=> await Query();
///
/// 查
///
///
private async Task Query()
{
Loading = true;
var query = _Repository.AsQueryable()
.LeftJoin((screen, site) => screen.SiteId == site.Id);
query = query
.WhereIF(filterData.siteName.IsNotNullOrEmpty(),(screen, site) => site.Name.Contains(filterData.siteName))
.WhereIF(filterData.sn.IsNotNullOrEmpty(), (screen, site) => screen.sn.Contains(filterData.sn));
Total = await query.CountAsync();
_DataSource = await query
.Select((screen,site) => new SnInfo()
{
Id = screen.Id,
sn = screen.sn,
SiteId = screen.SiteId,
siteName = site.Name,
CreateTime = screen.CreateTime,
EditTime = screen.EditTime,
Remark = screen.Remark,
})
.Skip((filterData.currentPage - 1) * filterData.pageSize)
.Take(filterData.pageSize)
.ToListAsync();
await searchSitesAsync();
Loading = false;
}
Func showTotal = ctx => $"总数 {ctx.Total} ";
private async Task OnPageChange(PaginationEventArgs args)
{
filterData.pageSize = args.PageSize;
filterData.currentPage = args.Page;
await Query();
}
///
/// 清空查询条件
///
private void HandleReset()
{
filterData = new();
}
private async Task searchSitesAsync()
{
if (Global.CurrentUser.Account == "admin")
{
Sites = await _SiteRepository.AsQueryable()
.Select()
.ToListAsync();
}
else
{
Sites = await _SiteRepository.AsQueryable()
.Where(it => it.Id == Global.CurrentUser.SiteId || it.ParentId == Global.CurrentUser.SiteId)
.Select()
.ToListAsync();
}
}
protected override async void OnInitialized()
{
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await NavigationManager.RedirectLogin(IJSRuntime);
await Query();
}
}
private async Task OnChange(QueryModel query)
=> await Query();
///
/// 查找条件
///
public class FilterData
{
///
/// 页码
///
public int currentPage { set; get; } = 1;
///
/// 页数
///
public int pageSize { set; get; } = 10;
///
/// 要查找的站名
///
public string? siteName { set; get; }
///
/// 要查找的SN
///
public string? sn{ set; get; }
}
}