public void importExcel() { try { const int startRow = 1; string filePath = HttpContext.Current.Server.MapPath("~/App_Data/txi.xlsx"); // Get the file we are going to process var existingFile = new FileInfo(filePath); List<DowntimeData> dtData = new List<DowntimeData>(); // Open and read the XlSX file. using (var package = new ExcelPackage(existingFile)) { using (DB db = new DB()) { // Get the work book in the file ExcelWorkbook workBook = package.Workbook; if (workBook != null) { if (workBook.Worksheets.Count > 0) { string client = "TXI"; // Get the first worksheet int index = 0; List<Options> options = (from o in db.Options where o.Client == client select o).ToList(); // UPDATE [thrivedcs].[dbo].[DowntimeData] SET Line = 'Finish_Mill_3' WHERE Line = 'company-demo' AND Client = 'TXI'; // int counter = 0; foreach(ExcelWorksheet currentWorksheet in workBook.Worksheets.ToList()) { string line = "Kiln_2"; if(index == 1) line = "Raw_Mill_2"; else if(index == 2) line = "Finish_Mill_3"; // read some data object dateHeader = currentWorksheet.Cells[startRow, 1].Value;//Date object eventStartHeader = currentWorksheet.Cells[startRow, 3].Value;//EvenStart object minutesHeader = currentWorksheet.Cells[startRow, 6].Value;//Minutes object reasonHeader = currentWorksheet.Cells[startRow, 7].Value;//Reason object stoppageHeader = currentWorksheet.Cells[startRow, 9].Value;//Stoppage Nature for (int rowNumber = startRow + 1; rowNumber <= currentWorksheet.Dimension.End.Row; rowNumber++) // read each row from the start of the data (start row + 1 header row) to the end of the spreadsheet. { object oDate = currentWorksheet.Cells[rowNumber, 1].Value; object oEventStart = currentWorksheet.Cells[rowNumber, 3].Value; object oMinutes = currentWorksheet.Cells[rowNumber, 6].Value;//It's a Date in H:MM:SS format object oReason = currentWorksheet.Cells[rowNumber, 7].Value; object oStoppageOfNature = currentWorksheet.Cells[rowNumber, 9].Value; if ((oEventStart != null && oEventStart.ToString() != "") && (oStoppageOfNature != null && oStoppageOfNature.ToString() != "")) { decimal minutes = 0; if (oMinutes != null) { DateTime minTime = (DateTime)oMinutes; minutes = Convert.ToDecimal((minTime.TimeOfDay.TotalHours / 60) + minTime.TimeOfDay.TotalMinutes); } DateTime EventStart = DateTime.Now; try { EventStart = (DateTime)oEventStart; } catch (Exception e) { Console.Write(e.Message); } DateTime EventStop = EventStart.AddMinutes((double)minutes); string reason = oReason.ToString(); string nos = oStoppageOfNature.ToString(); Options opt = (from o in options where o.Name.Equals(nos, StringComparison.InvariantCultureIgnoreCase) select o).FirstOrDefault(); if (opt != null) { DowntimeData dt = new DowntimeData(); dt.Client = client; dt.Comment = reason; dt.EventStart = EventStart; dt.EventStop = EventStop; dt.Line = line; dt.Minutes = minutes; dt.IsCreatedByAcromag = true; dtData.Add(dt); db.AddToDowntimeDataSet(dt); db.SaveChanges(); if (dt.ID > 0) { NatureOfStoppage NOS = new NatureOfStoppage(); NOS.OptionId = opt.Id; NOS.DowntimeId = dt.ID; db.AddToNatureOfStoppages(NOS); } } else { Console.Write("NOS IS EMPTY"); } } } index++; } Console.Write("DONE" + dtData.Count); } } } } } catch (IOException ioEx) { if (!String.IsNullOrEmpty(ioEx.Message)) { if (ioEx.Message.Contains("because it is being used by another process.")) { Console.WriteLine("Could not read example data. Please make sure it not open in Excel."); } } Console.WriteLine("Could not read example data. " + ioEx.Message, ioEx); } catch (Exception ex) { Console.WriteLine("An error occured while reading example data. " + ex.Message, ex); } finally { Console.WriteLine(); Console.WriteLine("press any key to exit."); } }
public bool InsertDowntimeData(DateTime eventStart, DateTime eventStop, decimal? minutes, string reasonCode, int? reasonCodeID, string comment, string line, string client, string str_eventStart, string str_eventStop) { bool result; using (DB db = new DB()) { DowntimeData latestDowntime = (from o in db.DowntimeDataSet where o.Client == client && o.Line == line orderby o.EventStop.Value descending select o).FirstOrDefault(); DateTime? latestDate = (latestDowntime != null ? latestDowntime.EventStop : null); bool create = !latestDate.HasValue; DowntimeData dd = new DowntimeData(); //为了避免时区问题,用string转过来转换 DateTime es, et; if (!DateTime.TryParse(str_eventStart, out es) || !DateTime.TryParse(str_eventStop, out et)) { return false; } if (latestDate.HasValue) { create = latestDate.Value < et; } if (create) { //dd.EventStart = eventStart; //dd.EventStop = eventStop; dd.EventStart = es; dd.EventStop = et; dd.Minutes = minutes; dd.ReasonCodeID = reasonCodeID; dd.ReasonCode = reasonCode; dd.Line = line; dd.Comment = comment; dd.Client = client; dd.IsCreatedByAcromag = true; db.AddToDowntimeDataSet(dd); result = db.SaveChanges() > 0; if (result) { DBHelper.UpdateAscommPing(client, DateTime.Now, line, true); } } else result = false; } return result; }
public bool LoginAndCreateDowntimeData(DateTime? eventStart, DateTime? eventStop, decimal? minutes, string reasonCode, int? reasonCodeID, string comment, string line, string client, string password) { bool result; bool loggedIn = Login(client, password); if (!loggedIn) return false; if (eventStart.HasValue == false || eventStop.HasValue == false) return false; if (minutes.HasValue == false) minutes = Convert.ToInt32(eventStop.Value.Subtract(eventStart.Value).TotalMinutes); using (DB db = new DB()) { DowntimeData latestDowntime = (from o in db.DowntimeDataSet where o.Client == client && o.Line == line orderby o.EventStop.Value descending select o).FirstOrDefault(); DateTime? latestDate = null; if (latestDowntime != null) latestDate = latestDowntime.EventStop; bool create = !latestDate.HasValue; DowntimeData dd = new DowntimeData(); if (latestDate.HasValue) { create = !(latestDate.Value >= eventStop); } if (create) { //dd.EventStart = eventStart; //dd.EventStop = eventStop; dd.EventStart = eventStart; dd.EventStop = eventStop; dd.Minutes = minutes; dd.ReasonCodeID = reasonCodeID; dd.ReasonCode = reasonCode; dd.Line = line; dd.Comment = comment; dd.Client = client; dd.IsCreatedByAcromag = true; db.AddToDowntimeDataSet(dd); result = db.SaveChanges() > 0; if (result) { DBHelper.UpdateAscommPing(client, DateTime.Now, line, true); } } else result = false; } return result; }
public int CreateDowntimeEventWithTimeZone(DateTime? eStart, DateTime? eStop, string strTimeZone, decimal? minutes, string reasonCode, int? reasonCodeID, string comment, string line, string client) { int result = 0; if (eStart.HasValue == false || eStop.HasValue == false) return 0; if (minutes.HasValue == false) minutes = Convert.ToInt32(eStop.Value.Subtract(eStart.Value).TotalMinutes); DateTime eventStart = eStart.Value; DateTime eventStop = eStop.Value; if (!string.IsNullOrEmpty(strTimeZone)) { TimeZoneInfo timeZone = TimeZoneInfo.FromSerializedString(strTimeZone); eventStart = TimeZoneInfo.ConvertTime(eStart.Value, timeZone); eventStop = TimeZoneInfo.ConvertTime(eStop.Value, timeZone); } decimal totalMinutes = (decimal)eventStop.Subtract(eventStart).TotalMinutes; if (totalMinutes > 60 && minutes < 60)//If eventstop is greater than an hour or more, but the minute difference isn't, then assume timezone conversion or something is wrong for the times { //assume Event Start is correct eventStop = eventStart.AddMinutes((double)minutes); } using (DB db = new DB()) { if (alreadyExists(client, line, eventStart, eventStop)) return -2; DowntimeData dd = new DowntimeData(); dd.EventStart = eventStart; dd.EventStop = eventStop; dd.Minutes = minutes; dd.ReasonCodeID = reasonCodeID; dd.ReasonCode = reasonCode; dd.Line = line; dd.Comment = comment; dd.Client = client; dd.IsCreatedByAcromag = true; db.AddToDowntimeDataSet(dd); if (db.SaveChanges() > 0) { result = dd.ID; DBHelper.UpdateAscommPing(client, DateTime.Now, line, true); } } return result; }
public int CreateTComDowntimeEvent(string eStart, string eStop, decimal? minutes, string client, string line, string comment = null) { int result = 0; if (string.IsNullOrEmpty(eStart) || string.IsNullOrEmpty(eStop)) return -1; DateTime eventStart; DateTime eventStop; if (!DateTime.TryParse(eStart, out eventStart)) return -1; if (!DateTime.TryParse(eStop, out eventStop)) return -1; if (eventStop < eventStart) return -1; if (minutes.HasValue == false) minutes = Convert.ToInt32(eventStop.Subtract(eventStart).TotalMinutes); decimal totalMinutes = (decimal)eventStop.Subtract(eventStart).TotalMinutes; if (totalMinutes > 60 && minutes < 60)//If eventstop is greater than an hour or more, but the minute difference isn't, then assume timezone conversion or something is wrong for the times { //assume Event Start is correct eventStop = eventStart.AddMinutes((double)minutes); } using (DB db = new DB()) { if (alreadyExists(client, line, eventStart, eventStop) == true) return -2; DowntimeData dd = new DowntimeData(); dd.EventStart = eventStart; dd.EventStop = eventStop; dd.Minutes = minutes; dd.ReasonCodeID = null; dd.ReasonCode = null; dd.Line = line; dd.Comment = comment; dd.Client = client; dd.IsCreatedByAcromag = true; db.AddToDowntimeDataSet(dd); if (db.SaveChanges() <= 0) return result; result = dd.ID; DBHelper.UpdateAscommPing(client, DateTime.Now, line, true); } return result; }
private bool Stop(DateTime start,DateTime stop) { DowntimeData newdd = new DowntimeData() { EventStart = start, EventStop = stop, Minutes = Convert.ToDecimal(stop.Subtract(start).TotalSeconds) / 60 }; using (DB db = new DB(DBHelper.GetConnectionString(_currentClient))) { db.AddToDowntimeDataSet(newdd); db.SaveChanges(); } return true; }
public bool CreateDowntimeDataWithTimeZone(DateTime? eventStart, DateTime? eventStop, string strTimeZone, decimal? minutes, string reasonCode, int? reasonCodeID, string comment, string line, string client) { bool result = false; if (eventStart.HasValue == false || eventStop.HasValue == false) return false; if (minutes.HasValue == false) minutes = Convert.ToInt32(eventStop.Value.Subtract(eventStart.Value).TotalMinutes); if (!string.IsNullOrEmpty(strTimeZone)) { TimeZoneInfo timeZone = TimeZoneInfo.FromSerializedString(strTimeZone); eventStart = TimeZoneInfo.ConvertTime(eventStart.Value, timeZone); eventStop = TimeZoneInfo.ConvertTime(eventStop.Value, timeZone); } using (DB db = new DB()) { DowntimeData latestDowntime = (from o in db.DowntimeDataSet where o.Client == client && o.Line == line orderby o.EventStop.Value descending select o).FirstOrDefault(); DateTime? latestDate = null; if (latestDowntime != null) latestDate = latestDowntime.EventStop; bool create = !latestDate.HasValue; DowntimeData dd = new DowntimeData(); if (latestDate.HasValue) { create = !(latestDate.Value >= eventStop); } if (create) { //dd.EventStart = eventStart; //dd.EventStop = eventStop; dd.EventStart = eventStart; dd.EventStop = eventStop; dd.Minutes = minutes; dd.ReasonCodeID = reasonCodeID; dd.ReasonCode = reasonCode; dd.Line = line; dd.Comment = comment; dd.Client = client; dd.IsCreatedByAcromag = true; db.AddToDowntimeDataSet(dd); result = db.SaveChanges() > 0; if (result == true) { DBHelper.UpdateAscommPing(client, DateTime.Now, line, true); } } } return result; }
private string SplitDowntime(int downtimeId, decimal minutes) { using (var db = new DB(DBHelper.GetConnectionString(_currentClient))) { var q = from o in db.DowntimeDataSet where o.ID == downtimeId select o; DowntimeData dd = q.FirstOrDefault(); if (dd == null) return "false"; var seconds = Convert.ToInt32(minutes*60); if (dd.EventStop != null) { dd.EventStop = dd.EventStop.Value.Subtract(new TimeSpan(0, 0, 0, seconds)); dd.Minutes -= minutes; DowntimeData dd2 = new DowntimeData { Client = dd.Client, ReasonCode = null, ReasonCodeID = null, Line = dd.Line, Minutes = minutes, Comment = string.Empty, EventStart = dd.EventStop, EventStop = dd.EventStop.Value.AddSeconds(Convert.ToInt32(minutes*60)) }; db.AddToDowntimeDataSet(dd2); } db.SaveChanges(); } return "true"; }
private string Save(int id,HttpContext context, bool changeMinutes = false) { int reasonId; string reasonCode, comment, line, client; DateTime startdatetime, enddatetime; decimal minutes; int throughputId, option1Id, option2Id = -1; reasonCode = request.Form["reasonCode"]; comment = request.Form["comment"]; line = request.Form["line"]; client = _currentClient; line = line.Replace('#', ' '); int.TryParse(request.Form["throughput"], out throughputId); int.TryParse(request.Form["option1"], out option1Id); int.TryParse(request.Form["option2"], out option2Id); if (!DateTime.TryParse(request.Form["startdatetime"], out startdatetime)) { context.Response.Write("start date time is empty."); return "false"; } if (!int.TryParse(request.Form["reasonId"], out reasonId)) { context.Response.Write("reason id is empty."); return "false"; } if (request.Form["minutes"] != "tv") { if (!decimal.TryParse(request.Form["minutes"], out minutes)) { context.Response.Write("minutes id is empty."); return "false"; } } else { minutes = -1; } if (string.IsNullOrEmpty(line)) { context.Response.Write("line is empty."); return "false"; } if (string.IsNullOrEmpty(reasonCode)) { context.Response.Write("reasonCode is empty."); return "false"; } startdatetime.AddMinutes(Convert.ToDouble(minutes)); bool result; using (DB db = new DB(DBHelper.GetConnectionString(_currentClient))) { var q = from o in db.DowntimeDataSet where o.ID == id select o; var dd = q.FirstOrDefault(); if (dd == null) return false.ToString().ToLower(); DowntimeReason reason = DCSDashboardDemoHelper.getReason(reasonId); if (dd.EventStop.HasValue == false) { if (!DateTime.TryParse(request.Form["enddatetime"], out enddatetime)) { context.Response.Write("end date time is empty."); return "false"; } dd.EventStop = enddatetime; if (dd.EventStart != null) dd.Minutes = Convert.ToDecimal(dd.EventStop.Value.Subtract(dd.EventStart.Value).TotalMinutes); changeMinutes = false; SwitchLineStatus(line, true); } dd.ReasonCodeID = reasonId; dd.ReasonCode = reasonCode; dd.Line = line; dd.Comment = comment; if(changeMinutes) dd.Minutes = minutes; dd.Client = client; if (reason.HideReasonInReports && dd.Minutes > reason.Duration && reason.Duration > 0)//If Planned && duration is greater than 0 { DowntimeChild child1 = (from o in db.DowntimeChildren where o.ParentDowntimeId == dd.ID select o).FirstOrDefault(); if (child1 == null) { if (dd.EventStart != null) { dd.EventStop = dd.EventStart.Value.AddMinutes(reason.Duration); // Tim mark DowntimeData dd2 = new DowntimeData { Client = dd.Client, ReasonCode = null, ReasonCodeID = null, Line = dd.Line, Minutes = dd.Minutes - reason.Duration, Comment = string.Empty, EventStart = dd.EventStop }; dd2.EventStop = dd.EventStop.Value.AddMinutes(Convert.ToDouble(dd2.Minutes)); child1 = new DowntimeChild {ParentDowntimeId = dd.ID}; dd.Minutes = reason.Duration;//Change after setting dd2's minutes db.AddToDowntimeDataSet(dd2); db.SaveChanges(); child1.DowntimeId = dd2.ID; child1.ReasonCodeId = reason.ID; db.AddToDowntimeChildren(child1); } db.SaveChanges(); } } else { db.SaveChanges(); } result = true; if (reason.IsChangeOver && throughputId > 0) { DCSDashboardDemoHelper.CreateThroughPutHistory(throughputId, option1Id, option2Id, dd.EventStop.Value, line); } else if (option1Id > 0 || option2Id > 0) { if (option1Id > 0) DCSDashboardDemoHelper.CreateNOS(dd.ID, option1Id); if (option2Id > 0) DCSDashboardDemoHelper.CreateNOS(dd.ID, option2Id); } } return result.ToString().ToLower(); }
public string CreateDTWithNoStop() { DateTime sd; if (!DateTime.TryParse(request.Params["sd"], out sd)) { return null; } using (DB db = new DB(DBHelper.GetConnectionString(_currentClient))) { DowntimeData dt = new DowntimeData(); dt.Client = _currentClient; dt.EventStart = sd; dt.EventStop = null; dt.Line = Line; dt.Minutes = 0; db.AddToDowntimeDataSet(dt); db.SaveChanges(); SwitchLineStatus(Line, false); return ConvertToJsonString(new { ID = dt.ID, EventStart = (dt.EventStart.Value.ToString("MM/dd/yyyy hh:mm:ss tt")), EventStop = "", ReasonCodeID = dt.ReasonCodeID, ReasonCode = dt.ReasonCode, Minutes = (dt.Minutes.Value), Line = dt.Line, Comment = dt.Comment }); } }
private void addNewEvent(HttpContext context) { //reasonId&reasonCode&comment&startdatetime&minutes&line int reasonId, occurences; int throughputId, option1Id, option2Id = -1; DateTime startdatetime; decimal minutes; var reasonCode = request.Form["reasonCode"]; var comment = request.Form["comment"]; var line = request.Form["line"]; line = line.Replace('#', ' '); var client = _currentClient; int.TryParse(request.Form["Occurences"], out occurences); if (occurences == 0) occurences = 1; int.TryParse(request.Form["throughput"], out throughputId); int.TryParse(request.Form["option1"], out option1Id); int.TryParse(request.Form["option2"], out option2Id); if(!DateTime.TryParse(request.Form["startdatetime"],out startdatetime)) { context.Response.Write("start date time is empty."); return; } if(!int.TryParse(request.Form["reasonId"],out reasonId)) { context.Response.Write("reason id is empty."); return; } if(!decimal.TryParse(request.Form["minutes"],out minutes)) { context.Response.Write("minutes id is empty."); return; } if(string.IsNullOrEmpty(line)) { context.Response.Write("line is empty."); return; } if(string.IsNullOrEmpty(reasonCode)) { context.Response.Write("reasonCode is empty."); return; } var enddatetime = startdatetime.AddMinutes(Convert.ToDouble(minutes)); bool result = false; using (DB db = new DB(DBHelper.GetConnectionString(_currentClient))) { int count = 0; DowntimeReason reason = (from o in db.DowntimeReasonSet where o.ID == reasonId select o).FirstOrDefault(); if (reason == null) context.Response.Write("Reason Code doesn't exist"); if (throughputId > 0 && reason.IsChangeOver) { var throughput = (from o in db.Throughput where o.Id == throughputId select o).FirstOrDefault(); reasonCode = string.Format("{0} [{1}]", reasonCode, throughput.Name); } List<DowntimeData> dts = new List<DowntimeData>(); for (int cpnum = count; cpnum < occurences; cpnum++) { DowntimeData dd = new DowntimeData(); dd.EventStart = startdatetime; dd.EventStop = enddatetime; dd.Minutes = minutes; dd.ReasonCodeID = reasonId; dd.ReasonCode = reasonCode; dd.Line = line; dd.Comment = comment; dd.Client = client; dd.IsCreatedByAcromag = false; db.AddToDowntimeDataSet(dd); dts.Add(dd); } int updatedRecords = db.SaveChanges(); if (throughputId > 0 && reason.IsChangeOver) { DCSDashboardDemoHelper.CreateThroughPutHistory(throughputId, option1Id, option2Id, enddatetime, line); } else if(option1Id > 0 || option2Id > 0) { foreach (DowntimeData dt in dts) { if (dt.ID <= 0) continue; if (option1Id > 0) DCSDashboardDemoHelper.CreateNOS(dt.ID, option1Id); if (option2Id > 0) DCSDashboardDemoHelper.CreateNOS(dt.ID, option2Id); } } if (updatedRecords > 0) result = true; else if (updatedRecords == 0 && count == 1) result = true; else if (updatedRecords == 0 && count == 0) result = false; } context.Response.Write(result.ToString().ToLower()); }
private void addNewEvent(HttpContext context) { //reasonId&reasonCode&comment&startdatetime&minutes&line int reasonId, occurences; int throughputId, option1Id, option2Id = -1; string reasonCode, comment, line,client; DateTime startdatetime, enddatetime; decimal minutes; reasonCode=request.Form["reasonCode"]; comment=request.Form["comment"]; line=request.Form["line"]; line = line.Replace('#', ' '); client = _currentClient; int.TryParse(request.Form["Occurences"], out occurences); if (occurences == 0) occurences = 1; int.TryParse(request.Form["throughput"], out throughputId); int.TryParse(request.Form["option1"], out option1Id); int.TryParse(request.Form["option2"], out option2Id); if(!DateTime.TryParse(request.Form["startdatetime"],out startdatetime)) { context.Response.Write("start date time is empty."); return; } if(!int.TryParse(request.Form["reasonId"],out reasonId)) { context.Response.Write("reason id is empty."); return; } if(!decimal.TryParse(request.Form["minutes"],out minutes)) { context.Response.Write("minutes id is empty."); return; } if(string.IsNullOrEmpty(line)) { context.Response.Write("line is empty."); return; } if(string.IsNullOrEmpty(reasonCode)) { context.Response.Write("reasonCode is empty."); return; } enddatetime=startdatetime.AddMinutes(Convert.ToDouble(minutes)); bool result = false; using (DB db = new DB(DBHelper.GetConnectionString(_currentClient))) { int count = 0; /* if (id > 0) { DowntimeData dt = (from o in db.DowntimeDataSet where o.ID == id select o).FirstOrDefault(); if (dt != null) { startdatetime = dt.EventStart.Value;//Update STartTime dt.EventStop = enddatetime; dt.Minutes = minutes; dt.Comment = comment; dt.ReasonCode = reasonCode; dt.ReasonCodeID = reasonId; SwitchLineStatus(line, true); count = 1;//We already added a dt } } */ for (int cpnum = count; cpnum < occurences; cpnum++) { DowntimeData dd = new DowntimeData(); dd.EventStart = startdatetime; dd.EventStop = enddatetime; dd.Minutes = minutes; dd.ReasonCodeID = reasonId; dd.ReasonCode = reasonCode; dd.Line = line; dd.Comment = comment; dd.Client = client; db.AddToDowntimeDataSet(dd); } if (throughputId > 0) { DCSDashboardDemoHelper.CreateThroughPutHistory(throughputId, option1Id, option2Id, enddatetime, line); } int updatedRecords = db.SaveChanges(); if (updatedRecords > 0) result = true; else if (updatedRecords == 0 && count == 1) result = true; else if (updatedRecords == 0 && count == 0) result = false; } context.Response.Write(result.ToString().ToLower()); }