Waste/Waste.Application/ReportInfo/ReportService.cs

189 lines
8.3 KiB
C#
Raw Normal View History

2021-05-27 16:58:40 +08:00
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
{
/// <summary>
/// 统计管理
/// </summary>
public class ReportService : BaseInfoService, IReportService, ITransient
{
private readonly ISqlSugarRepository<W_DeviceStatistics> repository;
private readonly SqlSugarClient dbClient;
public ReportService(ISqlSugarRepository<W_DeviceStatistics> sqlSugarRepository)
{
repository = sqlSugarRepository;
dbClient = repository.Context;
}
/// <summary>
/// 统计列表
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public async Task<PageParms<ReportList>> GetListAsync(QueryParams param)
{
RefAsync<int> totalnum = 0;
var temquery = dbClient.Queryable<W_DeviceStatistics>();
if (param.queryParam != null && param.queryParam.Count > 0)
{
List<IConditionalModel> conModels = new List<IConditionalModel>();
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<W_Device>().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);
}
}
2021-05-30 16:02:38 +08:00
//针对非平台类型,则可以查看下面所有的子账户设备
2021-05-27 16:58:40 +08:00
if (currentUser.AccountType != (int)AccountType.platform)
{
2021-05-30 16:02:38 +08:00
var sql = $"code like '{currentUser.BusinessCode}'+'%' and id = x.businessid";
temquery = temquery.Where(x => SqlFunc.Subqueryable<W_Business>().Where(sql).Any());
2021-05-27 16:58:40 +08:00
}
2021-05-30 16:02:38 +08:00
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);
2021-05-27 16:58:40 +08:00
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<W_Business>().Context.Queryable<W_Business>().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<W_Device>().Context.Queryable<W_Device>().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 : "";
2021-05-30 16:02:38 +08:00
it.TotalDayCount = totaldaycount;
it.TotalDayPureWeight = totaldaypureweight;
it.TotalDayWeight = totaldayweight;
})
.ToPageListAsync(param.offset, param.limit, totalnum);
return new PageParms<ReportList>
{
page = param.offset,
Items = query,
totalnum = totalnum,
limit = param.limit
};
}
/// <summary>
/// 根据商户和垃圾类型进行汇总
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public async Task<PageParms<ReportList>> GetListByBusinessAsync(QueryParams param)
{
RefAsync<int> totalnum = 0;
var temquery = repository.Change<W_BusinessStatistics>().Context.Queryable<W_BusinessStatistics>();
if (param.queryParam != null && param.queryParam.Count > 0)
{
List<IConditionalModel> conModels = new List<IConditionalModel>();
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<W_Business>().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<W_Business>().Context.Queryable<W_Business>().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;
2021-05-27 16:58:40 +08:00
})
.ToPageListAsync(param.offset, param.limit, totalnum);
return new PageParms<ReportList>
{
page = param.offset,
Items = query,
totalnum = totalnum,
limit = param.limit
};
}
}
}