157 lines
5.9 KiB
C#
157 lines
5.9 KiB
C#
using MySql.Data.MySqlClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DataPoolingService
|
|
{
|
|
class DALmachine
|
|
{
|
|
private static int machine_id;
|
|
private static double hrs;
|
|
private static int count;
|
|
private static double efficiency;
|
|
private static DateTime date;
|
|
|
|
ConnectionClass ConnectionClass = new ConnectionClass();
|
|
Logger Logger = new Logger();
|
|
SqlCommand sqlCommand = new SqlCommand();
|
|
DataTable dataTable = new DataTable();
|
|
SqlDataAdapter SqlDataAdapter = new SqlDataAdapter();
|
|
|
|
MySqlCommand MySqlCommand = new MySqlCommand();
|
|
Boolean check = false;
|
|
public void executeProcess()
|
|
{
|
|
try
|
|
{
|
|
ConnectionClass.OpenConnection();
|
|
//string query = "Select * from utility_sub_meter_reading WHERE CONVERT(date, date) = '2024-06-11'";
|
|
string query = "Select * from machine_production a where CONVERT(date,a.reading_timestamp)= CONVERT(date,GETDATE())";
|
|
|
|
sqlCommand = new SqlCommand(query, ConnectionClass.connection);
|
|
SqlDataAdapter.SelectCommand = sqlCommand;
|
|
SqlDataAdapter.Fill(dataTable);
|
|
ConnectionClass.CloseConnection();
|
|
|
|
if (dataTable.Rows.Count > 0)
|
|
{
|
|
if (CheckRecordExist() == false)
|
|
{
|
|
InsertingData(dataTable);
|
|
}
|
|
else
|
|
{
|
|
//Logger.RecordExistLog();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//Logger.NotFoundLog();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ConnectionClass.CloseConnection();
|
|
}
|
|
|
|
}
|
|
private bool CheckRecordExist()
|
|
{
|
|
try
|
|
{
|
|
ConnectionClass.OpenConnectionMysql();
|
|
|
|
DataRowCollection rows = dataTable.Rows; // Get the collection of rows from the DataTable
|
|
|
|
string query = " SELECT * FROM machine_production WHERE DATE(reading_timestamp) = CURDATE() limit 1";
|
|
//string query = "Select * from utility_sub_meter_reading a where date(a.reading_timestamp) = '2024-04-05'";
|
|
|
|
MySqlCommand mySqlCommand = new MySqlCommand(query, ConnectionClass.MySqlConnection);
|
|
|
|
using (MySqlDataReader dataReader = mySqlCommand.ExecuteReader())
|
|
{
|
|
// Check if any rows are returned by the query
|
|
if (dataReader.HasRows)
|
|
{
|
|
check = true;
|
|
return true; // Records exist for the current date
|
|
}
|
|
}
|
|
check = false;
|
|
return false; // No records found for the current date
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Log or handle the exception appropriately
|
|
Console.WriteLine("Error: " + ex.Message);
|
|
return false; // Or return true/false based on your error handling strategy
|
|
}
|
|
finally
|
|
{
|
|
ConnectionClass.CloseConnectionMysql(); // Ensure connection is closed
|
|
}
|
|
}
|
|
|
|
private void InsertingData(DataTable dt)
|
|
{
|
|
try
|
|
{
|
|
//Logger.StartLogEvent();
|
|
ConnectionClass.OpenConnectionMysql();
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
machine_id = Convert.ToInt32(dr["machine_id"]);
|
|
hrs = Convert.ToDouble(dr["hrs"]);
|
|
count = Convert.ToInt32(dr["count"]);
|
|
efficiency = Convert.ToDouble(dr["efficiency"]);
|
|
date = Convert.ToDateTime(dr["reading_timestamp"]);
|
|
|
|
string mySqlDefaultFormat = date.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
string query = "INSERT INTO machine_production (machine_id, hrs, count, efficiency, reading_timestamp) " +
|
|
"VALUES (@machine_id, @hrs, @count, @efficiency, @date)";
|
|
|
|
using (MySqlCommand cmd = new MySqlCommand(query, ConnectionClass.MySqlConnection))
|
|
{
|
|
cmd.Parameters.AddWithValue("@machine_id", machine_id);
|
|
cmd.Parameters.AddWithValue("@hrs", hrs);
|
|
cmd.Parameters.AddWithValue("@count", count);
|
|
cmd.Parameters.AddWithValue("@efficiency", efficiency);
|
|
cmd.Parameters.AddWithValue("@date", mySqlDefaultFormat);
|
|
|
|
// Execute the query
|
|
int rowsAffected = cmd.ExecuteNonQuery();
|
|
//int rowsAffected = 1;
|
|
|
|
if (rowsAffected == 1)
|
|
{
|
|
// Log the successful insert to the text file
|
|
//Logger.RowSuccessLogMolding(machine_id, hrs, efficiency);
|
|
}
|
|
else
|
|
{
|
|
// Log the successful insert to the text file
|
|
//Logger.RowFailedLogMolding(machine_id, hrs, efficiency);
|
|
}
|
|
}
|
|
}
|
|
|
|
ConnectionClass.CloseConnectionMysql();
|
|
// Logger.EndLogEvent();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Handle any exceptions that may occur during execution.
|
|
// You can log the exception or perform any necessary error handling here.
|
|
ConnectionClass.CloseConnectionMysql();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|