using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DOAN.Model; using DOAN.Model.mes.echarts; using DOAN.Model.MES.dev; using DOAN.Model.MES.dev.Dto; using DOAN.Model.System; using DOAN.Repository; using DOAN.Service.MES.dev.IService; using Infrastructure.Attribute; using Infrastructure.Extensions; using Microsoft.AspNetCore.Components.Forms; using Microsoft.AspNetCore.Mvc; namespace DOAN.Service.MES.dev { /// /// 设备停机时间Service业务层处理 /// [AppService( ServiceType = typeof(IDeviceDowntimeRecordService), ServiceLifetime = LifeTime.Transient )] public class DeviceDowntimeRecordService : BaseService, IDeviceDowntimeRecordService { public List GetDevices(string query) { return Context .Queryable() .WhereIF(!string.IsNullOrEmpty(query), it => it.DeviceName.Contains(query)) .ToList(); } /// /// 查询设备停机时间列表 /// /// /// public PagedInfo GetList(DeviceDowntimeRecordQueryDto parm) { var predicate = Expressionable .Create() .AndIF( !string.IsNullOrEmpty(parm.DeviceName), it => it.DeviceName.Contains(parm.DeviceName) ) .AndIF( parm.DateTimeRange != null && parm.DateTimeRange.Length > 0 && parm.DateTimeRange[0] > DateTime.MinValue, it => it.StartTime >= parm.DateTimeRange[0] ) .AndIF( parm.DateTimeRange != null && parm.DateTimeRange.Length > 0 && parm.DateTimeRange[1] > DateTime.MinValue, it => it.StartTime <= parm.DateTimeRange[1] ); var response = Queryable() .Where(predicate.ToExpression()) .ToPage(parm); return response; } /// /// 获取详情 /// /// /// public DeviceDowntimeRecord GetInfo(int Id) { var response = Queryable().Where(x => x.Id == Id).First(); return response; } /// /// 添加设备停机时间 /// /// /// public DeviceDowntimeRecord AddDeviceDowntimeRecord(DeviceDowntimeRecord model) { if (model.DeviceId > 0) { model.DeviceName = Context .Queryable() .Where(x => x.Id == model.DeviceId) .Select(x => x.DeviceName) .First(); } return Context.Insertable(model).ExecuteReturnEntity(); } /// /// 修改设备停机时间 /// /// /// public int UpdateDeviceDowntimeRecord(DeviceDowntimeRecord model) { //var response = Update(w => w.Id == model.Id, it => new DeviceDowntimeRecord() //{ // DeviceName = model.DeviceName, // Downdate = model.Downdate, // StartTime = model.StartTime, // EndTime = model.EndTime, // ConsumeTime = model.ConsumeTime, // FaultPhenomenon = model.FaultPhenomenon, // FaultReason = model.FaultReason, // TemporaryMeasure = model.TemporaryMeasure, // PermanentMeasure = model.PermanentMeasure, // RepairPerson = model.RepairPerson, // RepairResult = model.RepairResult, // CreatedBy = model.CreatedBy, // CreatedTime = model.CreatedTime, // UpdatedBy = model.UpdatedBy, // UpdatedTime = model.UpdatedTime, //}); //return response; return Update(model, true); } public EchartsOptions GetDeviceDowntimeRecordMonthBarChart() { var result = Context.Queryable() .Where(x => x.Downdate != null && x.ConsumeTime != null) .GroupBy(x => new { Year = x.Downdate.Value.Year, Month = x.Downdate.Value.Month }) .Select(x => new { Year = x.Downdate.Value.Year, Month = x.Downdate.Value.Month, TotalDowntime = SqlFunc.AggregateSum(x.ConsumeTime.Value) }) .OrderByDescending(x => x.Year) .OrderByDescending(x => x.Month) .Take(24) .MergeTable() .OrderBy(x => x.Year) .OrderBy(x => x.Month) .Select(x => new EchartsSeriesData { Name = $"{x.Year}-{x.Month}", Value= x.TotalDowntime }) .ToList(); var echartsOptions = new EchartsOptions { Title = new EchartsTitle("按月统计停机时间", "按月统计停机时间"), XAxis = new EchartsXAxis { Type = "category", Data = result.Select(x=>x.Name).ToList(), }, YAxis = new EchartsYAxis { Type = "value" }, Series = new List { new EchartsSeries { Name = "停机时间", Type = "bar", Data = result } } }; return echartsOptions; } public EchartsOptions GetDeviceDowntimeRecordReasonTop3(int top) { var result= Context.Queryable() .Where(x => x.FaultReason != null) .GroupBy(x => x.FaultReason) .Select(x => new { FaultReason = x.FaultReason, TotalDowntime = SqlFunc.AggregateSum(x.ConsumeTime.Value) }) .OrderByDescending(x => x.TotalDowntime) .Take(top) .MergeTable() .Select(x => new EchartsSeriesData { Name = x.FaultReason, Value = x.TotalDowntime }) .ToList(); var echartsOptions = new EchartsOptions { Title = new EchartsTitle("设备停机原因TOP", "设备停机原因TOP"), YAxis = new EchartsYAxis { Type = "category", Data = result.Select(x => x.Name).ToList(), }, Series = new List { new EchartsSeries { Name = "停机时间", Type = "bar", Data = result } } }; return echartsOptions; } } }