using Cksoft.Data; using System; using System.Collections.Generic; using System.Text; using DllEapEntity.Dtos; using DllEapEntity.OFILM; using System.Linq; using System.Data; using DllEapEntity; using System.Threading.Tasks; using System.Diagnostics; using ChartDto = DllEapEntity.OFILM.ChartDto; using Cksoft.Data.Repository; namespace DllEapDal.OFILM { public class MacRunDataDal { private readonly string timeFormat = "yyyy-MM-dd HH:mm:ss"; private IDatabase CurrDb = null; public MacRunDataDal(IDatabase db) { CurrDb = db; } public DataTable GetMacRunDatas(DateTime startTime, DateTime endTime, string filter, int start, int length, ref string errorinfo, out IEnumerable machines, out int total) { var hours = Convert.ToInt32(Math.Ceiling((endTime - startTime).TotalHours)); var machineDal = new MachineDal(CurrDb); var list = new List(); var dt = new DataTable(); var macFilter = filter?.Replace("c.", "a."); total = machineDal.GetCount(macFilter); var macs = machineDal.Get(start, length, "asc", "a.FCode", macFilter,ref errorinfo); machines = macs; var timeSpan = (endTime - startTime).TotalSeconds; if (macs != null && macs.Count() > 0) { var macIdFilter = $" and b.macid in ({string.Join(",", macs.Select(c => c.ID))})"; var alarmCodeFilter = $" and a.McaCode in ({string.Join(",", macs.Select(c => $"'{c.FCode}'"))})"; var statusFilter = $" and a.MacCode in ({string.Join(",", macs.Select(c => $"'{c.FCode}'"))})"; var sql = $@"SELECT IFNULL(sum(a.FCount),0) Count,IFNULL(sum(a.FCount),0) IntCount,Year(b.edate) year,Month(b.edate) month,DAY(b.edate) DAY,HOUR(b.EDate) Hour, b.MacID MacId FROM maccountdetail a LEFT JOIN maccountmst b ON a.mstid = b.id LEFT JOIN machine c on b.macid=c.id WHERE a.typeID = 0 AND a.ParamCode = 'S00026' {macIdFilter} AND b.EDate BETWEEN '{startTime.ToString(timeFormat)}' AND '{endTime.ToString(timeFormat)}' GROUP BY b.MacID,Year(b.Edate),Month(b.EDate),DAY(b.Edate),HOUR(b.EDate);"; var outputDtos = CurrDb.FindList(sql).ToList(); #region // sql = $@"SELECT //count(1) IntCount, Year(a.stime) Year,Month(a.stime) Month,DAY(a.stime) DAY,HOUR(a.stime) Hour, // sum(flen) TimeLen,maccode MacCode,StatusID //FROM // macstatus a // LEFT JOIN machine c ON a.MacCode = c.fcode //WHERE // 1=1 // AND a.stime>='{startTime.ToString(timeFormat)}' // AND a.stime<='{endTime.ToString(timeFormat)}' // {statusFilter} // AND StatusID = 6 // GROUP BY maccode,Year(a.stime),Month(a.stime),DAY(a.stime),HOUR(a.stime),StatusID"; // var alarmDtos = CurrDb.FindList(sql); // sql = $@"SELECT //count(1) IntCount, Year(a.stime) Year,Month(a.stime) Month,DAY(a.stime) DAY,HOUR(a.stime) Hour, // sum(flen) TimeLen,maccode MacCode,StatusID //FROM // macstatus a //WHERE // a.stime>='{startTime.ToString(timeFormat)}' // AND a.stime<='{endTime.ToString(timeFormat)}' // {statusFilter} // AND StatusID in (4,3,6) // GROUP BY maccode,Year(a.stime),Month(a.stime),DAY(a.stime),HOUR(a.stime),StatusID"; #endregion sql = $@"SELECT count(1) IntCount, Year(a.stime) Year,Month(a.stime) Month,DAY(a.stime) DAY,HOUR(a.stime) Hour, sum(flen) TimeLen,maccode MacCode,StatusID FROM (select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime>='{startTime.ToString(timeFormat)}' AND t.stime<='{endTime.ToString(timeFormat)}' and StatusID=4 union all select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime>='{startTime.ToString(timeFormat)}' AND t.stime<='{endTime.ToString(timeFormat)}' and StatusID=3 union all select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t where 1=1 AND t.stime>='{startTime.ToString(timeFormat)}' AND t.stime<='{endTime.ToString(timeFormat)}' and StatusID=6 ) a WHERE 1=1 {statusFilter} GROUP BY maccode,Year(a.stime),Month(a.stime),DAY(a.stime),HOUR(a.stime),StatusID"; var statusDtos = CurrDb.FindList(sql); #region ////var dic = new Dictionary(); ////for (var k = 0; k < hours; k++) ////{ //// var date = startTime.AddHours(k); //// var endDate = date.AddHours(1); //// dic.Add(date, endDate); ////} ////StringBuilder firstSb = new StringBuilder(); ////StringBuilder lastSb = new StringBuilder(); //sql = $" "; //var baseSql = $"(select * from macstatus t where stime>='{startTime.AddMinutes(-10).ToString(timeFormat)}' " + // $" and t.etime<='{endTime.AddMinutes(10).ToString(timeFormat)}' and t.Flen>0)"; //baseSql = $" macstatus "; //IList allFirstsSqls = new List(); //var allLastsSqls = new List(); //if (dic != null && dic.Keys.Count > 0) //{ // foreach (var item in dic) // { // //sql = $" select a.stime,a.etime,a.maccode,a.statusid,a.flen from {baseSql} a " + // // $"where a.stime<'{item.Key.ToString(timeFormat)}' and a.etime>'{item.Key.ToString(timeFormat)}' " + // // $""; // //allFirstsSqls.Add(sql); // //sql = $" select a.stime,a.etime,a.maccode,a.statusid,a.flen from {baseSql} a " + // // $"where a.stime<'{item.Value.ToString(timeFormat)}' and a.etime>'{item.Value.ToString(timeFormat)}' " + // // $""; // //allLastsSqls.Add(sql); // firstSb.Append($@" (a.stime<'{item.Key.ToString(timeFormat)}' and a.etime>'{item.Key.ToString(timeFormat)}') or "); // lastSb.Append($@" (a.stime<'{item.Value.ToString(timeFormat)}' and a.etime>'{item.Value.ToString(timeFormat)}') or "); // } // firstSb = firstSb.Remove(firstSb.Length - 3, 3); // lastSb = lastSb.Remove(lastSb.Length - 3, 3); //} //var allFirstSql = string.Join(" union all ", allFirstsSqls); //var allLastSql = string.Join(" union all ", allLastsSqls); //var allFirsts = CurrDb.FindList($"select a.stime,a.etime,a.maccode,a.statusid,a.flen from ({allFirstSql}) a where 1=1 {statusFilter}"); #endregion sql = $@"select * from macstatus a where a.etime>'{startTime.AddMinutes(-5).ToString(timeFormat)}' and a.stime<'{endTime.AddMinutes(5).ToString(timeFormat)}' and a.FLen>0 and (day(a.stime)<>day(a.etime) or hour(a.stime)<>hour(a.etime)) "; var macCodes = machines.Select(c => c.FCode).ToList(); // var allFirsts = CurrDb.FindList(sql); var allFirsts = CurrDb.FindList(sql).Where(c => macCodes.Contains(c.MacCode)).ToList(); // var allFirsts = CurrDb.FindList($"select tt.stime,tt.etime,tt.maccode,tt.statusid,tt.flen from ({allFirstSql}) tt left join machine b on tt.maccode=b.fcode where 1=1 {macIdFilter}"); // var allFirsts = CurrDb.FindListForCondition($" {statusFilter} and ({firstSb.ToString()}) and a.flen>0", ref errorinfo); // var allLasts = CurrDb.FindList($"select a.stime,a.etime,a.maccode,a.statusid,a.flen from ({allLastSql}) a where 1=1 "); //var alllasts = CurrDb.FindListForCondition($" {statusFilter} and ({lastSb.ToString()}) and a.flen>0", ref errorinfo); #region 异步 Parallel.For(0, hours, i => { var date = startTime.AddHours(i); var endDate = date.AddHours(1); //var firsts = CurrDb.FindListForCondition($" {statusFilter} and " + // $" a.stime<'{date.ToString(timeFormat)}' and a.etime>'{date.ToString(timeFormat)}'", // ref errorinfo); //var lasts = CurrDb.FindListForCondition($" {statusFilter} and " + // $" a.stime<'{endDate.ToString(timeFormat)}' and a.etime>'{endDate.ToString(timeFormat)}'", // ref errorinfo); var firsts = allFirsts.Where(c => c.STime < date && c.ETime > date).ToList(); var lasts = allFirsts.Where(c => c.STime < endDate && c.ETime > endDate).ToList(); var hoursDtos = statusDtos.Where(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day && c.Hour == date.Hour); Parallel.For(0, hoursDtos.Count(), (j) => { var dto = hoursDtos.ElementAt(j); var first = firsts.FirstOrDefault(c => c.MacCode == dto.MacCode && c.StatusID == dto.StatusId && c.STime < date && c.ETime > date); if (first != null) { dto.TimeLen += (first.ETime - date).TotalSeconds; } var last = lasts.FirstOrDefault(c => c.MacCode == dto.MacCode && c.StatusID == dto.StatusId && c.STime < endDate && c.ETime > endDate); if (last != null) { dto.TimeLen -= (last.ETime - endDate).TotalSeconds; } }); }); #endregion #region 同步 //for (int i = 0; i < hours; i++) //{ // var date = startTime.AddHours(i); // var endDate = date.AddHours(1); // //var firsts = CurrDb.FindListForCondition($" {statusFilter} and " + // // $" a.stime<'{date.ToString(timeFormat)}' and a.etime>'{date.ToString(timeFormat)}'", // // ref errorinfo); // //var lasts = CurrDb.FindListForCondition($" {statusFilter} and " + // // $" a.stime<'{endDate.ToString(timeFormat)}' and a.etime>'{endDate.ToString(timeFormat)}'", // // ref errorinfo); // var firsts = allFirsts.Where(c => c.STime < date && c.ETime > date).ToList(); // var lasts = allFirsts.Where(c => c.STime < endDate && c.ETime > endDate).ToList(); // var hoursDtos = statusDtos.Where(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day && // c.Hour == date.Hour); // var totalCount = hoursDtos.Count(); // for (var j = 0; j < hoursDtos.Count(); j++) // { // var dto = hoursDtos.ElementAt(j); // var first = firsts.FirstOrDefault(c => c.MacCode == dto.MacCode && c.StatusID == dto.StatusId // && c.STime < date && c.ETime > date); // if (first != null) // { // dto.TimeLen += (first.ETime - date).TotalSeconds; // } // var last = lasts.FirstOrDefault(c => c.MacCode == dto.MacCode && c.StatusID == dto.StatusId // && c.STime < endDate && c.ETime > endDate); // if (last != null) // { // dto.TimeLen -= (last.ETime - endDate).TotalSeconds; // } // } //} #endregion var rowStrings = new string[] { "产量", "稼动率", "待机率", "故障率", "故障次数", "故障时长" }; dt.Columns.Add("园区"); dt.Columns.Add("楼层"); dt.Columns.Add("制程代码"); dt.Columns.Add("设备类型"); dt.Columns.Add("设备ID"); dt.Columns.Add("ModelRowSpan"); dt.Columns.Add("MacRowSpan"); //dt.Columns.Add("FactroyNameRowSpan"); dt.Columns.Add("指标"); dt.Columns.Add("汇总"); for (int i = 0; i < hours; i++) { var currTime = startTime.AddHours(i); var date = currTime.ToString("yyyy-MM-dd HH:00:00"); dt.Columns.Add(date); } foreach (var mac in macs) { AppendOutPutDataRow(outputDtos, mac, "产量", dt); AppendRunDataRow(statusDtos.Where(c => c.StatusId == 4), mac, "稼动率", 4, dt, timeSpan, startTime, endTime); AppendRunDataRow(statusDtos.Where(c => c.StatusId == 3), mac, "待机率", 3, dt, timeSpan, startTime, endTime); AppendAlarmRateDataRow(statusDtos.Where(c => c.StatusId == 6), mac, "故障率", dt, "RATE", timeSpan, startTime, endTime); AppendAlarmRateDataRow(statusDtos.Where(c => c.StatusId == 6), mac, "故障次数", dt, "COUNT", timeSpan, startTime, endTime); AppendAlarmRateDataRow(statusDtos.Where(c => c.StatusId == 6), mac, "故障时长", dt, "TIME", timeSpan, startTime, endTime); } var modelGroups = macs.GroupBy(c => c.MModeCode); IList codes = new List(); foreach (var model in modelGroups) { for (var i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i]["设备类型"].ToString() == model.Key) { dt.Rows[i]["ModelRowSpan"] = model.Count() * 6; //dt.Rows[i]["FactroyNameRowSpan"] = model.Count() * 6; } dt.Rows[i]["MacRowSpan"] = 6; } } // else if (colName == "园区") //{ // row[colName] = mac.FactoryName; //} //else if (colName == "楼层") //{ // row[colName] = mac.FloorName; //} //else if (colName == "制程代码") //{ // row[colName] = mac.PCode; //} } return dt; } public DataTable getDT(int hours, DateTime startTime ) { DataTable dt = new DataTable(); dt.Columns.Add("园区"); dt.Columns.Add("楼层"); dt.Columns.Add("制程代码"); dt.Columns.Add("设备类型"); dt.Columns.Add("设备ID"); dt.Columns.Add("ModelRowSpan"); dt.Columns.Add("MacRowSpan"); //dt.Columns.Add("FactroyNameRowSpan"); dt.Columns.Add("指标"); dt.Columns.Add("汇总"); for (int i = 0; i < hours; i++) { var currTime = startTime.AddHours(i); var date = currTime.ToString("yyyy-MM-dd HH:00:00"); dt.Columns.Add(date); } return dt; } /* public LayuiModel GetCountWeek(DateTime startTime, DateTime endTime, string filter, int pageIndex, int pageSize, out int total) { var time = Convert.ToDateTime("2022-03-03 14:00:00"); if (endTime < time) { return GetWeekNew(startTime, endTime, filter, pageIndex, pageSize, out total); } else if (startTime >= time) { return GetWeekNew(startTime, endTime, filter, pageIndex, pageSize, out total); } else { return GetWeekMix(GetWeekOld(startTime, endTime, filter, pageIndex, pageSize, out total), GetWeekNew(startTime, endTime, filter, pageIndex, pageSize, out total), startTime, endTime); } }*/ private LayuiModel GetWeekMix(LayuiModel layuiModel1, LayuiModel layuiModel2, DateTime startTime, DateTime endTime) { throw new NotImplementedException(); } public LayuiModel GetCountWeek(DateTime startTime, DateTime endTime, string filter, int pageIndex, int pageSize, out int total) { ChartDto dto = new ChartDto(); var data = new List(); var time = Convert.ToDateTime("2022-03-03 14:00:00"); var rowStrings = new string[] { "产量", "稼动率", "待机率", "故障率", "故障次数", "故障时长" }; /* string condition = $@" and date_add(a.day,interval a.`Hour` hour)>='{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' and date_add(a.day,interval a.`Hour` hour)<'{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' " + filter; */ int start = (pageIndex - 1) * pageSize + 1; int end = start + pageSize; var mac = GetMac(filter, out total); if (endTime < time) { data = GetDataDel(startTime, endTime, mac).ToList(); } else if (startTime >= time) { data = GetDataDel(startTime, endTime, mac).ToList(); } else { data = GetDataDel(startTime, time, mac).ToList(); data.AddRange(GetDataDel(time, endTime, mac).ToList()); } List list = new List(); List count = new List(); count = SetTotal(data, rowStrings); Parallel.Invoke(() => { dto = SetCharDto(count); }, () => { list.Add(new CountTotal() { Factory = "汇总", Datas = count }); }); for (int i = start-1; i < end&&i c.MacID == item.MacID).ToList(); if (datas==null||datas.Count<1) { continue; } list.Add(new CountTotal() { Factory = item.Factory, Floor = item.Floor, PCode = item.PCode, Model = item.MModelCode, FCode = item.MacCode, Datas = SetTotal(datas, rowStrings) }); } return new LayuiModel() { code = 1, data = list, count = total, extraObject = dto }; } private List GetMac(string filter, out int total) { string table = $@" machine c LEFT OUTER JOIN macmodel d ON c.MModeID = d.id LEFT OUTER JOIN factoryregion e ON c.FactoryId = e.id LEFT OUTER JOIN mactprocess f ON c.id = f.macid LEFT OUTER JOIN tprocess g ON f.PCode = g.FCode LEFT OUTER JOIN factoryregion j ON c.RegionId = j.id LEFT OUTER JOIN factoryregion k ON j.ParentId = k.id LEFT OUTER JOIN factoryregion l ON k.ParentId = l.id"; string select = $@"e.FName Factory,k.FName Floor,g.FCode PCode,d.FCode MModelCode,c.FCode MacCode,c.ID MacID"; total = Convert.ToInt32(CurrDb.FindObject($@"select count(*) from {table} where 1=1 {filter}")); return CurrDb.FindList($"select {select} from {table} where 1=1 {filter} order by c.Fcode").ToList(); } private ChartDto SetCharDto(List count) { ChartDto dto = new ChartDto() { LegendData = new List() { "产量","稼动率","待机率","故障率","故障次数","故障时长" }, XData = new List(), }; List y1 = new List(), y2 = new List(), y3 = new List(), y4 = new List(), y5 = new List(), y6 = new List(); for (int i = 0; i < 24; i++) { dto.XData.Add(i.ToString("00") + ":00:00"); y1.Add(Convert.ToDecimal(count[0].data[i])); y2.Add(Convert.ToDecimal(count[1].data[i].Replace("%",""))); y3.Add(Convert.ToDecimal(count[2].data[i].Replace("%", ""))); y4.Add(Convert.ToDecimal(count[3].data[i].Replace("%", ""))); y5.Add(Convert.ToDecimal(count[4].data[i])); y6.Add(Convert.ToDecimal(count[5].data[i])); } dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "产量", YAxis = 0, Type = "line", Data = y1}); dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "稼动率", YAxis = 1, Type = "line", Data = y2 }); dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "待机率", YAxis = 1, Type = "line", Data = y3 }); dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障率", YAxis = 1, Type = "line", Data = y4 }); dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障次数", YAxis = 0, Type = "line", Data = y5 }); dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障时长", YAxis = 0, Type = "line", Data = y6 }); return dto; } /* private List SetData(List datas, string[] rowStrings) { var data = datas.GroupBy(c => c.Hour).OrderBy(a => a.Key); CountTotal count = new CountTotal() { Factory = datas[0].Factory, Floor=datas[0].Floor, PCode=datas[0].PCode, Model=datas[0].MModelCode, FCode=datas[0].MacCode }; }*/ private List SetTotal(IEnumerable datas, string[] rowStrings) { var data = datas.GroupBy(c => c.Hour).OrderBy(a => a.Key); /* List str = new List() { "稼动率", "待机率", "故障率" };*/ List count = new List(); foreach (var item in rowStrings) { count.Add(new IndexDatas() { Index = item }); } foreach (var item in data) { var da = datas.Where(c => c.Hour == item.Key); var t1 = da.Sum(c => c.RunTime); var t2 = da.Sum(c => c.StandbyTime); var t3 = da.Sum(c => c.AlarmTime); foreach (var items in count) { if (items.Index==rowStrings[0]) { items.data.Add(da.Sum(c => c.Count).ToString()); } if (items.Index == rowStrings[1]) { items.data.Add((Math.Round(t1/(t1+t2+t3),4)*100).ToString()+"%"); } if (items.Index==rowStrings[2]) { items.data.Add((Math.Round(t2 / (t1 + t2 + t3), 4) * 100).ToString() + "%"); } if (items.Index == rowStrings[3]) { items.data.Add((Math.Round(t3 / (t1 + t2 + t3), 4) * 100).ToString() + "%"); } if (items.Index == rowStrings[4]) { items.data.Add(da.Sum(c => c.AlarmCount).ToString()); } if (items.Index == rowStrings[5]) { items.data.Add(Math.Round(da.Sum(c => c.AlarmTime)/60,2).ToString()); } } } return count; } private IEnumerable GetDataDel(DateTime startTime, DateTime endTime, List mac) { var time = Convert.ToDateTime("2022-03-03 14:00:00"); if (startTime>=time) { string condition = $@" and date_add(day,interval `Hour` hour)>='{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' and date_add(day,interval `Hour` hour)<'{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' "; condition += $" and macid in ({string.Join(",", mac.Select(c => c.MacID))})"; string sql = $@" select macid MacID,`Hour` Hour,SUM(RunTime) RunTime,SUM(StandbyTime) StandbyTime,SUM(AlarmTime) AlarmTime,SUM(AlarmCount) AlarmCount,SUM(Count) Count from outputtime where 1=1 {condition} GROUP BY macid, hour HAVING RunTime+AlarmTime+StandbyTime>0 "; var data= CurrDb.FindList(sql); foreach (var item in data) { var machine = mac.Where(c => c.MacID == item.MacID).FirstOrDefault(); item.MacCode = machine.MacCode; item.Factory = machine.Factory; item.Floor = machine.Floor; item.PCode = machine.PCode; item.MModelCode = machine.MModelCode; } return data; } else if (endTime<=time) { string str = $" and b.macid in ({string.Join(",", mac.Select(c => c.MacID))})"; string str1 = $"and MacCode in ({string.Join(",", mac.Select(c => $"'{c.MacCode}'"))})"; List data = new List(); List Tstatus = new List(); Parallel.Invoke(() => { string sqql = $@" SELECT count(1) IntCount, HOUR(a.stime) Hour,StatusID, sum(flen) TimeLen,maccode MacCode FROM (select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=4 {str1} union all select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime >='{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=3 {str1} union all select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=6 {str1}) a WHERE 1=1 GROUP BY HOUR(a.stime),StatusID,maccode "; Tstatus = CurrDb.FindList(sqql).ToList(); }, () => { using(IDatabase db = DbFactory.Base("eapslave")){ string sql = $@"SELECT IFNULL( sum( a.FCount ), 0 ) Count,IFNULL( sum( a.FCount ), 0 ) IntCount,HOUR ( b.EDate ) HOUR,b.MacID MacID FROM maccountdetail a LEFT JOIN maccountmst b ON a.mstid = b.id LEFT JOIN machine c ON b.macid = c.id WHERE a.typeID = 0 AND a.ParamCode = 'S00026' AND b.EDate BETWEEN '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' {str} GROUP BY HOUR ( b.EDate ),b.MacID "; data = db.FindList(sql).ToList(); } }); foreach (var item in data) { var machine = mac.Where(c => c.MacID == item.MacID).FirstOrDefault(); item.MacCode = machine.MacCode; item.Factory = machine.Factory; item.Floor = machine.Floor; item.PCode = machine.PCode; item.MModelCode = machine.MModelCode; Parallel.Invoke( () => { var x = Tstatus.Where(c => c.MacCode == item.MacCode && c.Hour == item.Hour && c.StatusId == 4).FirstOrDefault(); item.RunTime = x != null ? x.TimeLen : 0; }, () => { var x = Tstatus.Where(c => c.MacCode == item.MacCode && c.Hour == item.Hour && c.StatusId == 3).FirstOrDefault(); item.StandbyTime = x != null ? x.TimeLen : 0; }, () => { var x = Tstatus.Where(c => c.MacCode == item.MacCode && c.Hour == item.Hour && c.StatusId == 6).FirstOrDefault(); item.AlarmTime = x != null ? x.TimeLen : 0; item.AlarmCount = x!= null ?Convert.ToInt32( x.IntCount) : 0; } ); } return data; } return new List(); } /// /// 产量按24小时统计的时间段在2022-03-03 14:00:00之前的数据处理 /// /// /// /// /// /// /// /// public LayuiModel GetWeekOld(DateTime startTime, DateTime endTime, string filter, int pageIndex, int pageSize, out int total) { int start = (pageIndex - 1) * pageSize + 1; int end = start + pageSize; total = 1; var machineDal = new MachineDal(CurrDb); var list = new List(); string errorinfo = ""; var macFilter = filter?.Replace("c.", "a."); total += machineDal.GetCount(macFilter); var macs = machineDal.Get(start, end, "asc", "a.FCode", macFilter, ref errorinfo); List tests = new List(); ChartDto dto = new ChartDto(); if (string.IsNullOrEmpty(filter)) { tests.Add(GetTotal(startTime, endTime, "", "", out dto)); } else { var mac = machineDal.Get(1, total, "asc", "a.FCode", macFilter, ref errorinfo); if (mac != null && mac.Count() > 0) { var macIdFilter = $" and b.macid in ({string.Join(",", mac.Select(c => c.ID))})"; /*var alarmCodeFilter = $" and a.McaCode in ({string.Join(",", macs.Select(c => $"'{c.FCode}'"))})";*/ var statusFilter = $" and a.MacCode in ({string.Join(",", mac.Select(c => $"'{c.FCode}'"))})"; tests.Add(GetTotal(startTime, endTime, macIdFilter, statusFilter, out dto)); } } if (macs != null && macs.Count() > 0) { var macIdFilter = $" and b.macid in ({string.Join(",", macs.Select(c => c.ID))})"; /*var alarmCodeFilter = $" and a.McaCode in ({string.Join(",", macs.Select(c => $"'{c.FCode}'"))})";*/ var statusFilter = $" and a.MacCode in ({string.Join(",", macs.Select(c => $"'{c.FCode}'"))})"; tests.AddRange(GetDatas(macs, startTime, endTime, macIdFilter, statusFilter)); } return new LayuiModel() { code = 1, data = tests, count = total, extraObject = dto }; } /// /// 产量按24小时统计的单机台数据处理 /// /// /// /// /// /// /// private IEnumerable GetDatas(IEnumerable macs, DateTime startTime, DateTime endTime, string macFilter, string statusFilter) { string sql = $@" SELECT IFNULL(sum(a.FCount),0) Count,IFNULL(sum(a.FCount),0) IntCount,HOUR(b.EDate) Hour, b.MacID MacId FROM maccountdetail a LEFT JOIN maccountmst b ON a.mstid = b.id LEFT JOIN machine c on b.macid=c.id WHERE a.typeID = 0 AND a.ParamCode = 'S00026' {macFilter} AND b.EDate BETWEEN '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' GROUP BY HOUR(b.EDate), b.MacID"; var TCount = CurrDb.FindList(sql); sql = $@" SELECT count(1) IntCount, HOUR(a.stime) Hour, sum(flen) TimeLen,maccode MacCode FROM (select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=4 union all select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=3 union all select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=6 ) a WHERE 1=1 {statusFilter} GROUP BY HOUR(a.stime),maccode"; var Time = CurrDb.FindList(sql); sql = $@" SELECT count(1) IntCount, HOUR(a.stime) Hour,StatusID, sum(flen) TimeLen,maccode MacCode FROM (select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=4 union all select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime >='{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=3 union all select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=6 ) a WHERE 1=1 {statusFilter} GROUP BY HOUR(a.stime),StatusID,maccode"; var TStatus = CurrDb.FindList(sql); List data = new List(); foreach (var item in macs) { var Sam = new CountTotal() { Factory = item.FactoryName, Floor = item.FloorName, PCode = item.PCode, Model = item.MModeCode, FCode = item.FCode }; var x1 = new IndexDatas() { Index = "产量" }; var x2 = new IndexDatas() { Index = "稼动率" }; var x3 = new IndexDatas() { Index = "待机率" }; var x4 = new IndexDatas() { Index = "故障率" }; var x5 = new IndexDatas() { Index = "故障次数" }; var x6 = new IndexDatas() { Index = "故障时长" }; for (int i = 0; i <24; i++) { var a = TCount.Where(c => c.Hour == i && c.MacId == item.ID)?.FirstOrDefault()?.Count; a = a != null ? a : "0"; var b = TStatus.Where(c => c.MacCode == item.FCode && c.Hour == i && c.StatusId == 4)?.FirstOrDefault()?.TimeLen; b = b != null ? b : 0; var d = Time.Where(c => c.MacCode == item.FCode && c.Hour == i)?.FirstOrDefault()?.TimeLen; d = d != null ? d : 1; var e = TStatus.Where(c => c.MacCode == item.FCode && c.Hour == i && c.StatusId == 3)?.FirstOrDefault()?.TimeLen; e = e != null ? e : 0; var f = TStatus.Where(c => c.MacCode == item.FCode && c.Hour == i && c.StatusId == 6)?.FirstOrDefault()?.TimeLen; f = f != null ? f : 0; var g= TStatus.Where(c => c.MacCode == item.FCode && c.Hour == i && c.StatusId == 6)?.FirstOrDefault()?.IntCount; g = g != null ? g : 0; x1.data.Add(a); x2.data.Add(Math.Round(b.Value / d.Value * 100, 2).ToString()+"%"); x3.data.Add(Math.Round(e.Value / d.Value * 100, 2).ToString()+"%"); x4.data.Add(Math.Round(f.Value / d.Value * 100, 2).ToString()+"%"); x5.data.Add(g.Value.ToString()); x6.data.Add(Math.Round(f.Value / 60, 2).ToString()); } Sam.Datas.Add(x1); Sam.Datas.Add(x2); Sam.Datas.Add(x3); Sam.Datas.Add(x4); Sam.Datas.Add(x5); Sam.Datas.Add(x6); data.Add(Sam); } return data; } /// /// 产量按24小时统计的汇总数据处理 /// /// /// /// /// /// /// public CountTotal GetTotal(DateTime startTime, DateTime endTime, string filter1,string filter,out ChartDto dtos) { try { #region 获取数据 string sql = $@" SELECT IFNULL(sum(a.FCount),0) Count,IFNULL(sum(a.FCount),0) IntCount,HOUR(b.EDate) Hour FROM maccountdetail a LEFT JOIN maccountmst b ON a.mstid = b.id LEFT JOIN machine c on b.macid=c.id WHERE a.typeID = 0 AND a.ParamCode = 'S00026' {filter1} AND b.EDate BETWEEN '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' GROUP BY HOUR(b.EDate)"; var TCount = CurrDb.FindList(sql); sql = $@" SELECT count(1) IntCount, HOUR(a.stime) Hour, sum(flen) TimeLen FROM (select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=4 union all select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=3 union all select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=6 ) a WHERE 1=1 {filter} GROUP BY HOUR(a.stime)"; var Time = CurrDb.FindList(sql); sql = $@" SELECT count(1) IntCount, HOUR(a.stime) Hour,StatusID, sum(flen) TimeLen FROM (select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=4 union all select t.stime,t.statusid,t.maccode,t.flen from macstatus t where 1=1 AND t.stime >='{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=3 union all select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t where 1=1 AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=6 ) a WHERE 1=1 {filter} GROUP BY HOUR(a.stime),StatusID"; var TStatus = CurrDb.FindList(sql); #endregion ChartDto dto = new ChartDto() { LegendData = new List() { "产量","稼动率","待机率","故障率","故障次数","故障时长" }, XData = new List(), }; #region 数据处理 List y1 = new List(), y2 = new List(), y3 = new List(), y4 = new List(), y5 = new List(), y6 = new List(); var Sam = new CountTotal(); Sam.Factory = "汇总"; var x1 = new IndexDatas() { Index = "产量" }; var x2 = new IndexDatas() { Index = "稼动率" }; var x3 = new IndexDatas() { Index = "待机率" }; var x4 = new IndexDatas() { Index = "故障率" }; var x5 = new IndexDatas() { Index = "故障次数" }; var x6 = new IndexDatas() { Index = "故障时长" }; for (int i = 0; i < 24; i++) { dto.XData.Add(i.ToString("00") + ":00:00"); var a = TCount.Where(c => c.Hour == i)?.FirstOrDefault()?.Count; a = a != null ? a : "0"; var b = TStatus.Where(c => c.Hour == i && c.StatusId == 4)?.FirstOrDefault()?.TimeLen; b = b != null ? b : 0; var d = Time.Where(c => c.Hour == i)?.FirstOrDefault()?.TimeLen; d = d != null ? d : 1; var e = TStatus.Where(c => c.Hour == i && c.StatusId == 3)?.FirstOrDefault()?.TimeLen; e = e != null ? e : 0; var f = TStatus.Where(c => c.Hour == i && c.StatusId == 6)?.FirstOrDefault()?.TimeLen; f = f != null ? f : 0; var g = TStatus.Where(c => c.Hour == i && c.StatusId == 6)?.FirstOrDefault()?.IntCount; g = g != null ? g : 0; var t1 = Convert.ToDecimal(Math.Round(b.Value / d.Value * 100, 2)); var t2 = Convert.ToDecimal(Math.Round(e.Value / d.Value * 100, 2)); var t3 = Convert.ToDecimal(Math.Round(f.Value / d.Value * 100, 2)); var t4 = Convert.ToInt32(g.Value); var t5 = Convert.ToDecimal(Math.Round(f.Value / 60, 2)); x1.data.Add(a); x2.data.Add(t1.ToString() + "%"); x3.data.Add(t2.ToString() + "%"); x4.data.Add(t3.ToString() + "%"); x5.data.Add(t4.ToString()); x6.data.Add(t5.ToString()); y1.Add(Convert.ToDecimal(a)); y2.Add(t1); y3.Add(t2); y4.Add(t3); y5.Add(t4); y6.Add(t5); #endregion #region 没有用 /*if (i < Time.Count()) { x1.data.Add(Convert.ToInt32(TCount.Where(c => c.Hour == i).FirstOrDefault().Count)); x2.data.Add(Convert.ToDouble(Math.Round((TStatus.Where(c => c.Hour == i && c.StatusId == 4).FirstOrDefault().TimeLen / Time.Where(c => c.Hour == i).FirstOrDefault().TimeLen) * 100, 2))); x3.data.Add(Convert.ToDouble(Math.Round((TStatus.Where(c => c.Hour == i && c.StatusId == 3).FirstOrDefault().TimeLen / Time.Where(c => c.Hour == i).FirstOrDefault().TimeLen) * 100, 2))); x4.data.Add(Convert.ToDouble(Math.Round((TStatus.Where(c => c.Hour == i && c.StatusId == 6).FirstOrDefault().TimeLen / Time.Where(c => c.Hour == i).FirstOrDefault().TimeLen) * 100, 4))); x5.data.Add(Convert.ToInt32(TStatus.Where(c => c.Hour == i && c.StatusId == 6).FirstOrDefault().IntCount)); x6.data.Add(Convert.ToDouble(Math.Round((TStatus.Where(c => c.Hour == i && c.StatusId == 6).FirstOrDefault().TimeLen) / 3600, 2))); *//*indexDatas.Add(new IndexData() { hour = i.ToString("00") + ":00:00", count = Convert.ToInt32(TCount.Where(c => c.Hour == i).FirstOrDefault().Count), JDrate = Convert.ToDouble(Math.Round(TStatus.Where(c => c.Hour == i && c.StatusId == 4).FirstOrDefault().TimeLen / Time.Where(c => c.Hour == i).FirstOrDefault().TimeLen, 4) * 100), DJrate = Convert.ToDouble(Math.Round(TStatus.Where(c => c.Hour == i && c.StatusId == 3).FirstOrDefault().TimeLen / Time.Where(c => c.Hour == i).FirstOrDefault().TimeLen, 4) * 100), GZrate = Convert.ToDouble(Math.Round(TStatus.Where(c => c.Hour == i && c.StatusId == 6).FirstOrDefault().TimeLen / Time.Where(c => c.Hour == i).FirstOrDefault().TimeLen, 4) * 100), GZcount = Convert.ToInt32(TStatus.Where(c => c.Hour == i && c.StatusId == 6).FirstOrDefault().IntCount), GZtime = Convert.ToDouble(Math.Round((TStatus.Where(c => c.Hour == i && c.StatusId == 6).FirstOrDefault().TimeLen) / 3600, 2)) });*//* } else { *//*indexDatas.Add(new IndexData() { hour = i.ToString("00") + ":00:00", count = 0, JDrate=0.00, DJrate=0.00, GZrate=0.00, GZcount=0, GZtime=0.00 }); ;*//* x1.data.Add(0); x2.data.Add(0); x3.data.Add(0); x4.data.Add(0); x5.data.Add(0); x6.data.Add(0); }*/ #endregion } dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "产量", YAxis = 0, Type = "line", Data = y1 }); dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "稼动率", YAxis = 1, Type = "line", Data = y2 }); dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "待机率", YAxis = 1, Type = "line", Data = y3 }); dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障率", YAxis = 1, Type = "line", Data = y4 }); dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障次数", YAxis = 0, Type = "line", Data = y5 }); dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障时长", YAxis = 0, Type = "line", Data = y6 }); Sam.Datas.Add(x1); Sam.Datas.Add(x2); Sam.Datas.Add(x3); Sam.Datas.Add(x4); Sam.Datas.Add(x5); Sam.Datas.Add(x6); dtos = dto; return Sam; } catch (Exception ex) { throw; } } /// /// 时段产量时间在2022-03-03 14:00:00之前的数据处理方法 /// /// /// /// /// /// /// /// /// public AntdComplexTableDto GetOldData(DateTime startTime, DateTime endTime, string filter, int start, int length, ref string errorinfo, out int total) { IEnumerable macs = null; var dt = this.GetMacRunDatas(startTime, endTime, filter, start, length, ref errorinfo, out macs, out total); var headers = new List(); if (dt != null && dt.Columns.Count > 0) { foreach (DataColumn item in dt.Columns) { if (item.ColumnName == "MacCode" || item.ColumnName == "MacModel" || item.ColumnName == "ModelRowSpan" || item.ColumnName == "MacRowSpan") continue; headers.Add(new AntdColumn { ColName = item.ColumnName }); } } var antdData = new AntdComplexTableDto(); antdData.Columns = headers; var modelGroups = macs.GroupBy(c => c.MModeCode); IList antdComplexDatas = new List(); foreach (var item in modelGroups) { var complexData = new AntdComplexData { MacModel = item.Key, RowSpan = item.Count() }; var modelMacs = item; var macDatas = new List(); complexData.Machines = macDatas; antdComplexDatas.Add(complexData); } antdData.Datas = dt; return antdData; } /// /// 时段产量数据获取 /// /// /// /// /// /// /// /// /// public AntdComplexTableDto GetComplexData(DateTime startTime, DateTime endTime, string filter, int start, int length, ref string errorinfo, out int total) { var time = Convert.ToDateTime("2022-03-03 14:00:00"); if (endTime < time) { return GetOldData(startTime, endTime, filter, start, length, ref errorinfo, out total); } else if (startTime >= time) { return GetNewData(startTime, endTime, filter, start, length, ref errorinfo, out total); } else { return GetMixData(GetOldData(startTime, time, filter, start, length, ref errorinfo, out total).Datas, GetNewData(time, endTime, filter, start, length, ref errorinfo, out total).Datas, startTime,endTime); } } /// /// 时段产量时间包含2022-03-03 14:00:00的数据处理方法 /// /// /// /// /// /// private AntdComplexTableDto GetMixData(DataTable datas1, DataTable datas2, DateTime startTime, DateTime endTime) { List colList1 = new List(); List colList2 = new List(); foreach (DataColumn col in datas1.Columns) { colList1.Add(col.ColumnName); } foreach (DataColumn col in datas2.Columns) { colList2.Add(col.ColumnName); } int hour = Convert.ToInt32(Math.Ceiling((endTime - startTime).TotalHours)); var dt = getDT(hour, startTime); int i = 0; while (i(); if (dt != null && dt.Columns.Count > 0) { foreach (DataColumn item in dt.Columns) { if (item.ColumnName == "MacCode" || item.ColumnName == "MacModel" || item.ColumnName == "ModelRowSpan" || item.ColumnName == "MacRowSpan") continue; headers.Add(new AntdColumn { ColName = item.ColumnName }); } } return new AntdComplexTableDto() { Datas = dt, Columns = headers }; } /// /// 时段产量时间在2022-03-03 14:00:00之后的数据处理方法 /// /// /// /// /// /// /// /// /// private AntdComplexTableDto GetNewData(DateTime startTime, DateTime endTime, string filter, int start, int length, ref string errorinfo, out int total) { int hour = Convert.ToInt32(Math.Ceiling((endTime - startTime).TotalHours)); var rowStrings = new string[] { "产量", "稼动率", "待机率", "故障率", "故障次数", "故障时长" }; var dt = getDT(hour, startTime); string condition = $@" and date_add(a.day,interval a.`Hour` hour)>='{startTime.ToString("yyyy-MM-dd HH:mm:ss")}' and date_add(a.day,interval a.`Hour` hour)<'{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' " + filter; string table = new OutPutTimeInfo().GetQueryTabSql(); total = Convert.ToInt32( CurrDb.FindObject($@"select count(DISTINCT(a.macid)) from {table} where 1=1 {condition}")); var mac = CurrDb.FindList($@"select DISTINCT (a.MacID ) MacID,c.FCode MacCode from {table} where 1=1 {condition} order by c.Fcode limit {start - 1},{length}"); condition += $" and a.macid in ({string.Join(",", mac.Select(c => c.MacID))})"; var data = CurrDb.FindListForCondition(condition, ref errorinfo); foreach (var item in mac) { var datas = data.Where(c => c.MacID == item.MacID).OrderBy(c => c.ID).ToList(); var row1 = dt.NewRow(); var row2 = dt.NewRow(); var row3 = dt.NewRow(); var row4 = dt.NewRow(); var row5 = dt.NewRow(); var row6 = dt.NewRow(); foreach (DataColumn col in dt.Columns) { var colName = col.ColumnName; if (colName == "汇总") { row1[colName] = datas.Sum(c => c.Count); row2[colName] = Math.Round(datas.Sum(c => c.RunRate) / hour, 4); row3[colName] = Math.Round(datas.Sum(c => c.StandbyRate) / hour, 4); row4[colName] = Math.Round(datas.Sum(c => c.AlarmRate) / hour, 4); row5[colName] = datas.Sum(c => c.AlarmCount); row6[colName] = Math.Round(datas.Sum(c => c.AlarmTime) , 4); } else if (colName == "指标") { row1[colName] = rowStrings[0]; row2[colName] = rowStrings[1]; row3[colName] = rowStrings[2]; row4[colName] = rowStrings[3]; row5[colName] = rowStrings[4]; row6[colName] = rowStrings[5]; } else if (colName == "设备ID") { row1[colName] = datas[0].MacCode; row2[colName] = datas[0].MacCode; row3[colName] = datas[0].MacCode; row4[colName] = datas[0].MacCode; row5[colName] = datas[0].MacCode; row6[colName] = datas[0].MacCode; } else if (colName == "设备类型") { row1[colName] = datas[0].MModelCode; row2[colName] = datas[0].MModelCode; row3[colName] = datas[0].MModelCode; row4[colName] = datas[0].MModelCode; row5[colName] = datas[0].MModelCode; row6[colName] = datas[0].MModelCode; } else if (colName == "MacRowSpan" || colName == "ModelRowSpan") { continue; } else if (colName == "园区") { row1[colName] = datas[0].Factory; row2[colName] = datas[0].Factory; row3[colName] = datas[0].Factory; row4[colName] = datas[0].Factory; row5[colName] = datas[0].Factory; row6[colName] = datas[0].Factory; } else if (colName == "楼层") { row1[colName] = datas[0].Floor; row2[colName] = datas[0].Floor; row3[colName] = datas[0].Floor; row4[colName] = datas[0].Floor; row5[colName] = datas[0].Floor; row6[colName] = datas[0].Floor; } else if (colName == "制程代码") { row1[colName] = datas[0].PCode; row2[colName] = datas[0].PCode; row3[colName] = datas[0].PCode; row4[colName] = datas[0].PCode; row5[colName] = datas[0].PCode; row6[colName] = datas[0].PCode; } else { var da = new OutPutTimeInfo(); var day = Convert.ToDateTime( Convert.ToDateTime(colName).ToString("yyyy-MM-dd")); var hou= Convert.ToDouble(Convert.ToDateTime(colName).ToString("HH")); da = datas.Where(c => c.Day == day && c.Hour == hou)?.FirstOrDefault(); row1[colName] = da.Count; row2[colName] = da.RunRate; row3[colName] = da.StandbyRate; row4[colName] = da.AlarmRate; row5[colName] = da.AlarmCount; row6[colName] = da.AlarmTime / 60; } } dt.Rows.Add(row1); dt.Rows.Add(row2); dt.Rows.Add(row3); dt.Rows.Add(row4); dt.Rows.Add(row5); dt.Rows.Add(row6); } var modelGroups = data.GroupBy(c => c.MModelCode); foreach (var model in modelGroups) { for (var i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i]["设备类型"].ToString() == model.Key) { dt.Rows[i]["ModelRowSpan"] = model.Count() * 6; //dt.Rows[i]["FactroyNameRowSpan"] = model.Count() * 6; } dt.Rows[i]["MacRowSpan"] = 6; } } var headers = new List(); if (dt != null && dt.Columns.Count > 0) { foreach (DataColumn item in dt.Columns) { if (item.ColumnName == "MacCode" || item.ColumnName == "MacModel" || item.ColumnName == "ModelRowSpan" || item.ColumnName == "MacRowSpan") continue; headers.Add(new AntdColumn { ColName = item.ColumnName }); } } return new AntdComplexTableDto() { Datas = dt, Columns = headers, }; } #region 拼接DataTable /// /// 添加产量行 /// /// /// /// /// private void AppendOutPutDataRow(IEnumerable datas, Machine mac, string type, DataTable dt) { DataRow row = dt.NewRow(); Func func = t => t.MacCode == mac.FCode; if (type == "产量") { func = t => t.MacId == mac.ID; } foreach (DataColumn col in dt.Columns) { var colName = col.ColumnName; if (colName == "汇总") { row[colName] = datas.Where(c => c.MacId == mac.ID).Sum(c => c.IntCount); } else if (colName == "指标") { row[colName] = type; } else if (colName == "设备ID") { row[colName] = mac.FCode; } else if (colName == "设备类型") { row[colName] = mac.MModeCode; } else if (colName == "MacRowSpan" || colName == "ModelRowSpan" ) { continue; } else if (colName == "园区") { row[colName] = mac.FactoryName; } else if (colName == "楼层") { row[colName] = mac.FloorName; } else if (colName == "制程代码") { row[colName] = mac.PCode; } else { var currTime = Convert.ToDateTime(colName); var year = currTime.Year; var month = currTime.Month; var day = currTime.Day; var hour = currTime.Hour; var entity = datas.Where(func).FirstOrDefault(c => c.Year == year && c.Month == month && c.Day == day && c.Hour == hour); if (entity == null) { row[colName] = 0; } else { row[colName] = entity.Count; } } } dt.Rows.Add(row); } /// /// 添加稼动率及闲置率 /// /// /// /// /// private void AppendRunDataRow(IEnumerable datas, Machine mac, string type, int statusId, DataTable dt, double timeSpan, DateTime startTime, DateTime endTime) { //string errorinfo = string.Empty; //var hours = Math.Ceiling((endTime - startTime).TotalHours); //if (datas.Any(c => c.StatusId == statusId && c.MacCode == mac.FCode)) //{ // for (int i = 0; i < hours; i++) // { // var date = startTime.AddHours(i); // var endDate = date.AddHours(1); // var first = CurrDb.FindListForCondition($" and statusId={statusId} and maccode='{mac.FCode}' " + // $"and etime>'{date.ToString(timeFormat)}' order by stime asc limit 0,1 ", ref errorinfo) // .FirstOrDefault(); // if (first != null && first.STime < date) // { // datas.FirstOrDefault(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day // && c.Hour == date.Hour && c.StatusId == statusId && c.MacCode == mac.FCode).TimeLen += (first.ETime - date).TotalSeconds; // } // var last = CurrDb.FindListForCondition($" and statusId={statusId} and maccode='{mac.FCode}' " + // $"and stime<'{endDate.ToString(timeFormat)}' order by stime desc limit 0,1 ", ref errorinfo) // .FirstOrDefault(); // if (last != null && last.ETime > endDate) // { // datas.FirstOrDefault(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day // && c.Hour == date.Hour && c.StatusId == statusId && c.MacCode == mac.FCode).TimeLen -= (last.ETime - endDate).TotalSeconds; // } // } //} DataRow row = dt.NewRow(); Func func = t => t.MacCode == mac.FCode; foreach (DataColumn col in dt.Columns) { var colName = col.ColumnName; if (colName == "汇总") { double rate = 0; var totalTime = datas.Where(c => c.MacCode == mac.FCode && c.StatusId == statusId)?.Sum(c => c.TimeLen); if (totalTime != null) { rate = totalTime.Value / (timeSpan); } row[colName] = rate; } else if (colName == "指标") { row[colName] = type; } else if (colName == "设备ID") { row[colName] = mac.FCode; } else if (colName == "设备类型") { row[colName] = mac.MModeCode; } else if (colName == "MacRowSpan" || colName == "ModelRowSpan" ) { continue; } else if (colName == "园区") { row[colName] = mac.FactoryName; } else if (colName == "楼层") { row[colName] = mac.FloorName; } else if (colName == "制程代码") { row[colName] = mac.PCode; } else { var currTime = Convert.ToDateTime(colName); var year = currTime.Year; var month = currTime.Month; var day = currTime.Day; var hour = currTime.Hour; var statuses = datas.Where(c => c.MacCode == mac.FCode && c.Year == year && c.Month == month && c.Day == day && c.Hour == hour && c.StatusId == statusId); if (statuses == null || statuses.Count() <= 0) { row[colName] = 0; } else { row[colName] = statuses.Sum(c => c.TimeLen) / 3600; } } } dt.Rows.Add(row); } /// /// 生成报警数据行 /// /// /// /// /// /// private void AppendAlarmRateDataRow(IEnumerable datas, Machine mac, string type, DataTable dt, string alarmType, double timeSpan, DateTime startTime, DateTime endTime) { //string errorinfo = string.Empty; //var hours = Math.Ceiling((endTime - startTime).TotalHours); //if (datas.Any(c => c.StatusId == 6 && c.MacCode == mac.FCode)) //{ // for (int i = 0; i < hours; i++) // { // var date = startTime.AddHours(i); // var endDate = date.AddHours(1); // var first = CurrDb.FindListForCondition($" and statusId={6} and maccode='{mac.FCode}' " + // $"and etime>'{date.ToString(timeFormat)}' order by stime asc limit 0,1 ", ref errorinfo) // .FirstOrDefault(); // if (first != null && first.STime < date) // { // datas.FirstOrDefault(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day // && c.Hour == date.Hour && c.StatusId == 6 && c.MacCode == mac.FCode).TimeLen += (first.ETime - date).TotalSeconds; // } // var last = CurrDb.FindListForCondition($" and statusId={6} and maccode='{mac.FCode}' " + // $"and stime<'{endDate.ToString(timeFormat)}' order by stime desc limit 0,1 ", ref errorinfo) // .FirstOrDefault(); // if (last != null && last.ETime > endDate) // { // datas.FirstOrDefault(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day // && c.Hour == date.Hour && c.StatusId == 6 && c.MacCode == mac.FCode).TimeLen -= (last.ETime - endDate).TotalSeconds; // } // } //} DataRow row = dt.NewRow(); Func func = t => t.MacCode == mac.FCode; foreach (DataColumn col in dt.Columns) { var colName = col.ColumnName; if (colName == "汇总") { var totalTime = datas.Where(c => c.MacCode == mac.FCode); if (alarmType == "COUNT") { int count = 0; if (totalTime != null) { count = totalTime.Sum(c => c.IntCount); } row[colName] = count; } else if (alarmType == "TIME") { double time = 0; if (totalTime != null) { time = totalTime.Sum(c => c.TimeLen) / 60; } row[colName] = time; } else { double time = 0; if (totalTime != null) { time = totalTime.Sum(c => c.TimeLen) / (timeSpan); } row[colName] = time; } } else if (colName == "指标") { row[colName] = type; } else if (colName == "设备ID") { row[colName] = mac.FCode; } else if (colName == "设备类型") { row[colName] = mac.MModeCode; } else if (colName == "MacRowSpan" || colName == "ModelRowSpan") { continue; } else if (colName == "园区") { row[colName] = mac.FactoryName; } else if (colName == "楼层") { row[colName] = mac.FloorName; } else if (colName == "制程代码") { row[colName] = mac.PCode; } else { var currTime = Convert.ToDateTime(colName); var year = currTime.Year; var month = currTime.Month; var day = currTime.Day; var hour = currTime.Hour; var statuses = datas.Where(c => c.MacCode == mac.FCode && c.Year == year && c.Month == month && c.Day == day && c.Hour == hour); if (statuses == null || statuses.Count() <= 0) { row[colName] = 0; } else { switch (alarmType) { case "COUNT": row[colName] = statuses.Sum(c => c.IntCount); break; case "TIME": row[colName] = statuses.Sum(c => c.TimeLen) / 60; break; default: row[colName] = statuses.Sum(c => c.TimeLen) / 3600; break; } } } } dt.Rows.Add(row); } #endregion } }