This commit is contained in:
2024-10-08 17:30:40 +08:00
parent 9858bc13e2
commit 9f4ba1a788
5 changed files with 388 additions and 1 deletions

View File

@@ -0,0 +1,213 @@
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<EchartsOptions>, IFQCEchartsService
{
public EchartsOptions GetFQCQualityOptions(FQCQualityQuery query)
{
try
{
EchartsOptions echartsOptions = new() { Series = new List<EchartsSeries>() };
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);
}
}
/// <summary>
/// 生成X轴数据
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public static List<string> GetXAxisList(FQCQualityQuery query)
{
List<string> 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;
}
/// <summary>
/// 生成日期数据
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public static List<DateTime> GetDateTimeList(FQCQualityQuery query)
{
int nowYear = DateTime.Now.Year;
int nowMonth = DateTime.Now.Month;
int nowDay = DateTime.Now.Day;
List<DateTime> 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;
}
/// ======================================= 报表结果 =============================
/// <summary>
/// 获取工单数echarts图表数据
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public List<EchartsSeriesData> GetWorkOrderNumberSeriesData(FQCQualityQuery query)
{
List<EchartsSeriesData> seriesDataList = new();
List<DateTime> dateTimes = GetDateTimeList(query);
if (query.ReportType == 1)
{
var predicate = Expressionable
.Create<QcQualityStatisticsFirst>()
.And(it => it.StartTime.Value >= dateTimes[0])
.And(it => it.StartTime.Value <= dateTimes[1])
.And(it => it.Remark2 == 2)
.ToExpression();
seriesDataList = Context
.Queryable<QcQualityStatisticsFirst>()
.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;
}
/// <summary>
/// 获取 日期时间内缺陷总数饼图
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public List<EchartsSeriesData> GetDefectTotalSeriesData(FQCQualityQuery query)
{
List<EchartsSeriesData> seriesDataList = new();
List<DateTime> dateTimes = GetDateTimeList(query);
if (query.ReportType == 1)
{
var predicate = Expressionable
.Create<QcQualityStatisticsFirst>()
.And(it => it.StartTime.Value >= dateTimes[0])
.And(it => it.StartTime.Value <= dateTimes[1])
.And(it => it.Remark2 == 2)
.ToExpression();
seriesDataList = Context
.Queryable<QcQualityStatisticsFirst>()
.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;
}
}
}

View File

@@ -0,0 +1,14 @@
using ZR.Model.mes.echarts;
using ZR.Model.mes.md;
namespace ZR.Service.MES.echarts.IService
{
public interface IFQCEchartsService
{
/// <summary>
/// 获取质量报表echarts数据图
/// </summary>
/// <returns></returns>
EchartsOptions GetFQCQualityOptions(FQCQualityQuery query);
}
}