using CloudGaming.Repository.Game.Entities.App; using CloudGaming.Repository.Game.Entities.Game; using NPOI.SS.Formula.Functions; namespace CloudGaming.Api.Admin.ApplicationServices.Apps.App; /// /// 兑换码表 服务 T_RedemptionCodeService /// public class T_RedemptionCodeService(IServiceProvider serviceProvider, IRepository userRedemptionUsage ) : ApplicationUserService(serviceProvider) { /// /// 获取列表数据 /// /// /// public async override Task FindListAsync(PagingSearchInput pagingSearchInput) { var query = this.Repository.Select //兑换码(唯一) .WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search?.Code), w => w.Code.Contains(pagingSearchInput.Search.Code ?? "")) //兑换码类型 .WhereIf(pagingSearchInput.Search?.Type != null && pagingSearchInput.Search?.Type > 0, w => w.Type == pagingSearchInput.Search.Type) .OrderByDescending(w => w.Id) .Select(w => new { w.Id, w.Code, w.Type, w.UsageLimit, w.ExpirationDate, w.IsActive, w.CreatedDate, w.Remarks, w.TenantId, UserCount = 0, Statistic = "" }) ; var result = await Repository.AsPagingViewAsync(query, pagingSearchInput); result.DataSource.ForEach(item => { if (item.TryGetValue("Code", out var codeObj)) { var code = codeObj.ToString(); var count = userRedemptionUsage.SelectNoTracking.Where(it => it.Code == code && it.Status == 1).Count(); item["UserCount"] = count; var shibai1 = userRedemptionUsage.SelectNoTracking.Where(it => it.Code == code && it.Status == 2).Count(); var guoqi = userRedemptionUsage.SelectNoTracking.Where(it => it.Code == code && it.Status == 3).Count(); item["Statistic"] = $"领取成功:{count},领取失败:{shibai1},已过期:{guoqi}"; } }); // 设置列 //result.GetColumn(query, w => w.OperatorName).SetColumn("操作人"); //result.GetColumn(query, w => w. !).SetColumn(w => w.Name!); result .FormatValue(query, w => w.ExpirationDate, (oldValue) => oldValue.ToString("yyyy-MM-dd")) .FormatValue(query, w => w.CreatedDate, (oldValue) => oldValue.ToString("yyyy-MM-dd")) ; return result; } /// /// 保存数据 /// /// /// public override async Task SaveFormAsync(T_RedemptionCode form) { //添加 var isSelect = await Repository.Select.Where(it => it.Code == form.Code).FirstOrDefaultAsync(); if (isSelect != null && form.Id != isSelect.Id) { throw MessageBox.Show("兑换码不能重复"); } await Repository.InsertOrUpdateAsync(form ?? throw MessageBox.Show("保存函数参数Dto不是数据库类型,请对保存函数重写!")); } }