using Furion.DependencyInjection; using Nirvana.Common; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Waste.Domain; namespace Waste.Application { /// /// 统计管理 /// public class ReportService : BaseInfoService, IReportService, ITransient { private readonly ISqlSugarRepository repository; private readonly SqlSugarClient dbClient; public ReportService(ISqlSugarRepository sqlSugarRepository) { repository = sqlSugarRepository; dbClient = repository.Context; } /// /// 统计列表 /// /// /// public async Task> GetListAsync(QueryParams param) { RefAsync totalnum = 0; var temquery = dbClient.Queryable(); if (param.queryParam != null && param.queryParam.Count > 0) { List conModels = new List(); param.queryParam.ForEach(x => { x.Value = x.Value.ToStr(); if (!string.IsNullOrEmpty(x.Value)) { if(x.Name.ToLower() == "devname") { temquery = temquery.Where(e => SqlFunc.Subqueryable().Where(w => w.Name == x.Value && e.DevId == w.Id).Any()); } else { conModels.Add(new ConditionalModel() { FieldName = x.Name, ConditionalType = (ConditionalType)x.Type, FieldValue = x.Value.Trim() }); } } }); if (conModels.Count > 0) { temquery = temquery.Where(conModels); } } //针对非平台类型,则可以查看下面所有的子账户设备 if (currentUser.AccountType != (int)AccountType.platform) { var sql = $"code like '{currentUser.BusinessCode}'+'%' and id = x.businessid"; temquery = temquery.Where(x => SqlFunc.Subqueryable().Where(sql).Any()); } int totaldaycount = await temquery.Clone().SumAsync(x => x.DayCount); decimal totaldaypureweight = await temquery.Clone().SumAsync(x => x.DayPureWeight); decimal totaldayweight = await temquery.Clone().SumAsync(x => x.DayWeight); string sorts = string.Format("{0} {1}", param.sort, param.order); var query = await temquery.OrderBy(sorts) .Select(x => new ReportList { Id = x.Id, Businessid = x.Businessid, DevId = x.DevId, CreateTime = x.CreateTime, DayCount = x.DayCount, DayPureWeight = x.DayPureWeight, DayWeight = x.DayWeight, WasteType = x.WasteType }) .Mapper((it, cache) => { var allbus = cache.Get(list => { var ids = list.Where(x => x.Businessid != Guid.Empty).Select(x => x.Businessid).ToList(); return repository.Change().Context.Queryable().Where(e => ids.Contains(e.Id)).ToList(); }); var bus = allbus.FirstOrDefault(x => x.Id == it.Businessid); it.BusinessName = bus != null ? bus.Name : ""; var alldev = cache.Get(list => { var ids = list.Where(x => x.DevId != Guid.Empty).Select(x => x.DevId).ToList(); return repository.Change().Context.Queryable().Where(e => ids.Contains(e.Id)).ToList(); }); var dev = alldev.FirstOrDefault(x => x.Id == it.DevId); it.DevName = dev != null ? dev.Name : ""; it.DevCode = dev != null ? dev.Ecode : ""; it.TotalDayCount = totaldaycount; it.TotalDayPureWeight = totaldaypureweight; it.TotalDayWeight = totaldayweight; }) .ToPageListAsync(param.offset, param.limit, totalnum); return new PageParms { page = param.offset, Items = query, totalnum = totalnum, limit = param.limit }; } /// /// 根据商户和垃圾类型进行汇总 /// /// /// public async Task> GetListByBusinessAsync(QueryParams param) { RefAsync totalnum = 0; var temquery = repository.Change().Context.Queryable(); if (param.queryParam != null && param.queryParam.Count > 0) { List conModels = new List(); param.queryParam.ForEach(x => { x.Value = x.Value.ToStr(); if (!string.IsNullOrEmpty(x.Value)) { conModels.Add(new ConditionalModel() { FieldName = x.Name, ConditionalType = (ConditionalType)x.Type, FieldValue = x.Value.Trim() }); } }); if (conModels.Count > 0) { temquery = temquery.Where(conModels); } } //针对非平台类型,则可以查看下面所有的子账户设备 if (currentUser.AccountType != (int)AccountType.platform) { var sql = $"code like '{currentUser.BusinessCode}'+'%' and id = x.businessid"; temquery = temquery.Where(x => SqlFunc.Subqueryable().Where(sql).Any()); } int totaldaycount = await temquery.Clone().SumAsync(x => x.DayCount); decimal totaldaypureweight = await temquery.Clone().SumAsync(x => x.DayPureWeight); decimal totaldayweight = await temquery.Clone().SumAsync(x => x.DayWeight); string sorts = string.Format("{0} {1}", param.sort, param.order); var query = await temquery.Clone().OrderBy(sorts) .Select(x => new ReportList { Businessid = x.BusinessId, CreateTime = x.CreateTime, DayCount = x.DayCount, DayPureWeight = x.DayPureWeight, DayWeight = x.DayWeight, WasteType = x.WasteType }) .Mapper((it, cache) => { var allbus = cache.Get(list => { var ids = list.Where(x => x.Businessid != Guid.Empty).Select(x => x.Businessid).ToList(); return repository.Change().Context.Queryable().Where(e => ids.Contains(e.Id)).ToList(); }); var bus = allbus.FirstOrDefault(x => x.Id == it.Businessid); it.BusinessName = bus != null ? bus.Name : ""; it.TotalDayCount = totaldaycount; it.TotalDayPureWeight = totaldaypureweight; it.TotalDayWeight = totaldayweight; }) .ToPageListAsync(param.offset, param.limit, totalnum); return new PageParms { page = param.offset, Items = query, totalnum = totalnum, limit = param.limit }; } } }