MacRunDataDal.cs 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  1. using Cksoft.Data;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using DllEapEntity.Dtos;
  6. using DllEapEntity.OFILM;
  7. using System.Linq;
  8. using System.Data;
  9. using DllEapEntity;
  10. using System.Threading.Tasks;
  11. using System.Diagnostics;
  12. using ChartDto = DllEapEntity.OFILM.ChartDto;
  13. using Cksoft.Data.Repository;
  14. namespace DllEapDal.OFILM
  15. {
  16. public class MacRunDataDal
  17. {
  18. private readonly string timeFormat = "yyyy-MM-dd HH:mm:ss";
  19. private IDatabase CurrDb = null;
  20. public MacRunDataDal(IDatabase db)
  21. {
  22. CurrDb = db;
  23. }
  24. public DataTable GetMacRunDatas(DateTime startTime, DateTime endTime, string filter, int start, int length, ref string errorinfo,
  25. out IEnumerable<Machine> machines, out int total)
  26. {
  27. var hours = Convert.ToInt32(Math.Ceiling((endTime - startTime).TotalHours));
  28. var machineDal = new MachineDal(CurrDb);
  29. var list = new List<MacRunDataDto>();
  30. var dt = new DataTable();
  31. var macFilter = filter?.Replace("c.", "a.");
  32. total = machineDal.GetCount(macFilter);
  33. var macs = machineDal.Get(start, length, "asc", "a.FCode", macFilter,ref errorinfo);
  34. machines = macs;
  35. var timeSpan = (endTime - startTime).TotalSeconds;
  36. if (macs != null && macs.Count() > 0)
  37. {
  38. var macIdFilter = $" and b.macid in ({string.Join(",", macs.Select(c => c.ID))})";
  39. var alarmCodeFilter = $" and a.McaCode in ({string.Join(",", macs.Select(c => $"'{c.FCode}'"))})";
  40. var statusFilter = $" and a.MacCode in ({string.Join(",", macs.Select(c => $"'{c.FCode}'"))})";
  41. var sql = $@"SELECT
  42. 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,
  43. b.MacID MacId
  44. FROM
  45. maccountdetail a
  46. LEFT JOIN maccountmst b ON a.mstid = b.id
  47. LEFT JOIN machine c on b.macid=c.id
  48. WHERE
  49. a.typeID = 0
  50. AND a.ParamCode = 'S00026'
  51. {macIdFilter}
  52. AND b.EDate BETWEEN '{startTime.ToString(timeFormat)}'
  53. AND '{endTime.ToString(timeFormat)}'
  54. GROUP BY b.MacID,Year(b.Edate),Month(b.EDate),DAY(b.Edate),HOUR(b.EDate);";
  55. var outputDtos = CurrDb.FindList<HourCount>(sql).ToList();
  56. #region
  57. // sql = $@"SELECT
  58. //count(1) IntCount, Year(a.stime) Year,Month(a.stime) Month,DAY(a.stime) DAY,HOUR(a.stime) Hour,
  59. // sum(flen) TimeLen,maccode MacCode,StatusID
  60. //FROM
  61. // macstatus a
  62. // LEFT JOIN machine c ON a.MacCode = c.fcode
  63. //WHERE
  64. // 1=1
  65. // AND a.stime>='{startTime.ToString(timeFormat)}'
  66. // AND a.stime<='{endTime.ToString(timeFormat)}'
  67. // {statusFilter}
  68. // AND StatusID = 6
  69. // GROUP BY maccode,Year(a.stime),Month(a.stime),DAY(a.stime),HOUR(a.stime),StatusID";
  70. // var alarmDtos = CurrDb.FindList<HourCount>(sql);
  71. // sql = $@"SELECT
  72. //count(1) IntCount, Year(a.stime) Year,Month(a.stime) Month,DAY(a.stime) DAY,HOUR(a.stime) Hour,
  73. // sum(flen) TimeLen,maccode MacCode,StatusID
  74. //FROM
  75. // macstatus a
  76. //WHERE
  77. // a.stime>='{startTime.ToString(timeFormat)}'
  78. // AND a.stime<='{endTime.ToString(timeFormat)}'
  79. // {statusFilter}
  80. // AND StatusID in (4,3,6)
  81. // GROUP BY maccode,Year(a.stime),Month(a.stime),DAY(a.stime),HOUR(a.stime),StatusID";
  82. #endregion
  83. sql = $@"SELECT
  84. count(1) IntCount, Year(a.stime) Year,Month(a.stime) Month,DAY(a.stime) DAY,HOUR(a.stime) Hour,
  85. sum(flen) TimeLen,maccode MacCode,StatusID
  86. FROM
  87. (select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  88. where 1=1
  89. AND t.stime>='{startTime.ToString(timeFormat)}'
  90. AND t.stime<='{endTime.ToString(timeFormat)}' and StatusID=4
  91. union all
  92. select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  93. where 1=1
  94. AND t.stime>='{startTime.ToString(timeFormat)}'
  95. AND t.stime<='{endTime.ToString(timeFormat)}' and StatusID=3
  96. union all
  97. select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t
  98. where 1=1
  99. AND t.stime>='{startTime.ToString(timeFormat)}'
  100. AND t.stime<='{endTime.ToString(timeFormat)}' and StatusID=6 )
  101. a
  102. WHERE
  103. 1=1
  104. {statusFilter}
  105. GROUP BY maccode,Year(a.stime),Month(a.stime),DAY(a.stime),HOUR(a.stime),StatusID";
  106. var statusDtos = CurrDb.FindList<HourCount>(sql);
  107. #region
  108. ////var dic = new Dictionary<DateTime, DateTime>();
  109. ////for (var k = 0; k < hours; k++)
  110. ////{
  111. //// var date = startTime.AddHours(k);
  112. //// var endDate = date.AddHours(1);
  113. //// dic.Add(date, endDate);
  114. ////}
  115. ////StringBuilder firstSb = new StringBuilder();
  116. ////StringBuilder lastSb = new StringBuilder();
  117. //sql = $" ";
  118. //var baseSql = $"(select * from macstatus t where stime>='{startTime.AddMinutes(-10).ToString(timeFormat)}' " +
  119. // $" and t.etime<='{endTime.AddMinutes(10).ToString(timeFormat)}' and t.Flen>0)";
  120. //baseSql = $" macstatus ";
  121. //IList<string> allFirstsSqls = new List<string>();
  122. //var allLastsSqls = new List<string>();
  123. //if (dic != null && dic.Keys.Count > 0)
  124. //{
  125. // foreach (var item in dic)
  126. // {
  127. // //sql = $" select a.stime,a.etime,a.maccode,a.statusid,a.flen from {baseSql} a " +
  128. // // $"where a.stime<'{item.Key.ToString(timeFormat)}' and a.etime>'{item.Key.ToString(timeFormat)}' " +
  129. // // $"";
  130. // //allFirstsSqls.Add(sql);
  131. // //sql = $" select a.stime,a.etime,a.maccode,a.statusid,a.flen from {baseSql} a " +
  132. // // $"where a.stime<'{item.Value.ToString(timeFormat)}' and a.etime>'{item.Value.ToString(timeFormat)}' " +
  133. // // $"";
  134. // //allLastsSqls.Add(sql);
  135. // firstSb.Append($@" (a.stime<'{item.Key.ToString(timeFormat)}' and a.etime>'{item.Key.ToString(timeFormat)}') or ");
  136. // lastSb.Append($@" (a.stime<'{item.Value.ToString(timeFormat)}' and a.etime>'{item.Value.ToString(timeFormat)}') or ");
  137. // }
  138. // firstSb = firstSb.Remove(firstSb.Length - 3, 3);
  139. // lastSb = lastSb.Remove(lastSb.Length - 3, 3);
  140. //}
  141. //var allFirstSql = string.Join(" union all ", allFirstsSqls);
  142. //var allLastSql = string.Join(" union all ", allLastsSqls);
  143. //var allFirsts = CurrDb.FindList<MacStatus>($"select a.stime,a.etime,a.maccode,a.statusid,a.flen from ({allFirstSql}) a where 1=1 {statusFilter}");
  144. #endregion
  145. sql = $@"select * from macstatus a where
  146. a.etime>'{startTime.AddMinutes(-5).ToString(timeFormat)}'
  147. and a.stime<'{endTime.AddMinutes(5).ToString(timeFormat)}'
  148. and a.FLen>0
  149. and (day(a.stime)<>day(a.etime) or hour(a.stime)<>hour(a.etime))
  150. ";
  151. var macCodes = machines.Select(c => c.FCode).ToList();
  152. // var allFirsts = CurrDb.FindList<MacStatus>(sql);
  153. var allFirsts = CurrDb.FindList<MacStatus>(sql).Where(c => macCodes.Contains(c.MacCode)).ToList();
  154. // var allFirsts = CurrDb.FindList<MacStatus>($"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}");
  155. // var allFirsts = CurrDb.FindListForCondition<MacStatus>($" {statusFilter} and ({firstSb.ToString()}) and a.flen>0", ref errorinfo);
  156. // var allLasts = CurrDb.FindList<MacStatus>($"select a.stime,a.etime,a.maccode,a.statusid,a.flen from ({allLastSql}) a where 1=1 ");
  157. //var alllasts = CurrDb.FindListForCondition<MacStatus>($" {statusFilter} and ({lastSb.ToString()}) and a.flen>0", ref errorinfo);
  158. #region 异步
  159. Parallel.For(0, hours, i =>
  160. {
  161. var date = startTime.AddHours(i);
  162. var endDate = date.AddHours(1);
  163. //var firsts = CurrDb.FindListForCondition<MacStatus>($" {statusFilter} and " +
  164. // $" a.stime<'{date.ToString(timeFormat)}' and a.etime>'{date.ToString(timeFormat)}'",
  165. // ref errorinfo);
  166. //var lasts = CurrDb.FindListForCondition<MacStatus>($" {statusFilter} and " +
  167. // $" a.stime<'{endDate.ToString(timeFormat)}' and a.etime>'{endDate.ToString(timeFormat)}'",
  168. // ref errorinfo);
  169. var firsts = allFirsts.Where(c => c.STime < date && c.ETime > date).ToList();
  170. var lasts = allFirsts.Where(c => c.STime < endDate && c.ETime > endDate).ToList();
  171. var hoursDtos = statusDtos.Where(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day &&
  172. c.Hour == date.Hour);
  173. Parallel.For(0, hoursDtos.Count(), (j) =>
  174. {
  175. var dto = hoursDtos.ElementAt(j);
  176. var first = firsts.FirstOrDefault(c => c.MacCode == dto.MacCode && c.StatusID == dto.StatusId
  177. && c.STime < date && c.ETime > date);
  178. if (first != null)
  179. {
  180. dto.TimeLen += (first.ETime - date).TotalSeconds;
  181. }
  182. var last = lasts.FirstOrDefault(c => c.MacCode == dto.MacCode && c.StatusID == dto.StatusId
  183. && c.STime < endDate && c.ETime > endDate);
  184. if (last != null)
  185. {
  186. dto.TimeLen -= (last.ETime - endDate).TotalSeconds;
  187. }
  188. });
  189. });
  190. #endregion
  191. #region 同步
  192. //for (int i = 0; i < hours; i++)
  193. //{
  194. // var date = startTime.AddHours(i);
  195. // var endDate = date.AddHours(1);
  196. // //var firsts = CurrDb.FindListForCondition<MacStatus>($" {statusFilter} and " +
  197. // // $" a.stime<'{date.ToString(timeFormat)}' and a.etime>'{date.ToString(timeFormat)}'",
  198. // // ref errorinfo);
  199. // //var lasts = CurrDb.FindListForCondition<MacStatus>($" {statusFilter} and " +
  200. // // $" a.stime<'{endDate.ToString(timeFormat)}' and a.etime>'{endDate.ToString(timeFormat)}'",
  201. // // ref errorinfo);
  202. // var firsts = allFirsts.Where(c => c.STime < date && c.ETime > date).ToList();
  203. // var lasts = allFirsts.Where(c => c.STime < endDate && c.ETime > endDate).ToList();
  204. // var hoursDtos = statusDtos.Where(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day &&
  205. // c.Hour == date.Hour);
  206. // var totalCount = hoursDtos.Count();
  207. // for (var j = 0; j < hoursDtos.Count(); j++)
  208. // {
  209. // var dto = hoursDtos.ElementAt(j);
  210. // var first = firsts.FirstOrDefault(c => c.MacCode == dto.MacCode && c.StatusID == dto.StatusId
  211. // && c.STime < date && c.ETime > date);
  212. // if (first != null)
  213. // {
  214. // dto.TimeLen += (first.ETime - date).TotalSeconds;
  215. // }
  216. // var last = lasts.FirstOrDefault(c => c.MacCode == dto.MacCode && c.StatusID == dto.StatusId
  217. // && c.STime < endDate && c.ETime > endDate);
  218. // if (last != null)
  219. // {
  220. // dto.TimeLen -= (last.ETime - endDate).TotalSeconds;
  221. // }
  222. // }
  223. //}
  224. #endregion
  225. var rowStrings = new string[] { "产量", "稼动率", "待机率", "故障率", "故障次数", "故障时长" };
  226. dt.Columns.Add("园区");
  227. dt.Columns.Add("楼层");
  228. dt.Columns.Add("制程代码");
  229. dt.Columns.Add("设备类型");
  230. dt.Columns.Add("设备ID");
  231. dt.Columns.Add("ModelRowSpan");
  232. dt.Columns.Add("MacRowSpan");
  233. //dt.Columns.Add("FactroyNameRowSpan");
  234. dt.Columns.Add("指标");
  235. dt.Columns.Add("汇总");
  236. for (int i = 0; i < hours; i++)
  237. {
  238. var currTime = startTime.AddHours(i);
  239. var date = currTime.ToString("yyyy-MM-dd HH:00:00");
  240. dt.Columns.Add(date);
  241. }
  242. foreach (var mac in macs)
  243. {
  244. AppendOutPutDataRow(outputDtos, mac, "产量", dt);
  245. AppendRunDataRow(statusDtos.Where(c => c.StatusId == 4), mac, "稼动率", 4, dt, timeSpan, startTime, endTime);
  246. AppendRunDataRow(statusDtos.Where(c => c.StatusId == 3), mac, "待机率", 3, dt, timeSpan, startTime, endTime);
  247. AppendAlarmRateDataRow(statusDtos.Where(c => c.StatusId == 6), mac, "故障率", dt, "RATE", timeSpan, startTime, endTime);
  248. AppendAlarmRateDataRow(statusDtos.Where(c => c.StatusId == 6), mac, "故障次数", dt, "COUNT", timeSpan, startTime, endTime);
  249. AppendAlarmRateDataRow(statusDtos.Where(c => c.StatusId == 6), mac, "故障时长", dt, "TIME", timeSpan, startTime, endTime);
  250. }
  251. var modelGroups = macs.GroupBy(c => c.MModeCode);
  252. IList<string> codes = new List<string>();
  253. foreach (var model in modelGroups)
  254. {
  255. for (var i = 0; i < dt.Rows.Count; i++)
  256. {
  257. if (dt.Rows[i]["设备类型"].ToString() == model.Key)
  258. {
  259. dt.Rows[i]["ModelRowSpan"] = model.Count() * 6;
  260. //dt.Rows[i]["FactroyNameRowSpan"] = model.Count() * 6;
  261. }
  262. dt.Rows[i]["MacRowSpan"] = 6;
  263. }
  264. }
  265. // else if (colName == "园区")
  266. //{
  267. // row[colName] = mac.FactoryName;
  268. //}
  269. //else if (colName == "楼层")
  270. //{
  271. // row[colName] = mac.FloorName;
  272. //}
  273. //else if (colName == "制程代码")
  274. //{
  275. // row[colName] = mac.PCode;
  276. //}
  277. }
  278. return dt;
  279. }
  280. public DataTable getDT(int hours, DateTime startTime )
  281. {
  282. DataTable dt = new DataTable();
  283. dt.Columns.Add("园区");
  284. dt.Columns.Add("楼层");
  285. dt.Columns.Add("制程代码");
  286. dt.Columns.Add("设备类型");
  287. dt.Columns.Add("设备ID");
  288. dt.Columns.Add("ModelRowSpan");
  289. dt.Columns.Add("MacRowSpan");
  290. //dt.Columns.Add("FactroyNameRowSpan");
  291. dt.Columns.Add("指标");
  292. dt.Columns.Add("汇总");
  293. for (int i = 0; i < hours; i++)
  294. {
  295. var currTime = startTime.AddHours(i);
  296. var date = currTime.ToString("yyyy-MM-dd HH:00:00");
  297. dt.Columns.Add(date);
  298. }
  299. return dt;
  300. }
  301. /* public LayuiModel<CountTotal> GetCountWeek(DateTime startTime, DateTime endTime, string filter, int pageIndex, int pageSize, out int total)
  302. {
  303. var time = Convert.ToDateTime("2022-03-03 14:00:00");
  304. if (endTime < time)
  305. {
  306. return GetWeekNew(startTime, endTime, filter, pageIndex, pageSize, out total);
  307. }
  308. else if (startTime >= time)
  309. {
  310. return GetWeekNew(startTime, endTime, filter, pageIndex, pageSize, out total);
  311. }
  312. else
  313. {
  314. return GetWeekMix(GetWeekOld(startTime, endTime, filter, pageIndex, pageSize, out total), GetWeekNew(startTime, endTime, filter, pageIndex, pageSize, out total), startTime, endTime);
  315. }
  316. }*/
  317. private LayuiModel<CountTotal> GetWeekMix(LayuiModel<CountTotal> layuiModel1, LayuiModel<CountTotal> layuiModel2, DateTime startTime, DateTime endTime)
  318. {
  319. throw new NotImplementedException();
  320. }
  321. public LayuiModel<CountTotal> GetCountWeek(DateTime startTime, DateTime endTime, string filter, int pageIndex, int pageSize, out int total)
  322. {
  323. ChartDto dto = new ChartDto();
  324. var data = new List<OutPutTimeInfo>();
  325. var time = Convert.ToDateTime("2022-03-03 14:00:00");
  326. var rowStrings = new string[] { "产量", "稼动率", "待机率", "故障率", "故障次数", "故障时长" };
  327. /* 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;
  328. */ int start = (pageIndex - 1) * pageSize + 1;
  329. int end = start + pageSize;
  330. var mac = GetMac(filter, out total);
  331. if (endTime < time)
  332. {
  333. data = GetDataDel(startTime, endTime, mac).ToList();
  334. }
  335. else if (startTime >= time)
  336. {
  337. data = GetDataDel(startTime, endTime, mac).ToList();
  338. }
  339. else
  340. {
  341. data = GetDataDel(startTime, time, mac).ToList();
  342. data.AddRange(GetDataDel(time, endTime, mac).ToList());
  343. }
  344. List<CountTotal> list = new List<CountTotal>();
  345. List<IndexDatas> count = new List<IndexDatas>();
  346. count = SetTotal(data, rowStrings);
  347. Parallel.Invoke(() => { dto = SetCharDto(count); }, () => {
  348. list.Add(new CountTotal()
  349. {
  350. Factory = "汇总",
  351. Datas = count
  352. });
  353. });
  354. for (int i = start-1; i < end&&i<total; i++)
  355. {
  356. var item = mac[i];
  357. var datas = data.Where(c => c.MacID == item.MacID).ToList();
  358. if (datas==null||datas.Count<1)
  359. {
  360. continue;
  361. }
  362. list.Add(new CountTotal()
  363. {
  364. Factory = item.Factory,
  365. Floor = item.Floor,
  366. PCode = item.PCode,
  367. Model = item.MModelCode,
  368. FCode = item.MacCode,
  369. Datas = SetTotal(datas, rowStrings)
  370. });
  371. }
  372. return new LayuiModel<CountTotal>()
  373. {
  374. code = 1,
  375. data = list,
  376. count = total,
  377. extraObject = dto
  378. };
  379. }
  380. private List<OutPutTimeInfo> GetMac(string filter, out int total)
  381. {
  382. string table = $@" machine c
  383. LEFT OUTER JOIN macmodel d ON c.MModeID = d.id
  384. LEFT OUTER JOIN factoryregion e ON c.FactoryId = e.id
  385. LEFT OUTER JOIN mactprocess f ON c.id = f.macid
  386. LEFT OUTER JOIN tprocess g ON f.PCode = g.FCode
  387. LEFT OUTER JOIN factoryregion j ON c.RegionId = j.id
  388. LEFT OUTER JOIN factoryregion k ON j.ParentId = k.id
  389. LEFT OUTER JOIN factoryregion l ON k.ParentId = l.id";
  390. string select = $@"e.FName Factory,k.FName Floor,g.FCode PCode,d.FCode MModelCode,c.FCode MacCode,c.ID MacID";
  391. total = Convert.ToInt32(CurrDb.FindObject($@"select count(*) from {table} where 1=1 {filter}"));
  392. return CurrDb.FindList<OutPutTimeInfo>($"select {select} from {table} where 1=1 {filter} order by c.Fcode").ToList();
  393. }
  394. private ChartDto SetCharDto(List<IndexDatas> count)
  395. {
  396. ChartDto dto = new ChartDto()
  397. {
  398. LegendData = new List<string>()
  399. {
  400. "产量","稼动率","待机率","故障率","故障次数","故障时长"
  401. },
  402. XData = new List<string>(),
  403. };
  404. List<decimal> y1 = new List<decimal>(), y2 = new List<decimal>(), y3 = new List<decimal>(), y4 = new List<decimal>(), y5 = new List<decimal>(), y6 = new List<decimal>();
  405. for (int i = 0; i < 24; i++)
  406. {
  407. dto.XData.Add(i.ToString("00") + ":00:00");
  408. y1.Add(Convert.ToDecimal(count[0].data[i]));
  409. y2.Add(Convert.ToDecimal(count[1].data[i].Replace("%","")));
  410. y3.Add(Convert.ToDecimal(count[2].data[i].Replace("%", "")));
  411. y4.Add(Convert.ToDecimal(count[3].data[i].Replace("%", "")));
  412. y5.Add(Convert.ToDecimal(count[4].data[i]));
  413. y6.Add(Convert.ToDecimal(count[5].data[i]));
  414. }
  415. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "产量", YAxis = 0, Type = "line", Data = y1});
  416. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "稼动率", YAxis = 1, Type = "line", Data = y2 });
  417. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "待机率", YAxis = 1, Type = "line", Data = y3 });
  418. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障率", YAxis = 1, Type = "line", Data = y4 });
  419. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障次数", YAxis = 0, Type = "line", Data = y5 });
  420. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障时长", YAxis = 0, Type = "line", Data = y6 });
  421. return dto;
  422. }
  423. /* private List<IndexDatas> SetData(List<OutPutTimeInfo> datas, string[] rowStrings)
  424. {
  425. var data = datas.GroupBy(c => c.Hour).OrderBy(a => a.Key);
  426. CountTotal count = new CountTotal()
  427. {
  428. Factory = datas[0].Factory,
  429. Floor=datas[0].Floor,
  430. PCode=datas[0].PCode,
  431. Model=datas[0].MModelCode,
  432. FCode=datas[0].MacCode
  433. };
  434. }*/
  435. private List<IndexDatas> SetTotal(IEnumerable<OutPutTimeInfo> datas, string[] rowStrings)
  436. {
  437. var data = datas.GroupBy(c => c.Hour).OrderBy(a => a.Key);
  438. /* List<string> str = new List<string>() { "稼动率", "待机率", "故障率" };*/
  439. List<IndexDatas> count = new List<IndexDatas>();
  440. foreach (var item in rowStrings)
  441. {
  442. count.Add(new IndexDatas()
  443. {
  444. Index = item
  445. });
  446. }
  447. foreach (var item in data)
  448. {
  449. var da = datas.Where(c => c.Hour == item.Key);
  450. var t1 = da.Sum(c => c.RunTime);
  451. var t2 = da.Sum(c => c.StandbyTime);
  452. var t3 = da.Sum(c => c.AlarmTime);
  453. foreach (var items in count)
  454. {
  455. if (items.Index==rowStrings[0])
  456. {
  457. items.data.Add(da.Sum(c => c.Count).ToString());
  458. }
  459. if (items.Index == rowStrings[1])
  460. {
  461. items.data.Add((Math.Round(t1/(t1+t2+t3),4)*100).ToString()+"%");
  462. }
  463. if (items.Index==rowStrings[2])
  464. {
  465. items.data.Add((Math.Round(t2 / (t1 + t2 + t3), 4) * 100).ToString() + "%");
  466. }
  467. if (items.Index == rowStrings[3])
  468. {
  469. items.data.Add((Math.Round(t3 / (t1 + t2 + t3), 4) * 100).ToString() + "%");
  470. }
  471. if (items.Index == rowStrings[4])
  472. {
  473. items.data.Add(da.Sum(c => c.AlarmCount).ToString());
  474. }
  475. if (items.Index == rowStrings[5])
  476. {
  477. items.data.Add(Math.Round(da.Sum(c => c.AlarmTime)/60,2).ToString());
  478. }
  479. }
  480. }
  481. return count;
  482. }
  483. private IEnumerable<OutPutTimeInfo> GetDataDel(DateTime startTime, DateTime endTime, List<OutPutTimeInfo> mac)
  484. {
  485. var time = Convert.ToDateTime("2022-03-03 14:00:00");
  486. if (startTime>=time)
  487. {
  488. 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")}' ";
  489. condition += $" and macid in ({string.Join(",", mac.Select(c => c.MacID))})";
  490. string sql = $@" select macid MacID,`Hour` Hour,SUM(RunTime) RunTime,SUM(StandbyTime) StandbyTime,SUM(AlarmTime) AlarmTime,SUM(AlarmCount) AlarmCount,SUM(Count) Count
  491. from outputtime where 1=1 {condition} GROUP BY macid, hour HAVING RunTime+AlarmTime+StandbyTime>0 ";
  492. var data= CurrDb.FindList<OutPutTimeInfo>(sql);
  493. foreach (var item in data)
  494. {
  495. var machine = mac.Where(c => c.MacID == item.MacID).FirstOrDefault();
  496. item.MacCode = machine.MacCode;
  497. item.Factory = machine.Factory;
  498. item.Floor = machine.Floor;
  499. item.PCode = machine.PCode;
  500. item.MModelCode = machine.MModelCode;
  501. }
  502. return data;
  503. }
  504. else if (endTime<=time)
  505. {
  506. string str = $" and b.macid in ({string.Join(",", mac.Select(c => c.MacID))})";
  507. string str1 = $"and MacCode in ({string.Join(",", mac.Select(c => $"'{c.MacCode}'"))})";
  508. List<OutPutTimeInfo> data = new List<OutPutTimeInfo>();
  509. List<HourCount> Tstatus = new List<HourCount>();
  510. Parallel.Invoke(() =>
  511. {
  512. string sqql = $@" SELECT
  513. count(1) IntCount, HOUR(a.stime) Hour,StatusID,
  514. sum(flen) TimeLen,maccode MacCode
  515. FROM
  516. (select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  517. where 1=1
  518. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  519. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=4 {str1}
  520. union all
  521. select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  522. where 1=1
  523. AND t.stime >='{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  524. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=3 {str1}
  525. union all
  526. select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t
  527. where 1=1
  528. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  529. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=6 {str1})
  530. a
  531. WHERE
  532. 1=1
  533. GROUP BY HOUR(a.stime),StatusID,maccode ";
  534. Tstatus = CurrDb.FindList<HourCount>(sqql).ToList();
  535. }, () =>
  536. {
  537. using(IDatabase db = DbFactory.Base("eapslave")){
  538. string sql = $@"SELECT IFNULL( sum( a.FCount ), 0 ) Count,IFNULL( sum( a.FCount ), 0 ) IntCount,HOUR ( b.EDate ) HOUR,b.MacID MacID
  539. FROM
  540. maccountdetail a
  541. LEFT JOIN maccountmst b ON a.mstid = b.id
  542. LEFT JOIN machine c ON b.macid = c.id
  543. WHERE
  544. a.typeID = 0
  545. AND a.ParamCode = 'S00026'
  546. AND b.EDate BETWEEN '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  547. AND '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' {str}
  548. GROUP BY HOUR ( b.EDate ),b.MacID ";
  549. data = db.FindList<OutPutTimeInfo>(sql).ToList();
  550. }
  551. });
  552. foreach (var item in data)
  553. {
  554. var machine = mac.Where(c => c.MacID == item.MacID).FirstOrDefault();
  555. item.MacCode = machine.MacCode;
  556. item.Factory = machine.Factory;
  557. item.Floor = machine.Floor;
  558. item.PCode = machine.PCode;
  559. item.MModelCode = machine.MModelCode;
  560. Parallel.Invoke(
  561. () => {
  562. var x = Tstatus.Where(c => c.MacCode == item.MacCode && c.Hour == item.Hour && c.StatusId == 4).FirstOrDefault();
  563. item.RunTime = x != null ? x.TimeLen : 0;
  564. },
  565. () => {
  566. var x = Tstatus.Where(c => c.MacCode == item.MacCode && c.Hour == item.Hour && c.StatusId == 3).FirstOrDefault();
  567. item.StandbyTime = x != null ? x.TimeLen : 0;
  568. },
  569. () => {
  570. var x = Tstatus.Where(c => c.MacCode == item.MacCode && c.Hour == item.Hour && c.StatusId == 6).FirstOrDefault();
  571. item.AlarmTime = x != null ? x.TimeLen : 0;
  572. item.AlarmCount = x!= null ?Convert.ToInt32( x.IntCount) : 0;
  573. }
  574. );
  575. }
  576. return data;
  577. }
  578. return new List<OutPutTimeInfo>();
  579. }
  580. /// <summary>
  581. /// 产量按24小时统计的时间段在2022-03-03 14:00:00之前的数据处理
  582. /// </summary>
  583. /// <param name="startTime"></param>
  584. /// <param name="endTime"></param>
  585. /// <param name="filter"></param>
  586. /// <param name="pageIndex"></param>
  587. /// <param name="pageSize"></param>
  588. /// <param name="total"></param>
  589. /// <returns></returns>
  590. public LayuiModel<CountTotal> GetWeekOld(DateTime startTime, DateTime endTime, string filter, int pageIndex, int pageSize, out int total)
  591. {
  592. int start = (pageIndex - 1) * pageSize + 1;
  593. int end = start + pageSize;
  594. total = 1;
  595. var machineDal = new MachineDal(CurrDb);
  596. var list = new List<MacRunDataDto>();
  597. string errorinfo = "";
  598. var macFilter = filter?.Replace("c.", "a.");
  599. total += machineDal.GetCount(macFilter);
  600. var macs = machineDal.Get(start, end, "asc", "a.FCode", macFilter, ref errorinfo);
  601. List<CountTotal> tests = new List<CountTotal>();
  602. ChartDto dto = new ChartDto();
  603. if (string.IsNullOrEmpty(filter))
  604. {
  605. tests.Add(GetTotal(startTime, endTime, "", "", out dto));
  606. }
  607. else
  608. {
  609. var mac = machineDal.Get(1, total, "asc", "a.FCode", macFilter, ref errorinfo);
  610. if (mac != null && mac.Count() > 0)
  611. {
  612. var macIdFilter = $" and b.macid in ({string.Join(",", mac.Select(c => c.ID))})";
  613. /*var alarmCodeFilter = $" and a.McaCode in ({string.Join(",", macs.Select(c => $"'{c.FCode}'"))})";*/
  614. var statusFilter = $" and a.MacCode in ({string.Join(",", mac.Select(c => $"'{c.FCode}'"))})";
  615. tests.Add(GetTotal(startTime, endTime, macIdFilter, statusFilter, out dto));
  616. }
  617. }
  618. if (macs != null && macs.Count() > 0)
  619. {
  620. var macIdFilter = $" and b.macid in ({string.Join(",", macs.Select(c => c.ID))})";
  621. /*var alarmCodeFilter = $" and a.McaCode in ({string.Join(",", macs.Select(c => $"'{c.FCode}'"))})";*/
  622. var statusFilter = $" and a.MacCode in ({string.Join(",", macs.Select(c => $"'{c.FCode}'"))})";
  623. tests.AddRange(GetDatas(macs, startTime, endTime, macIdFilter, statusFilter));
  624. }
  625. return new LayuiModel<CountTotal>()
  626. {
  627. code = 1,
  628. data = tests,
  629. count = total,
  630. extraObject = dto
  631. };
  632. }
  633. /// <summary>
  634. /// 产量按24小时统计的单机台数据处理
  635. /// </summary>
  636. /// <param name="macs"></param>
  637. /// <param name="startTime"></param>
  638. /// <param name="endTime"></param>
  639. /// <param name="macFilter"></param>
  640. /// <param name="statusFilter"></param>
  641. /// <returns></returns>
  642. private IEnumerable<CountTotal> GetDatas(IEnumerable<Machine> macs, DateTime startTime, DateTime endTime, string macFilter, string statusFilter)
  643. {
  644. string sql = $@" SELECT
  645. IFNULL(sum(a.FCount),0) Count,IFNULL(sum(a.FCount),0) IntCount,HOUR(b.EDate) Hour, b.MacID MacId
  646. FROM
  647. maccountdetail a
  648. LEFT JOIN maccountmst b ON a.mstid = b.id
  649. LEFT JOIN machine c on b.macid=c.id
  650. WHERE
  651. a.typeID = 0
  652. AND a.ParamCode = 'S00026'
  653. {macFilter}
  654. AND b.EDate BETWEEN '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  655. AND '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  656. GROUP BY HOUR(b.EDate), b.MacID";
  657. var TCount = CurrDb.FindList<HourCount>(sql);
  658. sql = $@" SELECT
  659. count(1) IntCount, HOUR(a.stime) Hour,
  660. sum(flen) TimeLen,maccode MacCode
  661. FROM
  662. (select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  663. where 1=1
  664. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  665. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=4
  666. union all
  667. select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  668. where 1=1
  669. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  670. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=3
  671. union all
  672. select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t
  673. where 1=1
  674. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  675. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=6 )
  676. a
  677. WHERE
  678. 1=1
  679. {statusFilter}
  680. GROUP BY HOUR(a.stime),maccode";
  681. var Time = CurrDb.FindList<HourCount>(sql);
  682. sql = $@" SELECT
  683. count(1) IntCount, HOUR(a.stime) Hour,StatusID,
  684. sum(flen) TimeLen,maccode MacCode
  685. FROM
  686. (select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  687. where 1=1
  688. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  689. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=4
  690. union all
  691. select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  692. where 1=1
  693. AND t.stime >='{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  694. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=3
  695. union all
  696. select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t
  697. where 1=1
  698. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  699. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=6 )
  700. a
  701. WHERE
  702. 1=1
  703. {statusFilter}
  704. GROUP BY HOUR(a.stime),StatusID,maccode";
  705. var TStatus = CurrDb.FindList<HourCount>(sql);
  706. List <CountTotal> data = new List<CountTotal>();
  707. foreach (var item in macs)
  708. {
  709. var Sam = new CountTotal()
  710. {
  711. Factory = item.FactoryName,
  712. Floor = item.FloorName,
  713. PCode = item.PCode,
  714. Model = item.MModeCode,
  715. FCode = item.FCode
  716. };
  717. var x1 = new IndexDatas()
  718. {
  719. Index = "产量"
  720. };
  721. var x2 = new IndexDatas()
  722. {
  723. Index = "稼动率"
  724. };
  725. var x3 = new IndexDatas()
  726. {
  727. Index = "待机率"
  728. };
  729. var x4 = new IndexDatas()
  730. {
  731. Index = "故障率"
  732. };
  733. var x5 = new IndexDatas()
  734. {
  735. Index = "故障次数"
  736. };
  737. var x6 = new IndexDatas()
  738. {
  739. Index = "故障时长"
  740. };
  741. for (int i = 0; i <24; i++)
  742. {
  743. var a = TCount.Where(c => c.Hour == i && c.MacId == item.ID)?.FirstOrDefault()?.Count;
  744. a = a != null ? a : "0";
  745. var b = TStatus.Where(c => c.MacCode == item.FCode && c.Hour == i && c.StatusId == 4)?.FirstOrDefault()?.TimeLen;
  746. b = b != null ? b : 0;
  747. var d = Time.Where(c => c.MacCode == item.FCode && c.Hour == i)?.FirstOrDefault()?.TimeLen;
  748. d = d != null ? d : 1;
  749. var e = TStatus.Where(c => c.MacCode == item.FCode && c.Hour == i && c.StatusId == 3)?.FirstOrDefault()?.TimeLen;
  750. e = e != null ? e : 0;
  751. var f = TStatus.Where(c => c.MacCode == item.FCode && c.Hour == i && c.StatusId == 6)?.FirstOrDefault()?.TimeLen;
  752. f = f != null ? f : 0;
  753. var g= TStatus.Where(c => c.MacCode == item.FCode && c.Hour == i && c.StatusId == 6)?.FirstOrDefault()?.IntCount;
  754. g = g != null ? g : 0;
  755. x1.data.Add(a);
  756. x2.data.Add(Math.Round(b.Value / d.Value * 100, 2).ToString()+"%");
  757. x3.data.Add(Math.Round(e.Value / d.Value * 100, 2).ToString()+"%");
  758. x4.data.Add(Math.Round(f.Value / d.Value * 100, 2).ToString()+"%");
  759. x5.data.Add(g.Value.ToString());
  760. x6.data.Add(Math.Round(f.Value / 60, 2).ToString());
  761. }
  762. Sam.Datas.Add(x1); Sam.Datas.Add(x2); Sam.Datas.Add(x3); Sam.Datas.Add(x4); Sam.Datas.Add(x5); Sam.Datas.Add(x6);
  763. data.Add(Sam);
  764. }
  765. return data;
  766. }
  767. /// <summary>
  768. /// 产量按24小时统计的汇总数据处理
  769. /// </summary>
  770. /// <param name="startTime"></param>
  771. /// <param name="endTime"></param>
  772. /// <param name="filter1"></param>
  773. /// <param name="filter"></param>
  774. /// <param name="dtos"></param>
  775. /// <returns></returns>
  776. public CountTotal GetTotal(DateTime startTime, DateTime endTime, string filter1,string filter,out ChartDto dtos)
  777. {
  778. try
  779. {
  780. #region 获取数据
  781. string sql = $@" SELECT
  782. IFNULL(sum(a.FCount),0) Count,IFNULL(sum(a.FCount),0) IntCount,HOUR(b.EDate) Hour
  783. FROM
  784. maccountdetail a
  785. LEFT JOIN maccountmst b ON a.mstid = b.id
  786. LEFT JOIN machine c on b.macid=c.id
  787. WHERE
  788. a.typeID = 0
  789. AND a.ParamCode = 'S00026'
  790. {filter1}
  791. AND b.EDate BETWEEN '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  792. AND '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  793. GROUP BY HOUR(b.EDate)";
  794. var TCount = CurrDb.FindList<HourCount>(sql);
  795. sql = $@" SELECT
  796. count(1) IntCount, HOUR(a.stime) Hour,
  797. sum(flen) TimeLen
  798. FROM
  799. (select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  800. where 1=1
  801. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  802. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=4
  803. union all
  804. select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  805. where 1=1
  806. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  807. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=3
  808. union all
  809. select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t
  810. where 1=1
  811. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  812. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=6 )
  813. a
  814. WHERE
  815. 1=1
  816. {filter}
  817. GROUP BY HOUR(a.stime)";
  818. var Time = CurrDb.FindList<HourCount>(sql);
  819. sql = $@" SELECT
  820. count(1) IntCount, HOUR(a.stime) Hour,StatusID,
  821. sum(flen) TimeLen
  822. FROM
  823. (select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  824. where 1=1
  825. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  826. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=4
  827. union all
  828. select t.stime,t.statusid,t.maccode,t.flen from macstatus t
  829. where 1=1
  830. AND t.stime >='{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  831. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=3
  832. union all
  833. select t.stime,t.statusid,t.maccode,if(flen=0,0.01,flen) flen from macstatus t
  834. where 1=1
  835. AND t.stime >= '{startTime.ToString("yyyy-MM-dd HH:mm:ss")}'
  836. AND t.stime<= '{endTime.ToString("yyyy-MM-dd HH:mm:ss")}' and StatusID=6 )
  837. a
  838. WHERE
  839. 1=1
  840. {filter}
  841. GROUP BY HOUR(a.stime),StatusID";
  842. var TStatus = CurrDb.FindList<HourCount>(sql);
  843. #endregion
  844. ChartDto dto = new ChartDto()
  845. {
  846. LegendData = new List<string>()
  847. {
  848. "产量","稼动率","待机率","故障率","故障次数","故障时长"
  849. },
  850. XData = new List<string>(),
  851. };
  852. #region 数据处理
  853. List<decimal> y1 = new List<decimal>(), y2 = new List<decimal>(), y3 = new List<decimal>(), y4 = new List<decimal>(), y5 = new List<decimal>(), y6 = new List<decimal>();
  854. var Sam = new CountTotal();
  855. Sam.Factory = "汇总";
  856. var x1 = new IndexDatas()
  857. {
  858. Index = "产量"
  859. };
  860. var x2 = new IndexDatas()
  861. {
  862. Index = "稼动率"
  863. };
  864. var x3 = new IndexDatas()
  865. {
  866. Index = "待机率"
  867. };
  868. var x4 = new IndexDatas()
  869. {
  870. Index = "故障率"
  871. };
  872. var x5 = new IndexDatas()
  873. {
  874. Index = "故障次数"
  875. };
  876. var x6 = new IndexDatas()
  877. {
  878. Index = "故障时长"
  879. };
  880. for (int i = 0; i < 24; i++)
  881. {
  882. dto.XData.Add(i.ToString("00") + ":00:00");
  883. var a = TCount.Where(c => c.Hour == i)?.FirstOrDefault()?.Count;
  884. a = a != null ? a : "0";
  885. var b = TStatus.Where(c => c.Hour == i && c.StatusId == 4)?.FirstOrDefault()?.TimeLen;
  886. b = b != null ? b : 0;
  887. var d = Time.Where(c => c.Hour == i)?.FirstOrDefault()?.TimeLen;
  888. d = d != null ? d : 1;
  889. var e = TStatus.Where(c => c.Hour == i && c.StatusId == 3)?.FirstOrDefault()?.TimeLen;
  890. e = e != null ? e : 0;
  891. var f = TStatus.Where(c => c.Hour == i && c.StatusId == 6)?.FirstOrDefault()?.TimeLen;
  892. f = f != null ? f : 0;
  893. var g = TStatus.Where(c => c.Hour == i && c.StatusId == 6)?.FirstOrDefault()?.IntCount;
  894. g = g != null ? g : 0;
  895. var t1 = Convert.ToDecimal(Math.Round(b.Value / d.Value * 100, 2));
  896. var t2 = Convert.ToDecimal(Math.Round(e.Value / d.Value * 100, 2));
  897. var t3 = Convert.ToDecimal(Math.Round(f.Value / d.Value * 100, 2));
  898. var t4 = Convert.ToInt32(g.Value);
  899. var t5 = Convert.ToDecimal(Math.Round(f.Value / 60, 2));
  900. x1.data.Add(a);
  901. x2.data.Add(t1.ToString() + "%");
  902. x3.data.Add(t2.ToString() + "%");
  903. x4.data.Add(t3.ToString() + "%");
  904. x5.data.Add(t4.ToString());
  905. x6.data.Add(t5.ToString());
  906. y1.Add(Convert.ToDecimal(a));
  907. y2.Add(t1);
  908. y3.Add(t2);
  909. y4.Add(t3);
  910. y5.Add(t4);
  911. y6.Add(t5);
  912. #endregion
  913. #region 没有用
  914. /*if (i < Time.Count())
  915. {
  916. x1.data.Add(Convert.ToInt32(TCount.Where(c => c.Hour == i).FirstOrDefault().Count));
  917. 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)));
  918. 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)));
  919. 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)));
  920. x5.data.Add(Convert.ToInt32(TStatus.Where(c => c.Hour == i && c.StatusId == 6).FirstOrDefault().IntCount));
  921. x6.data.Add(Convert.ToDouble(Math.Round((TStatus.Where(c => c.Hour == i && c.StatusId == 6).FirstOrDefault().TimeLen) / 3600, 2)));
  922. *//*indexDatas.Add(new IndexData()
  923. {
  924. hour = i.ToString("00") + ":00:00",
  925. count = Convert.ToInt32(TCount.Where(c => c.Hour == i).FirstOrDefault().Count),
  926. 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),
  927. 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),
  928. 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),
  929. GZcount = Convert.ToInt32(TStatus.Where(c => c.Hour == i && c.StatusId == 6).FirstOrDefault().IntCount),
  930. GZtime = Convert.ToDouble(Math.Round((TStatus.Where(c => c.Hour == i && c.StatusId == 6).FirstOrDefault().TimeLen) / 3600, 2))
  931. });*//*
  932. }
  933. else
  934. {
  935. *//*indexDatas.Add(new IndexData()
  936. {
  937. hour = i.ToString("00") + ":00:00",
  938. count = 0,
  939. JDrate=0.00,
  940. DJrate=0.00,
  941. GZrate=0.00,
  942. GZcount=0,
  943. GZtime=0.00
  944. }); ;*//*
  945. x1.data.Add(0);
  946. x2.data.Add(0);
  947. x3.data.Add(0);
  948. x4.data.Add(0);
  949. x5.data.Add(0);
  950. x6.data.Add(0);
  951. }*/
  952. #endregion
  953. }
  954. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "产量", YAxis = 0, Type = "line", Data = y1 });
  955. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "稼动率", YAxis = 1, Type = "line", Data = y2 });
  956. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "待机率", YAxis = 1, Type = "line", Data = y3 });
  957. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障率", YAxis = 1, Type = "line", Data = y4 });
  958. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障次数", YAxis = 0, Type = "line", Data = y5 });
  959. dto.SeriesData.Add(new DllEapEntity.OFILM.Series() { Name = "故障时长", YAxis = 0, Type = "line", Data = y6 });
  960. Sam.Datas.Add(x1); Sam.Datas.Add(x2); Sam.Datas.Add(x3); Sam.Datas.Add(x4); Sam.Datas.Add(x5); Sam.Datas.Add(x6);
  961. dtos = dto;
  962. return Sam;
  963. }
  964. catch (Exception ex)
  965. {
  966. throw;
  967. }
  968. }
  969. /// <summary>
  970. /// 时段产量时间在2022-03-03 14:00:00之前的数据处理方法
  971. /// </summary>
  972. /// <param name="startTime"></param>
  973. /// <param name="endTime"></param>
  974. /// <param name="filter"></param>
  975. /// <param name="start"></param>
  976. /// <param name="length"></param>
  977. /// <param name="errorinfo"></param>
  978. /// <param name="total"></param>
  979. /// <returns></returns>
  980. public AntdComplexTableDto GetOldData(DateTime startTime, DateTime endTime, string filter, int start,
  981. int length, ref string errorinfo, out int total)
  982. {
  983. IEnumerable<Machine> macs = null;
  984. var dt = this.GetMacRunDatas(startTime, endTime, filter, start, length, ref errorinfo, out macs, out total);
  985. var headers = new List<AntdColumn>();
  986. if (dt != null && dt.Columns.Count > 0)
  987. {
  988. foreach (DataColumn item in dt.Columns)
  989. {
  990. if (item.ColumnName == "MacCode" || item.ColumnName == "MacModel"
  991. || item.ColumnName == "ModelRowSpan" || item.ColumnName == "MacRowSpan")
  992. continue;
  993. headers.Add(new AntdColumn { ColName = item.ColumnName });
  994. }
  995. }
  996. var antdData = new AntdComplexTableDto();
  997. antdData.Columns = headers;
  998. var modelGroups = macs.GroupBy(c => c.MModeCode);
  999. IList<AntdComplexData> antdComplexDatas = new List<AntdComplexData>();
  1000. foreach (var item in modelGroups)
  1001. {
  1002. var complexData = new AntdComplexData { MacModel = item.Key, RowSpan = item.Count() };
  1003. var modelMacs = item;
  1004. var macDatas = new List<AntdComplexMachineData>();
  1005. complexData.Machines = macDatas;
  1006. antdComplexDatas.Add(complexData);
  1007. }
  1008. antdData.Datas = dt;
  1009. return antdData;
  1010. }
  1011. /// <summary>
  1012. /// 时段产量数据获取
  1013. /// </summary>
  1014. /// <param name="startTime"></param>
  1015. /// <param name="endTime"></param>
  1016. /// <param name="filter"></param>
  1017. /// <param name="start"></param>
  1018. /// <param name="length"></param>
  1019. /// <param name="errorinfo"></param>
  1020. /// <param name="total"></param>
  1021. /// <returns></returns>
  1022. public AntdComplexTableDto GetComplexData(DateTime startTime, DateTime endTime, string filter, int start,
  1023. int length, ref string errorinfo, out int total)
  1024. {
  1025. var time = Convert.ToDateTime("2022-03-03 14:00:00");
  1026. if (endTime < time)
  1027. {
  1028. return GetOldData(startTime, endTime, filter, start, length, ref errorinfo, out total);
  1029. }
  1030. else if (startTime >= time)
  1031. {
  1032. return GetNewData(startTime, endTime, filter, start, length, ref errorinfo, out total);
  1033. }
  1034. else
  1035. {
  1036. 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);
  1037. }
  1038. }
  1039. /// <summary>
  1040. /// 时段产量时间包含2022-03-03 14:00:00的数据处理方法
  1041. /// </summary>
  1042. /// <param name="datas1"></param>
  1043. /// <param name="datas2"></param>
  1044. /// <param name="startTime"></param>
  1045. /// <param name="endTime"></param>
  1046. /// <returns></returns>
  1047. private AntdComplexTableDto GetMixData(DataTable datas1, DataTable datas2, DateTime startTime, DateTime endTime)
  1048. {
  1049. List<string> colList1 = new List<string>();
  1050. List<string> colList2 = new List<string>();
  1051. foreach (DataColumn col in datas1.Columns)
  1052. {
  1053. colList1.Add(col.ColumnName);
  1054. }
  1055. foreach (DataColumn col in datas2.Columns)
  1056. {
  1057. colList2.Add(col.ColumnName);
  1058. }
  1059. int hour = Convert.ToInt32(Math.Ceiling((endTime - startTime).TotalHours));
  1060. var dt = getDT(hour, startTime);
  1061. int i = 0;
  1062. while (i<datas1.Rows.Count)
  1063. {
  1064. var row = dt.NewRow();
  1065. foreach (DataColumn col in dt.Columns)
  1066. {
  1067. var colName = col.ColumnName;
  1068. if (colName == "汇总")
  1069. {
  1070. if (i%6==1||i%6==2||i%6==3)
  1071. {
  1072. row[colName] = (Convert.ToDouble(datas1.Rows[i][colName]) + Convert.ToDouble(datas2.Rows[i][colName]))/2;
  1073. }
  1074. else
  1075. {
  1076. row[colName] = Convert.ToDouble(datas1.Rows[i][colName]) + Convert.ToDouble(datas2.Rows[i][colName]);
  1077. }
  1078. }
  1079. else
  1080. {
  1081. if (colList1.Contains(colName))
  1082. {
  1083. row[colName] = datas1.Rows[i][colName];
  1084. continue;
  1085. }
  1086. if (colList2.Contains(colName))
  1087. {
  1088. row[colName] = datas2.Rows[i][colName];
  1089. continue;
  1090. }
  1091. }
  1092. }
  1093. dt.Rows.Add(row);
  1094. i++;
  1095. }
  1096. var headers = new List<AntdColumn>();
  1097. if (dt != null && dt.Columns.Count > 0)
  1098. {
  1099. foreach (DataColumn item in dt.Columns)
  1100. {
  1101. if (item.ColumnName == "MacCode" || item.ColumnName == "MacModel"
  1102. || item.ColumnName == "ModelRowSpan" || item.ColumnName == "MacRowSpan")
  1103. continue;
  1104. headers.Add(new AntdColumn { ColName = item.ColumnName });
  1105. }
  1106. }
  1107. return new AntdComplexTableDto()
  1108. {
  1109. Datas = dt,
  1110. Columns = headers
  1111. };
  1112. }
  1113. /// <summary>
  1114. /// 时段产量时间在2022-03-03 14:00:00之后的数据处理方法
  1115. /// </summary>
  1116. /// <param name="startTime"></param>
  1117. /// <param name="endTime"></param>
  1118. /// <param name="filter"></param>
  1119. /// <param name="start"></param>
  1120. /// <param name="length"></param>
  1121. /// <param name="errorinfo"></param>
  1122. /// <param name="total"></param>
  1123. /// <returns></returns>
  1124. private AntdComplexTableDto GetNewData(DateTime startTime, DateTime endTime, string filter, int start, int length, ref string errorinfo, out int total)
  1125. {
  1126. int hour = Convert.ToInt32(Math.Ceiling((endTime - startTime).TotalHours));
  1127. var rowStrings = new string[] { "产量", "稼动率", "待机率", "故障率", "故障次数", "故障时长" };
  1128. var dt = getDT(hour, startTime);
  1129. 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;
  1130. string table = new OutPutTimeInfo().GetQueryTabSql();
  1131. total = Convert.ToInt32( CurrDb.FindObject($@"select count(DISTINCT(a.macid)) from {table} where 1=1 {condition}"));
  1132. var mac = CurrDb.FindList<OutPutTimeInfo>($@"select DISTINCT (a.MacID ) MacID,c.FCode MacCode from {table} where 1=1 {condition} order by c.Fcode limit {start - 1},{length}");
  1133. condition += $" and a.macid in ({string.Join(",", mac.Select(c => c.MacID))})";
  1134. var data = CurrDb.FindListForCondition<OutPutTimeInfo>(condition, ref errorinfo);
  1135. foreach (var item in mac)
  1136. {
  1137. var datas = data.Where(c => c.MacID == item.MacID).OrderBy(c => c.ID).ToList();
  1138. var row1 = dt.NewRow();
  1139. var row2 = dt.NewRow();
  1140. var row3 = dt.NewRow();
  1141. var row4 = dt.NewRow();
  1142. var row5 = dt.NewRow();
  1143. var row6 = dt.NewRow();
  1144. foreach (DataColumn col in dt.Columns)
  1145. {
  1146. var colName = col.ColumnName;
  1147. if (colName == "汇总")
  1148. {
  1149. row1[colName] = datas.Sum(c => c.Count);
  1150. row2[colName] = Math.Round(datas.Sum(c => c.RunRate) / hour, 4);
  1151. row3[colName] = Math.Round(datas.Sum(c => c.StandbyRate) / hour, 4);
  1152. row4[colName] = Math.Round(datas.Sum(c => c.AlarmRate) / hour, 4);
  1153. row5[colName] = datas.Sum(c => c.AlarmCount);
  1154. row6[colName] = Math.Round(datas.Sum(c => c.AlarmTime) , 4);
  1155. }
  1156. else if (colName == "指标")
  1157. {
  1158. row1[colName] = rowStrings[0];
  1159. row2[colName] = rowStrings[1];
  1160. row3[colName] = rowStrings[2];
  1161. row4[colName] = rowStrings[3];
  1162. row5[colName] = rowStrings[4];
  1163. row6[colName] = rowStrings[5];
  1164. }
  1165. else if (colName == "设备ID")
  1166. {
  1167. row1[colName] = datas[0].MacCode;
  1168. row2[colName] = datas[0].MacCode;
  1169. row3[colName] = datas[0].MacCode;
  1170. row4[colName] = datas[0].MacCode;
  1171. row5[colName] = datas[0].MacCode;
  1172. row6[colName] = datas[0].MacCode;
  1173. }
  1174. else if (colName == "设备类型")
  1175. {
  1176. row1[colName] = datas[0].MModelCode;
  1177. row2[colName] = datas[0].MModelCode;
  1178. row3[colName] = datas[0].MModelCode;
  1179. row4[colName] = datas[0].MModelCode;
  1180. row5[colName] = datas[0].MModelCode;
  1181. row6[colName] = datas[0].MModelCode;
  1182. }
  1183. else if (colName == "MacRowSpan" || colName == "ModelRowSpan")
  1184. {
  1185. continue;
  1186. }
  1187. else if (colName == "园区")
  1188. {
  1189. row1[colName] = datas[0].Factory;
  1190. row2[colName] = datas[0].Factory;
  1191. row3[colName] = datas[0].Factory;
  1192. row4[colName] = datas[0].Factory;
  1193. row5[colName] = datas[0].Factory;
  1194. row6[colName] = datas[0].Factory;
  1195. }
  1196. else if (colName == "楼层")
  1197. {
  1198. row1[colName] = datas[0].Floor;
  1199. row2[colName] = datas[0].Floor;
  1200. row3[colName] = datas[0].Floor;
  1201. row4[colName] = datas[0].Floor;
  1202. row5[colName] = datas[0].Floor;
  1203. row6[colName] = datas[0].Floor;
  1204. }
  1205. else if (colName == "制程代码")
  1206. {
  1207. row1[colName] = datas[0].PCode;
  1208. row2[colName] = datas[0].PCode;
  1209. row3[colName] = datas[0].PCode;
  1210. row4[colName] = datas[0].PCode;
  1211. row5[colName] = datas[0].PCode;
  1212. row6[colName] = datas[0].PCode;
  1213. }
  1214. else
  1215. {
  1216. var da = new OutPutTimeInfo();
  1217. var day = Convert.ToDateTime( Convert.ToDateTime(colName).ToString("yyyy-MM-dd"));
  1218. var hou= Convert.ToDouble(Convert.ToDateTime(colName).ToString("HH"));
  1219. da = datas.Where(c => c.Day == day && c.Hour == hou)?.FirstOrDefault();
  1220. row1[colName] = da.Count;
  1221. row2[colName] = da.RunRate;
  1222. row3[colName] = da.StandbyRate;
  1223. row4[colName] = da.AlarmRate;
  1224. row5[colName] = da.AlarmCount;
  1225. row6[colName] = da.AlarmTime / 60;
  1226. }
  1227. }
  1228. dt.Rows.Add(row1); dt.Rows.Add(row2); dt.Rows.Add(row3); dt.Rows.Add(row4); dt.Rows.Add(row5); dt.Rows.Add(row6);
  1229. }
  1230. var modelGroups = data.GroupBy(c => c.MModelCode);
  1231. foreach (var model in modelGroups)
  1232. {
  1233. for (var i = 0; i < dt.Rows.Count; i++)
  1234. {
  1235. if (dt.Rows[i]["设备类型"].ToString() == model.Key)
  1236. {
  1237. dt.Rows[i]["ModelRowSpan"] = model.Count() * 6;
  1238. //dt.Rows[i]["FactroyNameRowSpan"] = model.Count() * 6;
  1239. }
  1240. dt.Rows[i]["MacRowSpan"] = 6;
  1241. }
  1242. }
  1243. var headers = new List<AntdColumn>();
  1244. if (dt != null && dt.Columns.Count > 0)
  1245. {
  1246. foreach (DataColumn item in dt.Columns)
  1247. {
  1248. if (item.ColumnName == "MacCode" || item.ColumnName == "MacModel"
  1249. || item.ColumnName == "ModelRowSpan" || item.ColumnName == "MacRowSpan")
  1250. continue;
  1251. headers.Add(new AntdColumn { ColName = item.ColumnName });
  1252. }
  1253. }
  1254. return new AntdComplexTableDto()
  1255. {
  1256. Datas = dt,
  1257. Columns = headers,
  1258. };
  1259. }
  1260. #region 拼接DataTable
  1261. /// <summary>
  1262. /// 添加产量行
  1263. /// </summary>
  1264. /// <param name="datas"></param>
  1265. /// <param name="mac"></param>
  1266. /// <param name="type"></param>
  1267. /// <param name="dt"></param>
  1268. private void AppendOutPutDataRow(IEnumerable<HourCount> datas, Machine mac, string type, DataTable dt)
  1269. {
  1270. DataRow row = dt.NewRow();
  1271. Func<HourCount, bool> func = t => t.MacCode == mac.FCode;
  1272. if (type == "产量")
  1273. {
  1274. func = t => t.MacId == mac.ID;
  1275. }
  1276. foreach (DataColumn col in dt.Columns)
  1277. {
  1278. var colName = col.ColumnName;
  1279. if (colName == "汇总")
  1280. {
  1281. row[colName] = datas.Where(c => c.MacId == mac.ID).Sum(c => c.IntCount);
  1282. }
  1283. else if (colName == "指标")
  1284. {
  1285. row[colName] = type;
  1286. }
  1287. else if (colName == "设备ID")
  1288. {
  1289. row[colName] = mac.FCode;
  1290. }
  1291. else if (colName == "设备类型")
  1292. {
  1293. row[colName] = mac.MModeCode;
  1294. }
  1295. else if (colName == "MacRowSpan" || colName == "ModelRowSpan" )
  1296. {
  1297. continue;
  1298. }
  1299. else if (colName == "园区")
  1300. {
  1301. row[colName] = mac.FactoryName;
  1302. }
  1303. else if (colName == "楼层")
  1304. {
  1305. row[colName] = mac.FloorName;
  1306. }
  1307. else if (colName == "制程代码")
  1308. {
  1309. row[colName] = mac.PCode;
  1310. }
  1311. else
  1312. {
  1313. var currTime = Convert.ToDateTime(colName);
  1314. var year = currTime.Year;
  1315. var month = currTime.Month;
  1316. var day = currTime.Day;
  1317. var hour = currTime.Hour;
  1318. var entity = datas.Where(func).FirstOrDefault(c => c.Year == year
  1319. && c.Month == month && c.Day == day && c.Hour == hour);
  1320. if (entity == null)
  1321. {
  1322. row[colName] = 0;
  1323. }
  1324. else
  1325. {
  1326. row[colName] = entity.Count;
  1327. }
  1328. }
  1329. }
  1330. dt.Rows.Add(row);
  1331. }
  1332. /// <summary>
  1333. /// 添加稼动率及闲置率
  1334. /// </summary>
  1335. /// <param name="datas"></param>
  1336. /// <param name="mac"></param>
  1337. /// <param name="type"></param>
  1338. /// <param name="dt"></param>
  1339. private void AppendRunDataRow(IEnumerable<HourCount> datas, Machine mac, string type, int statusId, DataTable dt,
  1340. double timeSpan, DateTime startTime, DateTime endTime)
  1341. {
  1342. //string errorinfo = string.Empty;
  1343. //var hours = Math.Ceiling((endTime - startTime).TotalHours);
  1344. //if (datas.Any(c => c.StatusId == statusId && c.MacCode == mac.FCode))
  1345. //{
  1346. // for (int i = 0; i < hours; i++)
  1347. // {
  1348. // var date = startTime.AddHours(i);
  1349. // var endDate = date.AddHours(1);
  1350. // var first = CurrDb.FindListForCondition<MacStatus>($" and statusId={statusId} and maccode='{mac.FCode}' " +
  1351. // $"and etime>'{date.ToString(timeFormat)}' order by stime asc limit 0,1 ", ref errorinfo)
  1352. // .FirstOrDefault();
  1353. // if (first != null && first.STime < date)
  1354. // {
  1355. // datas.FirstOrDefault(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day
  1356. // && c.Hour == date.Hour && c.StatusId == statusId && c.MacCode == mac.FCode).TimeLen += (first.ETime - date).TotalSeconds;
  1357. // }
  1358. // var last = CurrDb.FindListForCondition<MacStatus>($" and statusId={statusId} and maccode='{mac.FCode}' " +
  1359. // $"and stime<'{endDate.ToString(timeFormat)}' order by stime desc limit 0,1 ", ref errorinfo)
  1360. // .FirstOrDefault();
  1361. // if (last != null && last.ETime > endDate)
  1362. // {
  1363. // datas.FirstOrDefault(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day
  1364. // && c.Hour == date.Hour && c.StatusId == statusId && c.MacCode == mac.FCode).TimeLen -= (last.ETime - endDate).TotalSeconds;
  1365. // }
  1366. // }
  1367. //}
  1368. DataRow row = dt.NewRow();
  1369. Func<HourCount, bool> func = t => t.MacCode == mac.FCode;
  1370. foreach (DataColumn col in dt.Columns)
  1371. {
  1372. var colName = col.ColumnName;
  1373. if (colName == "汇总")
  1374. {
  1375. double rate = 0;
  1376. var totalTime = datas.Where(c => c.MacCode == mac.FCode && c.StatusId == statusId)?.Sum(c => c.TimeLen);
  1377. if (totalTime != null)
  1378. {
  1379. rate = totalTime.Value / (timeSpan);
  1380. }
  1381. row[colName] = rate;
  1382. }
  1383. else if (colName == "指标")
  1384. {
  1385. row[colName] = type;
  1386. }
  1387. else if (colName == "设备ID")
  1388. {
  1389. row[colName] = mac.FCode;
  1390. }
  1391. else if (colName == "设备类型")
  1392. {
  1393. row[colName] = mac.MModeCode;
  1394. }
  1395. else if (colName == "MacRowSpan" || colName == "ModelRowSpan" )
  1396. {
  1397. continue;
  1398. }
  1399. else if (colName == "园区")
  1400. {
  1401. row[colName] = mac.FactoryName;
  1402. }
  1403. else if (colName == "楼层")
  1404. {
  1405. row[colName] = mac.FloorName;
  1406. }
  1407. else if (colName == "制程代码")
  1408. {
  1409. row[colName] = mac.PCode;
  1410. }
  1411. else
  1412. {
  1413. var currTime = Convert.ToDateTime(colName);
  1414. var year = currTime.Year;
  1415. var month = currTime.Month;
  1416. var day = currTime.Day;
  1417. var hour = currTime.Hour;
  1418. var statuses = datas.Where(c => c.MacCode == mac.FCode && c.Year == year
  1419. && c.Month == month && c.Day == day && c.Hour == hour && c.StatusId == statusId);
  1420. if (statuses == null || statuses.Count() <= 0)
  1421. {
  1422. row[colName] = 0;
  1423. }
  1424. else
  1425. {
  1426. row[colName] = statuses.Sum(c => c.TimeLen) / 3600;
  1427. }
  1428. }
  1429. }
  1430. dt.Rows.Add(row);
  1431. }
  1432. /// <summary>
  1433. /// 生成报警数据行
  1434. /// </summary>
  1435. /// <param name="datas"></param>
  1436. /// <param name="mac"></param>
  1437. /// <param name="type"></param>
  1438. /// <param name="statusId"></param>
  1439. /// <param name="dt"></param>
  1440. private void AppendAlarmRateDataRow(IEnumerable<HourCount> datas, Machine mac, string type, DataTable dt, string alarmType, double timeSpan, DateTime startTime, DateTime endTime)
  1441. {
  1442. //string errorinfo = string.Empty;
  1443. //var hours = Math.Ceiling((endTime - startTime).TotalHours);
  1444. //if (datas.Any(c => c.StatusId == 6 && c.MacCode == mac.FCode))
  1445. //{
  1446. // for (int i = 0; i < hours; i++)
  1447. // {
  1448. // var date = startTime.AddHours(i);
  1449. // var endDate = date.AddHours(1);
  1450. // var first = CurrDb.FindListForCondition<MacStatus>($" and statusId={6} and maccode='{mac.FCode}' " +
  1451. // $"and etime>'{date.ToString(timeFormat)}' order by stime asc limit 0,1 ", ref errorinfo)
  1452. // .FirstOrDefault();
  1453. // if (first != null && first.STime < date)
  1454. // {
  1455. // datas.FirstOrDefault(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day
  1456. // && c.Hour == date.Hour && c.StatusId == 6 && c.MacCode == mac.FCode).TimeLen += (first.ETime - date).TotalSeconds;
  1457. // }
  1458. // var last = CurrDb.FindListForCondition<MacStatus>($" and statusId={6} and maccode='{mac.FCode}' " +
  1459. // $"and stime<'{endDate.ToString(timeFormat)}' order by stime desc limit 0,1 ", ref errorinfo)
  1460. // .FirstOrDefault();
  1461. // if (last != null && last.ETime > endDate)
  1462. // {
  1463. // datas.FirstOrDefault(c => c.Year == date.Year && c.Month == date.Month && c.Day == date.Day
  1464. // && c.Hour == date.Hour && c.StatusId == 6 && c.MacCode == mac.FCode).TimeLen -= (last.ETime - endDate).TotalSeconds;
  1465. // }
  1466. // }
  1467. //}
  1468. DataRow row = dt.NewRow();
  1469. Func<HourCount, bool> func = t => t.MacCode == mac.FCode;
  1470. foreach (DataColumn col in dt.Columns)
  1471. {
  1472. var colName = col.ColumnName;
  1473. if (colName == "汇总")
  1474. {
  1475. var totalTime = datas.Where(c => c.MacCode == mac.FCode);
  1476. if (alarmType == "COUNT")
  1477. {
  1478. int count = 0;
  1479. if (totalTime != null)
  1480. {
  1481. count = totalTime.Sum(c => c.IntCount);
  1482. }
  1483. row[colName] = count;
  1484. }
  1485. else if (alarmType == "TIME")
  1486. {
  1487. double time = 0;
  1488. if (totalTime != null)
  1489. {
  1490. time = totalTime.Sum(c => c.TimeLen) / 60;
  1491. }
  1492. row[colName] = time;
  1493. }
  1494. else
  1495. {
  1496. double time = 0;
  1497. if (totalTime != null)
  1498. {
  1499. time = totalTime.Sum(c => c.TimeLen) / (timeSpan);
  1500. }
  1501. row[colName] = time;
  1502. }
  1503. }
  1504. else if (colName == "指标")
  1505. {
  1506. row[colName] = type;
  1507. }
  1508. else if (colName == "设备ID")
  1509. {
  1510. row[colName] = mac.FCode;
  1511. }
  1512. else if (colName == "设备类型")
  1513. {
  1514. row[colName] = mac.MModeCode;
  1515. }
  1516. else if (colName == "MacRowSpan" || colName == "ModelRowSpan")
  1517. {
  1518. continue;
  1519. }
  1520. else if (colName == "园区")
  1521. {
  1522. row[colName] = mac.FactoryName;
  1523. }
  1524. else if (colName == "楼层")
  1525. {
  1526. row[colName] = mac.FloorName;
  1527. }
  1528. else if (colName == "制程代码")
  1529. {
  1530. row[colName] = mac.PCode;
  1531. }
  1532. else
  1533. {
  1534. var currTime = Convert.ToDateTime(colName);
  1535. var year = currTime.Year;
  1536. var month = currTime.Month;
  1537. var day = currTime.Day;
  1538. var hour = currTime.Hour;
  1539. var statuses = datas.Where(c => c.MacCode == mac.FCode && c.Year == year
  1540. && c.Month == month && c.Day == day && c.Hour == hour);
  1541. if (statuses == null || statuses.Count() <= 0)
  1542. {
  1543. row[colName] = 0;
  1544. }
  1545. else
  1546. {
  1547. switch (alarmType)
  1548. {
  1549. case "COUNT": row[colName] = statuses.Sum(c => c.IntCount); break;
  1550. case "TIME": row[colName] = statuses.Sum(c => c.TimeLen) / 60; break;
  1551. default: row[colName] = statuses.Sum(c => c.TimeLen) / 3600; break;
  1552. }
  1553. }
  1554. }
  1555. }
  1556. dt.Rows.Add(row);
  1557. }
  1558. #endregion
  1559. }
  1560. }