Files
shgx_tz_mes_backend_sync/ZR.Service/mes/echarts/FQCEchartsService.cs

405 lines
15 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Infrastructure.Attribute;
using SqlSugar;
using System;
using System.Data;
using System.Linq;
using ZR.Model.mes.echarts;
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 == 1)
{
List<EchartsSeriesData> datas = GetRejectRateSeriesData(query);
EchartsXAxis xAxis =
new()
{
Type = "value",
Min = "0",
Max = "100",
Data = GenerateXAxisList(datas)
};
echartsOptions.XAxis = xAxis;
EchartsYAxis yAxis =
new() { Type = "category", Data = GenerateXAxisList(datas) };
echartsOptions.YAxis = yAxis;
EchartsSeries series =
new()
{
Name = "近一周废品率top3",
Type = "bar",
Data = datas
};
echartsOptions.Series.Add(series);
}
if (query.ChartType == 2)
{
List<EchartsSeriesData> datas = GetDefectCategorySeriesData(query);
EchartsXAxis xAxis = new() { Type = "value", Data = GenerateXAxisList(datas) };
echartsOptions.XAxis = xAxis;
EchartsYAxis yAxis =
new() { Type = "category", Data = GenerateXAxisList(datas) };
echartsOptions.YAxis = yAxis;
EchartsSeries series =
new()
{
Name = "近一周缺陷类别Top3",
Type = "bar",
Data = datas
};
echartsOptions.Series.Add(series);
}
if (query.ChartType == 3)
{
EchartsSeries series =
new()
{
Name = "近一周缺陷最多零件top3",
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;
}
/// 通用提取X轴数据
public static List<string> GenerateXAxisList(List<EchartsSeriesData> data)
{
try
{
return data.Select(item => item.Name).ToList();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// ======================================= 报表结果 =============================
/// <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;
}
/// <summary>
/// 获取废品率Top3柱状图
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public List<EchartsSeriesData> GetRejectRateSeriesData(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
)
/ SqlFunc.AggregateSumNoNull(it.RequireNumber)
* 100
?? 0
})
.OrderBy(it => it.Value, OrderByType.Desc)
.Take(3)
.ToList();
}
return seriesDataList;
}
/// <summary>
/// 获取缺陷类别Top3柱状图
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public List<EchartsSeriesData> GetDefectCategorySeriesData(FQCQualityQuery query)
{
string[] detailDict =
{
"缩孔",
"针孔",
"失光",
"色差",
"点子",
"其他",
"水斑",
"脏点",
"变形",
"油珠",
"脱落",
"撞伤",
"其他",
"毛刺",
"缩印",
"擦伤",
"砂印",
"流痕",
"开裂",
"流挂",
"色漆缺漆",
"清漆缺漆",
"桔皮",
"其他",
"下件擦伤",
"清漆漆块",
"色漆漆块",
"发花",
"停机",
"喷漏",
};
string[] detailColDict =
{
"PaintSuokong",
"PaintZhengkong",
"PaintShiguang",
"PaintSecha",
"PaintDianzi",
"PaintOther",
"DeviceShuibian",
"DeviceZandian",
"DeviceBianxing",
"DeviceYouzhu",
"DeviceTuoluo",
"DeviceZhuangshang",
"DeviceOther",
"BlankMaoci",
"BlankSuoyin",
"BlankCanshuang",
"BlankShaying",
"BlankZangdian",
"BlankDamo",
"ProgramLiuguang",
"ProgramSeqiqueqi",
"ProgramQingqiqueqi",
"ProgramOther",
"TeamTuoluocanshuang",
"TeamQingqiqikuai",
"TeamSeqiqikuai",
"TeamFahua",
"TeamLiangbang",
"TeamPenglou",
};
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])
.ToExpression();
for (int index = 0; index < detailColDict.Length; index++)
{
// 在此lambda上应用Sum函数
decimal total = (decimal)
Context
.Queryable<QcQualityStatisticsFirst>()
.Where(predicate)
.Sum(it => it.PaintSuokong);
seriesDataList.Add(
new EchartsSeriesData() { Name = detailDict[index], Value = total, }
);
}
}
return seriesDataList.OrderByDescending(s => s.Value).Take(3).ToList();
}
}
}