using System; using System.Collections.Generic; using System.Data; using System.Linq; using Infrastructure.Attribute; using Infrastructure.Extensions; using SqlSugar; using ZR.Model.mes.echarts; using ZR.Model.MES.pro; using ZR.Model.MES.qc; using ZR.Service.MES.echarts.IService; namespace ZR.Service.MES.md { [AppService(ServiceType = typeof(IFQCEchartsService), ServiceLifetime = LifeTime.Transient)] public class FQCEchartsService : BaseService, IFQCEchartsService { public EchartsOptions GetFQCQualityOptions(FQCQualityQuery query) { try { EchartsOptions echartsOptions = new() { Series = new List() }; if (query.ChartType == 0) { EchartsSeries series = new() { Name = "工单统计", Type = "bar", Data = GetWorkOrderNumberSeriesData(query) }; echartsOptions.Series.Add(series); } if (query.ChartType == 2) { EchartsSeries series = new() { Name = "缺陷数", Type = "pie", Data = GetDefectTotalSeriesData(query) }; echartsOptions.Series.Add(series); } return echartsOptions; } catch (Exception e) { throw new Exception(e.Message); } } /// /// 生成X轴数据 /// /// /// public static List GetXAxisList(FQCQualityQuery query) { List list = new(); int nowYear = DateTime.Now.Year; int nowMonth = DateTime.Now.Month; if (query.TimeType == 1) { int daysInMonth = DateTime.DaysInMonth(nowYear, nowMonth); for (int day = 1; day <= daysInMonth; day++) { // 创建日期对象 DateTime date = new(nowYear, nowMonth, day); // 将日期格式化为字符串 string dateString = date.ToString("yyyy-MM-dd"); list.Add(dateString); } } return list; } /// /// 生成日期数据 /// /// /// public static List GetDateTimeList(FQCQualityQuery query) { int nowYear = DateTime.Now.Year; int nowMonth = DateTime.Now.Month; int nowDay = DateTime.Now.Day; List list = new(); if (query.TimeType == 0) { list.Add(query.StartTime.Value.ToLocalTime()); list.Add(query.EndTime.Value.ToLocalTime()); } else if (query.TimeType == 1) { int daysInMonth = DateTime.DaysInMonth(nowYear, nowMonth); list.Add(new DateTime(nowYear, nowMonth, 1).ToLocalTime()); list.Add( new DateTime(nowYear, nowMonth, daysInMonth) .AddDays(1) .AddTicks(-1) .ToLocalTime() ); } else if (query.TimeType == 2) { list.Add(new DateTime(nowYear, nowMonth, nowDay).ToLocalTime()); list.Add( new DateTime(nowYear, nowMonth, nowDay).AddDays(1).AddTicks(-1).ToLocalTime() ); } else if (query.TimeType == 3) { int dayOfWeek = (int)DateTime.Now.DayOfWeek; DateTime start = DateTime.Now.AddDays(1 - dayOfWeek).Date; list.Add(start.ToLocalTime()); list.Add(start.AddDays(7).AddTicks(-1).ToLocalTime()); } else if (query.TimeType == 4) { int startMonth = (nowMonth - 1) / 3 * 3 + 1; DateTime start = new DateTime(nowYear, startMonth, 1); // 计算季度的最后一个月 int endMonth = start.Month + 2; int daysInMonth = DateTime.DaysInMonth(nowYear, endMonth); list.Add(start.ToLocalTime()); list.Add( new DateTime(nowYear, endMonth, daysInMonth) .AddDays(1) .AddTicks(-1) .ToLocalTime() ); } else if (query.TimeType == 5) { list.Add(new DateTime(nowYear, 1, 1).ToLocalTime()); list.Add(new DateTime(nowYear, 12, 31).AddDays(1).AddTicks(-1).ToLocalTime()); } return list; } /// ======================================= 报表结果 ============================= /// /// 获取工单数echarts图表数据 /// /// /// public List GetWorkOrderNumberSeriesData(FQCQualityQuery query) { List seriesDataList = new(); List dateTimes = GetDateTimeList(query); if (query.ReportType == 1) { var predicate = Expressionable .Create() .And(it => it.StartTime.Value >= dateTimes[0]) .And(it => it.StartTime.Value <= dateTimes[1]) .And(it => it.Remark2 == 2) .ToExpression(); seriesDataList = Context .Queryable() .Where(predicate) .GroupBy(it => it.StartTime.Value.ToString("yyyy-MM-dd")) .Select(it => new EchartsSeriesData { Name = it.StartTime.Value.ToString("yyyy-MM-dd"), Value = SqlFunc.AggregateCount(it.Id) }) .ToList(); } return seriesDataList; } /// /// 获取 日期时间内缺陷总数饼图 /// /// /// public List GetDefectTotalSeriesData(FQCQualityQuery query) { List seriesDataList = new(); List dateTimes = GetDateTimeList(query); if (query.ReportType == 1) { var predicate = Expressionable .Create() .And(it => it.StartTime.Value >= dateTimes[0]) .And(it => it.StartTime.Value <= dateTimes[1]) .And(it => it.Remark2 == 2) .ToExpression(); seriesDataList = Context .Queryable() .Where(predicate) .GroupBy(it => it.FinishedPartNumber + " " + it.ProductDescription) .Select(it => new EchartsSeriesData { Name = it.FinishedPartNumber + " " + it.ProductDescription, Value = (decimal) SqlFunc.AggregateSumNoNull( it.PaoguangTotal + it.DamoTotal + it.BaofeiTotal ) }) .OrderBy(it => it.Value, OrderByType.Desc) .Take(3) .ToList(); } return seriesDataList; } } }