Files
2026-09-03 10:50:01 +02:00

228 lines
9.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StacjaProb
{
public partial class PumpStockEditForm : Form
{
public int devID;
public string selectedStatus = "";
public string selectedCompanies = "";
public static int newPumpID = 0;
public static string newPumpName = "";
public PumpStockEditForm()
{
InitializeComponent();
}
private void PumpStockEditForm_Load(object sender, EventArgs e)
{
LoadStatuses();
LoadCompanies();
if (devID > 0)
{
pictureBoxSearchPump.Visible = false;
LoadConectMotor();
buttonSave.Text = "Aktualizuj";
richTextBoxUwagi.Text = mySqlFunctions.sqlStringValue("select DRS_DEV_DESCRIPTION from seg_dev_resources where DRS_ID="+ devID.ToString(), "DRS_DEV_DESCRIPTION");
}
else
{
buttonSave.Enabled = false;
buttonDelete.Enabled = false;
}
}
private void LoadStatuses()
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
int index = 0;
// Ładuje listę dostępnych Pomp
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("select DIC_ID,DIC_POL_LANG from sys_dictionaries where DIC_TYPE_ID=1 order by DIC_POL_LANG", mySqlFunctions.connection);
MySql.Data.MySqlClient.MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
comboBoxStatus.Items.Insert(index, dataReader["DIC_POL_LANG"].ToString());
// zaznacza jako selected
if (dataReader["DIC_POL_LANG"].ToString() == selectedStatus)
comboBoxStatus.SelectedIndex = comboBoxStatus.Items.Count - 1;
index++;
}
dataReader.Close();
mySqlFunctions.CloseConnection();
}
}
private void LoadCompanies()
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
int index = 0;
// Ładuje listę dostępnych Pomp
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("select * from sys_participants order by PRT_NAME", mySqlFunctions.connection);
MySql.Data.MySqlClient.MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
comboBoxCompany.Items.Insert(index, dataReader["PRT_DESCRIPTION"].ToString());
if (dataReader["PRT_DESCRIPTION"].ToString() == selectedCompanies)
comboBoxCompany.SelectedIndex = comboBoxCompany.Items.Count - 1;
index++;
}
dataReader.Close();
mySqlFunctions.CloseConnection();
}
}
private void LoadConectMotor()
{
try
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
// Ładuje listę dostępnych Pomp
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("select APE_ENG_NAME,APE_ENG_NO,ENG_VOLTAGE,ENG_POWER from seg_aggregate_pup_eng left join seg_engines on ENG_ID=APE_ENG_ID where APE_PUP_DRS_ID=" + devID.ToString(), mySqlFunctions.connection);
MySql.Data.MySqlClient.MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
labelMName.Text = dataReader["APE_ENG_NAME"].ToString();
labelMSN.Text = dataReader["APE_ENG_NO"].ToString();
labelMV.Text = dataReader["ENG_VOLTAGE"].ToString();
labelMkW.Text = dataReader["ENG_POWER"].ToString();
buttonDelete.Enabled = false;
}
dataReader.Close();
mySqlFunctions.CloseConnection();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
string statID = mySqlFunctions.sqlStringValue("select DIC_ID from sys_dictionaries where DIC_POL_LANG='" + comboBoxStatus.SelectedItem.ToString() + "'", "DIC_ID");
string partID = mySqlFunctions.sqlStringValue("select PRT_ID from sys_participants where PRT_DESCRIPTION ='" + comboBoxCompany.SelectedItem.ToString() + "'", "PRT_ID");
//jeśli mamy do czynienia z EDYCJĄ - aktualizaujemy
if (devID > 0)
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("UPDATE seg_dev_resources SET DRS_SN_NUMBER='" + textBoxNS.Text + "',DRS_BUY_DATE='" + dateTimePickerBuy.Value.ToString("yyyy-MM-dd HH:mm:ss") + "',DRS_AVAILABLE_DATE='" + dateTimePickerAva.Value.ToString("yyyy-MM-dd HH:mm:ss") + "',DRS_STATUS_DIC_ID='" + statID + "',DRS_DEV_DESCRIPTION='" + richTextBoxUwagi.Text + "',DRS_AUDIT_DATE=now(), DRS_AUDIT_USR= '" + partID + "' WHERE DRS_ID=" + devID.ToString(), mySqlFunctions.connection);
cmd.ExecuteNonQuery();
// tabela historyczna
MySql.Data.MySqlClient.MySqlCommand his = new MySql.Data.MySqlClient.MySqlCommand("insert into seg_dev_resources_his select * from seg_dev_resources where DRS_ID = " + devID.ToString(), mySqlFunctions.connection);
his.ExecuteNonQuery();
mySqlFunctions.CloseConnection();
}
this.Close();
}
//dodajemy
else
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("INSERT INTO seg_dev_resources SET DRS_DEV_ID="+ newPumpID.ToString() + ",DRS_DEV_TYPE=10,DRS_SN_NUMBER='"+ textBoxNS.Text + "',DRS_BUY_DATE='" + dateTimePickerBuy.Value.ToString("yyyy-MM-dd HH:mm:ss") + "',DRS_AVAILABLE_DATE='" + dateTimePickerAva.Value.ToString("yyyy-MM-dd HH:mm:ss") + "',DRS_STATUS_DIC_ID='" + statID + "',DRS_DEV_DESCRIPTION='" + richTextBoxUwagi.Text + "',DRS_AUDIT_USR='" + partID + "',DRS_AUDIT_DATE=now()", mySqlFunctions.connection);
cmd.ExecuteNonQuery();
// tabela historyczna
MySql.Data.MySqlClient.MySqlCommand his = new MySql.Data.MySqlClient.MySqlCommand("insert into seg_dev_resources_his select * from seg_dev_resources where DRS_ID=(select max(DRS_ID) from seg_dev_resources)", mySqlFunctions.connection);
his.ExecuteNonQuery();
mySqlFunctions.CloseConnection();
}
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void pictureBoxSearchPump_FormClosed(object sender, FormClosedEventArgs e)
{
textBoxPName.Text = newPumpName;
}
private void pictureBoxSearchPump_Click(object sender, EventArgs e)
{
PumpStockChFromCatalogForm pscfcForm = new PumpStockChFromCatalogForm();
pscfcForm.FormClosed += new FormClosedEventHandler(pictureBoxSearchPump_FormClosed);
pscfcForm.ShowDialog();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void textBoxNS_Leave(object sender, EventArgs e)
{
if (textBoxNS.TextLength > 0)
buttonSave.Enabled = true;
}
private void linkLabelHistoria_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
PumpStockHistoryForm pshForm = new PumpStockHistoryForm();
pshForm.devID = devID;
pshForm.ShowDialog();
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("UWAGA!\nProces nieodwracalny.\nCzy na pewno chcesz usunąć pompę ze stanu?", "Potwierdzenie usunięcia z bazy", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
mySqlFunctions.initConnection();
if (mySqlFunctions.OpenConnection())
{
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("DELETE FROM seg_dev_resources WHERE DRS_ID = "+ devID.ToString(), mySqlFunctions.connection);
cmd.ExecuteNonQuery();
mySqlFunctions.CloseConnection();
}
this.Close();
}
}
}
}