public void Execute(CrossHourContext context) { if (!context.bCrossHour) { return; } Thread thread = new Thread(new ParameterizedThreadStart(this.CrossHourProcessing)); thread.Name = "CrossHourProcessing"; thread.IsBackground = true; thread.Start(context); thread.Join(); }
private void CrossHourProcessing(object obj) { CrossHourContext crossHourContext = (CrossHourContext)obj; DateTime tInsertTime = crossHourContext.tInsertTime; List <string> list = new List <string>(); List <string> list2 = new List <string>(); List <DateTime> list3 = new List <DateTime>(); List <string> list4 = new List <string>(); List <DateTime> list5 = new List <DateTime>(); DateTime dateTime = new DateTime(tInsertTime.Year, tInsertTime.Month, tInsertTime.Day, tInsertTime.Hour, 0, 0); DateTime dateTime2 = DataCrossHourProcessing._tNextHour2Handle; if (dateTime2 == DateTime.MinValue) { dateTime2 = dateTime; } while (dateTime2 <= dateTime) { this.PrepareTables(dateTime2); string text = dateTime2.ToString("yyyyMMdd"); string text2 = string.Format("insert into dev_hour_pd_{0} (device_id,power_consumption,power_max,insert_time) ", text); text2 += string.Format(" (select device_id,sum(power_consumption),max(power),{0} from dev_min_pw_pd_{1} ", dateTime2.Hour, text); text2 += string.Format(" where (insert_time >= {0} and insert_time <= {1}) group by device_id)", dateTime2.Hour * 60, dateTime2.Hour * 60 + 59); list2.Add(text2); list3.Add(dateTime2); if (!list.Contains("dev_hour_pd_" + text)) { list.Add("dev_hour_pd_" + text); } text2 = string.Format("insert into port_hour_pd_{0} (port_id,power_consumption,power_max,insert_time) ", text); text2 += string.Format(" (select port_id,sum(power_consumption),max(power),{0} from port_min_pw_pd_{1} ", dateTime2.Hour, text); text2 += string.Format(" where (insert_time >= {0} and insert_time <= {1}) group by port_id)", dateTime2.Hour * 60, dateTime2.Hour * 60 + 59); list2.Add(text2); list3.Add(dateTime2); if (!list.Contains("port_hour_pd_" + text)) { list.Add("port_hour_pd_" + text); } dateTime2 = dateTime2.AddHours(1.0); } DateTime dateTime3 = new DateTime(tInsertTime.Year, tInsertTime.Month, tInsertTime.Day, 0, 0, 0); DateTime dateTime4 = DataCrossHourProcessing._tNextDay2Handle; if (dateTime4 == DateTime.MinValue) { dateTime4 = dateTime3; } while (dateTime4 <= dateTime3 && crossHourContext.bCrossDay) { string text = dateTime4.ToString("yyyyMMdd"); string text2 = string.Format("insert into dev_day_pd_pm_{0} (device_id,power_consumption,power_max) ", text); text2 += string.Format(" (select device_id,sum(power_consumption),max(power) from dev_hour_pd_{0} group by device_id)", text); list4.Add(text2); list5.Add(dateTime4); if (!list.Contains("dev_day_pd_pm_" + text)) { list.Add("dev_day_pd_pm_" + text); } text2 = string.Format("insert into port_day_pd_pm_{0} (port_id,power_consumption,power_max) ", text); text2 += string.Format(" (select port_id,sum(power_consumption),max(power) from port_hour_pd_{0} group by port_id)", text); list4.Add(text2); list5.Add(dateTime4); if (!list.Contains("port_day_pd_pm_" + text)) { list.Add("port_day_pd_pm_" + text); } dateTime4 = dateTime4.AddDays(1.0); } DbConnection dbConnection = null; DbCommand dbCommand = null; try { dbConnection = new MySqlConnection(DataCrossHourProcessing._dbConnectString); dbConnection.Open(); dbCommand = dbConnection.CreateCommand(); for (int i = 0; i < list3.Count; i++) { try { dbCommand.CommandText = list2[i]; dbCommand.ExecuteNonQuery(); this.setDbTime("lasthour", list3[i].ToString("yyyy-MM-dd HH:mm:ss")); } catch (Exception) { } } if (crossHourContext.bCrossDay) { for (int j = 0; j < list5.Count; j++) { try { dbCommand.CommandText = list4[j]; dbCommand.ExecuteNonQuery(); this.setDbTime("lastday", list5[j].ToString("yyyy-MM-dd HH:mm:ss")); } catch (Exception) { } } } for (int k = 0; k < list.Count; k++) { dbCommand.CommandText = "OPTIMIZE TABLE " + list[k]; dbCommand.ExecuteNonQuery(); } } catch (Exception) { } finally { try { if (dbCommand != null) { dbCommand.Dispose(); } if (dbConnection != null) { dbConnection.Close(); dbConnection.Dispose(); } } catch { } } }